int eximsrs_init() { uschar *list = srs_config; uschar secret_buf[SRS_MAX_SECRET_LENGTH]; uschar *secret = NULL; uschar sbuf[4]; uschar *sbufp; /* Check if this instance of Exim has not initialized SRS */ if(srs == NULL) { int co = 0; int hashlen, maxage; BOOL usetimestamp, usehash; /* Copy config vars */ hashlen = srs_hashlength; maxage = srs_maxage; usetimestamp = srs_usetimestamp; usehash = srs_usehash; /* Pass srs_config var (overrides new config vars) */ co = 0; if(srs_config != NULL) { secret = string_nextinlist(&list, &co, secret_buf, SRS_MAX_SECRET_LENGTH); if((sbufp = string_nextinlist(&list, &co, sbuf, sizeof(sbuf))) != NULL) maxage = atoi(sbuf); if((sbufp = string_nextinlist(&list, &co, sbuf, sizeof(sbuf))) != NULL) hashlen = atoi(sbuf); if((sbufp = string_nextinlist(&list, &co, sbuf, sizeof(sbuf))) != NULL) usetimestamp = atoi(sbuf); if((sbufp = string_nextinlist(&list, &co, sbuf, sizeof(sbuf))) != NULL) usehash = atoi(sbuf); } if(srs_hashmin == -1) srs_hashmin = hashlen; /* First secret specified in secrets? */ co = 0; list = srs_secrets; if(secret == NULL || *secret == '\0') { if((secret = string_nextinlist(&list, &co, secret_buf, SRS_MAX_SECRET_LENGTH)) == NULL) { log_write(0, LOG_MAIN | LOG_PANIC, "SRS Configuration Error: No secret specified"); return DEFER; } } /* Check config */ if(maxage < 0 || maxage > 365) { log_write(0, LOG_MAIN | LOG_PANIC, "SRS Configuration Error: Invalid maximum timestamp age"); return DEFER; } if(hashlen < 1 || hashlen > 20 || srs_hashmin < 1 || srs_hashmin > 20) { log_write(0, LOG_MAIN | LOG_PANIC, "SRS Configuration Error: Invalid hash length"); return DEFER; } if((srs = srs_open(secret, Ustrlen(secret), maxage, hashlen, srs_hashmin)) == NULL) { log_write(0, LOG_MAIN | LOG_PANIC, "Failed to allocate SRS memory"); return DEFER; } srs_set_option(srs, SRS_OPTION_USETIMESTAMP, usetimestamp); srs_set_option(srs, SRS_OPTION_USEHASH, usehash); /* Extra secrets? */ while((secret = string_nextinlist(&list, &co, secret_buf, SRS_MAX_SECRET_LENGTH)) != NULL) srs_add_secret(srs, secret, (Ustrlen(secret) > SRS_MAX_SECRET_LENGTH) ? SRS_MAX_SECRET_LENGTH : Ustrlen(secret)); DEBUG(D_any) debug_printf("SRS initialized\n"); } return OK; }
int main(int argc, char **argv) { char buf[1024]; char res[1024]; char starbuf[1204]; int n, m, errs = 0, star; srs_t *srs; struct timeb start, finish; printf("\nlibsrs_alt test program\n(C)2004 Miles Wilton <*****@*****.**>\n\n"); if(argc < 3 && !(argc == 2 && argv[1][0] == '~')) { printf("FORWARD Syntax: test <sender address> [*]<return/local domain>+\n\n"); printf("REVERSE Syntax: test ~<SRS address>\n\nOptional * in front of domain causes a 10000 repeat loop\non that domain. It is recommended to redirect to a file\nas output affects speed report.\n\nPrefixing ~ to first parameter performs reverse SRS on that\naddress (and no further parameters are processed)\n\n"); printf("The test utility uses a set of default values and will only successfully reverse hashes made by itself.\n\n"); exit(0); } printf("Encoding test on '%s':\n", argv[1]); n = srs__base64enc(argv[1], strlen(argv[1]), buf, 256); if(n == SRS_RESULT_OK) printf(" - base64enc: %s\n", buf); else { printf(" - base64enc: Error %d - %s\n", n, srs_geterrormsg(n)); errs++; } n = srs__base32enc(argv[1], strlen(argv[1]), buf, 256); if(n == SRS_RESULT_OK) printf(" - base32enc: %s\n", buf); else { printf(" - base32enc: Error %d - %s\n", n, srs_geterrormsg(n)); errs++; } printf("\nStarting SRS test:\n\n"); ftime(&start); if((srs = srs_open("mysecret", 8, 0, 0, 0)) == NULL) { printf("srs_open: Failed (returned NULL)\n"); errs++; } else { // srs_set_separator(srs, '+'); // srs_set_option(srs, SRS_OPTION_USETIMESTAMP, 0); // srs_set_option(srs, SRS_OPTION_USEHASH, 0); // srs_set_db_functions(srs, do_db_insert, do_db_lookup); if(argv[1][0] == '~') { strcpy(buf, &argv[1][1]); while(strncasecmp(buf, "SRS", 3) == 0) { printf("Resolving SRS address <%s>\n", buf); n = srs_reverse(srs, buf, res, 1024); if(n & SRS_RESULT_FAIL) { printf("srs_forward: Error %d - %s\n", n, srs_geterrormsg(n)); errs++; break; } else if(n != SRS_RESULT_OK) printf("srs_forward: Warning %d - %s\n", n, srs_geterrormsg(n)); printf(" - SRS forward to: <%s>\n", res); strcpy(buf, res); } } else { strcpy(buf, argv[1]); star = 0; for(m = 2; m < argc; m++) { if(argv[m][0] == '*') { sprintf(starbuf, "%d-%s", star, &argv[m][1]); star++; if(star < 10000) m--; else star = 0; } else strcpy(starbuf, argv[m]); printf("Fowarding MAIL FROM:<%s> on DOMAIN %s\n", buf, argv[m]); n = srs_forward(srs, buf, starbuf, res, 1024); if(n == SRS_RESULT_OK) printf(" - SRS return path: <%s>\n", res); else { printf("srs_forward: Error %d - %s\n", n, srs_geterrormsg(n)); errs++; break; } strcpy(buf, res); } } srs_close(srs); } ftime(&finish); if(errs) printf("\n\nThere were errors: %d\n\n", errs); else printf("\n\nTests completed successfully (%.3f seconds).\n\n", (float)(finish.time - start.time) + (float)(finish.millitm - start.millitm) / 1000); exit(errs); }