1 /*$Id: checktag.c,v 1.3 1999/12/23 02:40:57 lindberg Exp $*/
2 /*$Name: ezmlm-idx-040 $*/
10 #include "subscribe.h"
14 static stralloc key
= {0};
15 static stralloc line
= {0};
16 static strnum
[FMT_ULONG
];
17 static newcookie
[COOKIE
];
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. */
25 char *dir
; /* the db base dir */
26 unsigned long num
; /* message number */
27 unsigned long listno
; /* bottom of range => slave */
29 char *seed
; /* cookie base */
30 char *hash
; /* cookie */
34 char *table
= (char *) 0;
37 if ((r
= opensql(dir
,&table
))) {
39 if (!seed
) return (char *) 0; /* no data - accept */
41 strnum
[fmt_ulong(strnum
,num
)] = '\0'; /* message nr ->string*/
43 switch(slurp("key",&key
,32)) {
47 return ERR_NOEXIST_KEY
;
50 cookie(newcookie
,key
.s
,key
.len
,strnum
,seed
,action
);
51 if (byte_diff(hash
,COOKIE
,newcookie
)) return "";
52 else return (char *) 0;
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
)))
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
;
69 if (!stralloc_0(&line
)) return ERR_NOMEM
;
70 result
= PQexec( psql
, line
.s
);
72 return (PQerrorMessage(psql
));
73 if( PQresultStatus(result
) != PGRES_TUPLES_OK
)
74 return (char *) (PQresultErrorMessage(result
));
75 if( PQntuples(result
) > 0 ) {
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
;
90 if (!stralloc_0(&line
)) return ERR_NOMEM
;
91 result
= PQexec(psql
,line
.s
);
93 return (PQerrorMessage(psql
));
94 if (PQresultStatus(result
) != PGRES_TUPLES_OK
)
95 return (char *) (PQresultErrorMessage(result
));
96 if(PQntuples(result
) < 0) {
103 (void) logmsg(dir
,num
,listno
,0L,3); /* non-ess mysql logging */