Import ezmlm-idx 0.40
[ezmlm] / sub_pgsql / checktag.c
1 /*$Id: checktag.c,v 1.3 1999/12/23 02:40:57 lindberg Exp $*/
2 /*$Name: ezmlm-idx-040 $*/
3 #include "stralloc.h"
4 #include "scan.h"
5 #include "fmt.h"
6 #include "cookie.h"
7 #include "makehash.h"
8 #include "strerr.h"
9 #include "errtxt.h"
10 #include "subscribe.h"
11 #include <unistd.h>
12 #include <libpq-fe.h>
13
14 static stralloc key = {0};
15 static stralloc line = {0};
16 static strnum[FMT_ULONG];
17 static newcookie[COOKIE];
18
19 char *checktag (dir,num,listno,action,seed,hash)
20 /* reads dir/sql. If not present, returns success (NULL). If dir/sql is */
21 /* present, checks hash against the cookie table. If match, returns success*/
22 /* (NULL), else returns "". If error, returns error string. */
23
24
25 char *dir; /* the db base dir */
26 unsigned long num; /* message number */
27 unsigned long listno; /* bottom of range => slave */
28 char *action;
29 char *seed; /* cookie base */
30 char *hash; /* cookie */
31 {
32 PGresult *result;
33 /* int row; */
34 char *table = (char *) 0;
35 char *r;
36
37 if ((r = opensql(dir,&table))) {
38 if (*r) return r;
39 if (!seed) return (char *) 0; /* no data - accept */
40
41 strnum[fmt_ulong(strnum,num)] = '\0'; /* message nr ->string*/
42
43 switch(slurp("key",&key,32)) {
44 case -1:
45 return ERR_READ_KEY;
46 case 0:
47 return ERR_NOEXIST_KEY;
48 }
49
50 cookie(newcookie,key.s,key.len,strnum,seed,action);
51 if (byte_diff(hash,COOKIE,newcookie)) return "";
52 else return (char *) 0;
53
54 } else {
55
56 /* SELECT msgnum FROM table_cookie WHERE msgnum=num and cookie='hash' */
57 /* succeeds only is everything correct. 'hash' is quoted since it is */
58 /* potentially hostile. */
59 if (listno) { /* only for slaves */
60 if (!stralloc_copys(&line,"SELECT listno FROM ")) return ERR_NOMEM;
61 if (!stralloc_cats(&line,table)) return ERR_NOMEM;
62 if (!stralloc_cats(&line,"_mlog WHERE listno=")) return ERR_NOMEM;
63 if (!stralloc_catb(&line,strnum,fmt_ulong(strnum,listno)))
64 return ERR_NOMEM;
65 if (!stralloc_cats(&line," AND msgnum=")) return ERR_NOMEM;
66 if (!stralloc_catb(&line,strnum,fmt_ulong(strnum,num))) return ERR_NOMEM;
67 if (!stralloc_cats(&line," AND done > 3")) return ERR_NOMEM;
68
69 if (!stralloc_0(&line)) return ERR_NOMEM;
70 result = PQexec( psql, line.s );
71 if(result == NULL)
72 return (PQerrorMessage(psql));
73 if( PQresultStatus(result) != PGRES_TUPLES_OK)
74 return (char *) (PQresultErrorMessage(result));
75 if( PQntuples(result) > 0 ) {
76 PQclear(result);
77 return("");
78 } else
79 PQclear(result);
80 }
81
82 if (!stralloc_copys(&line,"SELECT msgnum FROM ")) return ERR_NOMEM;
83 if (!stralloc_cats(&line,table)) return ERR_NOMEM;
84 if (!stralloc_cats(&line,"_cookie WHERE msgnum=")) return ERR_NOMEM;
85 if (!stralloc_catb(&line,strnum,fmt_ulong(strnum,num))) return ERR_NOMEM;
86 if (!stralloc_cats(&line," and cookie='")) return ERR_NOMEM;
87 if (!stralloc_catb(&line,strnum,fmt_ulong(strnum,hash))) return ERR_NOMEM;
88 if (!stralloc_cats(&line,"'")) return ERR_NOMEM;
89
90 if (!stralloc_0(&line)) return ERR_NOMEM;
91 result = PQexec(psql,line.s);
92 if (result == NULL)
93 return (PQerrorMessage(psql));
94 if (PQresultStatus(result) != PGRES_TUPLES_OK)
95 return (char *) (PQresultErrorMessage(result));
96 if(PQntuples(result) < 0) {
97 PQclear( result );
98 return("");
99 }
100
101 PQclear(result);
102 if (listno)
103 (void) logmsg(dir,num,listno,0L,3); /* non-ess mysql logging */
104 return (char *)0;
105 }
106 }