]> diplodocus.org Git - nmh/blob - uip/popsbr.c
[bug #4296] PUTD() macro can't hack 10 digit values
[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 int
448 pop_init (char *host, char *user, char *pass, int snoop, int rpop, int kpop,
449 int sasl, char *mech)
450 {
451 int fd1, fd2;
452 char buffer[BUFSIZ];
453
454 #ifdef APOP
455 int apop;
456
457 if ((apop = rpop) < 0)
458 rpop = 0;
459 #endif
460
461 #ifndef NNTP
462 # ifdef KPOP
463 if ( kpop ) {
464 snprintf (buffer, sizeof(buffer), "%s/%s", KPOP_PRINCIPAL, "kpop");
465 if ((fd1 = client (host, "tcp", buffer, 0, response, sizeof(response))) == NOTOK) {
466 return NOTOK;
467 }
468 } else {
469 # endif /* KPOP */
470 if ((fd1 = client (host, "tcp", POPSERVICE, rpop, response, sizeof(response))) == NOTOK) {
471 return NOTOK;
472 }
473 # ifdef KPOP
474 }
475 # endif /* KPOP */
476 #else /* NNTP */
477 if ((fd1 = client (host, "tcp", "nntp", rpop, response, sizeof(response))) == NOTOK)
478 return NOTOK;
479 #endif
480
481 if ((fd2 = dup (fd1)) == NOTOK) {
482 char *s;
483
484 if ((s = strerror(errno)))
485 snprintf (response, sizeof(response),
486 "unable to dup connection descriptor: %s", s);
487 else
488 snprintf (response, sizeof(response),
489 "unable to dup connection descriptor: unknown error");
490 close (fd1);
491 return NOTOK;
492 }
493 #ifndef NNTP
494 if (pop_set (fd1, fd2, snoop) == NOTOK)
495 #else /* NNTP */
496 if (pop_set (fd1, fd2, snoop, (char *)0) == NOTOK)
497 #endif /* NNTP */
498 return NOTOK;
499
500 SIGNAL (SIGPIPE, SIG_IGN);
501
502 switch (getline (response, sizeof response, input)) {
503 case OK:
504 if (poprint)
505 fprintf (stderr, "<--- %s\n", response);
506 #ifndef NNTP
507 if (*response == '+') {
508 # ifndef KPOP
509 # ifdef APOP
510 if (apop < 0) {
511 char *cp = pop_auth (user, pass);
512
513 if (cp && command ("APOP %s", cp) != NOTOK)
514 return OK;
515 }
516 else
517 # endif /* APOP */
518 # ifdef CYRUS_SASL
519 if (sasl) {
520 if (pop_auth_sasl(user, host, mech) != NOTOK)
521 return OK;
522 } else
523 # endif /* CYRUS_SASL */
524 if (command ("USER %s", user) != NOTOK
525 && command ("%s %s", rpop ? "RPOP" : (pophack++, "PASS"),
526 pass) != NOTOK)
527 return OK;
528 # else /* KPOP */
529 if (command ("USER %s", user) != NOTOK
530 && command ("PASS %s", pass) != NOTOK)
531 return OK;
532 # endif
533 }
534 #else /* NNTP */
535 if (*response < CHAR_ERR) {
536 command ("MODE READER");
537 return OK;
538 }
539 #endif
540 strncpy (buffer, response, sizeof(buffer));
541 command ("QUIT");
542 strncpy (response, buffer, sizeof(response));
543 /* and fall */
544
545 case NOTOK:
546 case DONE:
547 if (poprint)
548 fprintf (stderr, "%s\n", response);
549 fclose (input);
550 fclose (output);
551 return NOTOK;
552 }
553
554 return NOTOK; /* NOTREACHED */
555 }
556
557 #ifdef NNTP
558 int
559 pop_set (int in, int out, int snoop, char *myname)
560 #else
561 int
562 pop_set (int in, int out, int snoop)
563 #endif
564 {
565
566 #ifdef NNTP
567 if (myname && *myname) {
568 /* interface from bbc to msh */
569 strncpy (xtnd_name, myname, sizeof(xtnd_name));
570 }
571 #endif /* NNTP */
572
573 if ((input = fdopen (in, "r")) == NULL
574 || (output = fdopen (out, "w")) == NULL) {
575 strncpy (response, "fdopen failed on connection descriptor", sizeof(response));
576 if (input)
577 fclose (input);
578 else
579 close (in);
580 close (out);
581 return NOTOK;
582 }
583
584 poprint = snoop;
585
586 return OK;
587 }
588
589
590 int
591 pop_fd (char *in, int inlen, char *out, int outlen)
592 {
593 snprintf (in, inlen, "%d", fileno (input));
594 snprintf (out, outlen, "%d", fileno (output));
595 return OK;
596 }
597
598
599 /*
600 * Find out number of messages available
601 * and their total size.
602 */
603
604 int
605 pop_stat (int *nmsgs, int *nbytes)
606 {
607 #ifdef NNTP
608 char **ap;
609 #endif /* NNTP */
610
611 #ifndef NNTP
612 if (command ("STAT") == NOTOK)
613 return NOTOK;
614
615 *nmsgs = *nbytes = 0;
616 sscanf (response, "+OK %d %d", nmsgs, nbytes);
617
618 #else /* NNTP */
619 if (xtnd_last < 0) { /* in msh, xtnd_name is set from myname */
620 if (command("GROUP %s", xtnd_name) == NOTOK)
621 return NOTOK;
622
623 ap = brkstring (response, " ", "\n"); /* "211 nart first last ggg" */
624 xtnd_first = atoi (ap[2]);
625 xtnd_last = atoi (ap[3]);
626 }
627
628 /* nmsgs is not the real nart, but an incredible simuation */
629 if (xtnd_last > 0)
630 *nmsgs = xtnd_last - xtnd_first + 1; /* because of holes... */
631 else
632 *nmsgs = 0;
633 *nbytes = xtnd_first; /* for subtracting offset in msh() */
634 #endif /* NNTP */
635
636 return OK;
637 }
638
639 #ifdef NNTP
640 int
641 pop_exists (int (*action)())
642 {
643 #ifdef XMSGS /* hacked into NNTP 1.5 */
644 if (traverse (action, "XMSGS %d-%d", (targ_t) xtnd_first, (targ_t) xtnd_last) == OK)
645 return OK;
646 #endif
647 /* provided by INN 1.4 */
648 if (traverse (action, "LISTGROUP") == OK)
649 return OK;
650 return traverse (action, "XHDR NONAME %d-%d", (targ_t) xtnd_first, (targ_t) xtnd_last);
651 }
652 #endif /* NNTP */
653
654
655 #ifdef BPOP
656 int
657 pop_list (int msgno, int *nmsgs, int *msgs, int *bytes, int *ids)
658 #else
659 int
660 pop_list (int msgno, int *nmsgs, int *msgs, int *bytes)
661 #endif
662 {
663 int i;
664 #ifndef BPOP
665 int *ids = NULL;
666 #endif
667
668 if (msgno) {
669 #ifndef NNTP
670 if (command ("LIST %d", msgno) == NOTOK)
671 return NOTOK;
672 *msgs = *bytes = 0;
673 if (ids) {
674 *ids = 0;
675 sscanf (response, "+OK %d %d %d", msgs, bytes, ids);
676 }
677 else
678 sscanf (response, "+OK %d %d", msgs, bytes);
679 #else /* NNTP */
680 *msgs = *bytes = 0;
681 if (command ("STAT %d", msgno) == NOTOK)
682 return NOTOK;
683 if (ids) {
684 *ids = msgno;
685 }
686 #endif /* NNTP */
687 return OK;
688 }
689
690 #ifndef NNTP
691 if (command ("LIST") == NOTOK)
692 return NOTOK;
693
694 for (i = 0; i < *nmsgs; i++)
695 switch (multiline ()) {
696 case NOTOK:
697 return NOTOK;
698 case DONE:
699 *nmsgs = ++i;
700 return OK;
701 case OK:
702 *msgs = *bytes = 0;
703 if (ids) {
704 *ids = 0;
705 sscanf (response, "%d %d %d",
706 msgs++, bytes++, ids++);
707 }
708 else
709 sscanf (response, "%d %d", msgs++, bytes++);
710 break;
711 }
712 for (;;)
713 switch (multiline ()) {
714 case NOTOK:
715 return NOTOK;
716 case DONE:
717 return OK;
718 case OK:
719 break;
720 }
721 #else /* NNTP */
722 return NOTOK;
723 #endif /* NNTP */
724 }
725
726
727 int
728 pop_retr (int msgno, int (*action)())
729 {
730 #ifndef NNTP
731 return traverse (action, "RETR %d", (targ_t) msgno);
732 #else /* NNTP */
733 return traverse (action, "ARTICLE %d", (targ_t) msgno);
734 #endif /* NNTP */
735 }
736
737
738 static int
739 traverse (int (*action)(), const char *fmt, ...)
740 {
741 int result;
742 va_list ap;
743 char buffer[sizeof(response)];
744
745 va_start(ap, fmt);
746 result = vcommand (fmt, ap);
747 va_end(ap);
748
749 if (result == NOTOK)
750 return NOTOK;
751 strncpy (buffer, response, sizeof(buffer));
752
753 for (;;)
754 switch (multiline ()) {
755 case NOTOK:
756 return NOTOK;
757
758 case DONE:
759 strncpy (response, buffer, sizeof(response));
760 return OK;
761
762 case OK:
763 (*action) (response);
764 break;
765 }
766 }
767
768
769 int
770 pop_dele (int msgno)
771 {
772 return command ("DELE %d", msgno);
773 }
774
775
776 int
777 pop_noop (void)
778 {
779 return command ("NOOP");
780 }
781
782
783 #if defined(MPOP) && !defined(NNTP)
784 int
785 pop_last (void)
786 {
787 return command ("LAST");
788 }
789 #endif
790
791
792 int
793 pop_rset (void)
794 {
795 return command ("RSET");
796 }
797
798
799 int
800 pop_top (int msgno, int lines, int (*action)())
801 {
802 #ifndef NNTP
803 return traverse (action, "TOP %d %d", (targ_t) msgno, (targ_t) lines);
804 #else /* NNTP */
805 return traverse (action, "HEAD %d", (targ_t) msgno);
806 #endif /* NNTP */
807 }
808
809
810 #ifdef BPOP
811 int
812 pop_xtnd (int (*action)(), char *fmt, ...)
813 {
814 int result;
815 va_list ap;
816 char buffer[BUFSIZ];
817
818 #ifdef NNTP
819 char **ap;
820 #endif
821
822 va_start(ap, fmt);
823 #ifndef NNTP
824 /* needs to be fixed... va_end needs to be added */
825 snprintf (buffer, sizeof(buffer), "XTND %s", fmt);
826 result = traverse (action, buffer, a, b, c, d);
827 va_end(ap);
828 return result;
829 #else /* NNTP */
830 snprintf (buffer, sizeof(buffer), fmt, a, b, c, d);
831 ap = brkstring (buffer, " ", "\n"); /* a hack, i know... */
832
833 if (!strcasecmp(ap[0], "x-bboards")) { /* XTND "X-BBOARDS group */
834 /* most of these parameters are meaningless under NNTP.
835 * bbc.c was modified to set AKA and LEADERS as appropriate,
836 * the rest are left blank.
837 */
838 return OK;
839 }
840 if (!strcasecmp (ap[0], "archive") && ap[1]) {
841 snprintf (xtnd_name, sizeof(xtnd_name), "%s", ap[1]); /* save the name */
842 xtnd_last = 0;
843 xtnd_first = 1; /* setup to fail in pop_stat */
844 return OK;
845 }
846 if (!strcasecmp (ap[0], "bboards")) {
847
848 if (ap[1]) { /* XTND "BBOARDS group" */
849 snprintf (xtnd_name, sizeof(xtnd_name), "%s", ap[1]); /* save the name */
850 if (command("GROUP %s", xtnd_name) == NOTOK)
851 return NOTOK;
852
853 /* action must ignore extra args */
854 strncpy (buffer, response, sizeof(buffer));
855 ap = brkstring (response, " ", "\n");/* "211 nart first last g" */
856 xtnd_first = atoi (ap[2]);
857 xtnd_last = atoi (ap[3]);
858
859 (*action) (buffer);
860 return OK;
861
862 } else { /* XTND "BBOARDS" */
863 return traverse (action, "LIST", a, b, c, d);
864 }
865 }
866 return NOTOK; /* unknown XTND command */
867 #endif /* NNTP */
868 }
869 #endif BPOP
870
871
872 int
873 pop_quit (void)
874 {
875 int i;
876
877 i = command ("QUIT");
878 pop_done ();
879
880 return i;
881 }
882
883
884 int
885 pop_done (void)
886 {
887 #ifdef CYRUS_SASL
888 if (conn)
889 sasl_dispose(&conn);
890 #endif /* CYRUS_SASL */
891 fclose (input);
892 fclose (output);
893
894 return OK;
895 }
896
897
898 #if !defined(MPOP) || defined(NNTP)
899 static
900 #endif
901 int
902 command(const char *fmt, ...)
903 {
904 va_list ap;
905 int result;
906
907 va_start(ap, fmt);
908 result = vcommand(fmt, ap);
909 va_end(ap);
910
911 return result;
912 }
913
914
915 static int
916 vcommand (const char *fmt, va_list ap)
917 {
918 char *cp, buffer[BUFSIZ];
919
920 vsnprintf (buffer, sizeof(buffer), fmt, ap);
921 if (poprint) {
922 #ifdef CYRUS_SASL
923 if (sasl_ssf)
924 fprintf(stderr, "(encrypted) ");
925 #endif /* CYRUS_SASL */
926 if (pophack) {
927 if ((cp = strchr (buffer, ' ')))
928 *cp = 0;
929 fprintf (stderr, "---> %s ********\n", buffer);
930 if (cp)
931 *cp = ' ';
932 pophack = 0;
933 }
934 else
935 fprintf (stderr, "---> %s\n", buffer);
936 }
937
938 if (putline (buffer, output) == NOTOK)
939 return NOTOK;
940
941 #ifdef CYRUS_SASL
942 if (poprint && sasl_ssf)
943 fprintf(stderr, "(decrypted) ");
944 #endif /* CYRUS_SASL */
945
946 switch (getline (response, sizeof response, input)) {
947 case OK:
948 if (poprint)
949 fprintf (stderr, "<--- %s\n", response);
950 #ifndef NNTP
951 return (*response == '+' ? OK : NOTOK);
952 #else /* NNTP */
953 return (*response < CHAR_ERR ? OK : NOTOK);
954 #endif /* NNTP */
955
956 case NOTOK:
957 case DONE:
958 if (poprint)
959 fprintf (stderr, "%s\n", response);
960 return NOTOK;
961 }
962
963 return NOTOK; /* NOTREACHED */
964 }
965
966
967 #if defined(MPOP) && !defined(NNTP)
968 int
969 multiline (void)
970 #else
971 static int
972 multiline (void)
973 #endif
974 {
975 char buffer[BUFSIZ + TRMLEN];
976
977 if (getline (buffer, sizeof buffer, input) != OK)
978 return NOTOK;
979 #ifdef DEBUG
980 if (poprint) {
981 #ifdef CYRUS_SASL
982 if (sasl_ssf)
983 fprintf(stderr, "(decrypted) ");
984 #endif /* CYRUS_SASL */
985 fprintf (stderr, "<--- %s\n", response);
986 }
987 #endif DEBUG
988 if (strncmp (buffer, TRM, TRMLEN) == 0) {
989 if (buffer[TRMLEN] == 0)
990 return DONE;
991 else
992 strncpy (response, buffer + TRMLEN, sizeof(response));
993 }
994 else
995 strncpy (response, buffer, sizeof(response));
996
997 return OK;
998 }
999
1000 /*
1001 * Note that these functions have been modified to deal with layer encryption
1002 * in the SASL case
1003 */
1004
1005 static int
1006 getline (char *s, int n, FILE *iop)
1007 {
1008 int c;
1009 char *p;
1010
1011 p = s;
1012 while (--n > 0 && (c = sasl_fgetc (iop)) != EOF && c != -2)
1013 if ((*p++ = c) == '\n')
1014 break;
1015 if (c == -2)
1016 return NOTOK;
1017 if (ferror (iop) && c != EOF) {
1018 strncpy (response, "error on connection", sizeof(response));
1019 return NOTOK;
1020 }
1021 if (c == EOF && p == s) {
1022 strncpy (response, "connection closed by foreign host", sizeof(response));
1023 return DONE;
1024 }
1025 *p = 0;
1026 if (*--p == '\n')
1027 *p = 0;
1028 if (*--p == '\r')
1029 *p = 0;
1030
1031 return OK;
1032 }
1033
1034
1035 static int
1036 putline (char *s, FILE *iop)
1037 {
1038 #ifdef CYRUS_SASL
1039 char outbuf[BUFSIZ], *buf;
1040 int result;
1041 unsigned int buflen;
1042
1043 if (!sasl_complete) {
1044 #endif /* CYRUS_SASL */
1045 fprintf (iop, "%s\r\n", s);
1046 #ifdef CYRUS_SASL
1047 } else {
1048 /*
1049 * Build an output buffer, encrypt it using sasl_encode, and
1050 * squirt out the results.
1051 */
1052 strncpy(outbuf, s, sizeof(outbuf) - 3);
1053 outbuf[sizeof(outbuf) - 3] = '\0'; /* Just in case */
1054 strcat(outbuf, "\r\n");
1055
1056 result = sasl_encode(conn, outbuf, strlen(outbuf),
1057 (const char **) &buf, &buflen);
1058
1059 if (result != SASL_OK) {
1060 snprintf(response, sizeof(response), "SASL encoding error: %s",
1061 sasl_errdetail(conn));
1062 return NOTOK;
1063 }
1064
1065 fwrite(buf, buflen, 1, iop);
1066 }
1067 #endif /* CYRUS_SASL */
1068
1069 fflush (iop);
1070 if (ferror (iop)) {
1071 strncpy (response, "lost connection", sizeof(response));
1072 return NOTOK;
1073 }
1074
1075 return OK;
1076 }
1077
1078 #ifdef CYRUS_SASL
1079 /*
1080 * Okay, our little fgetc replacement. Hopefully this is a little more
1081 * efficient than the last one.
1082 */
1083 static int
1084 sasl_fgetc(FILE *f)
1085 {
1086 static unsigned char *buffer = NULL, *ptr;
1087 static int size = 0;
1088 static int cnt = 0;
1089 unsigned int retbufsize = 0;
1090 int cc, result;
1091 char *retbuf, tmpbuf[BUFSIZ];
1092
1093 /*
1094 * If we have some leftover data, return that
1095 */
1096
1097 if (cnt) {
1098 cnt--;
1099 return (int) *ptr++;
1100 }
1101
1102 /*
1103 * Otherwise, fill our buffer until we have some data to return.
1104 */
1105
1106 while (retbufsize == 0) {
1107
1108 cc = read(fileno(f), tmpbuf, sizeof(tmpbuf));
1109
1110 if (cc == 0)
1111 return EOF;
1112
1113 if (cc < 0) {
1114 snprintf(response, sizeof(response), "Error during read from "
1115 "network: %s", strerror(errno));
1116 return -2;
1117 }
1118
1119 /*
1120 * We're not allowed to call sasl_decode until sasl_complete is
1121 * true, so we do these gyrations ...
1122 */
1123
1124 if (!sasl_complete) {
1125
1126 retbuf = tmpbuf;
1127 retbufsize = cc;
1128
1129 } else {
1130
1131 result = sasl_decode(conn, tmpbuf, cc,
1132 (const char **) &retbuf, &retbufsize);
1133
1134 if (result != SASL_OK) {
1135 snprintf(response, sizeof(response), "Error during SASL "
1136 "decoding: %s", sasl_errdetail(conn));
1137 return -2;
1138 }
1139 }
1140 }
1141
1142 if (retbufsize > size) {
1143 buffer = realloc(buffer, retbufsize);
1144 if (!buffer) {
1145 snprintf(response, sizeof(response), "Error during realloc in "
1146 "read routine: %s", strerror(errno));
1147 return -2;
1148 }
1149 size = retbufsize;
1150 }
1151
1152 memcpy(buffer, retbuf, retbufsize);
1153 ptr = buffer + 1;
1154 cnt = retbufsize - 1;
1155
1156 return (int) buffer[0];
1157 }
1158 #endif /* CYRUS_SASL */