10 #define MAX_SECRET 100
12 static void ohshit(const char *p) {
13 fprintf(stderr,"md5cookie1way: fatal error: %s\n",p); exit(1);
16 static void ohshite(const char *p) {
17 fprintf(stderr,"md5cookie1way: fatal system error: %s: %s\n",p,strerror(errno));
21 static void try(FILE **fp, const char *fn) {
26 static int parsehex(char *p, unsigned char *up, int maxlen) {
30 while (nused < maxlen && (n=-1, sscanf(p,"%x%n",&v,&n) >0) && n>0) {
39 int main(int argc, char **argv) {
40 char *authfds, *claim, *here, *p;
41 int rfd, wfd, n, nused;
43 unsigned char message[16+MAX_SECRET], reply[16];
44 struct MD5Context md5ctx;
47 if (argc != 2) ohshit("bad args");
50 authfds= getenv("NNTP_AUTH_FDS"); if (!authfds) ohshit("no NNTP_AUTH_FDS");
51 p= strchr(authfds,'.'); if (!p) ohshit("no . in NNTP_AUTH_FDS");
54 dup2(rfd,0); dup2(wfd,1);
57 if ((p= getenv("HOME"))) {
58 sprintf(buf,"%.180s/News/md5cookies",p); try(&file,buf);
59 sprintf(buf,"%.180s/.newscookies",p); try(&file,buf);
61 try(&file,"/etc/news/md5cookies.read");
62 try(&file,"/etc/news/md5cookies");
63 if (!file) ohshite("no cookies file");
66 errno= 0; if (!(fgets(buf,sizeof(buf),file))) ohshite("no line for claim");
67 if (!*buf || *buf == '\n' || *buf == '#' || *buf == '@') continue;
68 here= strtok(buf," \t\n");
70 if (!strcmp(here,claim)) break;
74 if (!p) ohshit("no cookie on line");
76 nused= parsehex(p,message+16,MAX_SECRET);
78 errno= 0; if (!fgets(buf,sizeof(buf),stdin)) ohshite("no comm with server");
79 if (strncmp(buf,"100 ",4)) {
80 fprintf(stderr,"\nunable to authenticate - server sent:\n %s",buf);
84 if (parsehex(buf+4,message,17) != 16) ohshit("server sent wrong amount of hex");
87 MD5Update(&md5ctx,message,16+nused);
88 MD5Final(reply,&md5ctx);
91 for (n=0; n<16; n++) {
92 printf("%s%02x", n?":":"", reply[n]);
97 errno= 0; if (!fgets(buf,sizeof(buf),stdin)) ohshite("no reply from server");
98 if (strncmp(buf,"281",3)) ohshit("server didn't send 281");