ezmlmx 0.68
ezmlmx
Loading...
Searching...
No Matches
checktag.c
Go to the documentation of this file.
1#include "slurp.h"
2#include "byte.h"
3#include "stralloc.h"
4#include "scan.h"
5#include "fmt.h"
6#include "cookie.h"
7#include "makehash.h"
8#include "subscribe.h"
9#include <mysql/mysql.h>
10#include "errtxt.h"
11#include "logmsg.h"
12
13#define WHO "checktag"
14
20
21extern MYSQL *mysql;
22
23static stralloc key = {0};
24static stralloc line = {0};
25static stralloc quoted = {0};
26static char strnum[FMT_ULONG];
27static char newcookie[COOKIE];
28
42
43const char *checktag(const char *dir,unsigned long num,unsigned long listno,
44 const char *action,const char *seed, const char *hash)
45{
46 MYSQL_RES *result;
47 MYSQL_ROW row;
48 const char *table = (char *) 0;
49 const char *r;
50
51 if ((r = opensql(dir,&table))) {
52 if (*r) return r;
53 if (!seed) return (char *) 0; /* no data - accept */
54
55 strnum[fmt_ulong(strnum,num)] = '\0'; /* message nr ->string*/
56
57 switch(slurp("key",&key,32)) {
58 case -1: return ERR_READ_KEY;
59 case 0: return ERR_NOEXIST_KEY;
60 }
61
62 cookie(newcookie,key.s,key.len,strnum,seed,action);
63 if (byte_diff(hash,COOKIE,newcookie)) return "";
64 else return (char *) 0;
65
66 } else {
67
68/* SELECT msgnum FROM table_cookie WHERE msgnum=num and cookie='hash' */
69/* succeeds only is everything correct. 'hash' is quoted since it is */
70/* potentially hostile. */
71
72 if (listno) { /* only for slaves */
73 if (!stralloc_copys(&line,"SELECT listno FROM ")) return ERR_NOMEM;
74 if (!stralloc_cats(&line,table)) return ERR_NOMEM;
75 if (!stralloc_cats(&line,"_mlog WHERE listno=")) return ERR_NOMEM;
76 if (!stralloc_catb(&line,strnum,fmt_ulong(strnum,listno))) return ERR_NOMEM;
77 if (!stralloc_cats(&line," AND msgnum=")) return ERR_NOMEM;
78 if (!stralloc_catb(&line,strnum,fmt_ulong(strnum,num))) return ERR_NOMEM;
79 if (!stralloc_cats(&line," AND done > 3")) return ERR_NOMEM;
80 if (mysql_real_query(mysql,line.s,line.len) != 0)
81 return mysql_error(mysql); /* query */
82
83 if (!(result = mysql_use_result(mysql)))
84 return mysql_error(mysql); /* use result */
85
86 return mysql_error(mysql);
87
88 if ((row = mysql_fetch_row(result)))
89 return ""; /* already done */
90 else /* no result */
91 if (!mysql_eof(result))
92 return mysql_error(mysql);
93
94 mysql_free_result(result); /* free res */
95 }
96
97 if (!stralloc_copys(&line,"SELECT msgnum FROM ")) return ERR_NOMEM;
98 if (!stralloc_cats(&line,table)) return ERR_NOMEM;
99 if (!stralloc_cats(&line,"_cookie WHERE msgnum=")) return ERR_NOMEM;
100 if (!stralloc_catb(&line,strnum,fmt_ulong(strnum,num))) return ERR_NOMEM;
101 if (!stralloc_cats(&line," and cookie='")) return ERR_NOMEM;
102 if (!stralloc_ready(&quoted,COOKIE * 2 + 1)) return ERR_NOMEM;
103 quoted.len = mysql_escape_string(quoted.s,hash,COOKIE);
104 if (!stralloc_cat(&line,&quoted)) return ERR_NOMEM;
105 if (!stralloc_cats(&line,"'")) return ERR_NOMEM;
106
107 if (mysql_real_query(mysql,line.s,line.len) != 0) /* select */
108 return mysql_error(mysql);
109
110 if (!(result = mysql_use_result(mysql)))
111 return mysql_error(mysql);
112
113 if (!mysql_fetch_row(result)) {
114 if (!mysql_eof(result)) /* some error occurred */
115 return mysql_error(mysql);
116 mysql_free_result(result); /* eof => query ok, but null result*/
117 return ""; /* not parent => perm error */
118 }
119 mysql_free_result(result); /* success! cookie matches */
120 if (listno)
121 loginfo(dir,num,listno,0L,3); /* non-ess mysql logging */
122 return (char *)0;
123 }
124}
int slurp(const char *fn, struct stralloc *sa, int bufsize)
#define COOKIE
Definition cookie.h:4
Error messages. If you translate these, I would urge you to keep the English version as well....
#define ERR_NOMEM
Definition errtxt.h:14
#define ERR_NOEXIST_KEY
Definition errtxt.h:41
#define ERR_READ_KEY
Definition errtxt.h:19
const char * loginfo(const char *dir, unsigned long msgnum, unsigned long, unsigned long subs, int done)
Definition loginfo.c:12
const char * opensql(const char *dir, const char **table)
Definition opensql.c:13
stralloc listno
Definition ezmlm-get.c:78
stralloc seed
Definition ezmlm-get.c:74
stralloc num
char * dir
void cookie(char *hash, const char *key, unsigned int keylen, const char *date, const char *addr, const char *action)
Definition cookie.c:14
const char * checktag(const char *dir, unsigned long num, unsigned long listno, const char *action, const char *seed, const char *hash)
Definition checktag.c:27
stralloc quoted
Definition ezmlm-clean.c:90
stralloc action
Definition ezmlm-store.c:84
MYSQL * mysql
Definition opensql.c:23