imapext-2007

view src/tmail/tmail.c @ 0:ada5e610ab86

imap-2007e
author yuuji@gentei.org
date Mon, 14 Sep 2009 15:17:45 +0900
parents
children
line source
1 /* ========================================================================
2 * Copyright 1988-2007 University of Washington
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *
11 * ========================================================================
12 */
14 /*
15 * Program: Mail Delivery Module
16 *
17 * Author: Mark Crispin
18 * Networks and Distributed Computing
19 * Computing & Communications
20 * University of Washington
21 * Administration Building, AG-44
22 * Seattle, WA 98195
23 * Internet: MRC@CAC.Washington.EDU
24 *
25 * Date: 5 April 1993
26 * Last Edited: 30 October 2008
27 */
29 #include <stdio.h>
30 #include <pwd.h>
31 #include <errno.h>
32 extern int errno; /* just in case */
33 #include <sysexits.h>
34 #include <sys/file.h>
35 #include <sys/stat.h>
36 #include "c-client.h"
37 #include "tquota.h"
40 /* Globals */
42 char *version = "22"; /* tmail edit version */
43 int debug = NIL; /* debugging (don't fork) */
44 int trycreate = NIL; /* flag saying gotta create before appending */
45 int critical = NIL; /* flag saying in critical code */
46 char *sender = NIL; /* message origin */
47 char *inbox = NIL; /* inbox file */
48 long precedence = 0; /* delivery precedence - used by quota hook */
49 DRIVER *format = NIL; /* desired format */
52 /* Function prototypes */
54 void file_string_init (STRING *s,void *data,unsigned long size);
55 char file_string_next (STRING *s);
56 void file_string_setpos (STRING *s,unsigned long i);
57 int main (int argc,char *argv[]);
58 int deliver (FILE *f,unsigned long msglen,char *user);
59 long ibxpath (MAILSTREAM *ds,char **mailbox,char *path);
60 int deliver_safely (MAILSTREAM *prt,STRING *st,char *mailbox,char *path,
61 uid_t uid,char *tmp);
62 int delivery_unsafe (char *path,uid_t uid,struct stat *sbuf,char *tmp);
63 int fail (char *string,int code);
64 char *getusername (char *s,char **t);
67 /* File string driver for file stringstructs */
69 STRINGDRIVER file_string = {
70 file_string_init, /* initialize string structure */
71 file_string_next, /* get next byte in string structure */
72 file_string_setpos /* set position in string structure */
73 };
76 /* Cache buffer for file stringstructs */
78 #define CHUNKLEN 16384
79 char chunk[CHUNKLEN];
81 /* Initialize file string structure for file stringstruct
82 * Accepts: string structure
83 * pointer to string
84 * size of string
85 */
87 void file_string_init (STRING *s,void *data,unsigned long size)
88 {
89 s->data = data; /* note fd */
90 s->size = size; /* note size */
91 s->chunk = chunk;
92 s->chunksize = (unsigned long) CHUNKLEN;
93 SETPOS (s,0); /* set initial position */
94 }
97 /* Get next character from file stringstruct
98 * Accepts: string structure
99 * Returns: character, string structure chunk refreshed
100 */
102 char file_string_next (STRING *s)
103 {
104 char c = *s->curpos++; /* get next byte */
105 SETPOS (s,GETPOS (s)); /* move to next chunk */
106 return c; /* return the byte */
107 }
110 /* Set string pointer position for file stringstruct
111 * Accepts: string structure
112 * new position
113 */
115 void file_string_setpos (STRING *s,unsigned long i)
116 {
117 if (i > s->size) i = s->size; /* don't permit setting beyond EOF */
118 s->offset = i; /* set new offset */
119 s->curpos = s->chunk; /* reset position */
120 /* set size of data */
121 if (s->cursize = min (s->chunksize,SIZE (s))) {
122 /* move to that position in the file */
123 fseek ((FILE *) s->data,s->offset,SEEK_SET);
124 fread (s->curpos,sizeof (char),(unsigned int) s->cursize,(FILE *) s->data);
125 }
126 }
128 /* Main program */
130 int main (int argc,char *argv[])
131 {
132 FILE *f = NIL;
133 int pid,c,ret = 0;
134 unsigned long msglen,status = 0;
135 char *s,tmp[MAILTMPLEN];
136 uid_t ruid = getuid ();
137 struct passwd *pwd;
138 openlog ("tmail",LOG_PID,LOG_MAIL);
139 #include "linkage.c"
140 /* make sure have some arguments */
141 if (--argc < 1) _exit (fail ("usage: tmail [-D] user[+folder]",EX_USAGE));
142 /* process all flags */
143 while (argc && (*(s = *++argv)) == '-') {
144 argc--; /* gobble this argument */
145 switch (s[1]) { /* what is this flag? */
146 case 'D': /* debug */
147 debug = T; /* don't fork */
148 break;
149 case 'I': /* inbox specifier */
150 if (inbox || format) _exit (fail ("duplicate -b or -I",EX_USAGE));
151 if (argc--) inbox = cpystr (*++argv);
152 else _exit (fail ("missing argument to -I",EX_USAGE));
153 break;
154 case 'f': /* new name for this flag */
155 case 'r': /* flag giving return path */
156 if (sender) _exit (fail ("duplicate -f or -r",EX_USAGE));
157 if (argc--) sender = cpystr (*++argv);
158 else _exit (fail ("missing argument to -f or -r",EX_USAGE));
159 break;
160 case 'b': /* create INBOX in this format */
161 if (inbox || format) _exit (fail ("duplicate -b or -I",EX_USAGE));
162 if (!argc--) _exit (fail ("missing argument to -b",EX_USAGE));
163 if (!(format = mail_parameters (NIL,GET_DRIVER,*++argv)))
164 _exit (fail ("unknown format to -b",EX_USAGE));
165 else if (!(format->flags & DR_LOCAL) ||
166 !compare_cstring (format->name,"dummy"))
167 _exit (fail ("invalid format to -b",EX_USAGE));
168 break;
169 /* following flags are undocumented */
170 case 'p': /* precedence for quota */
171 if (s[2] && ((s[2] == '-') || isdigit (s[2]))) precedence = atol (s + 2);
172 else if (argc-- && ((*(s = *++argv) == '-') || isdigit (*s)))
173 precedence = atol (s);
174 else _exit (fail ("missing argument to -p",EX_USAGE));
175 break;
176 case 'd': /* obsolete flag meaning multiple users */
177 break; /* ignore silently */
178 /* -s has been deprecated and replaced by the -s and -k flags in dmail.
179 * dmail's -k flag does what -s once did in tmail; dmail's -s flag
180 * takes no argument and just sets \Seen. Flag setting is more properly
181 * done in dmail which runs as the user and is clearly at the user's
182 * behest. Since tmail runs privileged, -s would have to be disabled
183 * unless the caller is also privileged.
184 */
185 case 's': /* obsolete flag meaning delivery flags */
186 if (!argc--) /* takes an argument */
187 _exit (fail ("missing argument to deprecated flag",EX_USAGE));
188 syslog (LOG_INFO,"tmail called with deprecated flag: -s %.200s",*++argv);
189 break;
190 default: /* anything else */
191 _exit (fail ("unknown switch",EX_USAGE));
192 }
193 }
195 if (!argc) ret = fail ("no recipients",EX_USAGE);
196 else if (!(f = tmpfile ())) ret = fail ("can't make temp file",EX_TEMPFAIL);
197 else { /* build delivery headers */
198 if (sender) fprintf (f,"Return-Path: <%s>\015\012",sender);
199 /* start Received line: */
200 fprintf (f,"Received: via tmail-%s.%s",CCLIENTVERSION,version);
201 /* not root or daemon? */
202 if (ruid && !((pwd = getpwnam ("daemon")) && (ruid == pwd->pw_uid))) {
203 pwd = getpwuid (ruid); /* get unprivileged user's information */
204 if (inbox || format) {
205 if (pwd) sprintf (tmp,"user %.80s",pwd->pw_name);
206 else sprintf (tmp,"UID %ld",(long) ruid);
207 strcat (tmp," is not privileged to use -b or -I");
208 _exit (fail (tmp,EX_USAGE));
209 }
210 fputs (" (invoked by ",f);
211 if (pwd) fprintf (f,"user %s",pwd->pw_name);
212 else fprintf (f,"UID %ld",(long) ruid);
213 fputs (")",f);
214 }
215 /* write "for" if single recipient */
216 if (argc == 1) fprintf (f," for %s",*argv);
217 fputs ("; ",f);
218 rfc822_date (tmp);
219 fputs (tmp,f);
220 fputs ("\015\012",f);
221 /* copy text from standard input */
222 if (!fgets (tmp,MAILTMPLEN-1,stdin) || !(s = strchr (tmp,'\n')) ||
223 (s == tmp) || s[1]) _exit (fail ("bad first message line",EX_USAGE));
224 if (s[-1] == '\015') { /* nuke leading "From " line */
225 if ((tmp[0] != 'F') || (tmp[1] != 'r') || (tmp[2] != 'o') ||
226 (tmp[3] != 'm') || (tmp[4] != ' ')) fputs (tmp,f);
227 while ((c = getchar ()) != EOF) putc (c,f);
228 }
229 else {
230 mm_log ("tmail called with LF-only newlines",WARN);
231 if ((tmp[0] != 'F') || (tmp[1] != 'r') || (tmp[2] != 'o') ||
232 (tmp[3] != 'm') || (tmp[4] != ' ')) {
233 *s++ = '\015'; /* overwrite NL with CRLF */
234 *s++ = '\012';
235 *s = '\0'; /* tie off string */
236 fputs (tmp,f); /* write line */
237 }
238 /* copy text from standard input */
239 while ((c = getchar ()) != EOF) {
240 /* add CR if needed */
241 if (c == '\012') putc ('\015',f);
242 putc (c,f);
243 }
244 }
245 msglen = ftell (f); /* size of message */
246 fflush (f); /* make sure all changes written out */
248 if (ferror (f)) ret = fail ("error writing temp file",EX_TEMPFAIL);
249 else if (!msglen) ret = fail ("empty message",EX_TEMPFAIL);
250 /* single delivery */
251 else if (argc == 1) ret = deliver (f,msglen,*argv);
252 else do { /* multiple delivery uses daughter forks */
253 if ((pid = fork ()) < 0) ret = fail (strerror (errno),EX_OSERR);
254 else if (pid) { /* mother process */
255 grim_pid_reap_status (pid,NIL,(void *) status);
256 /* normal termination? */
257 if (!ret) ret = (status & 0xff) ? EX_SOFTWARE : (status & 0xff00) >> 8;
258 }
259 /* daughter process */
260 else _exit (deliver (f,msglen,*argv));
261 } while (--argc && *argv++);
262 mm_dlog (ret ? "error in delivery" : "all recipients delivered");
263 }
264 if (f) fclose (f); /* all done with temporary file */
265 _exit (ret); /* normal exit */
266 return 0; /* stupid gcc */
267 }
269 /* Deliver message to recipient list
270 * Accepts: file description of message temporary file
271 * size of message temporary file in bytes
272 * recipient name
273 * Returns: NIL if success, else error code
274 */
276 int deliver (FILE *f,unsigned long msglen,char *user)
277 {
278 MAILSTREAM *ds = NIL;
279 char *s,*t,*mailbox,tmp[MAILTMPLEN],path[MAILTMPLEN];
280 struct passwd *pwd;
281 STRING st;
282 struct stat sbuf;
283 uid_t duid;
284 uid_t euid = geteuid ();
285 /* get user record */
286 if (!(pwd = getpwnam (getusername (user,&mailbox)))) {
287 sprintf (tmp,"no such user as %.80s",user);
288 return fail (tmp,EX_NOUSER);
289 }
290 /* absurd is absurd */
291 if (mailbox && (strlen (mailbox) > 256))
292 return fail ("absurd folder name",EX_NOUSER);
293 /* big security hole if this is allowed */
294 if (!(duid = pwd->pw_uid)) return fail ("mail to root prohibited",EX_NOUSER);
295 /* log in as user if different than euid */
296 if ((duid != euid) && !loginpw (pwd,1,&user)) {
297 sprintf (tmp,"unable to log in UID %ld from UID %ld",
298 (long) duid,(long) euid);
299 return fail (tmp,EX_NOUSER);
300 }
301 /* can't use pwd after this point */
302 env_init (pwd->pw_name,pwd->pw_dir);
303 sprintf (tmp,"delivering to %.80s+%.80s",user,mailbox ? mailbox : "INBOX");
304 mm_dlog (tmp);
305 /* prepare stringstruct */
306 INIT (&st,file_string,(void *) f,msglen);
307 if (mailbox) { /* non-INBOX name */
308 switch (mailbox[0]) { /* make sure a valid name */
309 default: /* other names, try to deliver if not INBOX */
310 if (!strstr (mailbox,"..") && !strstr (mailbox,"//") &&
311 !strstr (mailbox,"/~") && mailboxfile (path,mailbox) && path[0] &&
312 !deliver_safely (NIL,&st,mailbox,path,duid,tmp)) return NIL;
313 case '%': case '*': /* wildcards not valid */
314 case '#': /* namespace name not valid */
315 case '/': /* absolute path names not valid */
316 case '~': /* user names not valid */
317 sprintf (tmp,"invalid mailbox name %.80s+%.80s",user,mailbox);
318 mm_log (tmp,WARN);
319 break;
320 }
321 mm_dlog ("retrying delivery to INBOX");
322 SETPOS (&st,0); /* rewind stringstruct just in case */
323 }
325 /* -I specified and not "-I INBOX"? */
326 if (inbox && !(((inbox[0] == 'I') || (inbox[0] == 'i')) &&
327 ((inbox[1] == 'N') || (inbox[1] == 'n')) &&
328 ((inbox[2] == 'B') || (inbox[2] == 'b')) &&
329 ((inbox[3] == 'O') || (inbox[3] == 'o')) &&
330 ((inbox[4] == 'X') || (inbox[4] == 'x')) && !inbox[5])) {
331 DRIVER *dv = NIL;
332 /* "-I #driver.xxx/name"? */
333 if ((*inbox == '#') && ((inbox[1] == 'd') || (inbox[1] == 'D')) &&
334 ((inbox[2] == 'r') || (inbox[2] == 'R')) &&
335 ((inbox[3] == 'i') || (inbox[3] == 'I')) &&
336 ((inbox[4] == 'v') || (inbox[4] == 'V')) &&
337 ((inbox[5] == 'e') || (inbox[5] == 'E')) &&
338 ((inbox[6] == 'r') || (inbox[6] == 'R')) && (inbox[7] == '.') &&
339 (s = strchr (inbox+8,'/'))) {
340 *s = '\0'; /* temporarily tie off driver name */
341 if (!((dv = mail_parameters (NIL,GET_DRIVER,(void *) (inbox+8))) &&
342 (mailboxfile (path,s[1] ? s + 1 : "&&&&&") == path) &&
343 (s[1] || ((t = strstr (path,"&&&&&")) && strcpy (t,"INBOX"))))) {
344 path[0] = '\0'; /* bad -I argument, no path resolved */
345 sprintf (tmp,"Unable to resolve driver in %.80s, -I ignored",inbox);
346 mm_log (tmp,WARN);
347 }
348 *s = '/'; /* restore delimiter */
349 }
350 /* resolve "-I other" specification */
351 else if (mailboxfile (path,inbox) && path[0]) {
352 /* resolution succeeded, impute driver */
353 if (!strcmp (inbox,"mail.txt"))
354 dv = mail_parameters (NIL,GET_DRIVER,(void *) "tenex");
355 else if (!strcmp (inbox,"INBOX.MTX"))
356 dv = mail_parameters (NIL,GET_DRIVER,(void *) "mtx");
357 else if (!strcmp (inbox,"mbox"))
358 dv = mail_parameters (NIL,GET_DRIVER,(void *) "unix");
359 }
360 else { /* bad -I argument */
361 path[0] = '\0'; /* no path resolved */
362 sprintf (tmp,"Unable to resolve %.80s, -I ignored",inbox);
363 mm_log (tmp,WARN);
364 }
365 if (*path) { /* -I successfully resolved a path? */
366 MAILSTREAM dpr;
367 dpr.dtb = dv;
368 if (dv) ds = &dpr;
369 /* supplicate to the Evil One if necessary */
370 if (lstat (path,&sbuf) && !path_create (ds,path)) {
371 /* the Evil One rejected the plea */
372 sprintf (tmp,"Unable to create %.80s, -I ignored",path);
373 mm_log (tmp,WARN);
374 }
375 /* now attempt delivery */
376 else return deliver_safely (ds,&st,inbox,path,duid,tmp);
377 }
378 }
380 /* no -I, resolve "INBOX" into path */
381 if (mailboxfile (path,mailbox = "INBOX") && !path[0]) {
382 /* clear box, get generic INBOX prototype */
383 if (!(ds = mail_open (NIL,"INBOX",OP_PROTOTYPE)))
384 fatal ("no INBOX prototype");
385 /* standard system driver? */
386 if (!strcmp (ds->dtb->name,"unix") || !strcmp (ds->dtb->name,"mmdf")) {
387 strcpy (path,sysinbox ());/* use system INBOX */
388 if (!lstat (path,&sbuf)) /* deliver to existing system INBOX */
389 return deliver_safely (ds,&st,mailbox,path,duid,tmp);
390 }
391 else { /* other driver, try ~/INBOX */
392 if ((mailboxfile (path,"&&&&&") == path) &&
393 (s = strstr (path,"&&&&&")) && strcpy (s,"INBOX") &&
394 !lstat (path,&sbuf)){ /* deliver to existing ~/INBOX */
395 sprintf (tmp,"#driver.%s/INBOX",ds->dtb->name);
396 return deliver_safely (ds,&st,cpystr (tmp),path,duid,tmp);
397 }
398 }
399 /* not dummy, deliver to driver imputed path */
400 if (strcmp (ds->dtb->name,"dummy"))
401 return (ibxpath (ds,&mailbox,path) && !lstat (path,&sbuf)) ?
402 deliver_safely (ds,&st,mailbox,path,duid,tmp) :
403 fail ("unable to resolve INBOX path",EX_CANTCREAT);
404 /* dummy, empty imputed append path exist? */
405 if (ibxpath (ds = default_proto (T),&mailbox,path) &&
406 !lstat (path,&sbuf) && !sbuf.st_size)
407 return deliver_safely (ds,&st,mailbox,path,duid,tmp);
408 /* impute path that we will create */
409 if (!ibxpath (ds = format ? (format->open) (NIL) : default_proto (NIL),
410 &mailbox,path))
411 return fail ("unable to resolve INBOX",EX_CANTCREAT);
412 }
413 /* black box, must create, get create proto */
414 else if (lstat (path,&sbuf)) ds = default_proto (NIL);
415 else { /* black box, existing file */
416 /* empty file, get append prototype */
417 if (!sbuf.st_size) ds = default_proto (T);
418 /* non-empty, get prototype from its data */
419 else if (!(ds = mail_open (NIL,"INBOX",OP_PROTOTYPE)))
420 fatal ("no INBOX prototype");
421 /* error if unknown format */
422 if (!strcmp (ds->dtb->name,"phile"))
423 return fail ("unknown format INBOX",EX_UNAVAILABLE);
424 /* otherwise can deliver to it */
425 return deliver_safely (ds,&st,mailbox,path,duid,tmp);
426 }
427 sprintf (tmp,"attempting to create mailbox %.80s path %.80s",mailbox,path);
428 mm_dlog (tmp);
429 /* supplicate to the Evil One */
430 if (!path_create (ds,path)) return fail ("can't create INBOX",EX_CANTCREAT);
431 sprintf (tmp,"created %.80s",path);
432 mm_dlog (tmp);
433 /* deliver the message */
434 return deliver_safely (ds,&st,mailbox,path,duid,tmp);
435 }
437 /* Resolve INBOX from driver prototype into mailbox name and filesystem path
438 * Accepts: driver prototype
439 * pointer to mailbox name string pointer
440 * buffer to return mailbox path
441 * Returns: T if success, NIL if error
442 */
444 long ibxpath (MAILSTREAM *ds,char **mailbox,char *path)
445 {
446 char *s,tmp[MAILTMPLEN];
447 long ret = T;
448 if (!ds) ret = NIL;
449 else if (!strcmp (ds->dtb->name,"unix") || !strcmp (ds->dtb->name,"mmdf"))
450 strcpy (path,sysinbox ()); /* use system INBOX for unix and MMDF */
451 else if (!strcmp (ds->dtb->name,"tenex"))
452 ret = (mailboxfile (path,"mail.txt") == path) ? T : NIL;
453 else if (!strcmp (ds->dtb->name,"mtx"))
454 ret = (mailboxfile (path,"INBOX.MTX") == path) ? T : NIL;
455 else if (!strcmp (ds->dtb->name,"mbox"))
456 ret = (mailboxfile (path,"mbox") == path) ? T : NIL;
457 /* better not be a namespace driver */
458 else if (ds->dtb->flags & DR_NAMESPACE) return NIL;
459 /* INBOX in home directory */
460 else ret = ((mailboxfile (path,"&&&&&") == path) &&
461 (s = strstr (path,"&&&&&")) && strcpy (s,"INBOX")) ? T : NIL;
462 if (ret) { /* don't bother if lossage */
463 sprintf (tmp,"#driver.%s/INBOX",ds->dtb->name);
464 *mailbox = cpystr (tmp); /* name of INBOX in this namespace */
465 }
466 return ret;
467 }
469 /* Deliver safely
470 * Accepts: prototype stream to force mailbox format
471 * stringstruct of message temporary file
472 * mailbox name
473 * filesystem path name
474 * user id
475 * scratch buffer for messages
476 * Returns: NIL if success, else error code
477 */
479 int deliver_safely (MAILSTREAM *prt,STRING *st,char *mailbox,char *path,
480 uid_t uid,char *tmp)
481 {
482 struct stat sbuf;
483 int i = delivery_unsafe (path,uid,&sbuf,tmp);
484 if (i) return i; /* give up now if delivery unsafe */
485 /* directory, not file */
486 if ((sbuf.st_mode & S_IFMT) == S_IFDIR) {
487 if (sbuf.st_mode & 0001) { /* listable directories may be worrisome */
488 sprintf (tmp,"WARNING: directory %.80s is listable",path);
489 mm_log (tmp,WARN);
490 }
491 }
492 else { /* file, not directory */
493 if (sbuf.st_nlink != 1) { /* multiple links may be worrisome */
494 sprintf (tmp,"WARNING: multiple links to file %.80s",path);
495 mm_log (tmp,WARN);
496 }
497 if (sbuf.st_mode & 0111) { /* executable files may be worrisome */
498 sprintf (tmp,"WARNING: file %.80s is executable",path);
499 mm_log (tmp,WARN);
500 }
501 }
502 if (sbuf.st_mode & 0002) { /* public-write files may be worrisome */
503 sprintf (tmp,"WARNING: file %.80s is publicly-writable",path);
504 mm_log (tmp,WARN);
505 }
506 if (sbuf.st_mode & 0004) { /* public-write files may be worrisome */
507 sprintf (tmp,"WARNING: file %.80s is publicly-readable",path);
508 mm_log (tmp,WARN);
509 }
510 /* check site-written quota procedure */
511 if (!tmail_quota (st,path,uid,tmp,sender,precedence)) return fail (tmp,-1);
512 /* so far, so good */
513 sprintf (tmp,"%s appending to %.80s (%s %.80s)",
514 prt ? prt->dtb->name : "default",mailbox,
515 ((sbuf.st_mode & S_IFMT) == S_IFDIR) ? "directory" : "file",path);
516 mm_dlog (tmp);
517 /* do the append now! */
518 if (!mail_append (prt,mailbox,st)) {
519 sprintf (tmp,"message delivery failed to %.80s",path);
520 return fail (tmp,EX_CANTCREAT);
521 }
522 /* note success */
523 sprintf (tmp,"delivered to %.80s",path);
524 mm_log (tmp,NIL);
525 /* make sure nothing evil this way comes */
526 return delivery_unsafe (path,uid,&sbuf,tmp);
527 }
529 /* Verify that delivery is safe
530 * Accepts: path name
531 * user id
532 * stat buffer
533 * scratch buffer for messages
534 * Returns: NIL if delivery is safe, error code if unsafe
535 */
537 int delivery_unsafe (char *path,uid_t uid,struct stat *sbuf,char *tmp)
538 {
539 u_short type;
540 sprintf (tmp,"Verifying safe delivery to %.80s by UID %ld",path,(long) uid);
541 mm_dlog (tmp);
542 /* prepare message just in case */
543 sprintf (tmp,"delivery to %.80s unsafe: ",path);
544 /* unsafe if can't get its status */
545 if (lstat (path,sbuf)) strcat (tmp,strerror (errno));
546 else if (sbuf->st_uid != uid) /* unsafe if UID does not match */
547 sprintf (tmp + strlen (tmp),"uid mismatch (%ld != %ld)",
548 (long) sbuf->st_uid,(long) uid);
549 /* check file type */
550 else switch (sbuf->st_mode & S_IFMT) {
551 case S_IFDIR: /* directory is always OK */
552 return NIL;
553 case S_IFREG: /* file is unsafe if setuid */
554 if (sbuf->st_mode & S_ISUID) strcat (tmp,"setuid file");
555 /* or setgid */
556 else if (sbuf->st_mode & S_ISGID) strcat (tmp,"setgid file");
557 else return NIL; /* otherwise safe */
558 break;
559 case S_IFCHR: strcat (tmp,"character special"); break;
560 case S_IFBLK: strcat (tmp,"block special"); break;
561 case S_IFLNK: strcat (tmp,"symbolic link"); break;
562 case S_IFSOCK: strcat (tmp,"socket"); break;
563 default:
564 sprintf (tmp + strlen (tmp),"file type %07o",(unsigned int) type);
565 }
566 return fail (tmp,EX_CANTCREAT);
567 }
569 /* Report an error
570 * Accepts: string to output
571 */
573 int fail (char *string,int code)
574 {
575 mm_log (string,ERROR); /* pass up the string */
576 switch (code) {
577 #if T
578 case EX_USAGE:
579 case EX_OSERR:
580 case EX_SOFTWARE:
581 case EX_NOUSER:
582 case EX_CANTCREAT:
583 case EX_UNAVAILABLE:
584 code = EX_TEMPFAIL; /* coerce these to TEMPFAIL */
585 #endif
586 break;
587 case -1: /* quota failure... */
588 code = EX_CANTCREAT; /* ...really returns this code */
589 break;
590 default:
591 break;
592 }
593 return code; /* error code to return */
594 }
597 /* Get user name from username+mailbox specifier
598 * Accepts: username/mailbox specifier
599 * pointer to return location for mailbox specifier
600 * Returns: user name, mailbox specifier value NIL if INBOX, patches out +
601 */
603 char *getusername (char *s,char **t)
604 {
605 if (*t = strchr (s,'+')) { /* have a mailbox specifier? */
606 *(*t)++ = '\0'; /* yes, tie off user name */
607 /* user+ and user+INBOX same as user */
608 if (!**t || !compare_cstring ((unsigned char *) *t,"INBOX")) *t = NIL;
609 }
610 return s; /* return user name */
611 }
613 /* Co-routines from MAIL library */
616 /* Message matches a search
617 * Accepts: MAIL stream
618 * message number
619 */
621 void mm_searched (MAILSTREAM *stream,unsigned long msgno)
622 {
623 fatal ("mm_searched() call");
624 }
627 /* Message exists (i.e. there are that many messages in the mailbox)
628 * Accepts: MAIL stream
629 * message number
630 */
632 void mm_exists (MAILSTREAM *stream,unsigned long number)
633 {
634 fatal ("mm_exists() call");
635 }
638 /* Message expunged
639 * Accepts: MAIL stream
640 * message number
641 */
643 void mm_expunged (MAILSTREAM *stream,unsigned long number)
644 {
645 fatal ("mm_expunged() call");
646 }
649 /* Message flags update seen
650 * Accepts: MAIL stream
651 * message number
652 */
654 void mm_flags (MAILSTREAM *stream,unsigned long number)
655 {
656 }
658 /* Mailbox found
659 * Accepts: MAIL stream
660 * delimiter
661 * mailbox name
662 * mailbox attributes
663 */
665 void mm_list (MAILSTREAM *stream,int delimiter,char *name,long attributes)
666 {
667 fatal ("mm_list() call");
668 }
671 /* Subscribed mailbox found
672 * Accepts: MAIL stream
673 * delimiter
674 * mailbox name
675 * mailbox attributes
676 */
678 void mm_lsub (MAILSTREAM *stream,int delimiter,char *name,long attributes)
679 {
680 fatal ("mm_lsub() call");
681 }
684 /* Mailbox status
685 * Accepts: MAIL stream
686 * mailbox name
687 * mailbox status
688 */
690 void mm_status (MAILSTREAM *stream,char *mailbox,MAILSTATUS *status)
691 {
692 fatal ("mm_status() call");
693 }
695 /* Notification event
696 * Accepts: MAIL stream
697 * string to log
698 * error flag
699 */
701 void mm_notify (MAILSTREAM *stream,char *string,long errflg)
702 {
703 char tmp[MAILTMPLEN];
704 tmp[11] = '\0'; /* see if TRYCREATE */
705 if (!strcmp (ucase (strncpy (tmp,string,11)),"[TRYCREATE]")) trycreate = T;
706 mm_log (string,errflg); /* just do mm_log action */
707 }
710 /* Log an event for the user to see
711 * Accepts: string to log
712 * error flag
713 */
715 void mm_log (char *string,long errflg)
716 {
717 if (trycreate)mm_dlog(string);/* debug logging only if trycreate in effect */
718 else { /* ordinary logging */
719 fprintf (stderr,"%s\n",string);
720 switch (errflg) {
721 case NIL: /* no error */
722 syslog (LOG_INFO,"%s",string);
723 break;
724 case PARSE: /* parsing problem */
725 case WARN: /* warning */
726 syslog (LOG_WARNING,"%s",string);
727 break;
728 case ERROR: /* error */
729 default:
730 syslog (LOG_ERR,"%s",string);
731 break;
732 }
733 }
734 }
737 /* Log an event to debugging telemetry
738 * Accepts: string to log
739 */
741 void mm_dlog (char *string)
742 {
743 if (debug) fprintf (stderr,"%s\n",string);
744 syslog (LOG_DEBUG,"%s",string);
745 }
747 /* Get user name and password for this host
748 * Accepts: parse of network mailbox name
749 * where to return user name
750 * where to return password
751 * trial count
752 */
754 void mm_login (NETMBX *mb,char *username,char *password,long trial)
755 {
756 fatal ("mm_login() call");
757 }
760 /* About to enter critical code
761 * Accepts: stream
762 */
764 void mm_critical (MAILSTREAM *stream)
765 {
766 critical = T; /* note in critical code */
767 }
770 /* About to exit critical code
771 * Accepts: stream
772 */
774 void mm_nocritical (MAILSTREAM *stream)
775 {
776 critical = NIL; /* note not in critical code */
777 }
780 /* Disk error found
781 * Accepts: stream
782 * system error code
783 * flag indicating that mailbox may be clobbered
784 * Returns: T if user wants to abort
785 */
787 long mm_diskerror (MAILSTREAM *stream,long errcode,long serious)
788 {
789 return T;
790 }
793 /* Log a fatal error event
794 * Accepts: string to log
795 */
797 void mm_fatal (char *string)
798 {
799 printf ("?%s\n",string); /* shouldn't happen normally */
800 }

UW-IMAP'd extensions by yuuji