[5029] | 1 | /* |
---|
[12350] | 2 | * $Id: popmail.c,v 1.10 1999-01-22 23:10:24 ghudson Exp $ |
---|
[5029] | 3 | * |
---|
| 4 | */ |
---|
| 5 | |
---|
[5238] | 6 | #if !defined(lint) && !defined(SABER) |
---|
[12350] | 7 | static char *rcsid = "$Id: popmail.c,v 1.10 1999-01-22 23:10:24 ghudson Exp $"; |
---|
[5238] | 8 | #endif /* lint || SABER */ |
---|
[5029] | 9 | |
---|
| 10 | #include <stdio.h> |
---|
| 11 | #include <sys/types.h> |
---|
| 12 | #include <sys/file.h> |
---|
| 13 | #include <sys/socket.h> |
---|
| 14 | #include <errno.h> |
---|
[8855] | 15 | #include <string.h> |
---|
[10325] | 16 | #include <stdlib.h> |
---|
[10463] | 17 | #include <netinet/in.h> |
---|
| 18 | #include <netdb.h> |
---|
| 19 | #ifdef HAVE_HESIOD |
---|
[5029] | 20 | #include <hesiod.h> |
---|
| 21 | #endif |
---|
[10463] | 22 | #ifdef HAVE_KRB4 |
---|
[5029] | 23 | #include <krb.h> |
---|
| 24 | #endif |
---|
| 25 | |
---|
| 26 | #define NOTOK (-1) |
---|
| 27 | #define OK 0 |
---|
| 28 | #define DONE 1 |
---|
| 29 | |
---|
[10463] | 30 | extern FILE *sfi; |
---|
| 31 | extern FILE *sfo; |
---|
| 32 | extern char Errmsg[80]; |
---|
| 33 | #ifdef HAVE_KRB4 |
---|
[6359] | 34 | char *PrincipalHostname(); |
---|
[10325] | 35 | #define KPOP_PORT 1109 |
---|
[5029] | 36 | #endif |
---|
| 37 | extern int popmail_debug; |
---|
| 38 | |
---|
| 39 | pop_init(host) |
---|
| 40 | char *host; |
---|
| 41 | { |
---|
| 42 | register struct hostent *hp; |
---|
| 43 | register struct servent *sp; |
---|
| 44 | int lport = IPPORT_RESERVED - 1; |
---|
| 45 | struct sockaddr_in sin; |
---|
| 46 | register int s; |
---|
[10463] | 47 | #ifdef HAVE_KRB4 |
---|
[5029] | 48 | KTEXT ticket = (KTEXT)NULL; |
---|
| 49 | int rem; |
---|
| 50 | long authopts; |
---|
[10325] | 51 | char *hostname; |
---|
[5238] | 52 | #endif |
---|
[5029] | 53 | |
---|
[11448] | 54 | if (!host) |
---|
| 55 | return(NOTOK); |
---|
[5029] | 56 | hp = gethostbyname(host); |
---|
| 57 | if (hp == NULL) { |
---|
| 58 | (void) sprintf(Errmsg, "MAILHOST unknown: %s", host); |
---|
| 59 | return(NOTOK); |
---|
| 60 | } |
---|
| 61 | |
---|
[10463] | 62 | #ifdef HAVE_KRB4 |
---|
[5029] | 63 | sp = getservbyname("kpop", "tcp"); |
---|
[10325] | 64 | if (sp == 0) |
---|
| 65 | sin.sin_port = htons(KPOP_PORT); |
---|
| 66 | else |
---|
| 67 | sin.sin_port = sp->s_port; |
---|
[5238] | 68 | #else |
---|
[5029] | 69 | sp = getservbyname("pop", "tcp"); |
---|
| 70 | if (sp == 0) { |
---|
| 71 | (void) strcpy(Errmsg, "tcp/pop: unknown service"); |
---|
| 72 | return(NOTOK); |
---|
| 73 | } |
---|
[10325] | 74 | sin.sin_port = sp->s_port; |
---|
[5238] | 75 | #endif |
---|
[5029] | 76 | |
---|
| 77 | sin.sin_family = hp->h_addrtype; |
---|
[8855] | 78 | memcpy(&sin.sin_addr, hp->h_addr, hp->h_length); |
---|
[10463] | 79 | #ifdef HAVE_KRB4 |
---|
[5029] | 80 | s = socket(AF_INET, SOCK_STREAM, 0); |
---|
[5238] | 81 | #else |
---|
[5029] | 82 | s = rresvport(&lport); |
---|
[5238] | 83 | #endif |
---|
[5029] | 84 | if (s < 0) { |
---|
[10809] | 85 | (void) sprintf(Errmsg, "error creating socket: %s", strerror(errno)); |
---|
[5029] | 86 | return(NOTOK); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | if (connect(s, (struct sockaddr *)&sin, sizeof sin) < 0) { |
---|
[10809] | 90 | (void) sprintf(Errmsg, "error during connect: %s", strerror(errno)); |
---|
[5029] | 91 | (void) close(s); |
---|
| 92 | return(NOTOK); |
---|
| 93 | } |
---|
[10463] | 94 | #ifdef HAVE_KRB4 |
---|
[5029] | 95 | ticket = (KTEXT)malloc( sizeof(KTEXT_ST) ); |
---|
[10325] | 96 | if (ticket == NULL) { |
---|
[5238] | 97 | fprintf (stderr, "from: out of memory"); |
---|
| 98 | exit (1); |
---|
[10325] | 99 | } |
---|
[5029] | 100 | rem=KSUCCESS; |
---|
| 101 | authopts = 0L; |
---|
[10325] | 102 | |
---|
| 103 | /* We stash hp->h_name as krb_realmofhost may stomp on the |
---|
| 104 | internal structures pointed by hp*/ |
---|
| 105 | hostname = malloc(strlen(hp->h_name)+1); |
---|
| 106 | if (hostname == NULL) { |
---|
| 107 | fprintf(stderr, "from: out of memory"); |
---|
| 108 | exit(1); |
---|
| 109 | } |
---|
| 110 | strcpy(hostname, hp->h_name); |
---|
| 111 | rem = krb_sendauth(authopts, s, ticket, "pop", hostname, |
---|
| 112 | (char *) krb_realmofhost(hostname), |
---|
[5029] | 113 | 0, (MSG_DAT *) 0, (CREDENTIALS *) 0, |
---|
| 114 | (bit_64 *) 0, (struct sockaddr_in *)0, |
---|
| 115 | (struct sockaddr_in *)0,"ZMAIL0.0"); |
---|
[10325] | 116 | free(hostname); |
---|
[5029] | 117 | if (rem != KSUCCESS) { |
---|
| 118 | (void) sprintf(Errmsg, "kerberos error: %s",krb_err_txt[rem]); |
---|
| 119 | (void) close(s); |
---|
| 120 | return(NOTOK); |
---|
| 121 | } |
---|
[5238] | 122 | #endif |
---|
[5029] | 123 | |
---|
| 124 | sfi = fdopen(s, "r"); |
---|
| 125 | sfo = fdopen(s, "w"); |
---|
| 126 | if (sfi == NULL || sfo == NULL) { |
---|
[5238] | 127 | (void) sprintf(Errmsg, "error in fdopen\n"); |
---|
[5029] | 128 | (void) close(s); |
---|
| 129 | return(NOTOK); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | return(OK); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | /*VARARGS1*/ |
---|
| 136 | pop_command(fmt, a, b, c, d) |
---|
| 137 | char *fmt; |
---|
| 138 | { |
---|
| 139 | char buf[4096]; |
---|
| 140 | |
---|
| 141 | (void) sprintf(buf, fmt, a, b, c, d); |
---|
| 142 | |
---|
| 143 | if (popmail_debug) fprintf(stderr, "---> %s\n", buf); |
---|
| 144 | if (putline(buf, Errmsg, sfo) == NOTOK) return(NOTOK); |
---|
| 145 | |
---|
| 146 | if (getline(buf, sizeof buf, sfi) != OK) { |
---|
| 147 | (void) strcpy(Errmsg, buf); |
---|
| 148 | return(NOTOK); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | if (popmail_debug) fprintf(stderr, "<--- %s\n", buf); |
---|
| 152 | if (*buf != '+') { |
---|
| 153 | (void) strcpy(Errmsg, buf); |
---|
| 154 | return(NOTOK); |
---|
| 155 | } else { |
---|
| 156 | return(OK); |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | pop_stat(nmsgs, nbytes) |
---|
| 162 | int *nmsgs, *nbytes; |
---|
| 163 | { |
---|
| 164 | char buf[4096]; |
---|
| 165 | |
---|
| 166 | if (popmail_debug) fprintf(stderr, "---> STAT\n"); |
---|
| 167 | if (putline("STAT", Errmsg, sfo) == NOTOK) return(NOTOK); |
---|
| 168 | |
---|
| 169 | if (getline(buf, sizeof buf, sfi) != OK) { |
---|
| 170 | (void) strcpy(Errmsg, buf); |
---|
| 171 | return(NOTOK); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | if (popmail_debug) fprintf(stderr, "<--- %s\n", buf); |
---|
| 175 | if (*buf != '+') { |
---|
| 176 | (void) strcpy(Errmsg, buf); |
---|
| 177 | return(NOTOK); |
---|
| 178 | } else { |
---|
| 179 | (void) sscanf(buf, "+OK %d %d", nmsgs, nbytes); |
---|
| 180 | return(OK); |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | |
---|
| 185 | putline(buf, err, f) |
---|
| 186 | char *buf; |
---|
| 187 | char *err; |
---|
| 188 | FILE *f; |
---|
| 189 | { |
---|
| 190 | fprintf(f, "%s\r\n", buf); |
---|
| 191 | (void) fflush(f); |
---|
| 192 | if (ferror(f)) { |
---|
| 193 | (void) strcpy(err, "lost connection"); |
---|
| 194 | return(NOTOK); |
---|
| 195 | } |
---|
| 196 | return(OK); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | getline(buf, n, f) |
---|
| 200 | char *buf; |
---|
| 201 | register int n; |
---|
| 202 | FILE *f; |
---|
| 203 | { |
---|
| 204 | register char *p; |
---|
| 205 | int c; |
---|
| 206 | |
---|
| 207 | p = buf; |
---|
| 208 | while (--n > 0 && (c = fgetc(f)) != EOF) |
---|
| 209 | if ((*p++ = c) == '\n') break; |
---|
| 210 | |
---|
| 211 | if (ferror(f)) { |
---|
| 212 | (void) strcpy(buf, "error on connection"); |
---|
| 213 | return (NOTOK); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | if (c == EOF && p == buf) { |
---|
| 217 | (void) strcpy(buf, "connection closed by foreign host"); |
---|
| 218 | return (DONE); |
---|
| 219 | } |
---|
| 220 | |
---|
[5238] | 221 | *p = '\0'; |
---|
| 222 | if (*--p == '\n') *p = '\0'; |
---|
| 223 | if (*--p == '\r') *p = '\0'; |
---|
[5029] | 224 | return(OK); |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | multiline(buf, n, f) |
---|
| 228 | char *buf; |
---|
| 229 | register int n; |
---|
| 230 | FILE *f; |
---|
| 231 | { |
---|
| 232 | if (getline(buf, n, f) != OK) return (NOTOK); |
---|
| 233 | if (*buf == '.') { |
---|
[5238] | 234 | if (*(buf+1) == '\0') { |
---|
[5029] | 235 | return (DONE); |
---|
| 236 | } else { |
---|
| 237 | (void) strcpy(buf, buf+1); |
---|
| 238 | } |
---|
| 239 | } |
---|
| 240 | return(OK); |
---|
| 241 | } |
---|