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 "logmsg.h"
9#include "errtxt.h"
10#include "subscribe.h"
11#include <unistd.h>
12#include <libpq-fe.h>
13
14#define WHO "checktag"
15
21
22extern PGconn *pgsql;
23
24static stralloc key = {0};
25static stralloc line = {0};
26static char strnum[FMT_ULONG];
27static char newcookie[COOKIE];
28
42
43const char *checktag(const char *dir,unsigned long num,unsigned long listno,const char *action,const char *seed,const char *hash)
44{
45 PGresult *result; /* int row; */
46 const char *table = (char *) 0;
47 const char *r;
48
49 if ((r = opensql(dir,&table))) {
50 if (*r) return r;
51 if (!seed) return (char *) 0; /* no data - accept */
52
53 strnum[fmt_ulong(strnum,num)] = '\0'; /* message nr ->string*/
54
55 switch (openreadclose("key",&key,32)) {
56 case -1: return ERR_READ_KEY;
57 case 0: return ERR_NOEXIST_KEY;
58 }
59
60 cookie(newcookie,key.s,key.len,strnum,seed,action);
61
62 if (byte_diff(hash,COOKIE,newcookie)) return "";
63 else return (char *) 0;
64
65 } else {
66
67 /* SELECT msgnum FROM table_cookie WHERE msgnum=num and cookie='hash' */
68 /* succeeds only is everything correct. 'hash' is quoted since it is */
69 /* potentially hostile. */
70
71 if (listno) { /* only for slaves */
72 if (!stralloc_copys(&line,"SELECT listno FROM ")) return ERR_NOMEM;
73 if (!stralloc_cats(&line,table)) return ERR_NOMEM;
74 if (!stralloc_cats(&line,"_mlog WHERE listno=")) return ERR_NOMEM;
75 if (!stralloc_catb(&line,strnum,fmt_ulong(strnum,listno))) return ERR_NOMEM;
76 if (!stralloc_cats(&line," AND msgnum=")) return ERR_NOMEM;
77 if (!stralloc_catb(&line,strnum,fmt_ulong(strnum,num))) return ERR_NOMEM;
78 if (!stralloc_cats(&line," AND done > 3")) return ERR_NOMEM;
79 if (!stralloc_0(&line)) return ERR_NOMEM;
80
81 result = PQexec(pgsql,line.s);
82 if (result == NULL)
83 return (PQerrorMessage(pgsql));
84 if (PQresultStatus(result) != PGRES_TUPLES_OK)
85 return (char *) (PQresultErrorMessage(result));
86 if (PQntuples(result) > 0) {
87 PQclear(result);
88 return("");
89 } else
90 PQclear(result);
91 }
92
93 if (!stralloc_copys(&line,"SELECT msgnum FROM ")) return ERR_NOMEM;
94 if (!stralloc_cats(&line,table)) return ERR_NOMEM;
95 if (!stralloc_cats(&line,"_cookie WHERE msgnum=")) return ERR_NOMEM;
96 if (!stralloc_catb(&line,strnum,fmt_ulong(strnum,num))) return ERR_NOMEM;
97 if (!stralloc_cats(&line," and cookie='")) return ERR_NOMEM;
98 if (!stralloc_catb(&line,strnum,fmt_str(strnum,hash))) return ERR_NOMEM;
99 if (!stralloc_cats(&line,"'")) return ERR_NOMEM;
100 if (!stralloc_0(&line)) return ERR_NOMEM;
101
102 result = PQexec(pgsql,line.s);
103 if (result == NULL)
104 return (PQerrorMessage(pgsql));
105 if (PQresultStatus(result) != PGRES_TUPLES_OK)
106 return (char *) (PQresultErrorMessage(result));
107 if(PQntuples(result) < 0) {
108 PQclear( result );
109 return("");
110 }
111
112 PQclear(result);
113 if (listno)
114 loginfo(dir,num,listno,0L,3); /* non-ess mysql logging */
115 return (char *)0;
116 }
117}
#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 action
Definition ezmlm-store.c:84
PGconn * pgsql
Definition opensql.c:22