1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* SRS - Sender rewriting scheme support
6 (C)2004 Miles Wilton <miles@mirtol.com>
7 Copyright (c) The Exim Maintainers 2016
9 SRS Support Version: 1.0a
14 #ifdef EXPERIMENTAL_SRS
20 uschar *srs_db_forward = NULL;
21 uschar *srs_db_reverse = NULL;
24 /* srs_init just initialises libsrs and creates (if necessary)
25 an srs object to use for all srs calls in this instance */
29 const uschar *list = srs_config;
30 uschar secret_buf[SRS_MAX_SECRET_LENGTH];
31 uschar *secret = NULL;
35 /* Check if this instance of Exim has not initialized SRS */
40 BOOL usetimestamp, usehash;
42 /* Copy config vars */
43 hashlen = srs_hashlength;
45 usetimestamp = srs_usetimestamp;
46 usehash = srs_usehash;
48 /* Pass srs_config var (overrides new config vars) */
50 if(srs_config != NULL)
52 secret = string_nextinlist(&list, &co, secret_buf, SRS_MAX_SECRET_LENGTH);
54 if((sbufp = string_nextinlist(&list, &co, sbuf, sizeof(sbuf))) != NULL)
57 if((sbufp = string_nextinlist(&list, &co, sbuf, sizeof(sbuf))) != NULL)
60 if((sbufp = string_nextinlist(&list, &co, sbuf, sizeof(sbuf))) != NULL)
61 usetimestamp = atoi(sbuf);
63 if((sbufp = string_nextinlist(&list, &co, sbuf, sizeof(sbuf))) != NULL)
68 srs_hashmin = hashlen;
70 /* First secret specified in secrets? */
73 if(secret == NULL || *secret == '\0')
75 if((secret = string_nextinlist(&list, &co, secret_buf, SRS_MAX_SECRET_LENGTH)) == NULL)
77 log_write(0, LOG_MAIN | LOG_PANIC,
78 "SRS Configuration Error: No secret specified");
84 if(maxage < 0 || maxage > 365)
86 log_write(0, LOG_MAIN | LOG_PANIC,
87 "SRS Configuration Error: Invalid maximum timestamp age");
90 if(hashlen < 1 || hashlen > 20 || srs_hashmin < 1 || srs_hashmin > 20)
92 log_write(0, LOG_MAIN | LOG_PANIC,
93 "SRS Configuration Error: Invalid hash length");
97 if((srs = srs_open(secret, Ustrlen(secret), maxage, hashlen, srs_hashmin)) == NULL)
99 log_write(0, LOG_MAIN | LOG_PANIC,
100 "Failed to allocate SRS memory");
104 srs_set_option(srs, SRS_OPTION_USETIMESTAMP, usetimestamp);
105 srs_set_option(srs, SRS_OPTION_USEHASH, usehash);
108 while((secret = string_nextinlist(&list, &co, secret_buf, SRS_MAX_SECRET_LENGTH)) != NULL)
109 srs_add_secret(srs, secret, (Ustrlen(secret) > SRS_MAX_SECRET_LENGTH) ? SRS_MAX_SECRET_LENGTH : Ustrlen(secret));
112 debug_printf("SRS initialized\n");
130 int eximsrs_forward(uschar **result, uschar *orig_sender, uschar *domain)
135 if((n = srs_forward(srs, orig_sender, domain, res, sizeof(res))) & SRS_RESULT_FAIL)
138 debug_printf("srs_forward failed (%s, %s): %s\n", orig_sender, domain, srs_geterrormsg(n));
142 *result = string_copy(res);
147 int eximsrs_reverse(uschar **result, uschar *address)
152 if((n = srs_reverse(srs, address, res, sizeof(res))) & SRS_RESULT_FAIL)
155 debug_printf("srs_reverse failed (%s): %s\n", address, srs_geterrormsg(n));
156 if(n == SRS_RESULT_NOTSRS || n == SRS_RESULT_BADSRS)
158 if(n == SRS_RESULT_BADHASH || n == SRS_RESULT_BADTIMESTAMP || n == SRS_RESULT_TIMESTAMPEXPIRED)
163 *result = string_copy(res);
168 int eximsrs_db_set(BOOL reverse, uschar *srs_db)
171 srs_db_reverse = (srs_db == NULL ? NULL : string_copy(srs_db));
173 srs_db_forward = (srs_db == NULL ? NULL : string_copy(srs_db));
175 if(srs_set_db_functions(srs, (srs_db_forward ? eximsrs_db_insert : NULL),
176 (srs_db_reverse ? eximsrs_db_lookup : NULL)) & SRS_RESULT_FAIL)
183 srs_result eximsrs_db_insert(srs_t *srs, char *data, uint data_len, char *result, uint result_len)
188 if(srs_db_forward == NULL)
189 return SRS_RESULT_DBERROR;
191 srs_db_address = string_copyn(data, data_len);
192 if(srs_generate_unique_id(srs, srs_db_address, buf, 64) & SRS_RESULT_FAIL)
193 return SRS_RESULT_DBERROR;
195 srs_db_key = string_copyn(buf, 16);
197 if((res = expand_string(srs_db_forward)) == NULL)
198 return SRS_RESULT_DBERROR;
201 return SRS_RESULT_DBERROR;
203 Ustrncpy(result, srs_db_key, result_len);
205 return SRS_RESULT_OK;
209 srs_result eximsrs_db_lookup(srs_t *srs, char *data, uint data_len, char *result, uint result_len)
213 if(srs_db_reverse == NULL)
214 return SRS_RESULT_DBERROR;
216 srs_db_key = string_copyn(data, data_len);
217 if((res = expand_string(srs_db_reverse)) == NULL)
218 return SRS_RESULT_DBERROR;
220 if(Ustrlen(res) >= result_len)
221 return SRS_RESULT_ADDRESSTOOLONG;
223 strncpy(result, res, result_len);
225 return SRS_RESULT_OK;