* Applied wesley.craig@umich.edu's KPOP patches. According to him:
[mmh] / uip / popsbr.c
1
2 /*
3  * popsbr.c -- POP client subroutines
4  *
5  * $Id$
6  */
7
8 #include <h/mh.h>
9
10 extern int  client(char *args, char *protocol, char *service, int rproto,
11                    char *response, int len_response);
12
13 #if defined(NNTP) && !defined(PSHSBR)
14 # undef NNTP
15 #endif
16
17 #ifdef NNTP                     /* building pshsbr.o from popsbr.c */
18 # include <h/nntp.h>
19 #endif /* NNTP */
20
21 #if !defined(NNTP) && defined(APOP)
22 # include <h/md5.h>
23 #endif
24
25 #include <h/popsbr.h>
26 #include <h/signals.h>
27 #include <signal.h>
28 #include <errno.h>
29
30 #define TRM     "."
31 #define TRMLEN  (sizeof TRM - 1)
32
33 static int poprint = 0;
34 static int pophack = 0;
35
36 char response[BUFSIZ];
37
38 static FILE *input;
39 static FILE *output;
40
41 #define targ_t char *
42
43 #if !defined(NNTP) && defined(MPOP)
44 # define command pop_command
45 # define multiline pop_multiline
46 #endif
47
48 #ifdef NNTP
49 # ifdef BPOP    /* stupid */
50 static int xtnd_last = -1;
51 static int xtnd_first = 0;
52 static char xtnd_name[512];     /* INCREDIBLE HACK!! */
53 # endif
54 #endif /* NNTP */
55
56 /*
57  * static prototypes
58  */
59 #if !defined(NNTP) && defined(APOP)
60 static char *pop_auth (char *, char *);
61 #endif
62
63 #if defined(NNTP) || !defined(MPOP)
64 /* otherwise they are not static functions */
65 static int command(const char *, ...);
66 static int multiline(void);
67 #endif
68
69 static int traverse (int (*)(), const char *, ...);
70 static int vcommand(const char *, va_list);
71 static int getline (char *, int, FILE *);
72 static int putline (char *, FILE *);
73
74
75 #if !defined(NNTP) && defined(APOP)
76 static char *
77 pop_auth (char *user, char *pass)
78 {
79     int len, buflen;
80     char *cp, *lp;
81     unsigned char *dp, *ep, digest[16];
82     MD5_CTX mdContext;
83     static char buffer[BUFSIZ];
84
85     if ((cp = strchr (response, '<')) == NULL
86             || (lp = strchr (cp, '>')) == NULL) {
87         snprintf (buffer, sizeof(buffer), "APOP not available: %s", response);
88         strncpy (response, buffer, sizeof(response));
89         return NULL;
90     }
91
92     *++lp = NULL;
93     snprintf (buffer, sizeof(buffer), "%s%s", cp, pass);
94
95     MD5Init (&mdContext);
96     MD5Update (&mdContext, (unsigned char *) buffer,
97                (unsigned int) strlen (buffer));
98     MD5Final (digest, &mdContext);
99
100     cp = buffer;
101     buflen = sizeof(buffer);
102
103     snprintf (cp, buflen, "%s ", user);
104     len = strlen (cp);
105     cp += len;
106     buflen -= len;
107
108     for (ep = (dp = digest) + sizeof(digest) / sizeof(digest[0]); dp < ep; ) {
109         snprintf (cp, buflen, "%02x", *dp++ & 0xff);
110         cp += 2;
111         buflen -= 2;
112     }
113     *cp = NULL;
114
115     return buffer;
116 }
117 #endif  /* !NNTP && APOP */
118
119
120 int
121 pop_init (char *host, char *user, char *pass, int snoop, int rpop, int kpop)
122 {
123     int fd1, fd2;
124     char buffer[BUFSIZ];
125
126 #ifdef APOP
127     int apop;
128
129     if ((apop = rpop) < 0)
130         rpop = 0;
131 #endif
132
133 #ifndef NNTP
134 # ifdef KPOP
135     if ( kpop ) {
136         snprintf (buffer, sizeof(buffer), "%s/%s", KPOP_PRINCIPAL, "kpop");
137         if ((fd1 = client (host, "tcp", buffer, 0, response, sizeof(response))) == NOTOK) {
138             return NOTOK;
139         }
140     } else {
141 # endif /* KPOP */
142         if ((fd1 = client (host, "tcp", POPSERVICE, rpop, response, sizeof(response))) == NOTOK)
143             return NOTOK;
144 # ifdef KPOP
145    }
146 # endif /* KPOP */
147 #else   /* NNTP */
148     if ((fd1 = client (host, "tcp", "nntp", rpop, response, sizeof(response))) == NOTOK)
149         return NOTOK;
150 #endif
151
152     if ((fd2 = dup (fd1)) == NOTOK) {
153         char *s;
154
155         if ((s = strerror(errno)))
156             snprintf (response, sizeof(response),
157                 "unable to dup connection descriptor: %s", s);
158         else
159             snprintf (response, sizeof(response),
160                 "unable to dup connection descriptor: unknown error");
161         close (fd1);
162         return NOTOK;
163     }
164 #ifndef NNTP
165     if (pop_set (fd1, fd2, snoop) == NOTOK)
166 #else   /* NNTP */
167     if (pop_set (fd1, fd2, snoop, (char *)0) == NOTOK)
168 #endif  /* NNTP */
169         return NOTOK;
170
171     SIGNAL (SIGPIPE, SIG_IGN);
172
173     switch (getline (response, sizeof response, input)) {
174         case OK: 
175             if (poprint)
176                 fprintf (stderr, "<--- %s\n", response);
177 #ifndef NNTP
178             if (*response == '+') {
179 # ifndef KPOP
180 #  ifdef APOP
181                 if (apop < 0) {
182                     char *cp = pop_auth (user, pass);
183
184                     if (cp && command ("APOP %s", cp) != NOTOK)
185                         return OK;
186                 }
187                 else
188 #  endif /* APOP */
189                 if (command ("USER %s", user) != NOTOK
190                     && command ("%s %s", rpop ? "RPOP" : (pophack++, "PASS"),
191                                         pass) != NOTOK)
192                 return OK;
193 # else /* KPOP */
194                 if (command ("USER %s", user) != NOTOK
195                     && command ("PASS %s", pass) != NOTOK)
196                 return OK;
197 # endif
198             }
199 #else /* NNTP */
200             if (*response < CHAR_ERR) {
201                 command ("MODE READER");
202                 return OK;
203             }
204 #endif
205             strncpy (buffer, response, sizeof(buffer));
206             command ("QUIT");
207             strncpy (response, buffer, sizeof(response));
208                                 /* and fall */
209
210         case NOTOK: 
211         case DONE: 
212             if (poprint)            
213                 fprintf (stderr, "%s\n", response);
214             fclose (input);
215             fclose (output);
216             return NOTOK;
217     }
218
219     return NOTOK;       /* NOTREACHED */
220 }
221
222 #ifdef NNTP
223 int
224 pop_set (int in, int out, int snoop, char *myname)
225 #else
226 int
227 pop_set (int in, int out, int snoop)
228 #endif
229 {
230
231 #ifdef NNTP
232     if (myname && *myname) {
233         /* interface from bbc to msh */
234         strncpy (xtnd_name, myname, sizeof(xtnd_name));
235     }
236 #endif  /* NNTP */
237
238     if ((input = fdopen (in, "r")) == NULL
239             || (output = fdopen (out, "w")) == NULL) {
240         strncpy (response, "fdopen failed on connection descriptor", sizeof(response));
241         if (input)
242             fclose (input);
243         else
244             close (in);
245         close (out);
246         return NOTOK;
247     }
248
249     poprint = snoop;
250
251     return OK;
252 }
253
254
255 int
256 pop_fd (char *in, int inlen, char *out, int outlen)
257 {
258     snprintf (in, inlen, "%d", fileno (input));
259     snprintf (out, outlen, "%d", fileno (output));
260     return OK;
261 }
262
263
264 /*
265  * Find out number of messages available
266  * and their total size.
267  */
268
269 int
270 pop_stat (int *nmsgs, int *nbytes)
271 {
272 #ifdef NNTP
273     char **ap;
274 #endif /* NNTP */
275
276 #ifndef NNTP
277     if (command ("STAT") == NOTOK)
278         return NOTOK;
279
280     *nmsgs = *nbytes = 0;
281     sscanf (response, "+OK %d %d", nmsgs, nbytes);
282
283 #else /* NNTP */
284     if (xtnd_last < 0) {        /* in msh, xtnd_name is set from myname */
285         if (command("GROUP %s", xtnd_name) == NOTOK)
286             return NOTOK;
287
288         ap = brkstring (response, " ", "\n"); /* "211 nart first last ggg" */
289         xtnd_first = atoi (ap[2]);
290         xtnd_last  = atoi (ap[3]);
291     }
292
293     /* nmsgs is not the real nart, but an incredible simuation */
294     if (xtnd_last > 0)
295         *nmsgs = xtnd_last - xtnd_first + 1;    /* because of holes... */
296     else
297         *nmsgs = 0;
298     *nbytes = xtnd_first;       /* for subtracting offset in msh() */
299 #endif /* NNTP */
300
301     return OK;
302 }
303
304 #ifdef NNTP
305 int
306 pop_exists (int (*action)())
307 {
308 #ifdef XMSGS            /* hacked into NNTP 1.5 */
309     if (traverse (action, "XMSGS %d-%d", (targ_t) xtnd_first, (targ_t) xtnd_last) == OK)
310         return OK;
311 #endif
312     /* provided by INN 1.4 */
313     if (traverse (action, "LISTGROUP") == OK)
314         return OK;
315     return traverse (action, "XHDR NONAME %d-%d", (targ_t) xtnd_first, (targ_t) xtnd_last);
316 }
317 #endif  /* NNTP */
318
319
320 #ifdef BPOP
321 int
322 pop_list (int msgno, int *nmsgs, int *msgs, int *bytes, int *ids)
323 #else
324 int
325 pop_list (int msgno, int *nmsgs, int *msgs, int *bytes)
326 #endif
327 {
328     int i;
329 #ifndef BPOP
330     int *ids = NULL;
331 #endif
332
333     if (msgno) {
334 #ifndef NNTP
335         if (command ("LIST %d", msgno) == NOTOK)
336             return NOTOK;
337         *msgs = *bytes = 0;
338         if (ids) {
339             *ids = 0;
340             sscanf (response, "+OK %d %d %d", msgs, bytes, ids);
341         }
342         else
343             sscanf (response, "+OK %d %d", msgs, bytes);
344 #else /* NNTP */
345         *msgs = *bytes = 0;
346         if (command ("STAT %d", msgno) == NOTOK) 
347             return NOTOK;
348         if (ids) {
349             *ids = msgno;
350         }
351 #endif /* NNTP */
352         return OK;
353     }
354
355 #ifndef NNTP
356     if (command ("LIST") == NOTOK)
357         return NOTOK;
358
359     for (i = 0; i < *nmsgs; i++)
360         switch (multiline ()) {
361             case NOTOK: 
362                 return NOTOK;
363             case DONE: 
364                 *nmsgs = ++i;
365                 return OK;
366             case OK: 
367                 *msgs = *bytes = 0;
368                 if (ids) {
369                     *ids = 0;
370                     sscanf (response, "%d %d %d",
371                             msgs++, bytes++, ids++);
372                 }
373                 else
374                     sscanf (response, "%d %d", msgs++, bytes++);
375                 break;
376         }
377     for (;;)
378         switch (multiline ()) {
379             case NOTOK: 
380                 return NOTOK;
381             case DONE: 
382                 return OK;
383             case OK: 
384                 break;
385         }
386 #else /* NNTP */
387     return NOTOK;
388 #endif /* NNTP */
389 }
390
391
392 int
393 pop_retr (int msgno, int (*action)())
394 {
395 #ifndef NNTP
396     return traverse (action, "RETR %d", (targ_t) msgno);
397 #else /* NNTP */
398     return traverse (action, "ARTICLE %d", (targ_t) msgno);
399 #endif /* NNTP */
400 }
401
402
403 static int
404 traverse (int (*action)(), const char *fmt, ...)
405 {
406     int result;
407     va_list ap;
408     char buffer[sizeof(response)];
409
410     va_start(ap, fmt);
411     result = vcommand (fmt, ap);
412     va_end(ap);
413
414     if (result == NOTOK)
415         return NOTOK;
416     strncpy (buffer, response, sizeof(buffer));
417
418     for (;;)
419         switch (multiline ()) {
420             case NOTOK: 
421                 return NOTOK;
422
423             case DONE: 
424                 strncpy (response, buffer, sizeof(response));
425                 return OK;
426
427             case OK: 
428                 (*action) (response);
429                 break;
430         }
431 }
432
433
434 int
435 pop_dele (int msgno)
436 {
437     return command ("DELE %d", msgno);
438 }
439
440
441 int
442 pop_noop (void)
443 {
444     return command ("NOOP");
445 }
446
447
448 #if defined(MPOP) && !defined(NNTP)
449 int
450 pop_last (void)
451 {
452     return command ("LAST");
453 }
454 #endif
455
456
457 int
458 pop_rset (void)
459 {
460     return command ("RSET");
461 }
462
463
464 int
465 pop_top (int msgno, int lines, int (*action)())
466 {
467 #ifndef NNTP
468     return traverse (action, "TOP %d %d", (targ_t) msgno, (targ_t) lines);
469 #else   /* NNTP */
470     return traverse (action, "HEAD %d", (targ_t) msgno);
471 #endif /* NNTP */
472 }
473
474
475 #ifdef BPOP
476 int
477 pop_xtnd (int (*action)(), char *fmt, ...)
478 {
479     int result;
480     va_list ap;
481     char buffer[BUFSIZ];
482
483 #ifdef NNTP
484     char **ap;
485 #endif
486
487     va_start(ap, fmt);
488 #ifndef NNTP
489     /* needs to be fixed... va_end needs to be added */
490     snprintf (buffer, sizeof(buffer), "XTND %s", fmt);
491     result = traverse (action, buffer, a, b, c, d);
492     va_end(ap);
493     return result;
494 #else /* NNTP */
495     snprintf (buffer, sizeof(buffer), fmt, a, b, c, d);
496     ap = brkstring (buffer, " ", "\n"); /* a hack, i know... */
497
498     if (!strcasecmp(ap[0], "x-bboards")) {      /* XTND "X-BBOARDS group */
499         /* most of these parameters are meaningless under NNTP. 
500          * bbc.c was modified to set AKA and LEADERS as appropriate,
501          * the rest are left blank.
502          */
503         return OK;
504     }
505     if (!strcasecmp (ap[0], "archive") && ap[1]) {
506         snprintf (xtnd_name, sizeof(xtnd_name), "%s", ap[1]);   /* save the name */
507         xtnd_last = 0;
508         xtnd_first = 1;         /* setup to fail in pop_stat */
509         return OK;
510     }
511     if (!strcasecmp (ap[0], "bboards")) {
512
513         if (ap[1]) {                    /* XTND "BBOARDS group" */
514             snprintf (xtnd_name, sizeof(xtnd_name), "%s", ap[1]);       /* save the name */
515             if (command("GROUP %s", xtnd_name) == NOTOK)
516                 return NOTOK;
517
518             /* action must ignore extra args */
519             strncpy (buffer, response, sizeof(buffer));
520             ap = brkstring (response, " ", "\n");/* "211 nart first last g" */
521             xtnd_first = atoi (ap[2]);
522             xtnd_last  = atoi (ap[3]);
523
524             (*action) (buffer);         
525             return OK;
526
527         } else {                /* XTND "BBOARDS" */
528             return traverse (action, "LIST", a, b, c, d);
529         }
530     }
531     return NOTOK;       /* unknown XTND command */
532 #endif /* NNTP */
533 }
534 #endif BPOP
535
536
537 int
538 pop_quit (void)
539 {
540     int i;
541
542     i = command ("QUIT");
543     pop_done ();
544
545     return i;
546 }
547
548
549 int
550 pop_done (void)
551 {
552     fclose (input);
553     fclose (output);
554
555     return OK;
556 }
557
558
559 #if !defined(MPOP) || defined(NNTP)
560 static
561 #endif
562 int
563 command(const char *fmt, ...)
564 {
565     va_list ap;
566     int result;
567
568     va_start(ap, fmt);
569     result = vcommand(fmt, ap);
570     va_end(ap);
571
572     return result;
573 }
574
575
576 static int
577 vcommand (const char *fmt, va_list ap)
578 {
579     char *cp, buffer[BUFSIZ];
580
581     vsnprintf (buffer, sizeof(buffer), fmt, ap);
582     if (poprint) {
583         if (pophack) {
584             if ((cp = strchr (buffer, ' ')))
585                 *cp = 0;
586             fprintf (stderr, "---> %s ********\n", buffer);
587             if (cp)
588                 *cp = ' ';
589             pophack = 0;
590         }
591         else
592             fprintf (stderr, "---> %s\n", buffer);
593     }
594
595     if (putline (buffer, output) == NOTOK)
596         return NOTOK;
597
598     switch (getline (response, sizeof response, input)) {
599         case OK: 
600             if (poprint)
601                 fprintf (stderr, "<--- %s\n", response);
602 #ifndef NNTP
603             return (*response == '+' ? OK : NOTOK);
604 #else   /* NNTP */
605             return (*response < CHAR_ERR ? OK : NOTOK);
606 #endif  /* NNTP */
607
608         case NOTOK: 
609         case DONE: 
610             if (poprint)            
611                 fprintf (stderr, "%s\n", response);
612             return NOTOK;
613     }
614
615     return NOTOK;       /* NOTREACHED */
616 }
617
618
619 #if defined(MPOP) && !defined(NNTP)
620 int
621 multiline (void)
622 #else
623 static int
624 multiline (void)
625 #endif
626 {
627     char buffer[BUFSIZ + TRMLEN];
628
629     if (getline (buffer, sizeof buffer, input) != OK)
630         return NOTOK;
631 #ifdef DEBUG
632     if (poprint)
633         fprintf (stderr, "<--- %s\n", response);
634 #endif DEBUG
635     if (strncmp (buffer, TRM, TRMLEN) == 0) {
636         if (buffer[TRMLEN] == 0)
637             return DONE;
638         else
639             strncpy (response, buffer + TRMLEN, sizeof(response));
640     }
641     else
642         strncpy (response, buffer, sizeof(response));
643
644     return OK;
645 }
646
647
648 static int
649 getline (char *s, int n, FILE *iop)
650 {
651     int c;
652     char *p;
653
654     p = s;
655     while (--n > 0 && (c = fgetc (iop)) != EOF)
656         if ((*p++ = c) == '\n')
657             break;
658     if (ferror (iop) && c != EOF) {
659         strncpy (response, "error on connection", sizeof(response));
660         return NOTOK;
661     }
662     if (c == EOF && p == s) {
663         strncpy (response, "connection closed by foreign host", sizeof(response));
664         return DONE;
665     }
666     *p = 0;
667     if (*--p == '\n')
668         *p = 0;
669     if (*--p == '\r')
670         *p = 0;
671
672     return OK;
673 }
674
675
676 static int
677 putline (char *s, FILE *iop)
678 {
679     fprintf (iop, "%s\r\n", s);
680     fflush (iop);
681     if (ferror (iop)) {
682         strncpy (response, "lost connection", sizeof(response));
683         return NOTOK;
684     }
685
686     return OK;
687 }