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