]> diplodocus.org Git - nmh/blob - uip/popsbr.c
fix Debian bug #202667: crash when a message's filename overflows an int
[nmh] / uip / popsbr.c
1 /*
2 * popsbr.c -- POP client subroutines
3 *
4 * $Id$
5 *
6 * This code is Copyright (c) 2002, by the authors of nmh. See the
7 * COPYRIGHT file in the root directory of the nmh distribution for
8 * complete copyright information.
9 */
10
11 #include <h/mh.h>
12
13 extern int client(char *args, char *protocol, char *service, int rproto,
14 char *response, int len_response);
15
16 #if defined(NNTP) && !defined(PSHSBR)
17 # undef NNTP
18 #endif
19
20 #ifdef NNTP /* building pshsbr.o from popsbr.c */
21 # include <h/nntp.h>
22 #endif /* NNTP */
23
24 #if !defined(NNTP) && defined(APOP)
25 # include <h/md5.h>
26 #endif
27
28 #ifdef CYRUS_SASL
29 # include <sasl.h>
30 # include <saslutil.h>
31 #endif /* CYRUS_SASL */
32
33 #include <h/popsbr.h>
34 #include <h/signals.h>
35 #include <signal.h>
36 #include <errno.h>
37
38 #define TRM "."
39 #define TRMLEN (sizeof TRM - 1)
40
41 static int poprint = 0;
42 static int pophack = 0;
43
44 char response[BUFSIZ];
45
46 static FILE *input;
47 static FILE *output;
48
49 #define targ_t char *
50
51 #if !defined(NNTP) && defined(MPOP)
52 # define command pop_command
53 # define multiline pop_multiline
54 #endif
55
56 #ifdef NNTP
57 # ifdef BPOP /* stupid */
58 static int xtnd_last = -1;
59 static int xtnd_first = 0;
60 static char xtnd_name[512]; /* INCREDIBLE HACK!! */
61 # endif
62 #endif /* NNTP */
63
64 #ifdef CYRUS_SASL
65 static sasl_conn_t *conn; /* SASL connection state */
66 static int sasl_complete = 0; /* Has sasl authentication succeeded? */
67 static int maxoutbuf; /* Maximum output buffer size */
68 static sasl_ssf_t sasl_ssf = 0; /* Security strength factor */
69 static int sasl_get_user(void *, int, const char **, unsigned *);
70 static int sasl_get_pass(sasl_conn_t *, void *, int, sasl_secret_t **);
71 struct pass_context {
72 char *user;
73 char *host;
74 };
75
76 static sasl_callback_t callbacks[] = {
77 { SASL_CB_USER, sasl_get_user, NULL },
78 #define POP_SASL_CB_N_USER 0
79 { SASL_CB_PASS, sasl_get_pass, NULL },
80 #define POP_SASL_CB_N_PASS 1
81 { SASL_CB_LOG, NULL, NULL },
82 { SASL_CB_LIST_END, NULL, NULL },
83 };
84 #else /* CYRUS_SASL */
85 # define sasl_fgetc fgetc
86 #endif /* CYRUS_SASL */
87
88 /*
89 * static prototypes
90 */
91 #if !defined(NNTP) && defined(APOP)
92 static char *pop_auth (char *, char *);
93 #endif
94
95 #if defined(NNTP) || !defined(MPOP)
96 /* otherwise they are not static functions */
97 static int command(const char *, ...);
98 static int multiline(void);
99 #endif
100
101 #ifdef CYRUS_SASL
102 static int pop_auth_sasl(char *, char *, char *);
103 static int sasl_fgetc(FILE *);
104 #endif /* CYRUS_SASL */
105
106 static int traverse (int (*)(), const char *, ...);
107 static int vcommand(const char *, va_list);
108 static int getline (char *, int, FILE *);
109 static int putline (char *, FILE *);
110
111
112 #if !defined(NNTP) && defined(APOP)
113 static char *
114 pop_auth (char *user, char *pass)
115 {
116 int len, buflen;
117 char *cp, *lp;
118 unsigned char *dp, *ep, digest[16];
119 MD5_CTX mdContext;
120 static char buffer[BUFSIZ];
121
122 if ((cp = strchr (response, '<')) == NULL
123 || (lp = strchr (cp, '>')) == NULL) {
124 snprintf (buffer, sizeof(buffer), "APOP not available: %s", response);
125 strncpy (response, buffer, sizeof(response));
126 return NULL;
127 }
128
129 *(++lp) = '\0';
130 snprintf (buffer, sizeof(buffer), "%s%s", cp, pass);
131
132 MD5Init (&mdContext);
133 MD5Update (&mdContext, (unsigned char *) buffer,
134 (unsigned int) strlen (buffer));
135 MD5Final (digest, &mdContext);
136
137 cp = buffer;
138 buflen = sizeof(buffer);
139
140 snprintf (cp, buflen, "%s ", user);
141 len = strlen (cp);
142 cp += len;
143 buflen -= len;
144
145 for (ep = (dp = digest) + sizeof(digest) / sizeof(digest[0]); dp < ep; ) {
146 snprintf (cp, buflen, "%02x", *dp++ & 0xff);
147 cp += 2;
148 buflen -= 2;
149 }
150 *cp = '\0';
151
152 return buffer;
153 }
154 #endif /* !NNTP && APOP */
155
156 #ifdef CYRUS_SASL
157 /*
158 * This function implements the AUTH command for various SASL mechanisms
159 *
160 * We do the whole SASL dialog here. If this completes, then we've
161 * authenticated successfully and have (possibly) negotiated a security
162 * layer.
163 */
164
165 int
166 pop_auth_sasl(char *user, char *host, char *mech)
167 {
168 int result, status, sasl_capability = 0;
169 unsigned int buflen, outlen;
170 char server_mechs[256], *buf, outbuf[BUFSIZ];
171 const char *chosen_mech;
172 sasl_security_properties_t secprops;
173 struct pass_context p_context;
174 sasl_ssf_t *ssf;
175 int *moutbuf;
176
177 /*
178 * First off, we're going to send the CAPA command to see if we can
179 * even support the AUTH command, and if we do, then we'll get a
180 * list of mechanisms the server supports. If we don't support
181 * the CAPA command, then it's unlikely that we will support
182 * SASL
183 */
184
185 if (command("CAPA") == NOTOK) {
186 snprintf(response, sizeof(response),
187 "The POP CAPA command failed; POP server does not "
188 "support SASL");
189 return NOTOK;
190 }
191
192 while ((status = multiline()) != DONE)
193 switch (status) {
194 case NOTOK:
195 return NOTOK;
196 break;
197 case DONE: /* Shouldn't be possible, but just in case */
198 break;
199 case OK:
200 if (strncasecmp(response, "SASL ", 5) == 0) {
201 /*
202 * We've seen the SASL capability. Grab the mech list
203 */
204 sasl_capability++;
205 strncpy(server_mechs, response + 5, sizeof(server_mechs));
206 }
207 break;
208 }
209
210 if (!sasl_capability) {
211 snprintf(response, sizeof(response), "POP server does not support "
212 "SASL");
213 return NOTOK;
214 }
215
216 /*
217 * If we received a preferred mechanism, see if the server supports it.
218 */
219
220 if (mech && stringdex(mech, server_mechs) == -1) {
221 snprintf(response, sizeof(response), "Requested SASL mech \"%s\" is "
222 "not in list of supported mechanisms:\n%s",
223 mech, server_mechs);
224 return NOTOK;
225 }
226
227 /*
228 * Start the SASL process. First off, initialize the SASL library.
229 */
230
231 callbacks[POP_SASL_CB_N_USER].context = user;
232 p_context.user = user;
233 p_context.host = host;
234 callbacks[POP_SASL_CB_N_PASS].context = &p_context;
235
236 result = sasl_client_init(callbacks);
237
238 if (result != SASL_OK) {
239 snprintf(response, sizeof(response), "SASL library initialization "
240 "failed: %s", sasl_errstring(result, NULL, NULL));
241 return NOTOK;
242 }
243
244 result = sasl_client_new("pop", host, NULL, NULL, NULL, 0, &conn);
245
246 if (result != SASL_OK) {
247 snprintf(response, sizeof(response), "SASL client initialization "
248 "failed: %s", sasl_errstring(result, NULL, NULL));
249 return NOTOK;
250 }
251
252 /*
253 * Initialize the security properties
254 */
255
256 memset(&secprops, 0, sizeof(secprops));
257 secprops.maxbufsize = BUFSIZ;
258 secprops.max_ssf = UINT_MAX;
259
260 result = sasl_setprop(conn, SASL_SEC_PROPS, &secprops);
261
262 if (result != SASL_OK) {
263 snprintf(response, sizeof(response), "SASL security property "
264 "initialization failed: %s", sasl_errdetail(conn));
265 return NOTOK;
266 }
267
268 /*
269 * Start the actual protocol. Feed the mech list into the library
270 * and get out a possible initial challenge
271 */
272
273 result = sasl_client_start(conn,
274 (const char *) (mech ? mech : server_mechs),
275 NULL, (const char **) &buf,
276 &buflen, &chosen_mech);
277
278 if (result != SASL_OK && result != SASL_CONTINUE) {
279 snprintf(response, sizeof(response), "SASL client start failed: %s",
280 sasl_errdetail(conn));
281 return NOTOK;
282 }
283
284 if (buflen) {
285 status = sasl_encode64(buf, buflen, outbuf, sizeof(outbuf), NULL);
286 if (status != SASL_OK) {
287 snprintf(response, sizeof(response), "SASL base64 encode "
288 "failed: %s", sasl_errstring(status, NULL, NULL));
289 return NOTOK;
290 }
291
292 status = command("AUTH %s %s", chosen_mech, outbuf);
293 } else
294 status = command("AUTH %s", chosen_mech);
295
296 while (result == SASL_CONTINUE) {
297 if (status == NOTOK)
298 return NOTOK;
299
300 /*
301 * If we get a "+OK" prefix to our response, then we should
302 * exit out of this exchange now (because authenticated should
303 * have succeeded)
304 */
305
306 if (strncmp(response, "+OK", 3) == 0)
307 break;
308
309 /*
310 * Otherwise, make sure the server challenge is correctly formatted
311 */
312
313 if (strncmp(response, "+ ", 2) != 0) {
314 command("*");
315 snprintf(response, sizeof(response),
316 "Malformed authentication message from server");
317 return NOTOK;
318 }
319
320 result = sasl_decode64(response + 2, strlen(response + 2),
321 outbuf, sizeof(outbuf), &outlen);
322
323 if (result != SASL_OK) {
324 command("*");
325 snprintf(response, sizeof(response), "SASL base64 decode "
326 "failed: %s", sasl_errstring(result, NULL, NULL));
327 return NOTOK;
328 }
329
330 result = sasl_client_step(conn, outbuf, outlen, NULL,
331 (const char **) &buf, &buflen);
332
333 if (result != SASL_OK && result != SASL_CONTINUE) {
334 command("*");
335 snprintf(response, sizeof(response), "SASL client negotiaton "
336 "failed: %s", sasl_errdetail(conn));
337 return NOTOK;
338 }
339
340 status = sasl_encode64(buf, buflen, outbuf, sizeof(outbuf), NULL);
341
342 if (status != SASL_OK) {
343 command("*");
344 snprintf(response, sizeof(response), "SASL base64 encode "
345 "failed: %s", sasl_errstring(status, NULL, NULL));
346 return NOTOK;
347 }
348
349 status = command(outbuf);
350 }
351
352 /*
353 * If we didn't get a positive final response, then error out
354 * (that probably means we failed an authorization check).
355 */
356
357 if (status != OK)
358 return NOTOK;
359
360 /*
361 * We _should_ be okay now. Get a few properties now that negotiation
362 * has completed.
363 */
364
365 result = sasl_getprop(conn, SASL_MAXOUTBUF, (const void **) &moutbuf);
366
367 if (result != SASL_OK) {
368 snprintf(response, sizeof(response), "Cannot retrieve SASL negotiated "
369 "output buffer size: %s", sasl_errdetail(conn));
370 return NOTOK;
371 }
372
373 maxoutbuf = *moutbuf;
374
375 result = sasl_getprop(conn, SASL_SSF, (const void **) &ssf);
376
377 sasl_ssf = *ssf;
378
379 if (result != SASL_OK) {
380 snprintf(response, sizeof(response), "Cannot retrieve SASL negotiated "
381 "security strength factor: %s", sasl_errdetail(conn));
382 return NOTOK;
383 }
384
385 /*
386 * Limit this to what we can deal with.
387 */
388
389 if (maxoutbuf == 0 || maxoutbuf > BUFSIZ)
390 maxoutbuf = BUFSIZ;
391
392 sasl_complete = 1;
393
394 return status;
395 }
396
397 /*
398 * Callback to return the userid sent down via the user parameter
399 */
400
401 static int
402 sasl_get_user(void *context, int id, const char **result, unsigned *len)
403 {
404 char *user = (char *) context;
405
406 if (! result || id != SASL_CB_USER)
407 return SASL_BADPARAM;
408
409 *result = user;
410 if (len)
411 *len = strlen(user);
412
413 return SASL_OK;
414 }
415
416 /*
417 * Callback to return the password (we call ruserpass, which can get it
418 * out of the .netrc
419 */
420
421 static int
422 sasl_get_pass(sasl_conn_t *conn, void *context, int id, sasl_secret_t **psecret)
423 {
424 struct pass_context *p_context = (struct pass_context *) context;
425 char *pass = NULL;
426 int len;
427
428 if (! psecret || id != SASL_CB_PASS)
429 return SASL_BADPARAM;
430
431 ruserpass(p_context->user, &(p_context->host), &pass);
432
433 len = strlen(pass);
434
435 *psecret = (sasl_secret_t *) malloc(sizeof(sasl_secret_t) + len);
436
437 if (! *psecret)
438 return SASL_NOMEM;
439
440 (*psecret)->len = len;
441 strcpy((char *) (*psecret)->data, pass);
442
443 return SASL_OK;
444 }
445 #endif /* CYRUS_SASL */
446
447
448 /*
449 * Split string containing proxy command into an array of arguments
450 * suitable for passing to exec. Returned array must be freed. Shouldn't
451 * be possible to call this with host set to NULL.
452 */
453 char **
454 parse_proxy(char *proxy, char *host)
455 {
456 char **pargv, **p;
457 int pargc = 2;
458 int hlen = strlen(host);
459 int plen = 1;
460 char *cur, *pro;
461 char *c;
462
463 /* skip any initial space */
464 for (pro = proxy; isspace(*pro); pro++)
465 continue;
466
467 /* calculate required size for argument array */
468 for (cur = pro; *cur; cur++) {
469 if (isspace(*cur) && cur[1] && !isspace(cur[1]))
470 plen++, pargc++;
471 else if (*cur == '%' && cur[1] == 'h') {
472 plen += hlen;
473 cur++;
474 } else if (!isspace(*cur))
475 plen++;
476 }
477
478 /* put together list of arguments */
479 p = pargv = malloc(pargc * sizeof(char *));
480 c = *pargv = malloc(plen * sizeof(char));
481 for (cur = pro; *cur; cur++) {
482 if (isspace(*cur) && cur[1] && !isspace(cur[1])) {
483 *c++ = '\0';
484 *++p = c;
485 } else if (*cur == '%' && cur[1] == 'h') {
486 strcpy (c, host);
487 c += hlen;
488 cur++;
489 } else if (!isspace(*cur))
490 *c++ = *cur;
491 }
492 *++p = NULL;
493 return pargv;
494 }
495
496 int
497 pop_init (char *host, char *user, char *pass, char *proxy, int snoop,
498 int rpop, int kpop, int sasl, char *mech)
499 {
500 int fd1, fd2;
501 char buffer[BUFSIZ];
502
503 if (proxy && *proxy) {
504 int pid;
505 int inpipe[2]; /* for reading from the server */
506 int outpipe[2]; /* for sending to the server */
507
508 /* first give up any root priviledges we may have for rpop */
509 setuid(getuid());
510
511 pipe(inpipe);
512 pipe(outpipe);
513
514 pid=fork();
515 if (pid==0) {
516 char **argv;
517
518 /* in child */
519 close(0);
520 close(1);
521 dup2(outpipe[0],0); /* connect read end of connection */
522 dup2(inpipe[1], 1); /* connect write end of connection */
523 if(inpipe[0]>1) close(inpipe[0]);
524 if(inpipe[1]>1) close(inpipe[1]);
525 if(outpipe[0]>1) close(outpipe[0]);
526 if(outpipe[1]>1) close(outpipe[1]);
527
528 /* run the proxy command */
529 argv=parse_proxy(proxy, host);
530 execvp(argv[0],argv);
531
532 perror(argv[0]);
533 close(0);
534 close(1);
535 free(*argv);
536 free(argv);
537 exit(10);
538 }
539
540 /* okay in the parent we do some stuff */
541 close(inpipe[1]); /* child uses this */
542 close(outpipe[0]); /* child uses this */
543
544 /* we read on fd1 */
545 fd1=inpipe[0];
546
547 /* and write on fd2 */
548 fd2=outpipe[1];
549
550 } else {
551
552 #ifdef APOP
553 int apop;
554
555 if ((apop = rpop) < 0)
556 rpop = 0;
557 #endif
558
559 #ifndef NNTP
560 # ifdef KPOP
561 if ( kpop ) {
562 snprintf (buffer, sizeof(buffer), "%s/%s", KPOP_PRINCIPAL, "kpop");
563 if ((fd1 = client (host, "tcp", buffer, 0, response, sizeof(response))) == NOTOK) {
564 return NOTOK;
565 }
566 } else {
567 # endif /* KPOP */
568 if ((fd1 = client (host, "tcp", POPSERVICE, rpop, response, sizeof(response))) == NOTOK) {
569 return NOTOK;
570 }
571 # ifdef KPOP
572 }
573 # endif /* KPOP */
574 #else /* NNTP */
575 if ((fd1 = client (host, "tcp", "nntp", rpop, response, sizeof(response))) == NOTOK)
576 return NOTOK;
577 #endif
578
579 if ((fd2 = dup (fd1)) == NOTOK) {
580 char *s;
581
582 if ((s = strerror(errno)))
583 snprintf (response, sizeof(response),
584 "unable to dup connection descriptor: %s", s);
585 else
586 snprintf (response, sizeof(response),
587 "unable to dup connection descriptor: unknown error");
588 close (fd1);
589 return NOTOK;
590 }
591 }
592 #ifndef NNTP
593 if (pop_set (fd1, fd2, snoop) == NOTOK)
594 #else /* NNTP */
595 if (pop_set (fd1, fd2, snoop, (char *)0) == NOTOK)
596 #endif /* NNTP */
597 return NOTOK;
598
599 SIGNAL (SIGPIPE, SIG_IGN);
600
601 switch (getline (response, sizeof response, input)) {
602 case OK:
603 if (poprint)
604 fprintf (stderr, "<--- %s\n", response);
605 #ifndef NNTP
606 if (*response == '+') {
607 # ifndef KPOP
608 # ifdef APOP
609 if (apop < 0) {
610 char *cp = pop_auth (user, pass);
611
612 if (cp && command ("APOP %s", cp) != NOTOK)
613 return OK;
614 }
615 else
616 # endif /* APOP */
617 # ifdef CYRUS_SASL
618 if (sasl) {
619 if (pop_auth_sasl(user, host, mech) != NOTOK)
620 return OK;
621 } else
622 # endif /* CYRUS_SASL */
623 if (command ("USER %s", user) != NOTOK
624 && command ("%s %s", rpop ? "RPOP" : (pophack++, "PASS"),
625 pass) != NOTOK)
626 return OK;
627 # else /* KPOP */
628 if (command ("USER %s", user) != NOTOK
629 && command ("PASS %s", pass) != NOTOK)
630 return OK;
631 # endif
632 }
633 #else /* NNTP */
634 if (*response < CHAR_ERR) {
635 command ("MODE READER");
636 return OK;
637 }
638 #endif
639 strncpy (buffer, response, sizeof(buffer));
640 command ("QUIT");
641 strncpy (response, buffer, sizeof(response));
642 /* and fall */
643
644 case NOTOK:
645 case DONE:
646 if (poprint)
647 fprintf (stderr, "%s\n", response);
648 fclose (input);
649 fclose (output);
650 return NOTOK;
651 }
652
653 return NOTOK; /* NOTREACHED */
654 }
655
656 #ifdef NNTP
657 int
658 pop_set (int in, int out, int snoop, char *myname)
659 #else
660 int
661 pop_set (int in, int out, int snoop)
662 #endif
663 {
664
665 #ifdef NNTP
666 if (myname && *myname) {
667 /* interface from bbc to msh */
668 strncpy (xtnd_name, myname, sizeof(xtnd_name));
669 }
670 #endif /* NNTP */
671
672 if ((input = fdopen (in, "r")) == NULL
673 || (output = fdopen (out, "w")) == NULL) {
674 strncpy (response, "fdopen failed on connection descriptor", sizeof(response));
675 if (input)
676 fclose (input);
677 else
678 close (in);
679 close (out);
680 return NOTOK;
681 }
682
683 poprint = snoop;
684
685 return OK;
686 }
687
688
689 int
690 pop_fd (char *in, int inlen, char *out, int outlen)
691 {
692 snprintf (in, inlen, "%d", fileno (input));
693 snprintf (out, outlen, "%d", fileno (output));
694 return OK;
695 }
696
697
698 /*
699 * Find out number of messages available
700 * and their total size.
701 */
702
703 int
704 pop_stat (int *nmsgs, int *nbytes)
705 {
706 #ifdef NNTP
707 char **ap;
708 #endif /* NNTP */
709
710 #ifndef NNTP
711 if (command ("STAT") == NOTOK)
712 return NOTOK;
713
714 *nmsgs = *nbytes = 0;
715 sscanf (response, "+OK %d %d", nmsgs, nbytes);
716
717 #else /* NNTP */
718 if (xtnd_last < 0) { /* in msh, xtnd_name is set from myname */
719 if (command("GROUP %s", xtnd_name) == NOTOK)
720 return NOTOK;
721
722 ap = brkstring (response, " ", "\n"); /* "211 nart first last ggg" */
723 xtnd_first = atoi (ap[2]);
724 xtnd_last = atoi (ap[3]);
725 }
726
727 /* nmsgs is not the real nart, but an incredible simuation */
728 if (xtnd_last > 0)
729 *nmsgs = xtnd_last - xtnd_first + 1; /* because of holes... */
730 else
731 *nmsgs = 0;
732 *nbytes = xtnd_first; /* for subtracting offset in msh() */
733 #endif /* NNTP */
734
735 return OK;
736 }
737
738 #ifdef NNTP
739 int
740 pop_exists (int (*action)())
741 {
742 #ifdef XMSGS /* hacked into NNTP 1.5 */
743 if (traverse (action, "XMSGS %d-%d", (targ_t) xtnd_first, (targ_t) xtnd_last) == OK)
744 return OK;
745 #endif
746 /* provided by INN 1.4 */
747 if (traverse (action, "LISTGROUP") == OK)
748 return OK;
749 return traverse (action, "XHDR NONAME %d-%d", (targ_t) xtnd_first, (targ_t) xtnd_last);
750 }
751 #endif /* NNTP */
752
753
754 #ifdef BPOP
755 int
756 pop_list (int msgno, int *nmsgs, int *msgs, int *bytes, int *ids)
757 #else
758 int
759 pop_list (int msgno, int *nmsgs, int *msgs, int *bytes)
760 #endif
761 {
762 int i;
763 #ifndef BPOP
764 int *ids = NULL;
765 #endif
766
767 if (msgno) {
768 #ifndef NNTP
769 if (command ("LIST %d", msgno) == NOTOK)
770 return NOTOK;
771 *msgs = *bytes = 0;
772 if (ids) {
773 *ids = 0;
774 sscanf (response, "+OK %d %d %d", msgs, bytes, ids);
775 }
776 else
777 sscanf (response, "+OK %d %d", msgs, bytes);
778 #else /* NNTP */
779 *msgs = *bytes = 0;
780 if (command ("STAT %d", msgno) == NOTOK)
781 return NOTOK;
782 if (ids) {
783 *ids = msgno;
784 }
785 #endif /* NNTP */
786 return OK;
787 }
788
789 #ifndef NNTP
790 if (command ("LIST") == NOTOK)
791 return NOTOK;
792
793 for (i = 0; i < *nmsgs; i++)
794 switch (multiline ()) {
795 case NOTOK:
796 return NOTOK;
797 case DONE:
798 *nmsgs = ++i;
799 return OK;
800 case OK:
801 *msgs = *bytes = 0;
802 if (ids) {
803 *ids = 0;
804 sscanf (response, "%d %d %d",
805 msgs++, bytes++, ids++);
806 }
807 else
808 sscanf (response, "%d %d", msgs++, bytes++);
809 break;
810 }
811 for (;;)
812 switch (multiline ()) {
813 case NOTOK:
814 return NOTOK;
815 case DONE:
816 return OK;
817 case OK:
818 break;
819 }
820 #else /* NNTP */
821 return NOTOK;
822 #endif /* NNTP */
823 }
824
825
826 int
827 pop_retr (int msgno, int (*action)())
828 {
829 #ifndef NNTP
830 return traverse (action, "RETR %d", (targ_t) msgno);
831 #else /* NNTP */
832 return traverse (action, "ARTICLE %d", (targ_t) msgno);
833 #endif /* NNTP */
834 }
835
836
837 static int
838 traverse (int (*action)(), const char *fmt, ...)
839 {
840 int result;
841 va_list ap;
842 char buffer[sizeof(response)];
843
844 va_start(ap, fmt);
845 result = vcommand (fmt, ap);
846 va_end(ap);
847
848 if (result == NOTOK)
849 return NOTOK;
850 strncpy (buffer, response, sizeof(buffer));
851
852 for (;;)
853 switch (multiline ()) {
854 case NOTOK:
855 return NOTOK;
856
857 case DONE:
858 strncpy (response, buffer, sizeof(response));
859 return OK;
860
861 case OK:
862 (*action) (response);
863 break;
864 }
865 }
866
867
868 int
869 pop_dele (int msgno)
870 {
871 return command ("DELE %d", msgno);
872 }
873
874
875 int
876 pop_noop (void)
877 {
878 return command ("NOOP");
879 }
880
881
882 #if defined(MPOP) && !defined(NNTP)
883 int
884 pop_last (void)
885 {
886 return command ("LAST");
887 }
888 #endif
889
890
891 int
892 pop_rset (void)
893 {
894 return command ("RSET");
895 }
896
897
898 int
899 pop_top (int msgno, int lines, int (*action)())
900 {
901 #ifndef NNTP
902 return traverse (action, "TOP %d %d", (targ_t) msgno, (targ_t) lines);
903 #else /* NNTP */
904 return traverse (action, "HEAD %d", (targ_t) msgno);
905 #endif /* NNTP */
906 }
907
908
909 #ifdef BPOP
910 int
911 pop_xtnd (int (*action)(), char *fmt, ...)
912 {
913 int result;
914 va_list ap;
915 char buffer[BUFSIZ];
916
917 #ifdef NNTP
918 char **ap;
919 #endif
920
921 va_start(ap, fmt);
922 #ifndef NNTP
923 /* needs to be fixed... va_end needs to be added */
924 snprintf (buffer, sizeof(buffer), "XTND %s", fmt);
925 result = traverse (action, buffer, a, b, c, d);
926 va_end(ap);
927 return result;
928 #else /* NNTP */
929 snprintf (buffer, sizeof(buffer), fmt, a, b, c, d);
930 ap = brkstring (buffer, " ", "\n"); /* a hack, i know... */
931
932 if (!strcasecmp(ap[0], "x-bboards")) { /* XTND "X-BBOARDS group */
933 /* most of these parameters are meaningless under NNTP.
934 * bbc.c was modified to set AKA and LEADERS as appropriate,
935 * the rest are left blank.
936 */
937 return OK;
938 }
939 if (!strcasecmp (ap[0], "archive") && ap[1]) {
940 snprintf (xtnd_name, sizeof(xtnd_name), "%s", ap[1]); /* save the name */
941 xtnd_last = 0;
942 xtnd_first = 1; /* setup to fail in pop_stat */
943 return OK;
944 }
945 if (!strcasecmp (ap[0], "bboards")) {
946
947 if (ap[1]) { /* XTND "BBOARDS group" */
948 snprintf (xtnd_name, sizeof(xtnd_name), "%s", ap[1]); /* save the name */
949 if (command("GROUP %s", xtnd_name) == NOTOK)
950 return NOTOK;
951
952 /* action must ignore extra args */
953 strncpy (buffer, response, sizeof(buffer));
954 ap = brkstring (response, " ", "\n");/* "211 nart first last g" */
955 xtnd_first = atoi (ap[2]);
956 xtnd_last = atoi (ap[3]);
957
958 (*action) (buffer);
959 return OK;
960
961 } else { /* XTND "BBOARDS" */
962 return traverse (action, "LIST", a, b, c, d);
963 }
964 }
965 return NOTOK; /* unknown XTND command */
966 #endif /* NNTP */
967 }
968 #endif /* BPOP */
969
970
971 int
972 pop_quit (void)
973 {
974 int i;
975
976 i = command ("QUIT");
977 pop_done ();
978
979 return i;
980 }
981
982
983 int
984 pop_done (void)
985 {
986 #ifdef CYRUS_SASL
987 if (conn)
988 sasl_dispose(&conn);
989 #endif /* CYRUS_SASL */
990 fclose (input);
991 fclose (output);
992
993 return OK;
994 }
995
996
997 #if !defined(MPOP) || defined(NNTP)
998 static
999 #endif
1000 int
1001 command(const char *fmt, ...)
1002 {
1003 va_list ap;
1004 int result;
1005
1006 va_start(ap, fmt);
1007 result = vcommand(fmt, ap);
1008 va_end(ap);
1009
1010 return result;
1011 }
1012
1013
1014 static int
1015 vcommand (const char *fmt, va_list ap)
1016 {
1017 char *cp, buffer[BUFSIZ];
1018
1019 vsnprintf (buffer, sizeof(buffer), fmt, ap);
1020 if (poprint) {
1021 #ifdef CYRUS_SASL
1022 if (sasl_ssf)
1023 fprintf(stderr, "(encrypted) ");
1024 #endif /* CYRUS_SASL */
1025 if (pophack) {
1026 if ((cp = strchr (buffer, ' ')))
1027 *cp = 0;
1028 fprintf (stderr, "---> %s ********\n", buffer);
1029 if (cp)
1030 *cp = ' ';
1031 pophack = 0;
1032 }
1033 else
1034 fprintf (stderr, "---> %s\n", buffer);
1035 }
1036
1037 if (putline (buffer, output) == NOTOK)
1038 return NOTOK;
1039
1040 #ifdef CYRUS_SASL
1041 if (poprint && sasl_ssf)
1042 fprintf(stderr, "(decrypted) ");
1043 #endif /* CYRUS_SASL */
1044
1045 switch (getline (response, sizeof response, input)) {
1046 case OK:
1047 if (poprint)
1048 fprintf (stderr, "<--- %s\n", response);
1049 #ifndef NNTP
1050 return (*response == '+' ? OK : NOTOK);
1051 #else /* NNTP */
1052 return (*response < CHAR_ERR ? OK : NOTOK);
1053 #endif /* NNTP */
1054
1055 case NOTOK:
1056 case DONE:
1057 if (poprint)
1058 fprintf (stderr, "%s\n", response);
1059 return NOTOK;
1060 }
1061
1062 return NOTOK; /* NOTREACHED */
1063 }
1064
1065
1066 #if defined(MPOP) && !defined(NNTP)
1067 int
1068 multiline (void)
1069 #else
1070 static int
1071 multiline (void)
1072 #endif
1073 {
1074 char buffer[BUFSIZ + TRMLEN];
1075
1076 if (getline (buffer, sizeof buffer, input) != OK)
1077 return NOTOK;
1078 #ifdef DEBUG
1079 if (poprint) {
1080 #ifdef CYRUS_SASL
1081 if (sasl_ssf)
1082 fprintf(stderr, "(decrypted) ");
1083 #endif /* CYRUS_SASL */
1084 fprintf (stderr, "<--- %s\n", response);
1085 }
1086 #endif /* DEBUG */
1087 if (strncmp (buffer, TRM, TRMLEN) == 0) {
1088 if (buffer[TRMLEN] == 0)
1089 return DONE;
1090 else
1091 strncpy (response, buffer + TRMLEN, sizeof(response));
1092 }
1093 else
1094 strncpy (response, buffer, sizeof(response));
1095
1096 return OK;
1097 }
1098
1099 /*
1100 * Note that these functions have been modified to deal with layer encryption
1101 * in the SASL case
1102 */
1103
1104 static int
1105 getline (char *s, int n, FILE *iop)
1106 {
1107 int c;
1108 char *p;
1109
1110 p = s;
1111 while (--n > 0 && (c = sasl_fgetc (iop)) != EOF && c != -2)
1112 if ((*p++ = c) == '\n')
1113 break;
1114 if (c == -2)
1115 return NOTOK;
1116 if (ferror (iop) && c != EOF) {
1117 strncpy (response, "error on connection", sizeof(response));
1118 return NOTOK;
1119 }
1120 if (c == EOF && p == s) {
1121 strncpy (response, "connection closed by foreign host", sizeof(response));
1122 return DONE;
1123 }
1124 *p = 0;
1125 if (*--p == '\n')
1126 *p = 0;
1127 if (*--p == '\r')
1128 *p = 0;
1129
1130 return OK;
1131 }
1132
1133
1134 static int
1135 putline (char *s, FILE *iop)
1136 {
1137 #ifdef CYRUS_SASL
1138 char outbuf[BUFSIZ], *buf;
1139 int result;
1140 unsigned int buflen;
1141
1142 if (!sasl_complete) {
1143 #endif /* CYRUS_SASL */
1144 fprintf (iop, "%s\r\n", s);
1145 #ifdef CYRUS_SASL
1146 } else {
1147 /*
1148 * Build an output buffer, encrypt it using sasl_encode, and
1149 * squirt out the results.
1150 */
1151 strncpy(outbuf, s, sizeof(outbuf) - 3);
1152 outbuf[sizeof(outbuf) - 3] = '\0'; /* Just in case */
1153 strcat(outbuf, "\r\n");
1154
1155 result = sasl_encode(conn, outbuf, strlen(outbuf),
1156 (const char **) &buf, &buflen);
1157
1158 if (result != SASL_OK) {
1159 snprintf(response, sizeof(response), "SASL encoding error: %s",
1160 sasl_errdetail(conn));
1161 return NOTOK;
1162 }
1163
1164 fwrite(buf, buflen, 1, iop);
1165 }
1166 #endif /* CYRUS_SASL */
1167
1168 fflush (iop);
1169 if (ferror (iop)) {
1170 strncpy (response, "lost connection", sizeof(response));
1171 return NOTOK;
1172 }
1173
1174 return OK;
1175 }
1176
1177 #ifdef CYRUS_SASL
1178 /*
1179 * Okay, our little fgetc replacement. Hopefully this is a little more
1180 * efficient than the last one.
1181 */
1182 static int
1183 sasl_fgetc(FILE *f)
1184 {
1185 static unsigned char *buffer = NULL, *ptr;
1186 static int size = 0;
1187 static int cnt = 0;
1188 unsigned int retbufsize = 0;
1189 int cc, result;
1190 char *retbuf, tmpbuf[BUFSIZ];
1191
1192 /*
1193 * If we have some leftover data, return that
1194 */
1195
1196 if (cnt) {
1197 cnt--;
1198 return (int) *ptr++;
1199 }
1200
1201 /*
1202 * Otherwise, fill our buffer until we have some data to return.
1203 */
1204
1205 while (retbufsize == 0) {
1206
1207 cc = read(fileno(f), tmpbuf, sizeof(tmpbuf));
1208
1209 if (cc == 0)
1210 return EOF;
1211
1212 if (cc < 0) {
1213 snprintf(response, sizeof(response), "Error during read from "
1214 "network: %s", strerror(errno));
1215 return -2;
1216 }
1217
1218 /*
1219 * We're not allowed to call sasl_decode until sasl_complete is
1220 * true, so we do these gyrations ...
1221 */
1222
1223 if (!sasl_complete) {
1224
1225 retbuf = tmpbuf;
1226 retbufsize = cc;
1227
1228 } else {
1229
1230 result = sasl_decode(conn, tmpbuf, cc,
1231 (const char **) &retbuf, &retbufsize);
1232
1233 if (result != SASL_OK) {
1234 snprintf(response, sizeof(response), "Error during SASL "
1235 "decoding: %s", sasl_errdetail(conn));
1236 return -2;
1237 }
1238 }
1239 }
1240
1241 if (retbufsize > size) {
1242 buffer = realloc(buffer, retbufsize);
1243 if (!buffer) {
1244 snprintf(response, sizeof(response), "Error during realloc in "
1245 "read routine: %s", strerror(errno));
1246 return -2;
1247 }
1248 size = retbufsize;
1249 }
1250
1251 memcpy(buffer, retbuf, retbufsize);
1252 ptr = buffer + 1;
1253 cnt = retbufsize - 1;
1254
1255 return (int) buffer[0];
1256 }
1257 #endif /* CYRUS_SASL */