1 | /* |
---|
2 | * $Id: popmail.c,v 1.8 1997-11-22 19:23:21 ghudson Exp $ |
---|
3 | * $Source: /afs/dev.mit.edu/source/repository/athena/bin/from/popmail.c,v $ |
---|
4 | * $Author: ghudson $ |
---|
5 | * |
---|
6 | */ |
---|
7 | |
---|
8 | #if !defined(lint) && !defined(SABER) |
---|
9 | static char *rcsid = "$Id: popmail.c,v 1.8 1997-11-22 19:23:21 ghudson Exp $"; |
---|
10 | #endif /* lint || SABER */ |
---|
11 | |
---|
12 | #include <stdio.h> |
---|
13 | #include <sys/types.h> |
---|
14 | #include <sys/file.h> |
---|
15 | #include <sys/socket.h> |
---|
16 | #include <errno.h> |
---|
17 | #include <string.h> |
---|
18 | #include <stdlib.h> |
---|
19 | #include <netinet/in.h> |
---|
20 | #include <netdb.h> |
---|
21 | #ifdef HAVE_HESIOD |
---|
22 | #include <hesiod.h> |
---|
23 | #endif |
---|
24 | #ifdef HAVE_KRB4 |
---|
25 | #include <krb.h> |
---|
26 | #endif |
---|
27 | |
---|
28 | #define NOTOK (-1) |
---|
29 | #define OK 0 |
---|
30 | #define DONE 1 |
---|
31 | |
---|
32 | extern FILE *sfi; |
---|
33 | extern FILE *sfo; |
---|
34 | extern char Errmsg[80]; |
---|
35 | #ifdef HAVE_KRB4 |
---|
36 | char *PrincipalHostname(); |
---|
37 | #define KPOP_PORT 1109 |
---|
38 | #endif |
---|
39 | extern int popmail_debug; |
---|
40 | |
---|
41 | pop_init(host) |
---|
42 | char *host; |
---|
43 | { |
---|
44 | register struct hostent *hp; |
---|
45 | register struct servent *sp; |
---|
46 | int lport = IPPORT_RESERVED - 1; |
---|
47 | struct sockaddr_in sin; |
---|
48 | register int s; |
---|
49 | #ifdef HAVE_KRB4 |
---|
50 | KTEXT ticket = (KTEXT)NULL; |
---|
51 | int rem; |
---|
52 | long authopts; |
---|
53 | char *hostname; |
---|
54 | #endif |
---|
55 | |
---|
56 | hp = gethostbyname(host); |
---|
57 | if (hp == NULL) { |
---|
58 | (void) sprintf(Errmsg, "MAILHOST unknown: %s", host); |
---|
59 | return(NOTOK); |
---|
60 | } |
---|
61 | |
---|
62 | #ifdef HAVE_KRB4 |
---|
63 | sp = getservbyname("kpop", "tcp"); |
---|
64 | if (sp == 0) |
---|
65 | sin.sin_port = htons(KPOP_PORT); |
---|
66 | else |
---|
67 | sin.sin_port = sp->s_port; |
---|
68 | #else |
---|
69 | sp = getservbyname("pop", "tcp"); |
---|
70 | if (sp == 0) { |
---|
71 | (void) strcpy(Errmsg, "tcp/pop: unknown service"); |
---|
72 | return(NOTOK); |
---|
73 | } |
---|
74 | sin.sin_port = sp->s_port; |
---|
75 | #endif |
---|
76 | |
---|
77 | sin.sin_family = hp->h_addrtype; |
---|
78 | memcpy(&sin.sin_addr, hp->h_addr, hp->h_length); |
---|
79 | #ifdef HAVE_KRB4 |
---|
80 | s = socket(AF_INET, SOCK_STREAM, 0); |
---|
81 | #else |
---|
82 | s = rresvport(&lport); |
---|
83 | #endif |
---|
84 | if (s < 0) { |
---|
85 | (void) sprintf(Errmsg, "error creating socket: %s", strerror(errno)); |
---|
86 | return(NOTOK); |
---|
87 | } |
---|
88 | |
---|
89 | if (connect(s, (struct sockaddr *)&sin, sizeof sin) < 0) { |
---|
90 | (void) sprintf(Errmsg, "error during connect: %s", strerror(errno)); |
---|
91 | (void) close(s); |
---|
92 | return(NOTOK); |
---|
93 | } |
---|
94 | #ifdef HAVE_KRB4 |
---|
95 | ticket = (KTEXT)malloc( sizeof(KTEXT_ST) ); |
---|
96 | if (ticket == NULL) { |
---|
97 | fprintf (stderr, "from: out of memory"); |
---|
98 | exit (1); |
---|
99 | } |
---|
100 | rem=KSUCCESS; |
---|
101 | authopts = 0L; |
---|
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), |
---|
113 | 0, (MSG_DAT *) 0, (CREDENTIALS *) 0, |
---|
114 | (bit_64 *) 0, (struct sockaddr_in *)0, |
---|
115 | (struct sockaddr_in *)0,"ZMAIL0.0"); |
---|
116 | free(hostname); |
---|
117 | if (rem != KSUCCESS) { |
---|
118 | (void) sprintf(Errmsg, "kerberos error: %s",krb_err_txt[rem]); |
---|
119 | (void) close(s); |
---|
120 | return(NOTOK); |
---|
121 | } |
---|
122 | #endif |
---|
123 | |
---|
124 | sfi = fdopen(s, "r"); |
---|
125 | sfo = fdopen(s, "w"); |
---|
126 | if (sfi == NULL || sfo == NULL) { |
---|
127 | (void) sprintf(Errmsg, "error in fdopen\n"); |
---|
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 | |
---|
221 | *p = '\0'; |
---|
222 | if (*--p == '\n') *p = '\0'; |
---|
223 | if (*--p == '\r') *p = '\0'; |
---|
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 == '.') { |
---|
234 | if (*(buf+1) == '\0') { |
---|
235 | return (DONE); |
---|
236 | } else { |
---|
237 | (void) strcpy(buf, buf+1); |
---|
238 | } |
---|
239 | } |
---|
240 | return(OK); |
---|
241 | } |
---|