int absolutepath (stralloc *sa, const char *path) { int r = 1 ; if (!path) r = stralloc_copys(sa, "/") ; else if (path[0] == '/') r = stralloc_copys(sa, path) ; else { unsigned int n = 0 ; for (;;) { n += 1024 ; if (!stralloc_ready(sa, n)) { r = 0 ; break ; } if (getcwd(sa->s, n)) break ; if (errno != ENOMEM) { r = 0 ; break ; } } sa->len = str_len(sa->s) ; if (r) r = stralloc_append(sa, "/") && stralloc_cats(sa, path) ; if (!r) stralloc_free(sa) ; } return r ? stralloc_0(sa) : 0 ; }
/* Creates an entry for message num and the list listno and code "done". * Returns NULL on success, and the error string on error. */ const char *sub_sql_logmsg(struct subdbinfo *info, unsigned long num, unsigned long listno, unsigned long subs, int done) { char *s; char strnum[FMT_ULONG]; stralloc_copys(&query,"INSERT INTO "); stralloc_cats(&query,info->base_table); stralloc_cats(&query,"_mlog (msgnum,listno,subs,done) VALUES "); stralloc_cats(&query,sql_logmsg_values_defn); stralloc_copyb(¶ms[0],strnum,fmt_ulong(strnum,num)); stralloc_copyb(¶ms[1],strnum,fmt_ulong(strnum,listno)); stralloc_copyb(¶ms[2],strnum,fmt_ulong(strnum,subs)); s = strnum; if (done < 0) { done = - done; *s++ = '-'; } s[fmt_uint(s,done)] = 0; stralloc_copys(¶ms[3],s); sql_exec(info,&query,4,params); /* ignore dups */ return 0; }
int qldap_get_dotmode(qldap *q, stralloc *dm) { int r; /* get and check the status of the account */ r = qldap_get_attr(q, LDAP_DOTMODE, &ldap_attr, SINGLE_VALUE); if (r == NOSUCH) { if (!stralloc_copy(dm, &dotmode)) return ERRNO; return OK; } if (r != OK) return r; if (!case_diffs(DOTMODE_LDAPONLY, ldap_attr.s)) { if (!stralloc_copys(dm, DOTMODE_LDAPONLY)) return ERRNO; } else if (!str_diff(DOTMODE_LDAPWITHPROG, ldap_attr.s)) { if (!stralloc_copys(dm, DOTMODE_LDAPWITHPROG)) return ERRNO; } else if (!str_diff(DOTMODE_DOTONLY, ldap_attr.s)) { if (!stralloc_copys(dm, DOTMODE_DOTONLY)) return ERRNO; } else if (!str_diff(DOTMODE_BOTH, ldap_attr.s)) { if (!stralloc_copys(dm, DOTMODE_BOTH)) return ERRNO; } else if (!str_diff(DOTMODE_NONE, ldap_attr.s)) { if (!stralloc_copys(dm, DOTMODE_NONE)) return ERRNO; } else { return ILLVAL; } if (!stralloc_0(dm)) return ERRNO; return OK; }
void getmess() { int match; if (slurpclose(0,&message,1024) == -1) die_read(); strnum[fmt_uint(strnum,message.len)] = 0; if (!stralloc_copys(&beforemessage,strnum)) nomem(); if (!stralloc_cats(&beforemessage,":")) nomem(); if (!stralloc_copys(&aftermessage,",")) nomem(); if (getln(&envelope,&line,&match,'\0') == -1) die_read(); if (!match) die_format(); if (line.len < 2) die_format(); if (line.s[0] != 'F') die_format(); strnum[fmt_uint(strnum,line.len - 2)] = 0; if (!stralloc_cats(&aftermessage,strnum)) nomem(); if (!stralloc_cats(&aftermessage,":")) nomem(); if (!stralloc_catb(&aftermessage,line.s + 1,line.len - 2)) nomem(); if (!stralloc_cats(&aftermessage,",")) nomem(); for (;;) { if (getln(&envelope,&line,&match,'\0') == -1) die_read(); if (!match) die_format(); if (line.len < 2) break; if (line.s[0] != 'T') die_format(); strnum[fmt_uint(strnum,line.len - 2)] = 0; if (!stralloc_cats(&aftermessage,strnum)) nomem(); if (!stralloc_cats(&aftermessage,":")) nomem(); if (!stralloc_catb(&aftermessage,line.s + 1,line.len - 2)) nomem(); if (!stralloc_cats(&aftermessage,",")) nomem(); } }
/* Checks the hash against the cookie table. If it matches, returns NULL, * else returns "". If error, returns error string. */ static const char *_checktag (struct subdbinfo *info, unsigned long num, /* message number */ unsigned long listno, /* bottom of range => slave */ const char *action, const char *seed, const char *hash) /* cookie */ { sqlite3_stmt *stmt; int res; /* SELECT msgnum FROM table_cookie WHERE msgnum=num and cookie='hash' */ /* succeeds only is everything correct. 'hash' is quoted since it is */ /* potentially hostile. */ if (listno) { /* only for slaves */ if (!stralloc_copys(&line,"SELECT listno FROM ")) die_nomem(); if (!stralloc_cats(&line,info->base_table)) die_nomem(); if (!stralloc_cats(&line,"_mlog WHERE listno=")) die_nomem(); if (!stralloc_catb(&line,strnum,fmt_ulong(strnum,listno))) die_nomem(); if (!stralloc_cats(&line," AND msgnum=")) die_nomem(); if (!stralloc_catb(&line,strnum,fmt_ulong(strnum,num))) die_nomem(); if (!stralloc_cats(&line," AND done > 3")) die_nomem(); if (!stralloc_0(&line)) die_nomem(); if ((stmt = _sqlquery(info, &line)) == NULL) return sqlite3_errmsg((sqlite3*)info->conn); /* query */ res = sqlite3_step(stmt); sqlite3_finalize(stmt); /* free res */ if (res == SQLITE_ROW) return ""; /*already done */ else if (res != SQLITE_DONE) return sqlite3_errmsg((sqlite3*)info->conn); } if (!stralloc_copys(&line,"SELECT msgnum FROM ")) die_nomem(); if (!stralloc_cats(&line,info->base_table)) die_nomem(); if (!stralloc_cats(&line,"_cookie WHERE msgnum=")) die_nomem(); if (!stralloc_catb(&line,strnum,fmt_ulong(strnum,num))) die_nomem(); if (!stralloc_cats(&line," and cookie='")) die_nomem(); if (!stralloc_catb(&line,strnum,fmt_str(strnum,hash))) die_nomem(); if (!stralloc_cats(&line,"'")) die_nomem(); if (!stralloc_0(&line)) die_nomem(); if ((stmt = _sqlquery(info, &line)) == NULL) /* select */ return sqlite3_errmsg((sqlite3*)info->conn); res = sqlite3_step(stmt); sqlite3_finalize(stmt); /* free res */ if (res == SQLITE_DONE) return ""; /* eof => query ok, but null result*/ else if (res != SQLITE_ROW) return sqlite3_errmsg((sqlite3*)info->conn); /* some error occurred */ return (char *)0; /* success! cookie matches */ (void)action; (void)seed; }
int qldap_get_mailstore(qldap *q, stralloc *hd, stralloc *ms) { int r; /* * get and check the mailstores. * Both homedir and maildir are set from the three * values ~control/ldapmessagestore, homedirectory * and mailmessagestore. * ms is only filled with a value if both homedir * and maildir is used. */ r = qldap_get_attr(q, LDAP_HOMEDIR, hd, SINGLE_VALUE); if (r == NOSUCH) { if (!stralloc_copys(hd, "")) return ERRNO; } else if (r != OK) return r; if (0 < hd->len) { if (hd->s[0] != '/' || check_paths(hd->s) == 0) { /* probably some log warning would be good */ return ILLVAL; } } r = qldap_get_attr(q, LDAP_MAILSTORE, ms, SINGLE_VALUE); if (r == NOSUCH) { if (!stralloc_copys(ms, "")) return ERRNO; } else if (r != OK) return r; if (ms->len > 0) if (check_paths(ms->s) == 0) { /* probably some log warning would be good */ return ILLVAL; } if (hd->len > 0 && ms->len > 0) return OK; if (hd->len > 0) return OK; if (ms->len > 0) { if (ms->s[0] != '/') { if (default_messagestore.s == 0 || default_messagestore.len == 0) return ILLVAL; if (!stralloc_cat(hd, &default_messagestore)) return ERRNO; } if (!stralloc_cat(hd, ms)) return ERRNO; if (!stralloc_copys(ms, "")) return ERRNO; return OK; } return NEEDED; }
static int pls_reader(playlist* pl) { int ret; static playlist_entry entry; buffer* inbuf = pl->ptr; stralloc line; stralloc_init(&line); if(( ret = buffer_getline_sa(inbuf, &line))) { size_t index2, index; index2 = index = 0; while(line.len > 1 && (line.s[line.len - 1] == '\r' || line.s[line.len - 1] == '\n')) line.len--; stralloc_0(&line); if(!str_diffn(&line.s[index], "Number", 6)) { } else if(line.s[index] == '[') { } else if((index2 = str_chr(&line.s[index], '=')) > 0) { unsigned long trackno = 0; index = index2; index2++; do { index--; } while(isdigit(line.s[index]) && index > 0); scan_ulong(&line.s[index], &trackno); if(!str_diffn(&line.s[index], "File", 4)) { stralloc_copys(&entry.path, &line.s[index2]); stralloc_0(&entry.path); } else if(!str_diffn(&line.s[index], "Title", 5)) { stralloc_copys(&entry.title, &line.s[index2]); stralloc_0(&entry.title); } else if(!str_diffn(&line.s[index], "Length", 6)) { unsigned long len; scan_ulong(&line.s[index2], &len); entry.length = len; } /* uint32 index = 8; index += scan_ulong(&line.s[index], &len); entry.length = len; index++; stralloc_copys(&entry.title, &line.s[index]); stralloc_0(&entry.title); */ } else { /* stralloc_copy(&entry.path, &line); stralloc_0(&entry.path); if(pl->callback) { pl->callback(pl, &entry.title, &entry.path, entry.length); }*/ } } return ret; }
static int _issub(struct subdbinfo *info, const char *table, const char *userhost, stralloc *recorded) { sqlite3_stmt *stmt; unsigned int j; int res; /* SELECT address FROM list WHERE address = 'userhost' AND hash */ /* BETWEEN 0 AND 52. Without the hash restriction, we'd make it */ /* even easier to defeat. Just faking sender to the list name would*/ /* work. Since sender checks for posts are bogus anyway, I don't */ /* know if it's worth the cost of the "WHERE ...". */ if (!stralloc_copys(&addr,userhost)) die_nomem(); j = byte_rchr(addr.s,addr.len,'@'); if (j == addr.len) return 0; case_lowerb(addr.s + j + 1,addr.len - j - 1); if (!stralloc_copys(&line,"SELECT address FROM ")) die_nomem(); if (!stralloc_cat_table(&line,info,table)) die_nomem(); if (!stralloc_cats(&line," WHERE address LIKE '")) die_nomem(); if (!stralloc_cat(&line,&addr)) die_nomem(); if (!stralloc_cats(&line,"'")) die_nomem(); if (!stralloc_0(&line)) die_nomem(); if ((stmt = _sqlquery(info, &line)) == NULL) /* select */ strerr_die2x(111,FATAL,sqlite3_errmsg((sqlite3*)info->conn)); /* No data returned in QUERY */ res = sqlite3_step(stmt); if (res != SQLITE_ROW) { if (res != SQLITE_DONE) strerr_die2x(111,FATAL,sqlite3_errmsg((sqlite3*)info->conn)); sqlite3_finalize(stmt); return 0; } if (recorded) { if (!stralloc_copyb(recorded, (const char*)sqlite3_column_text(stmt, 0), sqlite3_column_bytes(stmt, 0))) die_nomem(); if (!stralloc_0(recorded)) die_nomem(); } sqlite3_finalize(stmt); return 1; }
/* Searches the subscriber log and outputs via subwrite(s,len) any entry * that matches search. A '_' is search is a wildcard. Any other * non-alphanum/'.' char is replaced by a '_'. */ void sub_sql_searchlog(struct subdbinfo *info, const char *table, char *search, /* search string */ int subwrite()) /* output fxn */ { void *result; datetime_sec when; struct datetime dt; char date[DATE822FMT]; int nparams; char strnum[FMT_ULONG]; make_name(info,table?"_":0,table,0); /* SELECT (*) FROM list_slog WHERE fromline LIKE '%search%' OR address */ /* LIKE '%search%' ORDER BY tai; */ /* The '*' is formatted to look like the output of the non-mysql version */ /* This requires reading the entire table, since search fields are not */ /* indexed, but this is a rare query and time is not of the essence. */ stralloc_copys(&query,"SELECT "); stralloc_cats(&query,sql_searchlog_select_defn); stralloc_cats(&query," FROM "); stralloc_cat(&query,&name); stralloc_cats(&query,"_slog"); if (*search) { /* We can afford to wait for LIKE '%xx%' */ stralloc_copys(¶ms[0],search); stralloc_copys(¶ms[1],search); nparams = 2; stralloc_cats(&query," WHERE "); stralloc_cats(&query,sql_searchlog_where_defn); } else nparams = 0; /* ordering by tai which is an index */ stralloc_cats(&query," ORDER by tai"); result = sql_select(info,&query,nparams,params); while (sql_fetch_row(info,result,2,params)) { stralloc_0(¶ms[0]); (void)scan_ulong(params[0].s,&when); datetime_tai(&dt,when); stralloc_copyb(¶ms[0],date,date822fmt(date,&dt)-1); stralloc_cats(¶ms[0],": "); stralloc_catb(¶ms[0],strnum,fmt_ulong(strnum,when)); stralloc_cats(¶ms[0]," "); stralloc_cat(¶ms[0],¶ms[1]); if (subwrite(params[0].s,params[0].len) == -1) die_write(); } sql_free_result(info,result); }
/* This routine inserts the cookie into table_cookie. We log arrival of * the message (done=0). */ void sub_sql_tagmsg(struct subdbinfo *info, unsigned long msgnum, /* number of this message */ const char *hashout, /* previously calculated hash */ unsigned long bodysize, unsigned long chunk) { const char *ret; char strnum[FMT_ULONG]; if (chunk >= 53L) chunk = 0L; /* sanity */ /* INSERT INTO table_cookie (msgnum,tai,cookie,bodysize,chunk) VALUES (...) */ /* (we may have tried message before, but failed to complete, so */ /* ER_DUP_ENTRY is ok) */ stralloc_copys(&query,"INSERT INTO "); stralloc_cats(&query,info->base_table); stralloc_cats(&query,"_cookie (msgnum,tai,cookie,bodysize,chunk) VALUES "); stralloc_cats(&query,sql_tagmsg_values_defn); stralloc_copyb(¶ms[0],strnum,fmt_ulong(strnum,msgnum)); stralloc_copyb(¶ms[1],hashout,COOKIE); stralloc_copyb(¶ms[2],strnum,fmt_ulong(strnum,bodysize)); stralloc_copyb(¶ms[3],strnum,fmt_ulong(strnum,chunk)); sql_exec(info,&query,4,params); /* ignore dups */ if (! (ret = logmsg(msgnum,0L,0L,1))) return; /* log done=1*/ if (*ret) strerr_die2x(111,FATAL,ret); }
void mx_list_fill_name( mx_list_t* ml, const char* host ) { stralloc out = {0}; stralloc shost = {0}; static char ip[ 16 ]; // 16 => xxx.xxx.xxx.xxx\0 uchar_t* xtra; int i; stralloc_copys( &shost, host ); // do the lookup dns_ip4( &out, &shost ); // for each entry, rebuild the IP address i = 0; xtra = (uchar_t*)out.s; while ( i < out.len ) { snprintf( ip, 16, "%d.%d.%d.%d", (uint16) xtra[i], (uint16) xtra[i+1], (uint16) xtra[i+2], (uint16) xtra[i+3] ); // and add it to the list mx_list_add_ip( ml, ip ); i += 4; } }
static const char *remove_table(struct subdbinfo *info, const char *suffix1, const char *suffix2) { sqlite3_stmt *stmt; int res; if (table_exists(info,suffix1,suffix2) == 0) return 0; if (!stralloc_copys(&line,"DROP TABLE ")) die_nomem(); if (!stralloc_cats(&line,info->base_table)) die_nomem(); if (!stralloc_cats(&line,suffix1)) die_nomem(); if (!stralloc_cats(&line,suffix2)) die_nomem(); if (!stralloc_0(&line)) die_nomem(); if ((stmt = _sqlquery(info, &line)) == NULL) return sqlite3_errmsg((sqlite3*)info->conn); res = sqlite3_step(stmt); sqlite3_finalize(stmt); if (res != SQLITE_DONE) return sqlite3_errmsg((sqlite3*)info->conn); return 0; }
void mx_list_fill( mx_list_t* ml, const char* domain ) { stralloc mxs = { 0 }; stralloc host = { 0 }; char* current; // create the "djb-string" and look up the MX servers stralloc_copys( &host, domain ); if ( 0 != dns_mx( &mxs, &host ) ) { fprintf( stderr, "Error resolving\n" ); exit( -1 ); } // now, for each MX, we resolve the hostname current = mxs.s; while ( current < ( mxs.s + mxs.len ) ) { int weight = 255 * current[ 0 ] + current[ 1 ]; current += 2; if ( mx_debug_mode ) fprintf( stdout, "%s\t\tMX\t%-2d\t%s\n", domain, weight, current ); // we have one hostname, look that up mx_list_fill_name( ml, current ); while ( *current != 0 ) current++; current++; } }
void encodeQ(const char *indata,unsigned int n,stralloc *outdata) /* converts any character with the high order bit set to */ /* quoted printable. In: n chars of indata, out: stralloc outdata*/ { char *cpout; char ch; unsigned int i; const char *cpin; cpin = indata; i = 0; /* max 3 outchars per inchar & 2 char newline per 72 chars */ stralloc_copys(outdata,""); stralloc_ready(outdata,n * 3 + n/36); /* worst case */ cpout = outdata->s; while (n--) { ch = *cpin++; if (ch != ' ' && ch != '\n' && ch != '\t' && (ch > 126 || ch < 33 || ch == 61)) { *(cpout++) = '='; *(cpout++) = hexchar[(ch >> 4) & 0xf]; *(cpout++) = hexchar[ch & 0xf]; i += 3; } else { if (ch == '\n')
/* Outputs all addresses in the table through subwrite. subwrite must be * a function returning >=0 on success, -1 on error, and taking * arguments (char* string, unsigned int length). It will be called once * per address and should take care of newline or whatever needed for * the output form. */ unsigned long sub_sql_putsubs(struct subdbinfo *info, const char *table, unsigned long hash_lo, unsigned long hash_hi, int subwrite()) /* write function. */ { void *result; unsigned long no = 0L; char strnum[FMT_ULONG]; stralloc_copyb(¶ms[0],strnum,fmt_ulong(strnum,hash_lo)); stralloc_copyb(¶ms[1],strnum,fmt_ulong(strnum,hash_hi)); make_name(info,table?"_":0,table,0); /* main query */ stralloc_copys(&query,"SELECT address FROM "); stralloc_cat(&query,&name); stralloc_cats(&query," WHERE "); stralloc_cats(&query,sql_putsubs_where_defn); result = sql_select(info,&query,2,params); no = 0; while (sql_fetch_row(info,result,1,&addr)) { if (subwrite(addr.s,addr.len) == -1) die_write(); no++; /* count for list-list fxn */ } sql_free_result(info,result); return no; }
void concatHDR(char *indata, unsigned int n, stralloc *outdata) /* takes a concatenated string of line and continuation line, trims leading */ /* and trailing LWSP and collapses line breaks and surrounding LWSP to ' '. */ /* indata has to end in \n or \0 or this routine will write beyond indata! */ /* if indata ends with \0, this will be changed to \n. */ { char *cp; char *cpout; char *cplast; if (!stralloc_copys(outdata,"")) die_nomem(); if (!stralloc_ready(outdata,n)) die_nomem(); cpout = outdata->s; if (n == 0) return; cplast = indata + n - 1; cp = cplast; while (*cplast == '\0' || *cplast == '\n') --cplast; if (cp == cplast) die_nomem(); /* just in case */ *(++cplast) = '\n'; /* have terminal '\n' */ cp = indata; while (cp <= cplast) { while (*cp == ' ' || *cp == '\t') ++cp; /* LWSP before */ while (*cp != '\n') *(cpout++) = *(cp++); /* text */ ++cp; /* skip \n */ --cpout; /* last char */ while (*cpout == ' ' || *cpout == '\t') --cpout; /* LWSP after */ *(++cpout) = ' '; /* replace with single ' ' */ ++cpout; /* point to free byte */ } outdata->len = cpout - outdata->s; }
int sln(const char* path) { stralloc s, d; char* to; ssize_t i; stralloc_init(&s); stralloc_copys(&s, path); stralloc_init(&d); stralloc_copy(&d, &s); while(reduce(&d)) { buffer_puts(buffer_2, "'"); buffer_putsa(buffer_2, &d); buffer_puts(buffer_2, "' -> '"); buffer_putsa(buffer_2, &s); buffer_puts(buffer_2, "'\n"); buffer_flush(buffer_2); stralloc_nul(&s); stralloc_nul(&d); if(mklink_sa(&s, &d) == -1) { errmsg_warnsys("symlink failed", NULL); exit(2); } stralloc_copy(&s, &d); } return 0; }
int main(int argc,char **argv) { int i; dns_random_init(seed); if (*argv) ++argv; while (*argv) { if (!stralloc_copys(&fqdn,*argv)) strerr_die2x(111,FATAL,"out of memory"); if (dns_ip4(&out,&fqdn) == -1) strerr_die4sys(111,FATAL,"unable to find IP address for ",*argv,": "); for (i = 0; i + 4 <= out.len; i += 4) { buffer_put(buffer_1,str,ip4_fmt(str,out.s + i)); buffer_puts(buffer_1," "); } buffer_puts(buffer_1,"\n"); ++argv; } buffer_flush(buffer_1); _exit(0); }
int read_file_config(void) { stralloc_copys(&oldflags,"-ABCDEFGHIJKLMNOPqRSTUVWXYZ"); oldflags.s[1] = "Aa"[exists("/archived")]; oldflags.s[2] = "Bb"[exists("/modgetonly")]; //oldflags.s[3] = "Cc"[exists("/ezmlmrc")]; /* Should always end up set */ oldflags.s[4] = "Dd"[exists("/digested")]; /* -e is not applicable */ oldflags.s[6] = "Ff"[exists("/prefix")]; oldflags.s[7] = "Gg"[exists("/subgetonly")]; oldflags.s[8] = "Hh"[exists("/nosubconfirm")]; oldflags.s[9] = "Ii"[exists("/threaded")]; oldflags.s[10] = "Jj"[exists("/nounsubconfirm")]; oldflags.s[11] = 'k'; /* -k is always enabled */ oldflags.s[12] = "Ll"[exists("/modcanlist")]; oldflags.s[13] = "Mm"[exists("/modpost")]; oldflags.s[14] = "Nn"[exists("/modcanedit")]; oldflags.s[15] = "Oo"[exists("/modpostonly")]; oldflags.s[16] = "Pp"[exists("/public")]; oldflags.s[17] = 'q'; /* -q is always enabled */ oldflags.s[18] = "Rr"[exists("/remote")]; oldflags.s[19] = "Ss"[exists("/modsub")]; oldflags.s[20] = "Tt"[exists("/addtrailer")]; oldflags.s[21] = "Uu"[exists("/subpostonly")]; /* -v is not applicable */ oldflags.s[23] = "Ww"[exists("/nowarn")]; oldflags.s[24] = "Xx"[exists("/mimeremove")]; oldflags.s[25] = "Yy"[exists("/confirmpost")]; /* -z is unused */ read_files(); return 1; }
int dns_mx_packet(stralloc *out,const char *buf,unsigned int len) { unsigned int pos; char header[12]; char pref[2]; uint16 numanswers; uint16 datalen; if (!stralloc_copys(out,"")) return -1; pos = dns_packet_copy(buf,len,0,header,12); if (!pos) return -1; uint16_unpack_big(header + 6,&numanswers); pos = dns_packet_skipname(buf,len,pos); if (!pos) return -1; pos += 4; while (numanswers--) { pos = dns_packet_skipname(buf,len,pos); if (!pos) return -1; pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) return -1; uint16_unpack_big(header + 8,&datalen); if (byte_equal(header,2,DNS_T_MX)) if (byte_equal(header + 2,2,DNS_C_IN)) { if (!dns_packet_copy(buf,len,pos,pref,2)) return -1; if (!dns_packet_getname(buf,len,pos + 2,&q)) return -1; if (!stralloc_catb(out,pref,2)) return -1; if (!dns_domain_todot_cat(out,q)) return -1; if (!stralloc_0(out)) return -1; } pos += datalen; } return 0; }
int dateline(stralloc *dt, unsigned long d) /* converts yyyymm from unsigned long d to text dt */ { const char *mo; switch (d % 100) { case 1: mo = "January"; break; case 2: mo = "February"; break; case 3: mo = "March"; break; case 4: mo = "April"; break; case 5: mo = "May"; break; case 6: mo = "June"; break; case 7: mo = "July"; break; case 8: mo = "August"; break; case 9: mo = "September"; break; case 10: mo = "October"; break; case 11: mo = "November"; break; case 12: mo = "December"; break; case 0: mo = "????"; break; default: cgierr("I don't know any month > 12", "",""); } if (!stralloc_copys(dt,mo)) return -1; if (!stralloc_cats(dt," ")) return -1; if ((d/100)) { if (!stralloc_catb(dt,strnum,fmt_ulong(strnum,d/100))) return -1; } else if (!stralloc_cats(dt,"????")) return 0; return 1; }
int dns_ip4_packet(stralloc *out,const char *buf,unsigned int len) { unsigned int pos; char header[12]; uint16 numanswers; uint16 datalen; if (!stralloc_copys(out,"")) return -1; pos = dns_packet_copy(buf,len,0,header,12); if (!pos) return -1; uint16_unpack_big(header + 6,&numanswers); pos = dns_packet_skipname(buf,len,pos); if (!pos) return -1; pos += 4; while (numanswers--) { pos = dns_packet_skipname(buf,len,pos); if (!pos) return -1; pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) return -1; uint16_unpack_big(header + 8,&datalen); if (byte_equal(header,2,DNS_T_A)) if (byte_equal(header + 2,2,DNS_C_IN)) if (datalen == 4) { if (!dns_packet_copy(buf,len,pos,header,4)) return -1; if (!stralloc_catb(out,header,4)) return -1; } pos += datalen; } dns_sortip(out->s,out->len); return 0; }
static int decode_prvs(const char *s) { /* The BATV standard says the user part should have the format * "tag-type=tag-val=loc-core" where "loc-core" is the original local * address, but all the examples I could find in actual use had the * last two parts reversed. What a mess. So, I have to check if * either the first or second part is a valid prvs tag, and use the * other one. */ int at; int sep; if (s[at = str_rchr(s,'@')] == 0) return 0; /* Format: [email protected] */ for (sep = 5; sep < at && s[sep] != '='; ++sep) ; if (sep >= at) return 0; if (is_prvs_tag(s+5,sep-5)) { if (!stralloc_copys(&realsender,s+sep+1)) die_nomem(); if (!stralloc_0(&realsender)) die_nomem(); return 1; } /* Format: [email protected] */ for (sep = at - 1; sep > 5 && s[sep] != '='; --sep) ; if (is_prvs_tag(s + sep + 1, at - sep - 1)) { if (!stralloc_copyb(&realsender,s+5,sep-5)) die_nomem(); if (!stralloc_cats(&realsender,s+at)) die_nomem(); if (!stralloc_0(&realsender)) die_nomem(); return 1; } return 0; }
int httpdate(stralloc *sa,struct tai *t) { struct caltime ct; int i; caltime_utc(&ct,t,&i,(int *) 0); if (i < 0) i = 0; if (i > 6) i = 6; if (!stralloc_copys(sa,weekday[i])) return 0; if (!stralloc_catuint0(sa,ct.date.day,2)) return 0; i = ct.date.month - 1; if (i < 0) i = 0; if (i > 11) i = 11; if (!stralloc_cats(sa,month[i])) return 0; if (!stralloc_catuint0(sa,ct.date.year,0)) return 0; if (!stralloc_cats(sa," ")) return 0; if (!stralloc_catuint0(sa,ct.hour,2)) return 0; if (!stralloc_cats(sa,":")) return 0; if (!stralloc_catuint0(sa,ct.minute,2)) return 0; if (!stralloc_cats(sa,":")) return 0; if (!stralloc_catuint0(sa,ct.second,2)) return 0; if (!stralloc_cats(sa," GMT")) return 0; return 1; }
int filter_start(stralloc *filter) { if (!stralloc_copys(filter, "")) return 0; if (objectclass.s != (char *)0 && objectclass.len != 0) { /* (&(objectclass=...)%searchfilter%) */ if (!stralloc_copys(filter, "(&(") || !stralloc_cats(filter, LDAP_OBJECTCLASS) || !stralloc_cats(filter, "=") || !stralloc_cat(filter, &objectclass) || !stralloc_cats(filter, ")")) return 0; } return 1; }
/* Checks the hash against the cookie table. If it matches, returns NULL, * else returns "". If error, returns error string. */ const char *sub_sql_checktag (struct subdbinfo *info, unsigned long num, /* message number */ unsigned long listno, /* bottom of range => slave */ const char *action, const char *seed, const char *hash) /* cookie */ { void *result; char strnum[FMT_ULONG]; /* SELECT msgnum FROM table_cookie WHERE msgnum=num and cookie='hash' */ /* succeeds only is everything correct. */ if (listno) { /* only for slaves */ stralloc_copyb(¶ms[0],strnum,fmt_ulong(strnum,listno)); stralloc_copyb(¶ms[1],strnum,fmt_ulong(strnum,num)); stralloc_copys(&query,"SELECT listno FROM "); stralloc_cats(&query,info->base_table); stralloc_cats(&query,"_mlog WHERE "); stralloc_cats(&query,sql_checktag_listno_where_defn); result = sql_select(info,&query,2,params); if (sql_fetch_row(info,result,1,params)) { sql_free_result(info,result); return ""; /* already done */ } /* no result */ sql_free_result(info,result); } stralloc_copyb(¶ms[0],strnum,fmt_ulong(strnum,num)); stralloc_copyb(¶ms[1],hash,COOKIE); stralloc_copys(&query,"SELECT msgnum FROM "); stralloc_cats(&query,info->base_table); stralloc_cats(&query,"_cookie WHERE "); stralloc_cats(&query,sql_checktag_msgnum_where_defn); result = sql_select(info,&query,2,params); if (!sql_fetch_row(info,result,1,params)) { sql_free_result(info,result); return ""; /* not parent => perm error */ } sql_free_result(info,result); /* success! cookie matches */ return (char *)0; (void)action; (void)seed; }
/* take an username and create a filename from it by prepending datadir, return it in \0 terminated tmpname */ void create_datafilename(stralloc *tmpname, stralloc *username) { /* create the filename in our datastructure */ if(!stralloc_copys(tmpname, datadir)) die_nomem(); if(!stralloc_cats(tmpname, "/")) die_nomem(); if(!stralloc_cat(tmpname, username)) die_nomem(); if(!stralloc_0(tmpname)) die_nomem(); }
int sub_sql_issub(struct subdbinfo *info, const char *table, const char *userhost, stralloc *recorded) { unsigned int j; void *result; int ret; /* SELECT address FROM list WHERE address = 'userhost' AND hash */ /* BETWEEN 0 AND 52. Without the hash restriction, we'd make it */ /* even easier to defeat. Just faking sender to the list name would*/ /* work. Since sender checks for posts are bogus anyway, I don't */ /* know if it's worth the cost of the "WHERE ...". */ make_name(info,table?"_":0,table,0); /* Lower-case the domain portion */ stralloc_copys(&addr,userhost); j = byte_rchr(addr.s,addr.len,'@'); if (j == addr.len) return 0; case_lowerb(addr.s + j + 1,addr.len - j - 1); stralloc_copys(&query,"SELECT address FROM "); stralloc_cat(&query,&name); stralloc_cats(&query," WHERE "); stralloc_cats(&query,sql_issub_where_defn); result = sql_select(info,&query,1,&addr); if (!sql_fetch_row(info,result,1,&addr)) ret = 0; else { /* we need to return the actual address as other dbs may accept * user-*@host, but we still want to make sure to send to e.g the * correct moderator address. */ if (recorded != 0) { stralloc_copy(recorded,&addr); stralloc_0(recorded); } ret = 1; } sql_free_result(info,result); return ret; }
int readclose(int fd,stralloc *sa,unsigned int bufsize) { if (!stralloc_copys(sa,"")) { close(fd); return -1; } return readclose_append(fd,sa,bufsize); }
void pop3_user(char *arg) { if (!*arg) { err_syntax(); return; } okay(); seenuser = 1; if (!stralloc_copys(&username,arg)) die_nomem(); if (!stralloc_0(&username)) die_nomem(); }