Mercurial > emacs
comparison lib-src/pop.c @ 9158:939488de5d3f
Initial revision
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Tue, 27 Sep 1994 19:49:25 +0000 |
parents | |
children | 0774e217e8aa |
comparison
equal
deleted
inserted
replaced
9157:29f4cce05fa5 | 9158:939488de5d3f |
---|---|
1 /* pop.c: client routines for talking to a POP3-protocol post-office server | |
2 Copyright (c) 1991,1993 Free Software Foundation, Inc. | |
3 Written by Jonathan Kamens, jik@security.ov.com. | |
4 | |
5 This file is part of GNU Emacs. | |
6 | |
7 GNU Emacs is free software; you can redistribute it and/or modify | |
8 it under the terms of the GNU General Public License as published by | |
9 the Free Software Foundation; either version 2, or (at your option) | |
10 any later version. | |
11 | |
12 GNU Emacs is distributed in the hope that it will be useful, | |
13 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 GNU General Public License for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with GNU Emacs; see the file COPYING. If not, write to | |
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | |
21 #ifdef MAIL_USE_POP | |
22 | |
23 #include <sys/types.h> | |
24 #include <netinet/in.h> | |
25 #include <sys/socket.h> | |
26 #include <pop.h> | |
27 #ifdef sun | |
28 #include <malloc.h> | |
29 #else | |
30 extern char *malloc (/* unsigned */); | |
31 extern char *realloc (/* char *, unsigned */); | |
32 extern void free (/* char * */); | |
33 #endif | |
34 #endif | |
35 #ifdef HESIOD | |
36 #include <hesiod.h> | |
37 /* | |
38 * It really shouldn't be necessary to put this declaration here, but | |
39 * the version of hesiod.h that Athena has installed in release 7.2 | |
40 * doesn't declare this function; I don't know if the 7.3 version of | |
41 * hesiod.h does. | |
42 */ | |
43 extern struct servent *hes_getservbyname (/* char *, char * */); | |
44 #endif | |
45 #include <pwd.h> | |
46 #include <string.h> | |
47 #include <strings.h> | |
48 #include <netdb.h> | |
49 #include <errno.h> | |
50 #include <stdio.h> | |
51 #ifdef KERBEROS | |
52 #ifndef KRB5 | |
53 #include <krb.h> | |
54 #include <des.h> | |
55 #else /* KRB5 */ | |
56 #include <krb5/krb5.h> | |
57 #include <krb5/ext-proto.h> | |
58 #include <ctype.h> | |
59 #endif /* KRB5 */ | |
60 | |
61 extern char *getenv (/* char * */); | |
62 extern char *getlogin (/* void */); | |
63 extern char *getpass (/* char * */); | |
64 extern char *strerror (/* int */); | |
65 | |
66 #ifdef KERBEROS | |
67 extern int krb_sendauth (/* long, int, KTEXT, char *, char *, char *, | |
68 u_long, MSG_DAT *, CREDENTIALS *, Key_schedule, | |
69 struct sockaddr_in *, struct sockaddr_in *, | |
70 char * */); | |
71 extern char *krb_realmofhost (/* char * */); | |
72 #endif | |
73 | |
74 #ifndef HAVE_H_ERRNO | |
75 extern int h_errno; | |
76 #endif | |
77 | |
78 static int socket_connection (/* char *, int */); | |
79 static char *getline (/* popserver */); | |
80 static int sendline (/* popserver, char * */); | |
81 static int fullwrite (/* int, char *, int */); | |
82 static int getok (/* popserver */); | |
83 #if 0 | |
84 static int gettermination (/* popserver */); | |
85 #endif | |
86 static void pop_trash (/* popserver */); | |
87 | |
88 #define ERROR_MAX 80 /* a pretty arbitrary size */ | |
89 #define POP_PORT 110 | |
90 #define KPOP_PORT 1109 | |
91 #define POP_SERVICE "pop" | |
92 #ifdef KERBEROS | |
93 #ifdef KRB5 | |
94 #define KPOP_SERVICE "k5pop"; | |
95 #else | |
96 #define KPOP_SERVICE "kpop" | |
97 #endif | |
98 #endif | |
99 | |
100 char pop_error[ERROR_MAX]; | |
101 int pop_debug = 0; | |
102 | |
103 #ifndef min | |
104 #define min(a,b) (((a) < (b)) ? (a) : (b)) | |
105 #endif | |
106 | |
107 /* | |
108 * Function: pop_open (char *host, char *username, char *password, | |
109 * int flags) | |
110 * | |
111 * Purpose: Establishes a connection with a post-office server, and | |
112 * completes the authorization portion of the session. | |
113 * | |
114 * Arguments: | |
115 * host The server host with which the connection should be | |
116 * established. Optional. If omitted, internal | |
117 * heuristics will be used to determine the server host, | |
118 * if possible. | |
119 * username | |
120 * The username of the mail-drop to access. Optional. | |
121 * If omitted, internal heuristics will be used to | |
122 * determine the username, if possible. | |
123 * password | |
124 * The password to use for authorization. If omitted, | |
125 * internal heuristics will be used to determine the | |
126 * password, if possible. | |
127 * flags A bit mask containing flags controlling certain | |
128 * functions of the routine. Valid flags are defined in | |
129 * the file pop.h | |
130 * | |
131 * Return value: Upon successful establishment of a connection, a | |
132 * non-null popserver will be returned. Otherwise, null will be | |
133 * returned, and the string variable pop_error will contain an | |
134 * explanation of the error. | |
135 */ | |
136 popserver | |
137 pop_open (host, username, password, flags) | |
138 char *host; | |
139 char *username; | |
140 char *password; | |
141 int flags; | |
142 { | |
143 int sock; | |
144 popserver server; | |
145 | |
146 /* Determine the user name */ | |
147 if (! username) | |
148 { | |
149 username = getenv ("USER"); | |
150 if (! (username && *username)) | |
151 { | |
152 username = getlogin (); | |
153 if (! (username && *username)) | |
154 { | |
155 struct passwd *passwd; | |
156 passwd = getpwuid (getuid ()); | |
157 if (passwd && passwd->pw_name && *passwd->pw_name) | |
158 { | |
159 username = passwd->pw_name; | |
160 } | |
161 else | |
162 { | |
163 strcpy (pop_error, "Could not determine username"); | |
164 return (0); | |
165 } | |
166 } | |
167 } | |
168 } | |
169 | |
170 /* | |
171 * Determine the mail host. | |
172 */ | |
173 | |
174 if (! host) | |
175 { | |
176 host = getenv ("MAILHOST"); | |
177 } | |
178 | |
179 #ifdef HESIOD | |
180 if ((! host) && (! (flags & POP_NO_HESIOD))) | |
181 { | |
182 struct hes_postoffice *office; | |
183 office = hes_getmailhost (username); | |
184 if (office && office->po_type && (! strcmp (office->po_type, "POP")) | |
185 && office->po_name && *office->po_name && office->po_host | |
186 && *office->po_host) | |
187 { | |
188 host = office->po_host; | |
189 username = office->po_name; | |
190 } | |
191 } | |
192 #endif | |
193 | |
194 #ifdef MAILHOST | |
195 if (! host) | |
196 { | |
197 host = MAILHOST; | |
198 } | |
199 #endif | |
200 | |
201 if (! host) | |
202 { | |
203 strcpy (pop_error, "Could not determine POP server"); | |
204 return (0); | |
205 } | |
206 | |
207 /* Determine the password */ | |
208 #ifdef KERBEROS | |
209 #define DONT_NEED_PASSWORD (! (flags & POP_NO_KERBEROS)) | |
210 #else | |
211 #define DONT_NEED_PASSWORD 0 | |
212 #endif | |
213 | |
214 if ((! password) && (! DONT_NEED_PASSWORD)) | |
215 { | |
216 if (! (flags & POP_NO_GETPASS)) | |
217 { | |
218 password = getpass ("Enter POP password:"); | |
219 } | |
220 if (! password) | |
221 { | |
222 strcpy (pop_error, "Could not determine POP password"); | |
223 return (0); | |
224 } | |
225 } | |
226 if (password) | |
227 flags |= POP_NO_KERBEROS; | |
228 else | |
229 password = username; | |
230 | |
231 sock = socket_connection (host, flags); | |
232 if (sock == -1) | |
233 return (0); | |
234 | |
235 server = (popserver) malloc (sizeof (struct _popserver)); | |
236 if (! server) | |
237 { | |
238 strcpy (pop_error, "Out of memory in pop_open"); | |
239 return (0); | |
240 } | |
241 server->buffer = (char *) malloc (GETLINE_MIN); | |
242 if (! server->buffer) | |
243 { | |
244 strcpy (pop_error, "Out of memory in pop_open"); | |
245 free ((char *) server); | |
246 return (0); | |
247 } | |
248 | |
249 server->file = sock; | |
250 server->data = 0; | |
251 server->buffer_index = 0; | |
252 server->buffer_size = GETLINE_MIN; | |
253 server->in_multi = 0; | |
254 | |
255 if (getok (server)) | |
256 return (0); | |
257 | |
258 /* | |
259 * I really shouldn't use the pop_error variable like this, but.... | |
260 */ | |
261 if (strlen (username) > ERROR_MAX - 6) | |
262 { | |
263 pop_close (server); | |
264 strcpy (pop_error, | |
265 "Username too long; recompile pop.c with larger ERROR_MAX"); | |
266 return (0); | |
267 } | |
268 sprintf (pop_error, "USER %s", username); | |
269 | |
270 if (sendline (server, pop_error) || getok (server)) | |
271 { | |
272 return (0); | |
273 } | |
274 | |
275 if (strlen (password) > ERROR_MAX - 6) | |
276 { | |
277 pop_close (server); | |
278 strcpy (pop_error, | |
279 "Password too long; recompile pop.c with larger ERROR_MAX"); | |
280 return (0); | |
281 } | |
282 sprintf (pop_error, "PASS %s", password); | |
283 | |
284 if (sendline (server, pop_error) || getok (server)) | |
285 { | |
286 return (0); | |
287 } | |
288 | |
289 return (server); | |
290 } | |
291 | |
292 /* | |
293 * Function: pop_stat | |
294 * | |
295 * Purpose: Issue the STAT command to the server and return (in the | |
296 * value parameters) the number of messages in the maildrop and | |
297 * the total size of the maildrop. | |
298 * | |
299 * Return value: 0 on success, or non-zero with an error in pop_error | |
300 * in failure. | |
301 * | |
302 * Side effects: On failure, may make further operations on the | |
303 * connection impossible. | |
304 */ | |
305 int | |
306 pop_stat (server, count, size) | |
307 popserver server; | |
308 int *count; | |
309 int *size; | |
310 { | |
311 char *fromserver; | |
312 | |
313 if (server->in_multi) | |
314 { | |
315 strcpy (pop_error, "In multi-line query in pop_stat"); | |
316 return (-1); | |
317 } | |
318 | |
319 if (sendline (server, "STAT") || (! (fromserver = getline (server)))) | |
320 return (-1); | |
321 | |
322 if (strncmp (fromserver, "+OK ", 4)) | |
323 { | |
324 if (0 == strncmp (fromserver, "-ERR", 4)) | |
325 { | |
326 strncpy (pop_error, fromserver, ERROR_MAX); | |
327 } | |
328 else | |
329 { | |
330 strcpy (pop_error, | |
331 "Unexpected response from POP server in pop_stat"); | |
332 pop_trash (server); | |
333 } | |
334 return (-1); | |
335 } | |
336 | |
337 *count = atoi (&fromserver[4]); | |
338 | |
339 fromserver = index (&fromserver[4], ' '); | |
340 if (! fromserver) | |
341 { | |
342 strcpy (pop_error, | |
343 "Badly formatted response from server in pop_stat"); | |
344 pop_trash (server); | |
345 return (-1); | |
346 } | |
347 | |
348 *size = atoi (fromserver + 1); | |
349 | |
350 return (0); | |
351 } | |
352 | |
353 /* | |
354 * Function: pop_list | |
355 * | |
356 * Purpose: Performs the POP "list" command and returns (in value | |
357 * parameters) two malloc'd zero-terminated arrays -- one of | |
358 * message IDs, and a parallel one of sizes. | |
359 * | |
360 * Arguments: | |
361 * server The pop connection to talk to. | |
362 * message The number of the one message about which to get | |
363 * information, or 0 to get information about all | |
364 * messages. | |
365 * | |
366 * Return value: 0 on success, non-zero with error in pop_error on | |
367 * failure. | |
368 * | |
369 * Side effects: On failure, may make further operations on the | |
370 * connection impossible. | |
371 */ | |
372 int | |
373 pop_list (server, message, IDs, sizes) | |
374 popserver server; | |
375 int message; | |
376 int **IDs; | |
377 int **sizes; | |
378 { | |
379 int how_many, i; | |
380 char *fromserver; | |
381 | |
382 if (server->in_multi) | |
383 { | |
384 strcpy (pop_error, "In multi-line query in pop_list"); | |
385 return (-1); | |
386 } | |
387 | |
388 if (message) | |
389 how_many = 1; | |
390 else | |
391 { | |
392 int count, size; | |
393 if (pop_stat (server, &count, &size)) | |
394 return (-1); | |
395 how_many = count; | |
396 } | |
397 | |
398 *IDs = (int *) malloc ((how_many + 1) * sizeof (int)); | |
399 *sizes = (int *) malloc ((how_many + 1) * sizeof (int)); | |
400 if (! (*IDs && *sizes)) | |
401 { | |
402 strcpy (pop_error, "Out of memory in pop_list"); | |
403 return (-1); | |
404 } | |
405 | |
406 if (message) | |
407 { | |
408 sprintf (pop_error, "LIST %d", message); | |
409 if (sendline (server, pop_error)) | |
410 { | |
411 free ((char *) *IDs); | |
412 free ((char *) *sizes); | |
413 return (-1); | |
414 } | |
415 if (! (fromserver = getline (server))) | |
416 { | |
417 free ((char *) *IDs); | |
418 free ((char *) *sizes); | |
419 return (-1); | |
420 } | |
421 if (strncmp (fromserver, "+OK ", 4)) | |
422 { | |
423 if (! strncmp (fromserver, "-ERR", 4)) | |
424 strncpy (pop_error, fromserver, ERROR_MAX); | |
425 else | |
426 { | |
427 strcpy (pop_error, | |
428 "Unexpected response from server in pop_list"); | |
429 pop_trash (server); | |
430 } | |
431 free ((char *) *IDs); | |
432 free ((char *) *sizes); | |
433 return (-1); | |
434 } | |
435 (*IDs)[0] = atoi (&fromserver[4]); | |
436 fromserver = index (&fromserver[4], ' '); | |
437 if (! fromserver) | |
438 { | |
439 strcpy (pop_error, | |
440 "Badly formatted response from server in pop_list"); | |
441 pop_trash (server); | |
442 free ((char *) *IDs); | |
443 free ((char *) *sizes); | |
444 return (-1); | |
445 } | |
446 (*sizes)[0] = atoi (fromserver); | |
447 (*IDs)[1] = (*sizes)[1] = 0; | |
448 return (0); | |
449 } | |
450 else | |
451 { | |
452 if (pop_multi_first (server, "LIST", &fromserver)) | |
453 { | |
454 free ((char *) *IDs); | |
455 free ((char *) *sizes); | |
456 return (-1); | |
457 } | |
458 for (i = 0; i < how_many; i++) | |
459 { | |
460 if (pop_multi_next (server, &fromserver)) | |
461 { | |
462 free ((char *) *IDs); | |
463 free ((char *) *sizes); | |
464 return (-1); | |
465 } | |
466 (*IDs)[i] = atoi (fromserver); | |
467 fromserver = index (fromserver, ' '); | |
468 if (! fromserver) | |
469 { | |
470 strcpy (pop_error, | |
471 "Badly formatted response from server in pop_list"); | |
472 free ((char *) *IDs); | |
473 free ((char *) *sizes); | |
474 pop_trash (server); | |
475 return (-1); | |
476 } | |
477 (*sizes)[i] = atoi (fromserver); | |
478 } | |
479 if (pop_multi_next (server, &fromserver)) | |
480 { | |
481 free ((char *) *IDs); | |
482 free ((char *) *sizes); | |
483 return (-1); | |
484 } | |
485 else if (fromserver) | |
486 { | |
487 strcpy (pop_error, | |
488 "Too many response lines from server in pop_list"); | |
489 free ((char *) *IDs); | |
490 free ((char *) *sizes); | |
491 return (-1); | |
492 } | |
493 (*IDs)[i] = (*sizes)[i] = 0; | |
494 return (0); | |
495 } | |
496 } | |
497 | |
498 /* | |
499 * Function: pop_retrieve | |
500 * | |
501 * Purpose: Retrieve a specified message from the maildrop. | |
502 * | |
503 * Arguments: | |
504 * server The server to retrieve from. | |
505 * message The message number to retrieve. | |
506 * markfrom | |
507 * If true, then mark the string "From " at the beginning | |
508 * of lines with '>'. | |
509 * | |
510 * Return value: A string pointing to the message, if successful, or | |
511 * null with pop_error set if not. | |
512 * | |
513 * Side effects: May kill connection on error. | |
514 */ | |
515 char * | |
516 pop_retrieve (server, message, markfrom) | |
517 popserver server; | |
518 int message; | |
519 int markfrom; | |
520 { | |
521 int *IDs, *sizes, bufsize, fromcount = 0, cp = 0; | |
522 char *ptr, *fromserver; | |
523 int ret; | |
524 | |
525 if (server->in_multi) | |
526 { | |
527 strcpy (pop_error, "In multi-line query in pop_retrieve"); | |
528 return (0); | |
529 } | |
530 | |
531 if (pop_list (server, message, &IDs, &sizes)) | |
532 return (0); | |
533 | |
534 if (pop_retrieve_first (server, message, &fromserver)) | |
535 { | |
536 return (0); | |
537 } | |
538 | |
539 /* | |
540 * The "5" below is an arbitrary constant -- I assume that if | |
541 * there are "From" lines in the text to be marked, there | |
542 * probably won't be more than 5 of them. If there are, I | |
543 * allocate more space for them below. | |
544 */ | |
545 bufsize = sizes[0] + (markfrom ? 5 : 0); | |
546 ptr = malloc (bufsize); | |
547 free ((char *) IDs); | |
548 free ((char *) sizes); | |
549 | |
550 if (! ptr) | |
551 { | |
552 strcpy (pop_error, "Out of memory in pop_retrieve"); | |
553 pop_retrieve_flush (server); | |
554 return (0); | |
555 } | |
556 | |
557 while (! (ret = pop_retrieve_next (server, &fromserver))) | |
558 { | |
559 int linesize; | |
560 | |
561 if (! fromserver) | |
562 { | |
563 ptr[cp] = '\0'; | |
564 return (ptr); | |
565 } | |
566 if (markfrom && fromserver[0] == 'F' && fromserver[1] == 'r' && | |
567 fromserver[2] == 'o' && fromserver[3] == 'm' && | |
568 fromserver[4] == ' ') | |
569 { | |
570 if (++fromcount == 5) | |
571 { | |
572 bufsize += 5; | |
573 ptr = realloc (ptr, bufsize); | |
574 if (! ptr) | |
575 { | |
576 strcpy (pop_error, "Out of memory in pop_retrieve"); | |
577 pop_retrieve_flush (server); | |
578 return (0); | |
579 } | |
580 fromcount = 0; | |
581 } | |
582 ptr[cp++] = '>'; | |
583 } | |
584 linesize = strlen (fromserver); | |
585 bcopy (fromserver, &ptr[cp], linesize); | |
586 cp += linesize; | |
587 ptr[cp++] = '\n'; | |
588 } | |
589 | |
590 if (ret) | |
591 { | |
592 free (ptr); | |
593 return (0); | |
594 } | |
595 } | |
596 | |
597 int | |
598 pop_retrieve_first (server, message, response) | |
599 popserver server; | |
600 int message; | |
601 char **response; | |
602 { | |
603 sprintf (pop_error, "RETR %d", message); | |
604 return (pop_multi_first (server, pop_error, response)); | |
605 } | |
606 | |
607 int | |
608 pop_retrieve_next (server, line) | |
609 popserver server; | |
610 char **line; | |
611 { | |
612 return (pop_multi_next (server, line)); | |
613 } | |
614 | |
615 int | |
616 pop_retrieve_flush (server) | |
617 popserver server; | |
618 { | |
619 return (pop_multi_flush (server)); | |
620 } | |
621 | |
622 int | |
623 pop_top_first (server, message, lines, response) | |
624 popserver server; | |
625 int message, lines; | |
626 char **response; | |
627 { | |
628 sprintf (pop_error, "TOP %d %d", message, lines); | |
629 return (pop_multi_first (server, pop_error, response)); | |
630 } | |
631 | |
632 int | |
633 pop_top_next (server, line) | |
634 popserver server; | |
635 char **line; | |
636 { | |
637 return (pop_multi_next (server, line)); | |
638 } | |
639 | |
640 int | |
641 pop_top_flush (server) | |
642 popserver server; | |
643 { | |
644 return (pop_multi_flush (server)); | |
645 } | |
646 | |
647 int | |
648 pop_multi_first (server, command, response) | |
649 popserver server; | |
650 char *command; | |
651 char **response; | |
652 { | |
653 if (server->in_multi) | |
654 { | |
655 strcpy (pop_error, | |
656 "Already in multi-line query in pop_multi_first"); | |
657 return (-1); | |
658 } | |
659 | |
660 if (sendline (server, command) || (! (*response = getline (server)))) | |
661 { | |
662 return (-1); | |
663 } | |
664 | |
665 if (0 == strncmp (*response, "-ERR", 4)) | |
666 { | |
667 strncpy (pop_error, *response, ERROR_MAX); | |
668 return (-1); | |
669 } | |
670 else if (0 == strncmp (*response, "+OK", 3)) | |
671 { | |
672 for (*response += 3; **response == ' '; (*response)++) /* empty */; | |
673 server->in_multi = 1; | |
674 return (0); | |
675 } | |
676 else | |
677 { | |
678 strcpy (pop_error, | |
679 "Unexpected response from server in pop_multi_first"); | |
680 return (-1); | |
681 } | |
682 } | |
683 | |
684 int | |
685 pop_multi_next (server, line) | |
686 popserver server; | |
687 char **line; | |
688 { | |
689 char *fromserver; | |
690 | |
691 if (! server->in_multi) | |
692 { | |
693 strcpy (pop_error, "Not in multi-line query in pop_multi_next"); | |
694 return (-1); | |
695 } | |
696 | |
697 fromserver = getline (server); | |
698 if (! fromserver) | |
699 { | |
700 return (-1); | |
701 } | |
702 | |
703 if (fromserver[0] == '.') | |
704 { | |
705 if (! fromserver[1]) | |
706 { | |
707 *line = 0; | |
708 server->in_multi = 0; | |
709 return (0); | |
710 } | |
711 else | |
712 { | |
713 *line = fromserver + 1; | |
714 return (0); | |
715 } | |
716 } | |
717 else | |
718 { | |
719 *line = fromserver; | |
720 return (0); | |
721 } | |
722 } | |
723 | |
724 int | |
725 pop_multi_flush (server) | |
726 popserver server; | |
727 { | |
728 char *line; | |
729 | |
730 if (! server->in_multi) | |
731 { | |
732 return (0); | |
733 } | |
734 | |
735 while (! pop_multi_next (server, &line)) | |
736 { | |
737 if (! line) | |
738 { | |
739 return (0); | |
740 } | |
741 } | |
742 | |
743 return (-1); | |
744 } | |
745 | |
746 /* Function: pop_delete | |
747 * | |
748 * Purpose: Delete a specified message. | |
749 * | |
750 * Arguments: | |
751 * server Server from which to delete the message. | |
752 * message Message to delete. | |
753 * | |
754 * Return value: 0 on success, non-zero with error in pop_error | |
755 * otherwise. | |
756 */ | |
757 int | |
758 pop_delete (server, message) | |
759 popserver server; | |
760 int message; | |
761 { | |
762 if (server->in_multi) | |
763 { | |
764 strcpy (pop_error, "In multi-line query in pop_delete"); | |
765 return (-1); | |
766 } | |
767 | |
768 sprintf (pop_error, "DELE %d", message); | |
769 | |
770 if (sendline (server, pop_error) || getok (server)) | |
771 return (-1); | |
772 | |
773 return (0); | |
774 } | |
775 | |
776 /* | |
777 * Function: pop_noop | |
778 * | |
779 * Purpose: Send a noop command to the server. | |
780 * | |
781 * Argument: | |
782 * server The server to send to. | |
783 * | |
784 * Return value: 0 on success, non-zero with error in pop_error | |
785 * otherwise. | |
786 * | |
787 * Side effects: Closes connection on error. | |
788 */ | |
789 int | |
790 pop_noop (server) | |
791 popserver server; | |
792 { | |
793 if (server->in_multi) | |
794 { | |
795 strcpy (pop_error, "In multi-line query in pop_noop"); | |
796 return (-1); | |
797 } | |
798 | |
799 if (sendline (server, "NOOP") || getok (server)) | |
800 return (-1); | |
801 | |
802 return (0); | |
803 } | |
804 | |
805 /* | |
806 * Function: pop_last | |
807 * | |
808 * Purpose: Find out the highest seen message from the server. | |
809 * | |
810 * Arguments: | |
811 * server The server. | |
812 * | |
813 * Return value: If successful, the highest seen message, which is | |
814 * greater than or equal to 0. Otherwise, a negative number with | |
815 * the error explained in pop_error. | |
816 * | |
817 * Side effects: Closes the connection on error. | |
818 */ | |
819 int | |
820 pop_last (server) | |
821 popserver server; | |
822 { | |
823 char *fromserver; | |
824 | |
825 if (server->in_multi) | |
826 { | |
827 strcpy (pop_error, "In multi-line query in pop_last"); | |
828 return (-1); | |
829 } | |
830 | |
831 if (sendline (server, "LAST")) | |
832 return (-1); | |
833 | |
834 if (! (fromserver = getline (server))) | |
835 return (-1); | |
836 | |
837 if (! strncmp (fromserver, "-ERR", 4)) | |
838 { | |
839 strncpy (pop_error, fromserver, ERROR_MAX); | |
840 return (-1); | |
841 } | |
842 else if (strncmp (fromserver, "+OK ", 4)) | |
843 { | |
844 strcpy (pop_error, "Unexpected response from server in pop_last"); | |
845 pop_trash (server); | |
846 return (-1); | |
847 } | |
848 else | |
849 { | |
850 return (atoi (&fromserver[4])); | |
851 } | |
852 } | |
853 | |
854 /* | |
855 * Function: pop_reset | |
856 * | |
857 * Purpose: Reset the server to its initial connect state | |
858 * | |
859 * Arguments: | |
860 * server The server. | |
861 * | |
862 * Return value: 0 for success, non-0 with error in pop_error | |
863 * otherwise. | |
864 * | |
865 * Side effects: Closes the connection on error. | |
866 */ | |
867 int | |
868 pop_reset (server) | |
869 popserver server; | |
870 { | |
871 if (pop_retrieve_flush (server)) | |
872 { | |
873 return (-1); | |
874 } | |
875 | |
876 if (sendline (server, "RSET") || getok (server)) | |
877 return (-1); | |
878 | |
879 return (0); | |
880 } | |
881 | |
882 /* | |
883 * Function: pop_quit | |
884 * | |
885 * Purpose: Quit the connection to the server, | |
886 * | |
887 * Arguments: | |
888 * server The server to quit. | |
889 * | |
890 * Return value: 0 for success, non-zero otherwise with error in | |
891 * pop_error. | |
892 * | |
893 * Side Effects: The popserver passed in is unuseable after this | |
894 * function is called, even if an error occurs. | |
895 */ | |
896 int | |
897 pop_quit (server) | |
898 popserver server; | |
899 { | |
900 int ret = 0; | |
901 | |
902 if (server->file >= 0) | |
903 { | |
904 if (pop_retrieve_flush (server)) | |
905 { | |
906 ret = -1; | |
907 } | |
908 | |
909 if (sendline (server, "QUIT") || getok (server)) | |
910 { | |
911 ret = -1; | |
912 } | |
913 | |
914 close (server->file); | |
915 } | |
916 | |
917 if (server->buffer) | |
918 free (server->buffer); | |
919 free ((char *) server); | |
920 | |
921 return (ret); | |
922 } | |
923 | |
924 /* | |
925 * Function: socket_connection | |
926 * | |
927 * Purpose: Opens the network connection with the mail host, without | |
928 * doing any sort of I/O with it or anything. | |
929 * | |
930 * Arguments: | |
931 * host The host to which to connect. | |
932 * flags Option flags. | |
933 * | |
934 * Return value: A file descriptor indicating the connection, or -1 | |
935 * indicating failure, in which case an error has been copied | |
936 * into pop_error. | |
937 */ | |
938 static int | |
939 socket_connection (host, flags) | |
940 char *host; | |
941 int flags; | |
942 { | |
943 struct hostent *hostent; | |
944 struct servent *servent; | |
945 struct sockaddr_in addr; | |
946 char found_port = 0; | |
947 char *service; | |
948 int sock; | |
949 #ifdef KERBEROS | |
950 #ifdef KRB5 | |
951 krb5_error_code rem; | |
952 krb5_ccache ccdef; | |
953 krb5_principal client, server; | |
954 krb5_error *err_ret; | |
955 register char *cp; | |
956 #else | |
957 KTEXT ticket; | |
958 MSG_DAT msg_data; | |
959 CREDENTIALS cred; | |
960 Key_schedule schedule; | |
961 int rem; | |
962 #endif /* KRB5 */ | |
963 #endif /* KERBEROS */ | |
964 | |
965 int try_count = 0; | |
966 | |
967 do | |
968 { | |
969 hostent = gethostbyname (host); | |
970 try_count++; | |
971 if ((! hostent) && ((h_errno != TRY_AGAIN) || (try_count == 5))) | |
972 { | |
973 strcpy (pop_error, "Could not determine POP server's address"); | |
974 return (-1); | |
975 } | |
976 } while (! hostent); | |
977 | |
978 bzero ((char *) &addr, sizeof (addr)); | |
979 addr.sin_family = AF_INET; | |
980 | |
981 #ifdef KERBEROS | |
982 service = (flags & POP_NO_KERBEROS) ? POP_SERVICE : KPOP_SERVICE; | |
983 #else | |
984 service = POP_SERVICE; | |
985 #endif | |
986 | |
987 #ifdef HESIOD | |
988 if (! (flags & POP_NO_HESIOD)) | |
989 { | |
990 servent = hes_getservbyname (service, "tcp"); | |
991 if (servent) | |
992 { | |
993 addr.sin_port = servent->s_port; | |
994 found_port = 1; | |
995 } | |
996 } | |
997 #endif | |
998 if (! found_port) | |
999 { | |
1000 servent = getservbyname (service, "tcp"); | |
1001 if (servent) | |
1002 { | |
1003 addr.sin_port = servent->s_port; | |
1004 } | |
1005 else | |
1006 { | |
1007 #ifdef KERBEROS | |
1008 addr.sin_port = htons ((flags & POP_NO_KERBEROS) ? | |
1009 POP_PORT : KPOP_PORT); | |
1010 #else | |
1011 addr.sin_port = htons (POP_PORT); | |
1012 #endif | |
1013 } | |
1014 } | |
1015 | |
1016 #define SOCKET_ERROR "Could not create socket for POP connection: " | |
1017 | |
1018 sock = socket (PF_INET, SOCK_STREAM, 0); | |
1019 if (sock < 0) | |
1020 { | |
1021 strcpy (pop_error, SOCKET_ERROR); | |
1022 strncat (pop_error, strerror (errno), | |
1023 ERROR_MAX - sizeof (SOCKET_ERROR)); | |
1024 return (-1); | |
1025 | |
1026 } | |
1027 | |
1028 while (*hostent->h_addr_list) | |
1029 { | |
1030 bcopy (*hostent->h_addr_list, (char *) &addr.sin_addr, | |
1031 hostent->h_length); | |
1032 if (! connect (sock, (struct sockaddr *) &addr, sizeof (addr))) | |
1033 break; | |
1034 hostent->h_addr_list++; | |
1035 } | |
1036 | |
1037 #define CONNECT_ERROR "Could not connect to POP server: " | |
1038 | |
1039 if (! *hostent->h_addr_list) | |
1040 { | |
1041 (void) close (sock); | |
1042 strcpy (pop_error, CONNECT_ERROR); | |
1043 strncat (pop_error, strerror (errno), | |
1044 ERROR_MAX - sizeof (CONNECT_ERROR)); | |
1045 return (-1); | |
1046 | |
1047 } | |
1048 | |
1049 #ifdef KERBEROS | |
1050 #define KRB_ERROR "Kerberos error connecting to POP server: " | |
1051 if (! (flags & POP_NO_KERBEROS)) | |
1052 { | |
1053 #ifdef KRB5 | |
1054 krb5_init_ets (); | |
1055 | |
1056 if (rem = krb5_cc_default (&ccdef)) | |
1057 { | |
1058 krb5error: | |
1059 strcpy (pop_error, KRB_ERROR); | |
1060 strncat (pop_error, error_message (rem), | |
1061 ERROR_MAX - sizeof(KRB_ERROR)); | |
1062 (void) close (sock); | |
1063 return (-1); | |
1064 } | |
1065 | |
1066 if (rem = krb5_cc_get_principal (ccdef, &client)) | |
1067 { | |
1068 goto krb5error; | |
1069 } | |
1070 | |
1071 for (cp = hostent->h_name; *cp; cp++) | |
1072 { | |
1073 if (isupper (*cp)) | |
1074 { | |
1075 *cp = tolower (*cp); | |
1076 } | |
1077 } | |
1078 | |
1079 if (rem = krb5_sname_to_principal (hostent->h_name, POP_SERVICE, | |
1080 FALSE, &server)) | |
1081 { | |
1082 goto krb5error; | |
1083 } | |
1084 | |
1085 rem = krb5_sendauth ((krb5_pointer) &sock, "KPOPV1.0", client, server, | |
1086 AP_OPTS_MUTUAL_REQUIRED, | |
1087 0, /* no checksum */ | |
1088 0, /* no creds, use ccache instead */ | |
1089 ccdef, | |
1090 0, /* don't need seq # */ | |
1091 0, /* don't need subsession key */ | |
1092 &err_ret, | |
1093 0); /* don't need reply */ | |
1094 krb5_free_principal (server); | |
1095 if (rem) | |
1096 { | |
1097 if (err_ret && err_ret->text.length) | |
1098 { | |
1099 strcpy (pop_error, KRB_ERROR); | |
1100 strncat (pop_error, error_message (rem), | |
1101 ERROR_MAX - sizeof (KRB_ERROR)); | |
1102 strncat (pop_error, " [server says '", | |
1103 ERROR_MAX - strlen (pop_error) - 1); | |
1104 strncat (pop_error, err_ret->text.data, | |
1105 min (ERROR_MAX - strlen (pop_error) - 1, | |
1106 err_ret->text.length)); | |
1107 strncat (pop_error, "']", | |
1108 ERROR_MAX - strlen (pop_error) - 1); | |
1109 } | |
1110 else | |
1111 { | |
1112 strcpy (pop_error, KRB_ERROR); | |
1113 strncat (pop_error, error_message (rem), | |
1114 ERROR_MAX - sizeof (KRB_ERROR)); | |
1115 } | |
1116 if (err_ret) | |
1117 krb5_free_error (err_ret); | |
1118 | |
1119 (void) close (sock); | |
1120 return (-1); | |
1121 } | |
1122 #else /* ! KRB5 */ | |
1123 ticket = (KTEXT) malloc (sizeof (KTEXT_ST)); | |
1124 rem = krb_sendauth (0L, sock, ticket, "pop", hostent->h_name, | |
1125 (char *) krb_realmofhost (hostent->h_name), | |
1126 (unsigned long) 0, &msg_data, &cred, schedule, | |
1127 (struct sockaddr_in *) 0, | |
1128 (struct sockaddr_in *) 0, | |
1129 "KPOPV0.1"); | |
1130 free ((char *) ticket); | |
1131 if (rem != KSUCCESS) | |
1132 { | |
1133 strcpy (pop_error, KRB_ERROR); | |
1134 strncat (pop_error, krb_err_txt[rem], | |
1135 ERROR_MAX - sizeof (KRB_ERROR)); | |
1136 (void) close (sock); | |
1137 return (-1); | |
1138 } | |
1139 #endif /* KRB5 */ | |
1140 } | |
1141 #endif /* KERBEROS */ | |
1142 | |
1143 return (sock); | |
1144 } /* socket_connection */ | |
1145 | |
1146 /* | |
1147 * Function: getline | |
1148 * | |
1149 * Purpose: Get a line of text from the connection and return a | |
1150 * pointer to it. The carriage return and linefeed at the end of | |
1151 * the line are stripped, but periods at the beginnings of lines | |
1152 * are NOT dealt with in any special way. | |
1153 * | |
1154 * Arguments: | |
1155 * server The server from which to get the line of text. | |
1156 * | |
1157 * Returns: A non-null pointer if successful, or a null pointer on any | |
1158 * error, with an error message copied into pop_error. | |
1159 * | |
1160 * Notes: The line returned is overwritten with each call to getline. | |
1161 * | |
1162 * Side effects: Closes the connection on error. | |
1163 */ | |
1164 static char * | |
1165 getline (server) | |
1166 popserver server; | |
1167 { | |
1168 #define GETLINE_ERROR "Error reading from server: " | |
1169 | |
1170 int ret; | |
1171 | |
1172 if (server->data) | |
1173 { | |
1174 char *cp = strstr (server->buffer + server->buffer_index, "\r\n"); | |
1175 if (cp) | |
1176 { | |
1177 int found; | |
1178 int data_used; | |
1179 | |
1180 found = server->buffer_index; | |
1181 data_used = (cp + 2) - server->buffer - found; | |
1182 | |
1183 *cp = '\0'; /* terminate the string to be returned */ | |
1184 server->data -= data_used; | |
1185 server->buffer_index += data_used; | |
1186 | |
1187 if (pop_debug) | |
1188 fprintf (stderr, "<<< %s\n", server->buffer + found); | |
1189 return (server->buffer + found); | |
1190 } | |
1191 else | |
1192 { | |
1193 bcopy (server->buffer + server->buffer_index, | |
1194 server->buffer, server->data); | |
1195 server->buffer_index = 0; | |
1196 } | |
1197 } | |
1198 else | |
1199 { | |
1200 server->buffer_index = 0; | |
1201 } | |
1202 | |
1203 while (1) | |
1204 { | |
1205 if (server->data == server->buffer_size) | |
1206 { | |
1207 server->buffer_size += GETLINE_INCR; | |
1208 server->buffer = realloc (server->buffer, server->buffer_size); | |
1209 if (! server->buffer) | |
1210 { | |
1211 strcpy (pop_error, "Out of memory in getline"); | |
1212 pop_trash (server); | |
1213 return (0); | |
1214 } | |
1215 } | |
1216 ret = read (server->file, server->buffer + server->data, | |
1217 server->buffer_size - server->data); | |
1218 if (ret < 0) | |
1219 { | |
1220 strcpy (pop_error, GETLINE_ERROR); | |
1221 strncat (pop_error, strerror (errno), | |
1222 ERROR_MAX - sizeof (GETLINE_ERROR)); | |
1223 pop_trash (server); | |
1224 return (0); | |
1225 } | |
1226 else if (ret == 0) | |
1227 { | |
1228 strcpy (pop_error, "Unexpected EOF from server in getline"); | |
1229 pop_trash (server); | |
1230 return (0); | |
1231 } | |
1232 else | |
1233 { | |
1234 char *cp = strstr (server->buffer, "\r\n"); | |
1235 server->data += ret; | |
1236 | |
1237 if (cp) | |
1238 { | |
1239 int data_used = (cp + 2) - server->buffer; | |
1240 *cp = '\0'; | |
1241 server->data -= data_used; | |
1242 server->buffer_index = data_used; | |
1243 | |
1244 if (pop_debug) | |
1245 fprintf (stderr, "<<< %s\n", server->buffer); | |
1246 return (server->buffer); | |
1247 } | |
1248 } | |
1249 } | |
1250 | |
1251 /* NOTREACHED */ | |
1252 } | |
1253 | |
1254 /* | |
1255 * Function: sendline | |
1256 * | |
1257 * Purpose: Sends a line of text to the POP server. The line of text | |
1258 * passed into this function should NOT have the carriage return | |
1259 * and linefeed on the end of it. Periods at beginnings of lines | |
1260 * will NOT be treated specially by this function. | |
1261 * | |
1262 * Arguments: | |
1263 * server The server to which to send the text. | |
1264 * line The line of text to send. | |
1265 * | |
1266 * Return value: Upon successful completion, a value of 0 will be | |
1267 * returned. Otherwise, a non-zero value will be returned, and | |
1268 * an error will be copied into pop_error. | |
1269 * | |
1270 * Side effects: Closes the connection on error. | |
1271 */ | |
1272 static int | |
1273 sendline (server, line) | |
1274 popserver server; | |
1275 char *line; | |
1276 { | |
1277 #define SENDLINE_ERROR "Error writing to POP server: " | |
1278 int ret; | |
1279 | |
1280 ret = fullwrite (server->file, line, strlen (line)); | |
1281 if (ret >= 0) | |
1282 { /* 0 indicates that a blank line was written */ | |
1283 ret = fullwrite (server->file, "\r\n", 2); | |
1284 } | |
1285 | |
1286 if (ret < 0) | |
1287 { | |
1288 pop_trash (server); | |
1289 strcpy (pop_error, SENDLINE_ERROR); | |
1290 strncat (pop_error, strerror (errno), | |
1291 ERROR_MAX - sizeof (SENDLINE_ERROR)); | |
1292 return (ret); | |
1293 } | |
1294 | |
1295 if (pop_debug) | |
1296 fprintf (stderr, ">>> %s\n", line); | |
1297 | |
1298 return (0); | |
1299 } | |
1300 | |
1301 /* | |
1302 * Procedure: fullwrite | |
1303 * | |
1304 * Purpose: Just like write, but keeps trying until the entire string | |
1305 * has been written. | |
1306 * | |
1307 * Return value: Same as write. Pop_error is not set. | |
1308 */ | |
1309 static int | |
1310 fullwrite (fd, buf, nbytes) | |
1311 int fd; | |
1312 char *buf; | |
1313 int nbytes; | |
1314 { | |
1315 char *cp; | |
1316 int ret; | |
1317 | |
1318 cp = buf; | |
1319 while ((ret = write (fd, cp, nbytes)) > 0) | |
1320 { | |
1321 cp += ret; | |
1322 nbytes -= ret; | |
1323 } | |
1324 | |
1325 return (ret); | |
1326 } | |
1327 | |
1328 /* | |
1329 * Procedure getok | |
1330 * | |
1331 * Purpose: Reads a line from the server. If the return indicator is | |
1332 * positive, return with a zero exit status. If not, return with | |
1333 * a negative exit status. | |
1334 * | |
1335 * Arguments: | |
1336 * server The server to read from. | |
1337 * | |
1338 * Returns: 0 for success, else for failure and puts error in pop_error. | |
1339 * | |
1340 * Side effects: On failure, may make the connection unuseable. | |
1341 */ | |
1342 static int | |
1343 getok (server) | |
1344 popserver server; | |
1345 { | |
1346 char *fromline; | |
1347 | |
1348 if (! (fromline = getline (server))) | |
1349 { | |
1350 return (-1); | |
1351 } | |
1352 | |
1353 if (! strncmp (fromline, "+OK", 3)) | |
1354 return (0); | |
1355 else if (! strncmp (fromline, "-ERR", 4)) | |
1356 { | |
1357 strncpy (pop_error, fromline, ERROR_MAX); | |
1358 pop_error[ERROR_MAX-1] = '\0'; | |
1359 return (-1); | |
1360 } | |
1361 else | |
1362 { | |
1363 strcpy (pop_error, | |
1364 "Unexpected response from server; expecting +OK or -ERR"); | |
1365 pop_trash (server); | |
1366 return (-1); | |
1367 } | |
1368 } | |
1369 | |
1370 #if 0 | |
1371 /* | |
1372 * Function: gettermination | |
1373 * | |
1374 * Purpose: Gets the next line and verifies that it is a termination | |
1375 * line (nothing but a dot). | |
1376 * | |
1377 * Return value: 0 on success, non-zero with pop_error set on error. | |
1378 * | |
1379 * Side effects: Closes the connection on error. | |
1380 */ | |
1381 static int | |
1382 gettermination (server) | |
1383 popserver server; | |
1384 { | |
1385 char *fromserver; | |
1386 | |
1387 fromserver = getline (server); | |
1388 if (! fromserver) | |
1389 return (-1); | |
1390 | |
1391 if (strcmp (fromserver, ".")) | |
1392 { | |
1393 strcpy (pop_error, | |
1394 "Unexpected response from server in gettermination"); | |
1395 pop_trash (server); | |
1396 return (-1); | |
1397 } | |
1398 | |
1399 return (0); | |
1400 } | |
1401 #endif | |
1402 | |
1403 /* | |
1404 * Function pop_close | |
1405 * | |
1406 * Purpose: Close a pop connection, sending a "RSET" command to try to | |
1407 * preserve any changes that were made and a "QUIT" command to | |
1408 * try to get the server to quit, but ignoring any responses that | |
1409 * are received. | |
1410 * | |
1411 * Side effects: The server is unuseable after this function returns. | |
1412 * Changes made to the maildrop since the session was started (or | |
1413 * since the last pop_reset) may be lost. | |
1414 */ | |
1415 void | |
1416 pop_close (server) | |
1417 popserver server; | |
1418 { | |
1419 pop_trash (server); | |
1420 free ((char *) server); | |
1421 | |
1422 return; | |
1423 } | |
1424 | |
1425 /* | |
1426 * Function: pop_trash | |
1427 * | |
1428 * Purpose: Like pop_close or pop_quit, but doesn't deallocate the | |
1429 * memory associated with the server. It is legal to call | |
1430 * pop_close or pop_quit after this function has been called. | |
1431 */ | |
1432 static void | |
1433 pop_trash (server) | |
1434 popserver server; | |
1435 { | |
1436 if (server->file >= 0) | |
1437 { | |
1438 sendline (server, "RSET"); | |
1439 sendline (server, "QUIT"); | |
1440 | |
1441 close (server->file); | |
1442 server->file = -1; | |
1443 if (server->buffer) | |
1444 { | |
1445 free (server->buffer); | |
1446 server->buffer = 0; | |
1447 } | |
1448 } | |
1449 } | |
1450 | |
1451 /* Search in STRING for an occurrence of SUBSTRING | |
1452 and return a pointer to that occurrence. | |
1453 Return 0 if SUBSTRING does not occur in STRING. */ | |
1454 | |
1455 static char * | |
1456 my_strstr (string, substring) | |
1457 char *string, *substring; | |
1458 { | |
1459 char *p = string; | |
1460 | |
1461 while (*p) | |
1462 { | |
1463 if (!strcmp (p, substring)) | |
1464 return p; | |
1465 p++; | |
1466 } | |
1467 return 0; | |
1468 } | |
1469 | |
1470 #endif /* MAIL_USE_POP */ |