summaryrefslogtreecommitdiff
path: root/clients/zmailnotify/zmailnotify.c
blob: 5b1ed0645a3ecc6639584d8beb3883f00b40d00b (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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/* This file is part of the Project Athena Zephyr Notification System.
 * It contains code for the "zmailnotify" command.
 *
 *	Created by:	Robert French
 *
 *	$Id$
 *
 *	Copyright (c) 1987,1993 by the Massachusetts Institute of Technology.
 *	For copying and distribution information, see the file
 *	"mit-copyright.h". 
 */

#include <sysdep.h>
#include <zephyr/mit-copyright.h>
#include <zephyr/zephyr.h>

#ifndef lint
static const char rcsid_zmailnotify_c[] =
    "$Id$";
#endif

#include <sys/socket.h>
#include <pwd.h>
#include <netdb.h>
#ifdef HAVE_HESIOD
#include <hesiod.h>
#endif

#ifndef HAVE_KRB4
#undef KPOP
#endif

#ifdef KPOP
#include <krb.h>
#endif

#define NOTOK (-1)
#define OK 0
#define DONE 1

FILE *sfi;
FILE *sfo;
char Errmsg[80];

#ifdef KPOP
char *PrincipalHostname();
#endif

void get_message(), pop_close(), mail_notify(), fatal_pop_err ();
int pop_command __P((char *, ...));
#define MAXMAIL 4

struct _mail {
	char *from;
	char *to;
	char *subj;
} maillist[MAXMAIL];

char *mailptr = NULL;
char *prog = "zmailnotify";

/* This entire program is a kludge - beware! */

main(argc, argv)
    char *argv[];
{
    FILE *lock;
    int nmsgs;
    char *user,response[512],lockfile[100];
    char *host,*dir;
    char *auth_cmd;
    int i,nbytes,retval,uselock;
    struct passwd *pwd;
    struct _mail mymail;
#ifdef HAVE_HESIOD
    struct hes_postoffice *p;
#endif

    if (argv[0] && *argv[0])
	prog = argv[0];
	
    if ((retval = ZInitialize()) != ZERR_NONE) {
	com_err(prog,retval,"while initializing");
	exit(1);
    }

    dir = (char *)getenv("HOME");
    user = (char *)getenv("USER");
    if (!user || !dir) {
	pwd = (struct passwd *)getpwuid((int) getuid());
	if (!pwd) {
	    fprintf(stderr,"%s: Can't figure out who you are!\n",
		    prog);
	    exit(1);
	}
	if (!user)
	    user = pwd->pw_name;
	if (!dir)
	    dir = pwd->pw_dir;
    }
    if (argc > 1)
	user = argv[1];

    (void) sprintf(lockfile,"%s/.maillock",dir);
	
    host = (char *)getenv("MAILHOST");
#ifdef HAVE_HESIOD
    if (host == NULL) {
	p = hes_getmailhost(user);
	if (p != NULL && strcmp(p->po_type, "POP") == 0)
	    host = p->po_host;
	else {
	    fprintf(stderr,
		    "%s: no POP server listed in Hesiod for %s\n",
		    prog, user);
	    exit(1);
	} 
    }
#endif
    if (host == NULL) {
	fprintf(stderr,"%s: no MAILHOST defined\n", prog);
	exit(1);
    }

    lock = fopen(lockfile,"r+");
#ifdef _POSIX_VERSION
    if (lock) {
	struct flock fl;

	/* lock the whole file exclusively */
	fl.l_type = F_WRLCK;
	fl.l_whence = SEEK_SET;
	fl.l_start = 0;
	fl.l_len = 0;
	(void) fcntl(fileno(lock),F_SETLKW,&fl);
    }
#else
    if (lock)
	(void) flock(fileno(lock),LOCK_EX);
#endif

    if (pop_init(host) == NOTOK) {
	fprintf(stderr,"%s: %s\n",prog, Errmsg);
	exit(1);
    }

    if ((getline(response, sizeof response, sfi) != OK) ||
	(*response != '+')) {
	fprintf(stderr,"%s: %s\n",prog,response);
	exit(1);
    }

#ifdef KPOP
    auth_cmd = "PASS %s";
#else
    auth_cmd = "RPOP %s";
#endif
    if (pop_command("USER %s", user) == NOTOK
	|| pop_command(auth_cmd, user) == NOTOK)
	fatal_pop_err ();

    if (pop_stat(&nmsgs, &nbytes) == NOTOK)
	fatal_pop_err ();

    if (!nmsgs) {
	if (lock) {
#ifdef _POSIX_VERSION
	    struct flock fl;

	    /* unlock the whole file */
	    fl.l_type = F_UNLCK;
	    fl.l_whence = SEEK_SET;
	    fl.l_start = 0;
	    fl.l_len = 0;
	    (void) fcntl(fileno(lock),F_SETLKW,&fl);
#else
	    (void) flock(fileno(lock),LOCK_UN);
#endif
	    (void) fclose(lock);
	} 
	(void) unlink(lockfile);
	(void) pop_command("QUIT");
	pop_close();
	exit (0);
    }

    uselock = 0;
    if (lock) {
	uselock = 1;
	mymail.to = (char *)malloc(BUFSIZ);
	mymail.from = (char *)malloc(BUFSIZ);
	mymail.subj = (char *)malloc(BUFSIZ);
	if (fgets(mymail.from,BUFSIZ,lock) != NULL)
	    mymail.from[strlen(mymail.from)-1] = 0;
	else
	    mymail.from[0]=0;
	if (fgets(mymail.to,BUFSIZ,lock) != NULL)
	    mymail.to[strlen(mymail.to)-1] = 0;
	else
	    mymail.to[0] = 0;
	if (fgets(mymail.subj,BUFSIZ,lock) != NULL)
	    mymail.subj[strlen(mymail.subj)-1] = 0;
	else
	    mymail.subj[0] = 0;
    }
    else {
	lock = fopen(lockfile,"w");
#ifdef _POSIX_VERSION
	if (lock) {
	    struct flock fl;

	    /* lock the whole file exclusively */
	    fl.l_type = F_WRLCK;
	    fl.l_whence = SEEK_SET;
	    fl.l_start = 0;
	    fl.l_len = 0;
	    (void) fcntl(fileno(lock),F_SETLKW,&fl);
	}
#else
	if (lock)
	    (void) flock(fileno(lock),LOCK_EX);
#endif
	uselock = 0;
    }
	
    for (i=nmsgs;i>0;i--) {
	if (nmsgs-i == MAXMAIL)
	    break;
	if (get_mail(i,&maillist[nmsgs-i]))
	    exit (1);
	if (uselock && (!strcmp(maillist[nmsgs-i].to,mymail.to) &&
			!strcmp(maillist[nmsgs-i].from,mymail.from) &&
			!strcmp(maillist[nmsgs-i].subj,mymail.subj)))
	    break;
    }

    (void) pop_command("QUIT");
    pop_close();

    i++;
    for (;i<=nmsgs;i++)
	mail_notify(&maillist[nmsgs-i]);
    i--;
    if (lock) {
#ifdef _POSIX_VERSION
	struct flock fl;

	/* unlock the whole file */
	fl.l_type = F_UNLCK;
	fl.l_whence = SEEK_SET;
	fl.l_start = 0;
	fl.l_len = 0;
	(void) fcntl(fileno(lock),F_SETLKW,&fl);
#else
	(void) flock(fileno(lock),LOCK_UN);
#endif
	(void) fclose(lock);
    } 
    lock = fopen(lockfile,"w");
    if (!lock)
	exit (1);
    fprintf(lock,"%s\n%s\n%s\n",
	    maillist[nmsgs-i].from,
	    maillist[nmsgs-i].to,
	    maillist[nmsgs-i].subj);
    (void) fclose(lock);

    exit(0);
}

void fatal_pop_err ()
{
    fprintf (stderr, "%s: %s\n", prog, Errmsg);
    (void) pop_command ("QUIT");
    pop_close ();
    exit (1);
}

void get_message(i)
	int i;
{
	int mbx_write();
	if (pop_scan(i, mbx_write, 0) != OK)
	    fatal_pop_err ();
}

/* Pop stuff */

void pop_close()
{
	if (sfi)
		(void) fclose(sfi);
	if (sfo)
		(void) fclose(sfo);
}

get_mail(i,mail)
	int i;
	struct _mail *mail;
{
	char from[512],to[512],subj[512];
	char *c,*ptr,*ptr2;
	
	*from = 0;
	*to = 0;
	*subj = 0;

	if (mailptr)
		free(mailptr);

	mailptr = 0;
	
	get_message(i);

	ptr = mailptr;
	while (ptr) {
		ptr2 = strchr(ptr,'\n');
		if (ptr2)
			*ptr2++ = 0;
		if (*ptr == '\0')
			break;
		if (!strncmp(ptr, "From: ", 6))
			(void) strcpy(from, ptr+6);
		else if (!strncmp(ptr, "To: ", 4))
			(void) strcpy(to, ptr+4);
		else if (!strncmp(ptr, "Subject: ", 9))
			(void) strcpy(subj, ptr+9);
		ptr = ptr2;
	}

	/* add elipsis at end of "To:" field if it continues onto */
	/* more than one line */
	i = strlen(to) - 2;
	c = to+i;
	if (*c++ == ',') {
		*c++ = ' ';
		*c++ = '.';
		*c++ = '.';
		*c++ = '.';
		*c++ = '\n';
		*c = 0;
	}

	mail->from = (char *)malloc((unsigned)(strlen(from)+1));
	(void) strcpy(mail->from,from);
	mail->to = (char *)malloc((unsigned)(strlen(to)+1));
	(void) strcpy(mail->to,to);
	mail->subj = (char *)malloc((unsigned)(strlen(subj)+1));
	(void) strcpy(mail->subj,subj);

	return (0);
}

void
mail_notify(mail)
	struct _mail *mail;
{
	int retval;
	char *fields[3];
	ZNotice_t notice;

	(void) memset((char *)&notice, 0, sizeof(notice));
	notice.z_kind = UNACKED;
	notice.z_port = 0;
	notice.z_class = "MAIL";
	notice.z_class_inst = "POPRET";
	notice.z_opcode = "NEW_MAIL";
	notice.z_sender = 0;
	notice.z_recipient = ZGetSender();
	notice.z_default_format = "You have new mail:\n\nFrom: $1\nTo: $2\nSubject: $3";

	fields[0] = mail->from;
	fields[1] = mail->to;
	fields[2] = mail->subj;
      
	if ((retval = ZSendList(&notice,fields,3,ZNOAUTH)) != ZERR_NONE)
		com_err(prog,retval,"while sending notice");
}

/*
 * These are the necessary KPOP routines snarfed from
 * the GNU movemail program.
 */

pop_init(host)
char *host;
{
    register struct hostent *hp;
    register struct servent *sp;
    int lport = IPPORT_RESERVED - 1;
    struct sockaddr_in sin;
    register int s;
#ifdef KPOP
    KTEXT ticket = (KTEXT)NULL;
    int rem;
    long authopts;
    char *host_save;
#endif
    char *svc_name;

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


#ifdef KPOP
#ifdef ATHENA_COMPAT
    svc_name = "knetd";
#else
    svc_name = "kpop";
#endif
#else
    svc_name = "pop";
#endif

    sp = getservbyname (svc_name, "tcp");
    if (sp == 0) {
	(void) sprintf (Errmsg, "%s/tcp: unknown service", svc_name);
	return NOTOK;
    }
    sin.sin_family = hp->h_addrtype;
    (void) 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
    s = rresvport(&lport);
#endif
    if (s < 0) {
	(void) sprintf(Errmsg, "error creating socket: %s", strerror(errno));
	return(NOTOK);
    }

    if (connect(s, (struct sockaddr *)&sin, sizeof sin) < 0) {
	(void) sprintf(Errmsg, "error during connect: %s", strerror(errno));
	(void) close(s);
	return(NOTOK);
    }
#ifdef KPOP
    ticket = (KTEXT)malloc( sizeof(KTEXT_ST) );
    rem=KSUCCESS;
#ifdef ATHENA_COMPAT
    authopts = KOPT_DO_OLDSTYLE;
    rem = krb_sendsvc(s,"pop");
    if (rem != KSUCCESS) {
	(void) sprintf(Errmsg, "kerberos error: %s", krb_get_err_text(rem));
	(void) close(s);
	return(NOTOK);
    }
#else
    authopts = 0L;
#endif
    host_save = malloc(strlen(hp->h_name) + 1);
    if (!host_save) {
	sprintf(Errmsg, "Out of memory.");
	return(NOTOK);
    }
    strcpy(host_save, hp->h_name);
    rem = krb_sendauth(authopts, s, ticket, "pop", host_save, (char *)0,
		       0, (MSG_DAT *) 0, (CREDENTIALS *) 0,
		       (bit_64 *) 0, (struct sockaddr_in *)0,
		       (struct sockaddr_in *)0,"ZMAIL0.0");
    free(host_save);
    free(ticket);
    if (rem != KSUCCESS) {
	(void) sprintf(Errmsg, "kerberos error: %s",krb_get_err_text(rem));
	(void) close(s);
	return(NOTOK);
    }
#endif

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

    return(OK);
}

#ifdef __STDC__
pop_command(char *fmt, ...)
#else
pop_command(fmt, va_alist)
    va_dcl
#endif
{
    va_list args;
    char buf[4096];

    VA_START(args, fmt);
    (void) vsprintf(buf, fmt, args);
    va_end(args);

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

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

    if (*buf != '+') {
	(void) strcpy(Errmsg, buf);
	return(NOTOK);
    } else {
	return(OK);
    }
}

    
pop_stat(nmsgs, nbytes)
int *nmsgs, *nbytes;
{
    char buf[4096];

    if (putline("STAT", Errmsg, sfo) == NOTOK) return(NOTOK);

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

    if (*buf != '+') {
	(void) strcpy(Errmsg, buf);
	return(NOTOK);
    } else {
	if (sscanf(buf, "+OK %d %d", nmsgs, nbytes) != 2)
	    return(NOTOK);
	return(OK);
    }
}

pop_scan(msgno, action, arg)
int (*action)();
{
    char buf[4096];

#ifdef HAVE_POP3_TOP
    (void) sprintf(buf, "TOP %d 0", msgno);
#else
    (void) sprintf(buf, "RETR %d", msgno);
#endif
    if (putline(buf, Errmsg, sfo) == NOTOK) return(NOTOK);

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

    while (1) {
	switch (multiline(buf, sizeof buf, sfi)) {
	case OK:
	    (*action)(buf, arg);
	    break;
	case DONE:
	    return (OK);
	case NOTOK:
	    (void) strcpy(Errmsg, buf);
	    return (NOTOK);
	}
    }
}

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

    p = fgets(buf, n, f);

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

    if (p == NULL) {
	(void) strcpy(buf, "connection closed by foreign host\n");
	return (DONE);
    }

    p = buf + strlen(buf);
    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 {
	    (void) strcpy(buf, buf+1);
	}
    } else if (*buf == '\0') {
      /* suck up all future lines, since this is after all only for headers */
	while(! ((buf[0]=='.') && (buf[1] == '\0')) ) {
	    if (getline(buf, n, f) != OK) return (NOTOK);
	}
	return DONE;
    }
    return(OK);
}

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

/*ARGSUSED*/
mbx_write(line, dummy)
char *line;
int dummy;				/* for consistency with pop_scan */
{
	if (mailptr) {
		mailptr = (char *)realloc(mailptr,(unsigned)(strlen(mailptr)+strlen(line)+2));
		(void) strcat(mailptr,line);
	} 
	else {
		mailptr = (char *)malloc((unsigned)(strlen(line)+2));
		(void) strcpy(mailptr,line);
	}
	(void) strcat(mailptr,"\n");
	return(0);
}