static char *fetch_smtproutes(const char *domain) { char *buf=get_control_smtproutes(); const char *p=buf; if (!buf) { struct dbobj d; char *p, *q; size_t l; p=config_search("esmtproutes.dat"); dbobj_init(&d); if (dbobj_open(&d, p, "R")) { free(p); return (0); } free(p); p=strcpy(courier_malloc(strlen(domain)+1), domain); for (q=p; *q; q++) *q=tolower(*q); q=dbobj_fetch(&d, p, strlen(p), &l, "D"); free(p); dbobj_close(&d); if (!q) return (0); p=courier_malloc(l+1); memcpy(p, q, l); p[l]=0; free(q); return (p); } while (*p) { unsigned i; for (i=0; p[i] && p[i] != '\n' && p[i] != '\r' && p[i] != ':'; ++i) ; if (p[i] == ':' && (i == 0 || config_domaincmp(domain, p, i) == 0)) { char *q; p += i; ++p; for (i=0; p[i] && p[i] != '\n' && p[i] != '\r'; i++) ; while (i && isspace((int)(unsigned char)p[i-1])) --i; while (i && isspace((int)(unsigned char)*p)) { ++p; --i; } if (i == 0) { free(buf); return (0); } q=courier_malloc(i+1); memcpy(q, p, i); q[i]=0; free(buf); return (q); } while (p[i]) { if (p[i] == '\n' || p[i] == '\r') { ++i; break; } ++i; } p += i; } free(buf); return (0); }
static void check_db() { char *dbname; char *lockname; int lockfd; struct dbobj db; time_t now; char *sender_key, *p; size_t val_len; char *val; if (!dbfile || !*dbfile) return; sender_key=strdup(sender); dbname=malloc(strlen(dbfile)+ sizeof( "." DBNAME)); lockname=malloc(strlen(dbfile)+ sizeof(".lock")); for (p=sender_key; *p; p++) *p=tolower((int)(unsigned char)*p); if (!dbname || !lockname || !sender) { perror("malloc"); exit(EX_TEMPFAIL); } strcat(strcpy(dbname, dbfile), "." DBNAME); strcat(strcpy(lockname, dbfile), ".lock"); lockfd=open(lockname, O_RDWR|O_CREAT, 0666); if (lockfd < 0 || ll_lock_ex(lockfd)) { perror(lockname); exit(EX_TEMPFAIL); } dbobj_init(&db); if (dbobj_open(&db, dbname, "C") < 0) { perror(dbname); exit(EX_TEMPFAIL); } time(&now); val=dbobj_fetch(&db, sender_key, strlen(sender_key), &val_len, ""); if (val) { time_t t; if (val_len >= sizeof(t)) { memcpy(&t, val, sizeof(t)); if (t >= now - interval * 60 * 60 * 24) { free(val); dbobj_close(&db); close(lockfd); exit(0); } } free(val); } dbobj_store(&db, sender_key, strlen(sender_key), (void *)&now, sizeof(now), "R"); dbobj_close(&db); close(lockfd); }
static void do_maildir_shared_sync(const char *dir, const char *shared) { struct dbobj obj; char *dbname; char *cur; char *shared_update_name; struct stat stat1, stat2; int fd; maildir_purgetmp(dir); /* clean up after myself */ maildir_getnew(dir, 0, NULL, NULL); maildir_purgetmp(shared); maildir_getnew(shared, 0, NULL, NULL); /* Figure out if we REALLY need to sync something */ shared_update_name=malloc(strlen(dir)+sizeof("/shared-timestamp")); if (!shared_update_name) return; strcat(strcpy(shared_update_name, dir), "/shared-timestamp"); cur=malloc(strlen(shared)+sizeof("/new")); if (!cur) { free(shared_update_name); return; } if (stat(shared_update_name, &stat1) == 0) { if ( stat( strcat(strcpy(cur, shared), "/new"), &stat2) == 0 && stat2.st_mtime < stat1.st_mtime && stat( strcat(strcpy(cur, shared), "/cur"), &stat2) == 0 && stat2.st_mtime < stat1.st_mtime) { free(shared_update_name); free(cur); return; } } if ((fd=maildir_safeopen(shared_update_name, O_RDWR|O_CREAT, 0600))>= 0) { if (write(fd, "", 1) < 0) perror("write"); close(fd); } free(cur); free(shared_update_name); if (create_db(&obj, dir, &dbname)) return; if (build_db(shared, &obj)) { dbobj_close(&obj); unlink(dbname); free(dbname); return; } if ((cur=malloc(strlen(dir)+sizeof("/cur"))) == 0) { perror("malloc"); dbobj_close(&obj); unlink(dbname); free(dbname); return; } strcat(strcpy(cur, dir), "/cur"); if (update_cur(cur, shared, &obj) == 0) { strcat(strcpy(cur, dir), "/new"); if (update_cur(cur, shared, &obj) == 0) { *strrchr(cur, '/')=0; /* Chop off the /new */ newmsgs(cur, shared, &obj); } } free(cur); dbobj_close(&obj); unlink(dbname); free(dbname); }