summaryrefslogtreecommitdiffstats
path: root/src/appl/mailquery/poplib.c
blob: 5c840b3908b0114857a0fd9ef232cd4e3d5d3e0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
 * (c) Copyright 1994 HEWLETT-PACKARD COMPANY
 * 
 * To anyone who acknowledges that this file is provided 
 * "AS IS" without any express or implied warranty:
 * permission to use, copy, modify, and distribute this 
 * file for any purpose is hereby granted without fee, 
 * provided that the above copyright notice and this 
 * notice appears in all copies, and that the name of 
 * Hewlett-Packard Company not be used in advertising or 
 * publicity pertaining to distribution of the software 
 * without specific, written prior permission.  Hewlett-
 * Packard Company makes no representations about the 
 * suitability of this software for any purpose.
 */

/*
 * Poplib - library routines for speaking POP
 */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <errno.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#if defined(KRB4) && defined(KRB5)
error You cannot define both KRB4 and KRB5
#endif
#ifndef KPOP_SERVICE
#define KPOP_SERVICE "kpop"
#endif
#ifdef KPOP
#ifdef KRB4
#include <krb.h>
#endif
#ifdef KRB5
#include <krb5/krb5.h>
#include <krb5/ext-proto.h>
#include <krb5/los-proto.h>
#include <com_err.h>
#include <ctype.h>
#endif
#endif

#include "pop.h"

void *xmalloc();

char Errmsg[80];		/* to return error messages */
int pop_debug;

static FILE *sfi = 0;
static FILE *sfo = 0;

pop_init(host, reserved)
char *host;
int reserved;
{
    register struct hostent *hp;
    register struct servent *sp;
    int lport = IPPORT_RESERVED - 1;
    struct sockaddr_in sin;
    int s;
    char *get_errmsg();
    char response[1024];
    char *routine;
#ifdef KPOP
#ifdef KRB4
    CREDENTIALS cred;
    KTEXT ticket = (KTEXT)NULL;
    int rem;
#endif
#ifdef KRB5
    krb5_error_code retval;
    krb5_context context;
    krb5_ccache ccdef;
    krb5_principal client = NULL, server = NULL;
    krb5_error *err_ret = NULL;
    register char *cp;
#endif
#endif

    if (sfi && sfo) {
	return OK;		/* guessing at this -- eichin -- XXX */
    }

    hp = gethostbyname(host);
    if (hp == NULL) {
	sprintf(Errmsg, "MAILHOST unknown: %s", host);
	return(NOTOK);
    }

#ifdef KPOP
    sp = getservbyname(KPOP_SERVICE, "tcp");
    if (sp == 0) {
	(void) strcpy(Errmsg, "tcp/kpop: unknown service");
	return(NOTOK);
    }
#else /* !KPOP */
    sp = getservbyname("pop", "tcp");
    if (sp == 0) {
	(void) strcpy(Errmsg, "tcp/pop: unknown service");
	return(NOTOK);
    }
#endif /* KPOP */
    if (sp == 0) {
	strcpy(Errmsg, "tcp/pop: unknown service");
	return(NOTOK);
    }

    sin.sin_family = hp->h_addrtype;
    memcpy((char *)&sin.sin_addr, hp->h_addr, hp->h_length);
    sin.sin_port = sp->s_port;
#ifdef KPOP
    s = socket(AF_INET, SOCK_STREAM, 0);
#else /* !KPOP */
    if (reserved) 
      s = rresvport(&lport);
    else
      s = socket(AF_INET, SOCK_STREAM, 0);
#endif /* KPOP */
    
    if (s < 0) {
	sprintf(Errmsg, "error creating socket: %s", get_errmsg());
	return(NOTOK);
    }

    if (connect(s, (struct sockaddr *)&sin, sizeof sin) < 0) {
	sprintf(Errmsg, "error during connect: %s", get_errmsg());
	close(s);
	return(NOTOK);
    }

#ifdef KPOP
#ifdef KRB4
    /* Get tgt creds from ticket file. This is used to calculate the
     * lifetime for the pop ticket so that it expires with the
     * tgt */
    rem = krb_get_cred("krbtgt", krb_realmofhost(hp->h_name), krb_realmofhost(hp->h_name), &cred);
    if (rem == KSUCCESS) {
            long lifetime;
            lifetime = ((cred.issue_date + ((unsigned char)cred.lifetime * 5 * 60)) - time(0)) / (5 * 60);
            if (lifetime > 0)
                    krb_set_lifetime(lifetime);
    }            
    ticket = (KTEXT)malloc( sizeof(KTEXT_ST) );
    rem = krb_sendauth(0L, s, ticket, "pop", hp->h_name, (char *)0,
		       0, (MSG_DAT *) 0, (CREDENTIALS *) 0,
		       (bit_64 *) 0, (struct sockaddr_in *)0,
		       (struct sockaddr_in *)0,"ZMAIL0.0");
    if (rem != KSUCCESS) {
	(void) sprintf(Errmsg, "kerberos error: %s",krb_err_txt[rem]);
	(void) close(s);
	return(NOTOK);
    }
#endif /* KRB4 */
#ifdef KRB5
    krb5_init_context(&context);
    krb5_init_ets(context);

    routine = "krb5_cc_default";
    if (retval = krb5_cc_default(context, &ccdef)) {
    krb5error:
	sprintf(Errmsg, "%s: krb5 error: %s", routine, error_message(retval));
	close(s);
	return(NOTOK);
    }
    routine = "krb5_cc_get_principal";
    if (retval = krb5_cc_get_principal(context, ccdef, &client)) {
	goto krb5error;
    }

#if 0
    /* lower-case to get name for "instance" part of service name */
    for (cp = hp->h_name; *cp; cp++)
	if (isupper(*cp))
	    *cp = tolower(*cp);
#endif

    routine = "krb5_sname_to_principal";
    if (retval = krb5_sname_to_principal(context, hp->h_name, "pop",
					 KRB5_NT_UNKNOWN,
					 &server)) {
	goto krb5error;
    }

    retval = krb5_sendauth(context, (krb5_pointer) &s, "KPOPV1.0", 
			   client, server,
			   AP_OPTS_MUTUAL_REQUIRED,
			   0,		/* no checksum */
			   0,		/* no creds, use ccache instead */
			   ccdef,
			   0,		/* don't need seq # */
			   0,		/* don't need a subsession key */
			   &err_ret,
			   0);		/* don't need reply */
    krb5_free_principal(context, server);
    if (retval) {
	if (err_ret && err_ret->text.length) {
	    sprintf(Errmsg, "krb5 error: %s [server says '%*s'] ",
		    error_message(retval),
		    err_ret->text.length,
		    err_ret->text.data);
	    krb5_free_error(context, err_ret);
	} else
	    sprintf(Errmsg, "krb5_sendauth: krb5 error: %s", error_message(retval));
	close(s);
	return(NOTOK);
    }
#endif /* KRB5 */
#endif /* KPOP */

    sfi = fdopen(s, "r");
    sfo = fdopen(s, "w");
    if (sfi == NULL || sfo == NULL) {
	sprintf(Errmsg, "error in fdopen: %s", get_errmsg());
	close(s);
	return(NOTOK);
    }

    if (getline(response, sizeof response, sfi) != OK) {
	error(response);
	return(NOTOK);
    }
    if (pop_debug)
      fprintf(stderr, "<--- %s\n", response);

    return(OK);
}

pop_command(fmt, a, b, c, d)
char *fmt;
char *a, *b, *c, *d;
{
    char buf[1024];

    sprintf(buf, fmt, a, b, c, d);

    if (pop_debug) fprintf(stderr, "---> %s\n", buf);
    if (putline(buf, Errmsg, sfo) == NOTOK) return(NOTOK);

    if (getline(buf, sizeof buf, sfi) != OK) {
	strcpy(Errmsg, buf);
	return(NOTOK);
    }

    if (pop_debug) fprintf(stderr, "<--- %s\n", buf);
    if (*buf != '+') {
	strcpy(Errmsg, buf);
	return(NOTOK);
    } else {
	return(OK);
    }
}

pop_query(nbytes, user)
     int *nbytes;
     char *user;
{
    char buf[1024];

    if (strlen(user) > 120) {
	if (pop_debug) fprintf(stderr, "username %s too long\n", user);
	return NOTOK;
    }
    
    sprintf(buf, "QUERY %s", user);    
    if (pop_debug) fprintf(stderr, "---> %s\n", buf);
    if (putline(buf, Errmsg, sfo) == NOTOK) return (NOTOK);

    if (getline(buf, sizeof buf, sfi) != OK) {
	strcpy(Errmsg, buf);
	return NOTOK;
    }

    if (pop_debug) fprintf(stderr, "<--- %s\n", buf);
    if (*buf != '+') {
	strcpy(Errmsg, buf);
	return NOTOK;
    } else {
	sscanf(buf, "+OK %d", nbytes);
	return OK;
    }
}
    
pop_stat(nmsgs, nbytes)
int *nmsgs, *nbytes;
{
    char buf[1024];

    if (pop_debug) fprintf(stderr, "---> STAT\n");
    if (putline("STAT", Errmsg, sfo) == NOTOK) return(NOTOK);

    if (getline(buf, sizeof buf, sfi) != OK) {
	strcpy(Errmsg, buf);
	return(NOTOK);
    }

    if (pop_debug) fprintf(stderr, "<--- %s\n", buf);
    if (*buf != '+') {
	strcpy(Errmsg, buf);
	return(NOTOK);
    } else {
	sscanf(buf, "+OK %d %d", nmsgs, nbytes);
	return(OK);
    }
}

pop_retr(msgno, action, arg)
int msgno;
int (*action)();
char *arg;			/* is this always FILE*??? -- XXX */
{
    char buf[1024];
    int nbytes = 0;
    
    sprintf(buf, "RETR %d", msgno);

    if (pop_debug)
      fprintf(stderr, "---> %s\n", buf);

    if (putline(buf, Errmsg, sfo) == NOTOK) return(NOTOK);

    if (getline(buf, sizeof buf, sfi) != OK) {
	strcpy(Errmsg, buf);
	return(NOTOK);
    }
    if (pop_debug)
      fprintf(stderr, "<--- %s\n", buf);

    sscanf(buf, "+OK %d", &nbytes);

    while (1) {
	switch (multiline(buf, sizeof buf, sfi)) {
	case OK:
            if ((*action)(buf, arg, nbytes) < 0) {
                strcat(Errmsg, get_errmsg());
                return (DONE);	/* Some error occured in action */
            }
	    break;
	case DONE:
	    return (OK);
	case NOTOK:
	    strcpy(Errmsg, buf);
	    return (NOTOK);
	}
    }
}

pop_getline(buf, n)
     char *buf;
     int n;
{
    return getline(buf, n, sfi);
}

getline(buf, n, f)
char *buf;
register int n;
FILE *f;
{
    register char *p;
    int c;

    p = buf;
    while (--n > 0 && (c = fgetc(f)) != EOF)
      if ((*p++ = c) == '\n') break;

    if (ferror(f)) {
	strcpy(buf, "error on connection");
	return (NOTOK);
    }

    if (c == EOF && p == buf) {
	strcpy(buf, "connection closed by foreign host");
	return (DONE);
    }

    *p = '\0';
    if (*--p == '\n') *p = '\0';
    if (*--p == '\r') *p = '\0';
    return(OK);
}

multiline(buf, n, f)
char *buf;
register int n;
FILE *f;
{
    if (getline(buf, n, f) != OK) return (NOTOK);
    if (*buf == '.') {
	if (*(buf+1) == '\0') {
	    return (DONE);
	} else {
	    strcpy(buf, buf+1);
	}
    }
    return(OK);
}

#ifndef HAS_STRERROR
char *
strerror(e)
    int e;
{
    extern int errno, sys_nerr;
    extern char *sys_errlist[];

    if (errno < sys_nerr)
      return sys_errlist[errno];
    else
      return "unknown error";
}
#endif

char *
get_errmsg()
{
    char *s = strerror(errno);
    
    return(s);
}

putline(buf, err, f)
char *buf;
char *err;
FILE *f;
{
    fprintf(f, "%s\r\n", buf);
    fflush(f);
    if (ferror(f)) {
	strcpy(err, "lost connection");
	return(NOTOK);
    }
    return(OK);
}


/* Print error message and exit.  */

fatal (s1, s2)
     char *s1, *s2;
{
  error (s1, s2);
  exit (1);
}

/* Print error message.  `s1' is printf control string, `s2' is arg for it. */

error (s1, s2, s3)
     char *s1, *s2, *s3;
{
  printf ("poplib: ");
  printf (s1, s2, s3);
  printf ("\n");
}

pfatal_with_name (name)
     char *name;
{
  char *s = concat ("", strerror(errno), " for %s");

  fatal (s, name);
}

/* Return a newly-allocated string whose contents concatenate those of s1, s2, s3.  */

char *
concat (s1, s2, s3)
     char *s1, *s2, *s3;
{
  int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  char *result = (char *) xmalloc (len1 + len2 + len3 + 1);

  strcpy (result, s1);
  strcpy (result + len1, s2);
  strcpy (result + len1 + len2, s3);
  *(result + len1 + len2 + len3) = 0;

  return result;
}

/* Like malloc but get fatal error if memory is exhausted.  */

void *
xmalloc (size)
     int size;
{
  void *result = malloc (size);
  if (!result)
    fatal ("virtual memory exhausted", 0);
  return result;
}