imapext-2007

view src/osdep/unix/env_unix.c @ 1:28a55bc1110c

[mq]: imapext
author yuuji@gentei.org
date Mon, 14 Sep 2009 19:23:11 +0900
parents ada5e610ab86
children 2366b362676d 20c025a380ab
line source
1 /* ========================================================================
2 * Copyright 1988-2008 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: UNIX environment routines
16 *
17 * Author: Mark Crispin
18 * UW Technology
19 * University of Washington
20 * Seattle, WA 98195
21 * Internet: MRC@Washington.EDU
22 *
23 * Date: 1 August 1988
24 * Last Edited: 15 May 2008
25 */
27 #include <grp.h>
28 #include <signal.h>
29 #include <sys/wait.h>
32 /* in case stat.h is ancient */
34 #ifndef S_IRUSR
35 #define S_IRUSR S_IREAD
36 #endif
37 #ifndef S_IWUSR
38 #define S_IWUSR S_IWRITE
39 #endif
40 #ifndef S_IXUSR
41 #define S_IXUSR S_IEXEC
42 #endif
43 #ifndef S_IRGRP
44 #define S_IRGRP (S_IREAD >> 3)
45 #endif
46 #ifndef S_IWGRP
47 #define S_IWGRP (S_IWRITE >> 3)
48 #endif
49 #ifndef S_IXGRP
50 #define S_IXGRP (S_IEXEC >> 3)
51 #endif
52 #ifndef S_IROTH
53 #define S_IROTH (S_IREAD >> 6)
54 #endif
55 #ifndef S_IWOTH
56 #define S_IWOTH (S_IWRITE >> 6)
57 #endif
58 #ifndef S_IXOTH
59 #define S_IXOTH (S_IEXEC >> 6)
60 #endif
62 /* c-client environment parameters */
64 static char *myUserName = NIL; /* user name */
65 static char *myHomeDir = NIL; /* home directory name */
66 static char *myServerName = NIL;/* server name */
67 static char *myLocalHost = NIL; /* local host name */
68 static char *myNewsrc = NIL; /* newsrc file name */
69 static char *mailsubdir = NIL; /* mailbox subdirectory name */
70 static char *sysInbox = NIL; /* system inbox name */
71 static char *newsActive = NIL; /* news active file */
72 static char *newsSpool = NIL; /* news spool */
73 static char *blackBoxDir = NIL; /* black box directory name */
74 /* black box default home directory */
75 static char *blackBoxDefaultHome = NIL;
76 static char *sslCApath = NIL; /* non-standard CA path */
77 static short anonymous = NIL; /* is anonymous */
78 static short blackBox = NIL; /* is a black box */
79 static short closedBox = NIL; /* is a closed box (uses chroot() jail) */
80 static short restrictBox = NIL; /* is a restricted box */
81 static short has_no_life = NIL; /* is a cretin with no life */
82 /* block environment init */
83 static short block_env_init = NIL;
84 static short hideDotFiles = NIL;/* hide files whose names start with . */
85 /* advertise filesystem root */
86 static short advertisetheworld = NIL;
87 /* only advertise own mailboxes and #shared */
88 static short limitedadvertise = NIL;
89 /* disable automatic shared namespaces */
90 static short noautomaticsharedns = NIL;
91 static short no822tztext = NIL; /* disable RFC [2]822 timezone text */
92 /* client principals include service name */
93 static short kerb_cp_svr_name = NIL;
94 static long locktimeout = 5; /* default lock timeout in minutes */
95 /* default prototypes */
96 static MAILSTREAM *createProto = NIL;
97 static MAILSTREAM *appendProto = NIL;
98 /* default user flags */
99 static char *userFlags[NUSERFLAGS] = {NIL};
100 static NAMESPACE *nslist[3]; /* namespace list */
101 static int logtry = 3; /* number of server login tries */
102 /* block notification */
103 static blocknotify_t mailblocknotify = mm_blocknotify;
104 /* logout function */
105 static logouthook_t maillogouthook = NIL;
106 /* logout data */
107 static void *maillogoutdata = NIL;
108 /* allow user config files */
109 static short allowuserconfig = NIL;
110 /* 1 = disable plaintext, 2 = if not SSL */
111 static long disablePlaintext = NIL;
112 static long list_max_level = 20;/* maximum level of list recursion */
113 /* facility for syslog */
114 static int syslog_facility = LOG_MAIL;
116 /* Path of the privileged system lock program (mlock). Normally set by
117 * logic test.
118 */
120 static char *lockpgm = LOCKPGM;
122 /* Directory used for shared locks. MUST be the same for all users of the
123 * system, and MUST be protected 1777. /var/tmp may be preferable on some
124 * systems.
125 */
127 static const char *tmpdir = "/tmp";
129 /* Do not change shlock_mode. Doing so can cause mailbox corruption and
130 * denial of service. It also defeats the entire purpose of the shared
131 * lock mechanism. The right way to avoid shared locks is to set up a
132 * closed box (see the closedBox setting).
133 */
135 /* shared lock mode */
136 static const int shlock_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
139 /* It is STRONGLY recommended that you do not change dotlock_mode. Doing so
140 * can cause denial of service with old dot-lock files left lying around.
141 * However, since dot-locks are only used with traditional UNIX and MMDF
142 * formats which are not normally shared, it is much less harmful to tamper
143 * with this than with shlock_mode.
144 */
146 /* dot-lock mode */
147 static long dotlock_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
149 /* File/directory access and protection policies */
151 /* Unlike shlock_mode, the ????_protection modes are intended to be fully
152 * customizable according to site policy. The values here are recommended
153 * settings, based upon the documented purposes of the namespaces.
154 */
156 /* user space - only owner can read/write */
157 static char *myMailboxDir = NIL;/* user space directory name */
158 /* default file protection */
159 static long mbx_protection = S_IRUSR|S_IWUSR;
160 /* default directory protection */
161 static long dir_protection = S_IRUSR|S_IWUSR|S_IXUSR;
163 /* user space for user "anonymous" */
164 /* anonymous home directory */
165 static char *anonymousHome = NIL;
167 /* #ftp - everybody can read, only owner can write */
168 static char *ftpHome = NIL; /* ftp export home directory */
169 /* default ftp file protection */
170 static long ftp_protection = S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH;
171 static long ftp_dir_protection =/* default ftp directory protection */
172 S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
174 /* #public - everybody can read/write */
175 static char *publicHome = NIL; /* public home directory */
176 static long public_protection = /* default public file protection */
177 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
178 /* default public directory protection */
179 static long public_dir_protection =
180 S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH;
182 /* #shared/ - owner and group members can read/write */
183 static char *sharedHome = NIL; /* shared home directory */
184 /* default shared file protection */
185 static long shared_protection = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
186 /* default shared directory protection */
187 static long shared_dir_protection =
188 S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP;
190 /* OS bug workarounds - should be avoided at all cost */
193 /* Don't set fcntlhangbug unless you really have to, since it risks mailbox
194 * corruption. The flocksim.c mechanism is designed to detect NFS access
195 * and no-op in that cases only, so this flag should be unnecessary.
196 */
198 static short fcntlhangbug = NIL;/* flock() emulator using fcntl() is a no-op */
201 /* Don't set netfsstatbug unless you really have to, since it dramatically
202 * slows down traditional UNIX and MMDF mailbox performance.
203 */
205 static short netfsstatbug = NIL;/* compensate for broken stat() on network
206 * filesystems (AFS and old NFS)
207 */
210 /* Note: setting disableLockWarning means that you assert that the
211 * so-modified copy of this software will NEVER be used:
212 * 1) in conjunction with any software which expects .lock files
213 * 2) to access NFS-mounted files and directories
214 *
215 * Unless both of these conditions apply, then do not set this flag.
216 * Instead, read the FAQ (item 7.10) and either use 1777 protection
217 * on the mail spool, or install mlock.
218 *
219 * In addition, by setting this flag you also agree that you are fully
220 * legally and morally responsible when (not if) mail files are damaged
221 * as the result of your choice.
222 *
223 * The mlock tool exists for a reason. Use it.
224 */
225 /* disable warning if can't make .lock file */
226 static short disableLockWarning = NIL;
228 /* UNIX Namespaces */
230 /* personal mh namespace */
231 static NAMESPACE nsmhf = {"#mh/",'/',NIL,NIL};
232 static NAMESPACE nsmh = {"#mhinbox",NIL,NIL,&nsmhf};
233 /* home namespace */
234 static NAMESPACE nshome = {"",'/',NIL,&nsmh};
235 /* UNIX other user namespace */
236 static NAMESPACE nsunixother = {"~",'/',NIL,NIL};
237 /* black box other user namespace */
238 static NAMESPACE nsblackother = {"/",'/',NIL,NIL};
239 /* public (anonymous OK) namespace */
240 static NAMESPACE nspublic = {"#public/",'/',NIL,NIL};
241 /* netnews namespace */
242 static NAMESPACE nsnews = {"#news.",'.',NIL,&nspublic};
243 /* FTP export namespace */
244 static NAMESPACE nsftp = {"#ftp/",'/',NIL,&nsnews};
245 /* shared (no anonymous) namespace */
246 static NAMESPACE nsshared = {"#shared/",'/',NIL,&nsftp};
247 /* world namespace */
248 static NAMESPACE nsworld = {"/",'/',NIL,&nsshared};
249 /* only shared and public namespaces */
250 static NAMESPACE nslimited = {"#shared/",'/',NIL,&nspublic};
254 #include "write.c" /* include safe writing routines */
255 #include "crexcl.c" /* include exclusive create */
256 #include "pmatch.c" /* include wildcard pattern matcher */
258 /* Get all authenticators */
260 #include "auths.c"
262 /* Environment manipulate parameters
263 * Accepts: function code
264 * function-dependent value
265 * Returns: function-dependent return value
266 */
268 void *env_parameters (long function,void *value)
269 {
270 void *ret = NIL;
271 switch ((int) function) {
272 case GET_NAMESPACE:
273 ret = (void *) nslist;
274 break;
275 case SET_USERNAME:
276 if (myUserName) fs_give ((void **) &myUserName);
277 myUserName = cpystr ((char *) value);
278 case GET_USERNAME:
279 ret = (void *) myUserName;
280 break;
281 case SET_HOMEDIR:
282 if (myHomeDir) fs_give ((void **) &myHomeDir);
283 myHomeDir = cpystr ((char *) value);
284 case GET_HOMEDIR:
285 ret = (void *) myHomeDir;
286 break;
287 case SET_LOCALHOST:
288 if (myLocalHost) fs_give ((void **) &myLocalHost);
289 myLocalHost = cpystr ((char *) value);
290 case GET_LOCALHOST:
291 ret = (void *) myLocalHost;
292 break;
293 case SET_NEWSRC:
294 if (myNewsrc) fs_give ((void **) &myNewsrc);
295 myNewsrc = cpystr ((char *) value);
296 case GET_NEWSRC:
297 ret = (void *) myNewsrc;
298 break;
299 case SET_NEWSACTIVE:
300 if (newsActive) fs_give ((void **) &newsActive);
301 newsActive = cpystr ((char *) value);
302 case GET_NEWSACTIVE:
303 ret = (void *) newsActive;
304 break;
305 case SET_NEWSSPOOL:
306 if (newsSpool) fs_give ((void **) &newsSpool);
307 newsSpool = cpystr ((char *) value);
308 case GET_NEWSSPOOL:
309 ret = (void *) newsSpool;
310 break;
312 case SET_ANONYMOUSHOME:
313 if (anonymousHome) fs_give ((void **) &anonymousHome);
314 anonymousHome = cpystr ((char *) value);
315 case GET_ANONYMOUSHOME:
316 if (!anonymousHome) anonymousHome = cpystr (ANONYMOUSHOME);
317 ret = (void *) anonymousHome;
318 break;
319 case SET_FTPHOME:
320 if (ftpHome) fs_give ((void **) &ftpHome);
321 ftpHome = cpystr ((char *) value);
322 case GET_FTPHOME:
323 ret = (void *) ftpHome;
324 break;
325 case SET_PUBLICHOME:
326 if (publicHome) fs_give ((void **) &publicHome);
327 publicHome = cpystr ((char *) value);
328 case GET_PUBLICHOME:
329 ret = (void *) publicHome;
330 break;
331 case SET_SHAREDHOME:
332 if (sharedHome) fs_give ((void **) &sharedHome);
333 sharedHome = cpystr ((char *) value);
334 case GET_SHAREDHOME:
335 ret = (void *) sharedHome;
336 break;
337 case SET_SYSINBOX:
338 if (sysInbox) fs_give ((void **) &sysInbox);
339 sysInbox = cpystr ((char *) value);
340 case GET_SYSINBOX:
341 ret = (void *) sysInbox;
342 break;
343 case SET_SSLCAPATH: /* this can be set null */
344 if (sslCApath) fs_give ((void **) &sslCApath);
345 sslCApath = value ? cpystr ((char *) value) : value;
346 break;
347 case GET_SSLCAPATH:
348 ret = (void *) sslCApath;
349 break;
350 case SET_LISTMAXLEVEL:
351 list_max_level = (long) value;
352 case GET_LISTMAXLEVEL:
353 ret = (void *) list_max_level;
354 break;
356 case SET_MBXPROTECTION:
357 mbx_protection = (long) value;
358 case GET_MBXPROTECTION:
359 ret = (void *) mbx_protection;
360 break;
361 case SET_DIRPROTECTION:
362 dir_protection = (long) value;
363 case GET_DIRPROTECTION:
364 ret = (void *) dir_protection;
365 break;
366 case SET_LOCKPROTECTION:
367 dotlock_mode = (long) value;
368 case GET_LOCKPROTECTION:
369 ret = (void *) dotlock_mode;
370 break;
371 case SET_FTPPROTECTION:
372 ftp_protection = (long) value;
373 case GET_FTPPROTECTION:
374 ret = (void *) ftp_protection;
375 break;
376 case SET_PUBLICPROTECTION:
377 public_protection = (long) value;
378 case GET_PUBLICPROTECTION:
379 ret = (void *) public_protection;
380 break;
381 case SET_SHAREDPROTECTION:
382 shared_protection = (long) value;
383 case GET_SHAREDPROTECTION:
384 ret = (void *) shared_protection;
385 break;
386 case SET_FTPDIRPROTECTION:
387 ftp_dir_protection = (long) value;
388 case GET_FTPDIRPROTECTION:
389 ret = (void *) ftp_dir_protection;
390 break;
391 case SET_PUBLICDIRPROTECTION:
392 public_dir_protection = (long) value;
393 case GET_PUBLICDIRPROTECTION:
394 ret = (void *) public_dir_protection;
395 break;
396 case SET_SHAREDDIRPROTECTION:
397 shared_dir_protection = (long) value;
398 case GET_SHAREDDIRPROTECTION:
399 ret = (void *) shared_dir_protection;
400 break;
402 case SET_LOCKTIMEOUT:
403 locktimeout = (long) value;
404 case GET_LOCKTIMEOUT:
405 ret = (void *) locktimeout;
406 break;
407 case SET_DISABLEFCNTLLOCK:
408 fcntlhangbug = value ? T : NIL;
409 case GET_DISABLEFCNTLLOCK:
410 ret = (void *) (fcntlhangbug ? VOIDT : NIL);
411 break;
412 case SET_LOCKEACCESERROR:
413 disableLockWarning = value ? NIL : T;
414 case GET_LOCKEACCESERROR:
415 ret = (void *) (disableLockWarning ? NIL : VOIDT);
416 break;
417 case SET_HIDEDOTFILES:
418 hideDotFiles = value ? T : NIL;
419 case GET_HIDEDOTFILES:
420 ret = (void *) (hideDotFiles ? VOIDT : NIL);
421 break;
422 case SET_DISABLEPLAINTEXT:
423 disablePlaintext = (long) value;
424 case GET_DISABLEPLAINTEXT:
425 #ifdef RESTRICT_POP
426 if (getenv("INTRANET") == NIL) disablePlaintext = 1;
427 else disablePlaintext = NIL;
428 #endif
429 ret = (void *) disablePlaintext;
430 break;
431 case SET_CHROOTSERVER:
432 closedBox = value ? T : NIL;
433 case GET_CHROOTSERVER:
434 ret = (void *) (closedBox ? VOIDT : NIL);
435 break;
436 case SET_ADVERTISETHEWORLD:
437 advertisetheworld = value ? T : NIL;
438 case GET_ADVERTISETHEWORLD:
439 ret = (void *) (advertisetheworld ? VOIDT : NIL);
440 break;
441 case SET_LIMITEDADVERTISE:
442 limitedadvertise = value ? T : NIL;
443 case GET_LIMITEDADVERTISE:
444 ret = (void *) (limitedadvertise ? VOIDT : NIL);
445 break;
446 case SET_DISABLEAUTOSHAREDNS:
447 noautomaticsharedns = value ? T : NIL;
448 case GET_DISABLEAUTOSHAREDNS:
449 ret = (void *) (noautomaticsharedns ? VOIDT : NIL);
450 break;
451 case SET_DISABLE822TZTEXT:
452 no822tztext = value ? T : NIL;
453 case GET_DISABLE822TZTEXT:
454 ret = (void *) (no822tztext ? VOIDT : NIL);
455 break;
457 case SET_USERHASNOLIFE:
458 has_no_life = value ? T : NIL;
459 case GET_USERHASNOLIFE:
460 ret = (void *) (has_no_life ? VOIDT : NIL);
461 break;
462 case SET_KERBEROS_CP_SVR_NAME:
463 kerb_cp_svr_name = value ? T : NIL;
464 case GET_KERBEROS_CP_SVR_NAME:
465 ret = (void *) (kerb_cp_svr_name ? VOIDT : NIL);
466 break;
467 case SET_NETFSSTATBUG:
468 netfsstatbug = value ? T : NIL;
469 case GET_NETFSSTATBUG:
470 ret = (void *) (netfsstatbug ? VOIDT : NIL);
471 break;
472 case SET_BLOCKENVINIT:
473 block_env_init = value ? T : NIL;
474 case GET_BLOCKENVINIT:
475 ret = (void *) (block_env_init ? VOIDT : NIL);
476 break;
477 case SET_BLOCKNOTIFY:
478 mailblocknotify = (blocknotify_t) value;
479 case GET_BLOCKNOTIFY:
480 ret = (void *) mailblocknotify;
481 break;
482 case SET_LOGOUTHOOK:
483 maillogouthook = (logouthook_t) value;
484 case GET_LOGOUTHOOK:
485 ret = maillogouthook;
486 break;
487 case SET_LOGOUTDATA:
488 maillogoutdata = (void *) value;
489 case GET_LOGOUTDATA:
490 ret = maillogoutdata;
491 }
492 return ret;
493 }
495 /* Write current time
496 * Accepts: destination string
497 * optional format of day-of-week prefix
498 * format of date and time
499 * flag whether to append symbolic timezone
500 */
502 static void do_date (char *date,char *prefix,char *fmt,int suffix)
503 {
504 time_t tn = time (0);
505 struct tm *t = gmtime (&tn);
506 int zone = t->tm_hour * 60 + t->tm_min;
507 int julian = t->tm_yday;
508 t = localtime (&tn); /* get local time now */
509 /* minus UTC minutes since midnight */
510 zone = t->tm_hour * 60 + t->tm_min - zone;
511 /* julian can be one of:
512 * 36x local time is December 31, UTC is January 1, offset -24 hours
513 * 1 local time is 1 day ahead of UTC, offset +24 hours
514 * 0 local time is same day as UTC, no offset
515 * -1 local time is 1 day behind UTC, offset -24 hours
516 * -36x local time is January 1, UTC is December 31, offset +24 hours
517 */
518 if (julian = t->tm_yday -julian)
519 zone += ((julian < 0) == (abs (julian) == 1)) ? -24*60 : 24*60;
520 if (prefix) { /* want day of week? */
521 sprintf (date,prefix,days[t->tm_wday]);
522 date += strlen (date); /* make next sprintf append */
523 }
524 /* output the date */
525 sprintf (date,fmt,t->tm_mday,months[t->tm_mon],t->tm_year+1900,
526 t->tm_hour,t->tm_min,t->tm_sec,zone/60,abs (zone) % 60);
527 /* append timezone suffix if desired */
528 if (suffix) rfc822_timezone (date,(void *) t);
529 }
531 /* Write current time in RFC 822 format
532 * Accepts: destination string
533 */
535 void rfc822_date (char *date)
536 {
537 do_date (date,"%s, ","%d %s %d %02d:%02d:%02d %+03d%02d",
538 no822tztext ? NIL : T);
539 }
542 /* Write current time in fixed-width RFC 822 format
543 * Accepts: destination string
544 */
546 void rfc822_fixed_date (char *date)
547 {
548 do_date (date,NIL,"%02d %s %4d %02d:%02d:%02d %+03d%02d",NIL);
549 }
552 /* Write current time in internal format
553 * Accepts: destination string
554 */
556 void internal_date (char *date)
557 {
558 do_date (date,NIL,"%02d-%s-%d %02d:%02d:%02d %+03d%02d",NIL);
559 }
561 /* Initialize server
562 * Accepts: server name for syslog or NIL
563 * /etc/services service name or NIL
564 * alternate /etc/services service name or NIL
565 * clock interrupt handler
566 * kiss-of-death interrupt handler
567 * hangup interrupt handler
568 * termination interrupt handler
569 */
571 void server_init (char *server,char *service,char *sslservice,
572 void *clkint,void *kodint,void *hupint,void *trmint,
573 void *staint)
574 {
575 int onceonly = server && service && sslservice;
576 if (onceonly) { /* set server name in syslog */
577 int mask;
578 openlog (myServerName = cpystr (server),LOG_PID,syslog_facility);
579 fclose (stderr); /* possibly save a process ID */
580 dorc (NIL,NIL); /* do systemwide configuration */
581 switch (mask = umask (022)){/* check old umask */
582 case 0: /* definitely unreasonable */
583 case 022: /* don't need to change it */
584 break;
585 default: /* already was a reasonable value */
586 umask (mask); /* so change it back */
587 }
588 }
589 arm_signal (SIGALRM,clkint); /* prepare for clock interrupt */
590 arm_signal (SIGUSR2,kodint); /* prepare for Kiss Of Death */
591 arm_signal (SIGHUP,hupint); /* prepare for hangup */
592 arm_signal (SIGPIPE,hupint); /* alternative hangup */
593 arm_signal (SIGTERM,trmint); /* prepare for termination */
594 /* status dump */
595 if (staint) arm_signal (SIGUSR1,staint);
596 if (onceonly) { /* set up network and maybe SSL */
597 long port;
598 struct servent *sv;
599 /* Use SSL if SSL service, or if server starts with "s" and not service */
600 if (((port = tcp_serverport ()) >= 0)) {
601 if ((sv = getservbyname (service,"tcp")) && (port == ntohs (sv->s_port)))
602 syslog (LOG_DEBUG,"%s service init from %s",service,tcp_clientaddr ());
603 else if ((sv = getservbyname (sslservice,"tcp")) &&
604 (port == ntohs (sv->s_port))) {
605 syslog (LOG_DEBUG,"%s SSL service init from %s",sslservice,
606 tcp_clientaddr ());
607 ssl_server_init (server);
608 }
609 else { /* not service or SSL service port */
610 syslog (LOG_DEBUG,"port %ld service init from %s",port,
611 tcp_clientaddr ());
612 if (*server == 's') ssl_server_init (server);
613 }
614 }
615 }
616 }
618 /* Wait for stdin input
619 * Accepts: timeout in seconds
620 * Returns: T if have input on stdin, else NIL
621 */
623 long server_input_wait (long seconds)
624 {
625 fd_set rfd,efd;
626 struct timeval tmo;
627 FD_ZERO (&rfd);
628 FD_ZERO (&efd);
629 FD_SET (0,&rfd);
630 FD_SET (0,&efd);
631 tmo.tv_sec = seconds; tmo.tv_usec = 0;
632 return select (1,&rfd,0,&efd,&tmo) ? LONGT : NIL;
633 }
635 /* Return UNIX password entry for user name
636 * Accepts: user name string
637 * Returns: password entry
638 *
639 * Tries all-lowercase form of user name if given user name fails
640 */
642 static struct passwd *pwuser (unsigned char *user)
643 {
644 unsigned char *s;
645 struct passwd *pw = getpwnam (user);
646 if (!pw) { /* failed, see if any uppercase characters */
647 for (s = user; *s && ((*s < 'A') || (*s > 'Z')); s++);
648 if (*s) { /* yes, try all lowercase form */
649 pw = getpwnam (s = lcase (cpystr (user)));
650 fs_give ((void **) &s);
651 }
652 }
653 return pw;
654 }
657 /* Validate password for user name
658 * Accepts: user name string
659 * password string
660 * argument count
661 * argument vector
662 * Returns: password entry if validated
663 *
664 * Tries password+1 if password fails and starts with space
665 */
667 static struct passwd *valpwd (char *user,char *pwd,int argc,char *argv[])
668 {
669 char *s;
670 struct passwd *pw;
671 struct passwd *ret = NIL;
672 #ifndef QMAIL /* imapext md5 checker run previously. no need to do here */
673 if (auth_md5.server) { /* using CRAM-MD5 authentication? */
674 if (s = auth_md5_pwd (user)) {
675 if (!strcmp (s,pwd) || ((*pwd == ' ') && pwd[1] && !strcmp (s,pwd+1)))
676 ret = pwuser (user); /* validated, get passwd entry for user */
677 memset (s,0,strlen (s)); /* erase sensitive information */
678 fs_give ((void **) &s);
679 }
680 }
681 else
682 #endif
683 if (pw = pwuser (user)) {/* can get user? */
684 s = cpystr (pw->pw_name); /* copy returned name in case we need it */
685 if (*pwd && !(ret = checkpw (pw,pwd,argc,argv)) &&
686 (*pwd == ' ') && pwd[1] && (ret = pwuser (s)))
687 ret = checkpw (pw,pwd+1,argc,argv);
688 fs_give ((void **) &s); /* don't need copy of name any more */
689 }
690 return ret;
691 }
693 /* Server log in
694 * Accepts: user name string
695 * password string
696 * authenticating user name string
697 * argument count
698 * argument vector
699 * Returns: T if password validated, NIL otherwise
700 */
702 long server_login (char *user,char *pwd,char *authuser,int argc,char *argv[])
703 {
704 struct passwd *pw = NIL;
705 int level = LOG_NOTICE;
706 char *err = "failed";
707 #ifdef QMAIL
708 char usr[MAILTMPLEN], *apoppswd;
709 strncpy(usr, user, MAILTMPLEN-1);
710 #endif
711 /* cretins still haven't given up */
712 if ((strlen (user) >= NETMAXUSER) ||
713 (authuser && (strlen (authuser) >= NETMAXUSER))) {
714 level = LOG_ALERT; /* escalate this alert */
715 err = "SYSTEM BREAK-IN ATTEMPT";
716 logtry = 0; /* render this session useless */
717 }
718 else if (logtry-- <= 0) err = "excessive login failures";
719 else if (disablePlaintext) err = "disabled";
720 #ifdef QMAIL
721 else if ((logtry > 0) &&
722 (apoppswd = auth_md5_pwd(usr))
723 && !strcmp(apoppswd, pwd)
724 && (pw = getpwnam(usr))) {
725 memset(apoppswd, 0, strlen(apoppswd));
726 fs_give((void**) &apoppswd);
727 return pw_login(pw, usr, pw->pw_name, pw->pw_dir, argc, argv);
728 }
729 #endif
730 else if (!(authuser && *authuser)) pw = valpwd (user,pwd,argc,argv);
731 else if (valpwd (authuser,pwd,argc,argv)) pw = pwuser (user);
732 if (pw && pw_login (pw,authuser,pw->pw_name,NIL,argc,argv)) return T;
733 syslog (level|LOG_AUTH,"Login %s user=%.64s auth=%.64s host=%.80s",err,
734 user,(authuser && *authuser) ? authuser : user,tcp_clienthost ());
735 sleep (3); /* slow down possible cracker */
736 return NIL;
737 }
739 /* Authenticated server log in
740 * Accepts: user name string
741 * authenticating user name string
742 * argument count
743 * argument vector
744 * Returns: T if password validated, NIL otherwise
745 */
747 long authserver_login (char *user,char *authuser,int argc,char *argv[])
748 {
749 return pw_login (pwuser (user),authuser,user,NIL,argc,argv);
750 }
752 void permitsmtp() /* to update tcp permission */
753 {
754 #ifdef POPBEFORESMTP
755 #include <sys/types.h>
756 #include <sys/wait.h>
757 #ifndef POP3RECORDER
758 # define POP3RECORDER "/usr/local/etc/pop3-record"
759 #endif
760 int child;
761 int wstat;
762 char *permsmtp = POP3RECORDER;
764 switch(child = fork())
765 {
766 case -1:
767 syslog (LOG_INFO,"Cannot exec %s", permsmtp);
768 _exit(111);
769 break;
770 case 0:
771 execl(permsmtp, permsmtp, 0);
772 syslog (LOG_INFO,"Cannot exec %s", permsmtp);
773 _exit(111); break;
774 }
775 waitpid(child, &wstat, 0);
776 #endif
777 }
781 /* Log in as anonymous daemon
782 * Accepts: argument count
783 * argument vector
784 * Returns: T if successful, NIL if error
785 */
787 long anonymous_login (int argc,char *argv[])
788 {
789 /* log in Mr. A. N. Onymous */
790 return pw_login (getpwnam (ANONYMOUSUSER),NIL,NIL,
791 (char *) mail_parameters (NIL,GET_ANONYMOUSHOME,NIL),
792 argc,argv);
793 }
795 /* Finish log in and environment initialization
796 * Accepts: passwd struct for loginpw()
797 * optional authentication user name
798 * user name (NIL for anonymous)
799 * home directory (NIL to use directory from passwd struct)
800 * argument count
801 * argument vector
802 * Returns: T if successful, NIL if error
803 */
805 long pw_login (struct passwd *pw,char *auser,char *user,char *home,int argc,
806 char *argv[])
807 {
808 struct group *gr;
809 char **t;
810 long ret = NIL;
811 if (pw && pw->pw_uid) { /* must have passwd struct for non-UID 0 */
812 /* make safe copies of user and home */
813 if (user) user = cpystr (pw->pw_name);
814 home = cpystr (home ? home : pw->pw_dir);
815 /* authorization ID .NE. authentication ID? */
816 if (user && auser && *auser && compare_cstring (auser,user)) {
817 /* scan list of mail administrators */
818 if ((gr = getgrnam (ADMINGROUP)) && (t = gr->gr_mem)) while (*t && !ret)
819 if (!compare_cstring (auser,*t++))
820 ret = pw_login (pw,NIL,user,home,argc,argv);
821 syslog (LOG_NOTICE|LOG_AUTH,"%s %.80s override of user=%.80s host=%.80s",
822 ret ? "Admin" : "Failed",auser,user,tcp_clienthost ());
823 }
824 else if (closedBox) { /* paranoid site, lock out other directories */
825 if (chdir (home) || chroot (home))
826 syslog (LOG_NOTICE|LOG_AUTH,
827 "Login %s failed: unable to set chroot=%.80s host=%.80s",
828 pw->pw_name,home,tcp_clienthost ());
829 else if (loginpw (pw,argc,argv)) ret = env_init (user,NIL);
830 else fatal ("Login failed after chroot");
831 }
832 /* normal login */
833 #ifdef QMAIL
834 else if (((pw->pw_uid == geteuid ()) || (permitsmtp(), loginpw (pw,argc,argv
835 ))) &&
836 (ret = env_init (user,home))) chdir (myhomedir ());
837 #else
838 else if (((pw->pw_uid == geteuid ()) || loginpw (pw,argc,argv)) &&
839 (ret = env_init (user,home))) chdir (myhomedir ());
840 #endif
841 fs_give ((void **) &home); /* clean up */
842 if (user) fs_give ((void **) &user);
843 }
844 endpwent (); /* in case shadow passwords in pw data */
845 return ret; /* return status */
846 }
848 /* Initialize environment
849 * Accepts: user name (NIL for anonymous)
850 * home directory name
851 * Returns: T, always
852 */
854 long env_init (char *user,char *home)
855 {
856 extern MAILSTREAM CREATEPROTO;
857 extern MAILSTREAM EMPTYPROTO;
858 struct passwd *pw;
859 struct stat sbuf;
860 char tmp[MAILTMPLEN];
861 /* don't init if blocked */
862 if (block_env_init) return LONGT;
863 if (myUserName) fatal ("env_init called twice!");
864 /* initially nothing in namespace list */
865 nslist[0] = nslist[1] = nslist[2] = NIL;
866 /* myUserName must be set before dorc() call */
867 myUserName = cpystr (user ? user : ANONYMOUSUSER);
868 /* force default prototypes to be set */
869 if (!createProto) createProto = &CREATEPROTO;
870 if (!appendProto) appendProto = &EMPTYPROTO;
871 dorc (NIL,NIL); /* do systemwide configuration */
872 if (!home) { /* closed box server */
873 /* standard user can only reference home */
874 if (user) nslist[0] = &nshome;
875 else { /* anonymous user */
876 nslist[0] = &nsblackother; /* set root */
877 anonymous = T; /* flag as anonymous */
878 }
879 myHomeDir = cpystr (""); /* home directory is root */
880 sysInbox = cpystr ("INBOX");/* make system INBOX */
881 }
882 else { /* open or black box */
883 closedBox = NIL; /* definitely not a closed box */
884 if (user) { /* remember user name and home directory */
885 if (blackBoxDir) { /* build black box directory name */
886 sprintf (tmp,"%s/%s",blackBoxDir,myUserName);
887 /* must exist */
888 if (!((!stat (home = tmp,&sbuf) && (sbuf.st_mode & S_IFDIR)) ||
889 (blackBoxDefaultHome &&
890 !stat (home = blackBoxDefaultHome,&sbuf) &&
891 (sbuf.st_mode & S_IFDIR)))) fatal ("no home");
892 sysInbox = (char *) fs_get (strlen (home) + 7);
893 /* set system INBOX */
894 sprintf (sysInbox,"%s/INBOX",home);
895 blackBox = T; /* mark that it's a black box */
896 /* mbox meaningless if black box */
897 mail_parameters (NIL,DISABLE_DRIVER,(void *) "mbox");
898 }
899 nslist[0] = &nshome; /* home namespace */
900 /* limited advertise namespaces */
901 if (limitedadvertise) nslist[2] = &nslimited;
902 else if (blackBox) { /* black box namespaces */
903 nslist[1] = &nsblackother;
904 nslist[2] = &nsshared;
905 }
906 else { /* open box namespaces */
907 nslist[1] = &nsunixother;
908 nslist[2] = advertisetheworld ? &nsworld : &nsshared;
909 }
910 }
911 else {
912 nslist[2] = &nsftp; /* anonymous user */
913 sprintf (tmp,"%s/INBOX",
914 home = (char *) mail_parameters (NIL,GET_ANONYMOUSHOME,NIL));
915 sysInbox = cpystr (tmp); /* make system INBOX */
916 anonymous = T; /* flag as anonymous */
917 }
918 myHomeDir = cpystr (home); /* set home directory */
919 }
921 if (allowuserconfig) { /* allow user config files */
922 dorc (strcat (strcpy (tmp,myHomeDir),"/.mminit"),T);
923 dorc (strcat (strcpy (tmp,myHomeDir),"/.imaprc"),NIL);
924 }
925 if (!closedBox && !noautomaticsharedns) {
926 /* #ftp namespace */
927 if (!ftpHome && (pw = getpwnam ("ftp"))) ftpHome = cpystr (pw->pw_dir);
928 /* #public namespace */
929 if (!publicHome && (pw = getpwnam ("imappublic")))
930 publicHome = cpystr (pw->pw_dir);
931 /* #shared namespace */
932 if (!anonymous && !sharedHome && (pw = getpwnam ("imapshared")))
933 sharedHome = cpystr (pw->pw_dir);
934 }
935 if (!myLocalHost) mylocalhost ();
936 if (!myNewsrc) myNewsrc = cpystr(strcat (strcpy (tmp,myHomeDir),"/.newsrc"));
937 if (!newsActive) newsActive = cpystr (ACTIVEFILE);
938 if (!newsSpool) newsSpool = cpystr (NEWSSPOOL);
939 /* re-do open action to get flags */
940 (*createProto->dtb->open) (NIL);
941 endpwent (); /* close pw database */
942 return T;
943 }
945 /* Return my user name
946 * Accepts: pointer to optional flags
947 * Returns: my user name
948 */
950 char *myusername_full (unsigned long *flags)
951 {
952 struct passwd *pw;
953 struct stat sbuf;
954 char *s;
955 unsigned long euid;
956 char *ret = UNLOGGEDUSER;
957 /* no user name yet and not root? */
958 if (!myUserName && (euid = geteuid ())) {
959 /* yes, look up getlogin() user name or EUID */
960 if (((s = (char *) getlogin ()) && *s && (strlen (s) < NETMAXUSER) &&
961 (pw = getpwnam (s)) && (pw->pw_uid == euid)) ||
962 (pw = getpwuid (euid))) {
963 if (block_env_init) { /* don't env_init if blocked */
964 if (flags) *flags = MU_LOGGEDIN;
965 return pw->pw_name;
966 }
967 env_init (pw->pw_name,
968 ((s = getenv ("HOME")) && *s && (strlen (s) < NETMAXMBX) &&
969 !stat (s,&sbuf) && ((sbuf.st_mode & S_IFMT) == S_IFDIR)) ?
970 s : pw->pw_dir);
971 }
972 else fatal ("Unable to look up user name");
973 }
974 if (myUserName) { /* logged in? */
975 if (flags) *flags = anonymous ? MU_ANONYMOUS : MU_LOGGEDIN;
976 ret = myUserName; /* return user name */
977 }
978 else if (flags) *flags = MU_NOTLOGGEDIN;
979 return ret;
980 }
983 /* Return my local host name
984 * Returns: my local host name
985 */
987 char *mylocalhost ()
988 {
989 if (!myLocalHost) {
990 char *s,tmp[MAILTMPLEN];
991 char *t = "unknown";
992 tmp[0] = tmp[MAILTMPLEN-1] = '\0';
993 if (!gethostname (tmp,MAILTMPLEN-1) && tmp[0]) {
994 /* sanity check of name */
995 for (s = tmp; (*s > 0x20) && (*s < 0x7f); ++s);
996 if (!*s) t = tcp_canonical (tmp);
997 }
998 myLocalHost = cpystr (t);
999 }
1000 return myLocalHost;
1003 /* Return my home directory name
1004 * Returns: my home directory name
1005 */
1007 char *myhomedir ()
1009 if (!myHomeDir) myusername ();/* initialize if first time */
1010 return myHomeDir ? myHomeDir : "";
1014 /* Return my home mailbox name
1015 * Returns: my home directory name
1016 */
1018 static char *mymailboxdir ()
1020 char *home = myhomedir ();
1021 /* initialize if first time */
1022 if (!myMailboxDir && myHomeDir) {
1023 if (mailsubdir) {
1024 char tmp[MAILTMPLEN];
1025 sprintf (tmp,"%s/%s",home,mailsubdir);
1026 myMailboxDir = cpystr (tmp);/* use pre-defined subdirectory of home */
1028 else myMailboxDir = cpystr (home);
1030 return myMailboxDir ? myMailboxDir : "";
1034 /* Return system standard INBOX
1035 * Accepts: buffer string
1036 */
1038 char *sysinbox ()
1040 char tmp[MAILTMPLEN];
1041 if (!sysInbox) { /* initialize if first time */
1042 sprintf (tmp,"%s/%s",MAILSPOOL,myusername ());
1043 sysInbox = cpystr (tmp); /* system inbox is from mail spool */
1045 return sysInbox;
1048 /* Return mailbox directory name
1049 * Accepts: destination buffer
1050 * directory prefix
1051 * name in directory
1052 * Returns: file name or NIL if error
1053 */
1055 char *mailboxdir (char *dst,char *dir,char *name)
1057 char tmp[MAILTMPLEN];
1058 if (dir || name) { /* if either argument provided */
1059 if (dir) {
1060 if (strlen (dir) > NETMAXMBX) return NIL;
1061 strcpy (tmp,dir); /* write directory prefix */
1063 else tmp[0] = '\0'; /* otherwise null string */
1064 if (name) {
1065 if (strlen (name) > NETMAXMBX) return NIL;
1066 strcat (tmp,name); /* write name in directory */
1068 /* validate name, return its name */
1069 if (!mailboxfile (dst,tmp)) return NIL;
1071 /* no arguments, wants mailbox directory */
1072 else strcpy (dst,mymailboxdir ());
1073 return dst; /* return the name */
1076 /* Return mailbox file name
1077 * Accepts: destination buffer
1078 * mailbox name
1079 * Returns: file name or empty string for driver-selected INBOX or NIL if error
1080 */
1082 char *mailboxfile (char *dst,char *name)
1084 struct passwd *pw;
1085 char *s;
1086 if (!name || !*name || (*name == '{') || (strlen (name) > NETMAXMBX) ||
1087 ((anonymous || blackBox || restrictBox || (*name == '#')) &&
1088 (strstr (name,"..") || strstr (name,"//") || strstr (name,"/~"))))
1089 dst = NIL; /* invalid name */
1090 else switch (*name) { /* determine mailbox type based upon name */
1091 case '#': /* namespace name */
1092 /* #ftp/ namespace */
1093 if (((name[1] == 'f') || (name[1] == 'F')) &&
1094 ((name[2] == 't') || (name[2] == 'T')) &&
1095 ((name[3] == 'p') || (name[3] == 'P')) &&
1096 (name[4] == '/') && ftpHome) sprintf (dst,"%s/%s",ftpHome,name+5);
1097 /* #public/ and #shared/ namespaces */
1098 else if ((((name[1] == 'p') || (name[1] == 'P')) &&
1099 ((name[2] == 'u') || (name[2] == 'U')) &&
1100 ((name[3] == 'b') || (name[3] == 'B')) &&
1101 ((name[4] == 'l') || (name[4] == 'L')) &&
1102 ((name[5] == 'i') || (name[5] == 'I')) &&
1103 ((name[6] == 'c') || (name[6] == 'C')) &&
1104 (name[7] == '/') && (s = publicHome)) ||
1105 (!anonymous && ((name[1] == 's') || (name[1] == 'S')) &&
1106 ((name[2] == 'h') || (name[2] == 'H')) &&
1107 ((name[3] == 'a') || (name[3] == 'A')) &&
1108 ((name[4] == 'r') || (name[4] == 'R')) &&
1109 ((name[5] == 'e') || (name[5] == 'E')) &&
1110 ((name[6] == 'd') || (name[6] == 'D')) &&
1111 (name[7] == '/') && (s = sharedHome)))
1112 sprintf (dst,"%s/%s",s,compare_cstring (name+8,"INBOX") ?
1113 name+8 : "INBOX");
1114 else dst = NIL; /* unknown namespace */
1115 break;
1117 case '/': /* root access */
1118 if (anonymous) dst = NIL; /* anonymous forbidden to do this */
1119 else if (blackBox) { /* other user access if blackbox */
1120 if (restrictBox & RESTRICTOTHERUSER) dst = NIL;
1121 /* see if other user INBOX */
1122 else if ((s = strchr (name+1,'/')) && !compare_cstring (s+1,"INBOX")) {
1123 *s = '\0'; /* temporarily tie off string */
1124 sprintf (dst,"%s/%s/INBOX",blackBoxDir,name+1);
1125 *s = '/'; /* in case caller cares */
1127 else sprintf (dst,"%s/%s",blackBoxDir,name+1);
1129 else if ((restrictBox & RESTRICTROOT) && strcmp (name,sysinbox ()))
1130 dst = NIL; /* restricted and not access to sysinbox */
1131 else strcpy (dst,name); /* unrestricted, copy root name */
1132 break;
1133 case '~': /* other user access */
1134 /* bad syntax or anonymous can't win */
1135 if (!*++name || anonymous) dst = NIL;
1136 /* ~/ equivalent to ordinary name */
1137 else if (*name == '/') sprintf (dst,"%s/%s",mymailboxdir (),name+1);
1138 /* other user forbidden if closed/restricted */
1139 else if (closedBox || (restrictBox & RESTRICTOTHERUSER)) dst = NIL;
1140 else if (blackBox) { /* black box form of other user */
1141 /* see if other user INBOX */
1142 if ((s = strchr (name,'/')) && compare_cstring (s+1,"INBOX")) {
1143 *s = '\0'; /* temporarily tie off string */
1144 sprintf (dst,"%s/%s/INBOX",blackBoxDir,name);
1145 *s = '/'; /* in case caller cares */
1147 else sprintf (dst,"%s/%s",blackBoxDir,name);
1149 else { /* clear box other user */
1150 /* copy user name */
1151 for (s = dst; *name && (*name != '/'); *s++ = *name++);
1152 *s++ = '\0'; /* tie off user name, look up in passwd file */
1153 if ((pw = getpwnam (dst)) && pw->pw_dir) {
1154 if (*name) name++; /* skip past the slash */
1155 /* canonicalize case of INBOX */
1156 if (!compare_cstring (name,"INBOX")) name = "INBOX";
1157 /* remove trailing / from directory */
1158 if ((s = strrchr (pw->pw_dir,'/')) && !s[1]) *s = '\0';
1159 /* don't allow ~root/ if restricted root */
1160 if ((restrictBox & RESTRICTROOT) && !*pw->pw_dir) dst = NIL;
1161 /* build final name w/ subdir if needed */
1162 else if (mailsubdir) sprintf (dst,"%s/%s/%s",pw->pw_dir,mailsubdir,name);
1163 else sprintf (dst,"%s/%s",pw->pw_dir,name);
1165 else dst = NIL; /* no such user */
1167 break;
1169 case 'I': case 'i': /* possible INBOX */
1170 if (!compare_cstring (name+1,"NBOX")) {
1171 /* if restricted, use INBOX in mailbox dir */
1172 if (anonymous || blackBox || closedBox)
1173 sprintf (dst,"%s/INBOX",mymailboxdir ());
1174 else *dst = '\0'; /* otherwise driver selects the name */
1175 break;
1177 /* drop into to ordinary name case */
1178 default: /* ordinary name is easy */
1179 sprintf (dst,"%s/%s",mymailboxdir (),name);
1180 break;
1182 return dst; /* return final name */
1185 /* Dot-lock file locker
1186 * Accepts: file name to lock
1187 * destination buffer for lock file name
1188 * open file description on file name to lock
1189 * Returns: T if success, NIL if failure
1190 */
1192 long dotlock_lock (char *file,DOTLOCK *base,int fd)
1194 int i = locktimeout * 60;
1195 int j,mask,retry,pi[2],po[2];
1196 char *s,tmp[MAILTMPLEN];
1197 struct stat sb;
1198 /* flush absurd file name */
1199 if (strlen (file) > 512) return NIL;
1200 /* build lock filename */
1201 sprintf (base->lock,"%s.lock",file);
1202 /* assume no pipe */
1203 base->pipei = base->pipeo = -1;
1204 do { /* make sure not symlink */
1205 if (!(j = chk_notsymlink (base->lock,&sb))) return NIL;
1206 /* time out if file older than 5 minutes */
1207 if ((j > 0) && ((time (0)) >= (sb.st_ctime + locktimeout * 60))) i = 0;
1208 /* try to create the lock */
1209 switch (retry = crexcl (base->lock)) {
1210 case -1: /* OK to retry */
1211 if (!(i%15)) { /* time to notify? */
1212 sprintf (tmp,"Mailbox %.80s is locked, will override in %d seconds...",
1213 file,i);
1214 MM_LOG (tmp,WARN);
1216 sleep (1); /* wait 1 second before next try */
1217 break;
1218 case NIL: /* failure, can't retry */
1219 i = 0;
1220 break;
1221 case T: /* success, make sure others can break lock */
1222 chmod (base->lock,(int) dotlock_mode);
1223 return LONGT;
1225 } while (i--); /* until out of retries */
1226 if (retry < 0) { /* still returning retry after locktimeout? */
1227 if (!(j = chk_notsymlink (base->lock,&sb))) return NIL;
1228 if ((j > 0) && ((time (0)) < (sb.st_ctime + locktimeout * 60))) {
1229 sprintf (tmp,"Mailbox vulnerable - seizing %ld second old lock",
1230 (long) (time (0) - sb.st_ctime));
1231 MM_LOG (tmp,WARN);
1233 mask = umask (0); /* want our lock protection */
1234 unlink (base->lock); /* try to remove the old file */
1235 /* seize the lock */
1236 if ((i = open (base->lock,O_WRONLY|O_CREAT,(int) dotlock_mode)) >= 0) {
1237 close (i); /* don't need descriptor any more */
1238 sprintf (tmp,"Mailbox %.80s lock overridden",file);
1239 MM_LOG (tmp,NIL);
1240 chmod (base->lock,(int) dotlock_mode);
1241 umask (mask); /* restore old umask */
1242 return LONGT;
1244 umask (mask); /* restore old umask */
1247 if (fd >= 0) switch (errno) {
1248 case EACCES: /* protection failure? */
1249 MM_CRITICAL (NIL); /* go critical */
1250 if (closedBox || !lockpgm); /* can't do on closed box or disabled */
1251 else if ((*lockpgm && stat (lockpgm,&sb)) ||
1252 (!*lockpgm && stat (lockpgm = LOCKPGM1,&sb) &&
1253 stat (lockpgm = LOCKPGM2,&sb) && stat (lockpgm = LOCKPGM3,&sb)))
1254 lockpgm = NIL; /* disable if can't find lockpgm */
1255 else if (pipe (pi) >= 0) { /* make command pipes */
1256 long cf;
1257 char *argv[4],arg[20];
1258 /* if input pipes usable create output pipes */
1259 if ((pi[0] < FD_SETSIZE) && (pi[1] < FD_SETSIZE) && (pipe (po) >= 0)) {
1260 /* make sure output pipes are usable */
1261 if ((po[0] >= FD_SETSIZE) || (po[1] >= FD_SETSIZE));
1262 /* all is good, make inferior process */
1263 else if (!(j = fork ())) {
1264 if (!fork ()) { /* make grandchild so it's inherited by init */
1265 /* prepare argument vector */
1266 sprintf (arg,"%d",fd);
1267 argv[0] = lockpgm; argv[1] = arg;
1268 argv[2] = file; argv[3] = NIL;
1269 /* set parent's I/O to my O/I */
1270 dup2 (pi[1],1); dup2 (pi[1],2); dup2 (po[0],0);
1271 /* close all unnecessary descriptors */
1272 for (cf = max (20,max (max (pi[0],pi[1]),max(po[0],po[1])));
1273 cf >= 3; --cf) if (cf != fd) close (cf);
1274 /* be our own process group */
1275 setpgrp (0,getpid ());
1276 /* now run it */
1277 _exit (execv (argv[0],argv));
1279 _exit (1); /* child is done */
1281 else if (j > 0) { /* parent process */
1282 fd_set rfd;
1283 struct timeval tmo;
1284 FD_ZERO (&rfd);
1285 FD_SET (pi[0],&rfd);
1286 tmo.tv_sec = locktimeout * 60;
1287 grim_pid_reap (j,NIL);/* reap child; grandchild now owned by init */
1288 /* read response from locking program */
1289 if (select (pi[0]+1,&rfd,0,0,&tmo) &&
1290 (read (pi[0],tmp,1) == 1) && (tmp[0] == '+')) {
1291 /* success, record pipes */
1292 base->pipei = pi[0]; base->pipeo = po[1];
1293 /* close child's side of the pipes */
1294 close (pi[1]); close (po[0]);
1295 MM_NOCRITICAL (NIL);/* no longer critical */
1296 return LONGT;
1299 close (po[0]); close (po[1]);
1301 close (pi[0]); close (pi[1]);
1304 MM_NOCRITICAL (NIL); /* no longer critical */
1305 /* find directory/file delimiter */
1306 if (s = strrchr (base->lock,'/')) {
1307 *s = '\0'; /* tie off at directory */
1308 sprintf(tmp, /* generate default message */
1309 "Mailbox vulnerable - directory %.80s must have 1777 protection",
1310 base->lock);
1311 /* definitely not 1777 if can't stat */
1312 mask = stat (base->lock,&sb) ? 0 : (sb.st_mode & 1777);
1313 *s = '/'; /* restore lock name */
1314 if (mask != 1777) { /* default warning if not 1777 */
1315 if (!disableLockWarning) MM_LOG (tmp,WARN);
1316 break;
1319 default:
1320 sprintf (tmp,"Mailbox vulnerable - error creating %.80s: %s",
1321 base->lock,strerror (errno));
1322 if (!disableLockWarning) MM_LOG (tmp,WARN);
1323 break;
1325 base->lock[0] = '\0'; /* don't use lock files */
1326 return NIL;
1329 /* Dot-lock file unlocker
1330 * Accepts: lock file name
1331 * Returns: T if success, NIL if failure
1332 */
1334 long dotlock_unlock (DOTLOCK *base)
1336 long ret = LONGT;
1337 if (base && base->lock[0]) {
1338 if (base->pipei >= 0) { /* if running through a pipe unlocker */
1339 ret = (write (base->pipeo,"+",1) == 1);
1340 /* nuke the pipes */
1341 close (base->pipei); close (base->pipeo);
1343 else ret = !unlink (base->lock);
1345 return ret;
1348 /* Lock file name
1349 * Accepts: scratch buffer
1350 * file name
1351 * type of locking operation (LOCK_SH or LOCK_EX)
1352 * pointer to return PID of locker
1353 * Returns: file descriptor of lock or negative if error
1354 */
1356 int lockname (char *lock,char *fname,int op,long *pid)
1358 struct stat sbuf;
1359 *pid = 0; /* no locker PID */
1360 return stat (fname,&sbuf) ? -1 : lock_work (lock,&sbuf,op,pid);
1364 /* Lock file descriptor
1365 * Accepts: file descriptor
1366 * lock file name buffer
1367 * type of locking operation (LOCK_SH or LOCK_EX)
1368 * Returns: file descriptor of lock or negative if error
1369 */
1371 int lockfd (int fd,char *lock,int op)
1373 struct stat sbuf;
1374 return fstat (fd,&sbuf) ? -1 : lock_work (lock,&sbuf,op,NIL);
1377 /* Lock file name worker
1378 * Accepts: lock file name
1379 * pointer to stat() buffer
1380 * type of locking operation (LOCK_SH or LOCK_EX)
1381 * pointer to return PID of locker
1382 * Returns: file descriptor of lock or negative if error
1383 */
1385 int lock_work (char *lock,void *sb,int op,long *pid)
1387 struct stat lsb,fsb;
1388 struct stat *sbuf = (struct stat *) sb;
1389 char tmp[MAILTMPLEN];
1390 long i;
1391 int fd;
1392 int mask = umask (0);
1393 if (pid) *pid = 0; /* initialize return PID */
1394 /* make temporary lock file name */
1395 sprintf (lock,"%s/.%lx.%lx",closedBox ? "" : tmpdir,
1396 (unsigned long) sbuf->st_dev,(unsigned long) sbuf->st_ino);
1397 while (T) { /* until get a good lock */
1398 do switch ((int) chk_notsymlink (lock,&lsb)) {
1399 case 1: /* exists just once */
1400 if (((fd = open (lock,O_RDWR,shlock_mode)) >= 0) ||
1401 (errno != ENOENT) || (chk_notsymlink (lock,&lsb) >= 0)) break;
1402 case -1: /* name doesn't exist */
1403 fd = open (lock,O_RDWR|O_CREAT|O_EXCL,shlock_mode);
1404 break;
1405 default: /* multiple hard links */
1406 MM_LOG ("hard link to lock name",ERROR);
1407 syslog (LOG_CRIT,"SECURITY PROBLEM: hard link to lock name: %.80s",lock);
1408 case 0: /* symlink (already did syslog) */
1409 umask (mask); /* restore old mask */
1410 return -1; /* fail: no lock file */
1411 } while ((fd < 0) && (errno == EEXIST));
1412 if (fd < 0) { /* failed to get file descriptor */
1413 syslog (LOG_INFO,"Mailbox lock file %s open failure: %s",lock,
1414 strerror (errno));
1415 if (!closedBox) { /* more explicit snarl for bad configuration */
1416 if (stat (tmpdir,&lsb))
1417 syslog (LOG_CRIT,"SYSTEM ERROR: no %s: %s",tmpdir,strerror (errno));
1418 else if ((lsb.st_mode & 01777) != 01777) {
1419 sprintf (tmp,"Can't lock for write: %.80s must have 1777 protection",
1420 tmpdir);
1421 MM_LOG (tmp,WARN);
1424 umask (mask); /* restore old mask */
1425 return -1; /* fail: can't open lock file */
1428 /* non-blocking form */
1429 if (op & LOCK_NB) i = flock (fd,op);
1430 else { /* blocking form */
1431 (*mailblocknotify) (BLOCK_FILELOCK,NIL);
1432 i = flock (fd,op);
1433 (*mailblocknotify) (BLOCK_NONE,NIL);
1435 if (i) { /* failed, get other process' PID */
1436 if (pid && !fstat (fd,&fsb) && (i = min (fsb.st_size,MAILTMPLEN-1)) &&
1437 (read (fd,tmp,i) == i) && !(tmp[i] = 0) && ((i = atol (tmp)) > 0))
1438 *pid = i;
1439 close (fd); /* failed, give up on lock */
1440 umask (mask); /* restore old mask */
1441 return -1; /* fail: can't lock */
1443 /* make sure this lock is good for us */
1444 if (!lstat (lock,&lsb) && ((lsb.st_mode & S_IFMT) != S_IFLNK) &&
1445 !fstat (fd,&fsb) && (lsb.st_dev == fsb.st_dev) &&
1446 (lsb.st_ino == fsb.st_ino) && (fsb.st_nlink == 1)) break;
1447 close (fd); /* lock not right, drop fd and try again */
1449 chmod (lock,shlock_mode); /* make sure mode OK (don't use fchmod()) */
1450 umask (mask); /* restore old mask */
1451 return fd; /* success */
1454 /* Check to make sure not a symlink
1455 * Accepts: file name
1456 * stat buffer
1457 * Returns: -1 if doesn't exist, NIL if symlink, else number of hard links
1458 */
1460 long chk_notsymlink (char *name,void *sb)
1462 struct stat *sbuf = (struct stat *) sb;
1463 /* name exists? */
1464 if (lstat (name,sbuf)) return -1;
1465 /* forbid symbolic link */
1466 if ((sbuf->st_mode & S_IFMT) == S_IFLNK) {
1467 MM_LOG ("symbolic link on lock name",ERROR);
1468 syslog (LOG_CRIT,"SECURITY PROBLEM: symbolic link on lock name: %.80s",
1469 name);
1470 return NIL;
1472 return (long) sbuf->st_nlink; /* return number of hard links */
1476 /* Unlock file descriptor
1477 * Accepts: file descriptor
1478 * lock file name from lockfd()
1479 */
1481 void unlockfd (int fd,char *lock)
1483 /* delete the file if no sharers */
1484 if (!flock (fd,LOCK_EX|LOCK_NB)) unlink (lock);
1485 flock (fd,LOCK_UN); /* unlock it */
1486 close (fd); /* close it */
1489 /* Set proper file protection for mailbox
1490 * Accepts: mailbox name
1491 * actual file path name
1492 * Returns: T, always
1493 */
1495 long set_mbx_protections (char *mailbox,char *path)
1497 struct stat sbuf;
1498 int mode = (int) mbx_protection;
1499 if (*mailbox == '#') { /* possible namespace? */
1500 if (((mailbox[1] == 'f') || (mailbox[1] == 'F')) &&
1501 ((mailbox[2] == 't') || (mailbox[2] == 'T')) &&
1502 ((mailbox[3] == 'p') || (mailbox[3] == 'P')) &&
1503 (mailbox[4] == '/')) mode = (int) ftp_protection;
1504 else if (((mailbox[1] == 'p') || (mailbox[1] == 'P')) &&
1505 ((mailbox[2] == 'u') || (mailbox[2] == 'U')) &&
1506 ((mailbox[3] == 'b') || (mailbox[3] == 'B')) &&
1507 ((mailbox[4] == 'l') || (mailbox[4] == 'L')) &&
1508 ((mailbox[5] == 'i') || (mailbox[5] == 'I')) &&
1509 ((mailbox[6] == 'c') || (mailbox[6] == 'C')) &&
1510 (mailbox[7] == '/')) mode = (int) public_protection;
1511 else if (((mailbox[1] == 's') || (mailbox[1] == 'S')) &&
1512 ((mailbox[2] == 'h') || (mailbox[2] == 'H')) &&
1513 ((mailbox[3] == 'a') || (mailbox[3] == 'A')) &&
1514 ((mailbox[4] == 'r') || (mailbox[4] == 'R')) &&
1515 ((mailbox[5] == 'e') || (mailbox[5] == 'E')) &&
1516 ((mailbox[6] == 'd') || (mailbox[6] == 'D')) &&
1517 (mailbox[7] == '/')) mode = (int) shared_protection;
1519 /* if a directory */
1520 if (!stat (path,&sbuf) && ((sbuf.st_mode & S_IFMT) == S_IFDIR)) {
1521 /* set owner search if allow read or write */
1522 if (mode & 0600) mode |= 0100;
1523 if (mode & 060) mode |= 010;/* set group search if allow read or write */
1524 if (mode & 06) mode |= 01; /* set world search if allow read or write */
1525 /* preserve directory SGID bit */
1526 if (sbuf.st_mode & S_ISGID) mode |= S_ISGID;
1528 chmod (path,mode); /* set the new protection, ignore failure */
1529 return LONGT;
1532 /* Get proper directory protection
1533 * Accepts: mailbox name
1534 * Returns: directory mode, always
1535 */
1537 long get_dir_protection (char *mailbox)
1539 if (*mailbox == '#') { /* possible namespace? */
1540 if (((mailbox[1] == 'f') || (mailbox[1] == 'F')) &&
1541 ((mailbox[2] == 't') || (mailbox[2] == 'T')) &&
1542 ((mailbox[3] == 'p') || (mailbox[3] == 'P')) &&
1543 (mailbox[4] == '/')) return ftp_dir_protection;
1544 else if (((mailbox[1] == 'p') || (mailbox[1] == 'P')) &&
1545 ((mailbox[2] == 'u') || (mailbox[2] == 'U')) &&
1546 ((mailbox[3] == 'b') || (mailbox[3] == 'B')) &&
1547 ((mailbox[4] == 'l') || (mailbox[4] == 'L')) &&
1548 ((mailbox[5] == 'i') || (mailbox[5] == 'I')) &&
1549 ((mailbox[6] == 'c') || (mailbox[6] == 'C')) &&
1550 (mailbox[7] == '/')) return public_dir_protection;
1551 else if (((mailbox[1] == 's') || (mailbox[1] == 'S')) &&
1552 ((mailbox[2] == 'h') || (mailbox[2] == 'H')) &&
1553 ((mailbox[3] == 'a') || (mailbox[3] == 'A')) &&
1554 ((mailbox[4] == 'r') || (mailbox[4] == 'R')) &&
1555 ((mailbox[5] == 'e') || (mailbox[5] == 'E')) &&
1556 ((mailbox[6] == 'd') || (mailbox[6] == 'D')) &&
1557 (mailbox[7] == '/')) return shared_dir_protection;
1559 return dir_protection;
1562 /* Determine default prototype stream to user
1563 * Accepts: type (NIL for create, T for append)
1564 * Returns: default prototype stream
1565 */
1567 MAILSTREAM *default_proto (long type)
1569 myusername (); /* make sure initialized */
1570 /* return default driver's prototype */
1571 return type ? appendProto : createProto;
1575 /* Set up user flags for stream
1576 * Accepts: MAIL stream
1577 * Returns: MAIL stream with user flags set up
1578 */
1580 MAILSTREAM *user_flags (MAILSTREAM *stream)
1582 int i;
1583 myusername (); /* make sure initialized */
1584 for (i = 0; i < NUSERFLAGS && userFlags[i]; ++i)
1585 if (!stream->user_flags[i]) stream->user_flags[i] = cpystr (userFlags[i]);
1586 return stream;
1590 /* Return nth user flag
1591 * Accepts: user flag number
1592 * Returns: flag
1593 */
1595 char *default_user_flag (unsigned long i)
1597 myusername (); /* make sure initialized */
1598 return userFlags[i];
1601 /* Process rc file
1602 * Accepts: file name
1603 * .mminit flag
1604 * Don't use this feature.
1605 */
1607 void dorc (char *file,long flag)
1609 int i;
1610 char *s,*t,*k,*r,tmp[MAILTMPLEN],tmpx[MAILTMPLEN];
1611 extern MAILSTREAM CREATEPROTO;
1612 extern MAILSTREAM EMPTYPROTO;
1613 DRIVER *d;
1614 FILE *f;
1615 if ((f = fopen (file ? file : SYSCONFIG,"r")) &&
1616 (s = fgets (tmp,MAILTMPLEN,f)) && (t = strchr (s,'\n'))) do {
1617 *t++ = '\0'; /* tie off line, find second space */
1618 if ((k = strchr (s,' ')) && (k = strchr (++k,' '))) {
1619 *k++ = '\0'; /* tie off two words */
1620 if (!compare_cstring (s,"set keywords") && !userFlags[0]) {
1621 /* yes, get first keyword */
1622 k = strtok_r (k,", ",&r);
1623 /* copy keyword list */
1624 for (i = 0; k && i < NUSERFLAGS; ++i) if (strlen (k) <= MAXUSERFLAG) {
1625 if (userFlags[i]) fs_give ((void **) &userFlags[i]);
1626 userFlags[i] = cpystr (k);
1627 k = strtok_r (NIL,", ",&r);
1629 if (flag) break; /* found "set keywords" in .mminit */
1632 else if (!flag) { /* none of these valid in .mminit */
1633 if (myUserName) { /* only valid if logged in */
1634 if (!compare_cstring (s,"set new-mailbox-format") ||
1635 !compare_cstring (s,"set new-folder-format")) {
1636 if (!compare_cstring (k,"same-as-inbox")) {
1637 if (d = mail_valid (NIL,"INBOX",NIL)) {
1638 if (!compare_cstring (d->name,"mbox"))
1639 d = (DRIVER *) mail_parameters (NIL,GET_DRIVER,
1640 (void *) "unix");
1641 else if (!compare_cstring (d->name,"dummy")) d = NIL;
1643 createProto = d ? ((*d->open) (NIL)) : &CREATEPROTO;
1645 else if (!compare_cstring (k,"system-standard"))
1646 createProto = &CREATEPROTO;
1647 else { /* canonicalize mbox to unix */
1648 if (!compare_cstring (k,"mbox")) k = "unix";
1649 /* see if a driver name */
1650 if (d = (DRIVER *) mail_parameters (NIL,GET_DRIVER,(void *) k))
1651 createProto = (*d->open) (NIL);
1652 else { /* duh... */
1653 sprintf (tmpx,"Unknown new mailbox format in %s: %s",
1654 file ? file : SYSCONFIG,k);
1655 MM_LOG (tmpx,WARN);
1659 if (!compare_cstring (s,"set empty-mailbox-format") ||
1660 !compare_cstring (s,"set empty-folder-format")) {
1661 if (!compare_cstring (k,"invalid")) appendProto = NIL;
1662 else if (!compare_cstring (k,"same-as-inbox"))
1663 appendProto = ((d = mail_valid (NIL,"INBOX",NIL)) &&
1664 compare_cstring (d->name,"dummy")) ?
1665 ((*d->open) (NIL)) : &EMPTYPROTO;
1666 else if (!compare_cstring (k,"system-standard"))
1667 appendProto = &EMPTYPROTO;
1668 else { /* see if a driver name */
1669 for (d = (DRIVER *) mail_parameters (NIL,GET_DRIVERS,NIL);
1670 d && compare_cstring (d->name,k); d = d->next);
1671 if (d) appendProto = (*d->open) (NIL);
1672 else { /* duh... */
1673 sprintf (tmpx,"Unknown empty mailbox format in %s: %s",
1674 file ? file : SYSCONFIG,k);
1675 MM_LOG (tmpx,WARN);
1681 if (!compare_cstring (s,"set local-host")) {
1682 fs_give ((void **) &myLocalHost);
1683 myLocalHost = cpystr (k);
1685 else if (!compare_cstring (s,"set news-active-file")) {
1686 fs_give ((void **) &newsActive);
1687 newsActive = cpystr (k);
1689 else if (!compare_cstring (s,"set news-spool-directory")) {
1690 fs_give ((void **) &newsSpool);
1691 newsSpool = cpystr (k);
1693 else if (!compare_cstring (s,"set mh-path"))
1694 mail_parameters (NIL,SET_MHPATH,(void *) k);
1695 else if (!compare_cstring (s,"set mh-allow-inbox"))
1696 mail_parameters (NIL,SET_MHALLOWINBOX,(void *) atol (k));
1697 else if (!compare_cstring (s,"set news-state-file")) {
1698 fs_give ((void **) &myNewsrc);
1699 myNewsrc = cpystr (k);
1701 else if (!compare_cstring (s,"set ftp-export-directory")) {
1702 fs_give ((void **) &ftpHome);
1703 ftpHome = cpystr (k);
1705 else if (!compare_cstring (s,"set public-home-directory")) {
1706 fs_give ((void **) &publicHome);
1707 publicHome = cpystr (k);
1709 else if (!compare_cstring (s,"set shared-home-directory")) {
1710 fs_give ((void **) &sharedHome);
1711 sharedHome = cpystr (k);
1713 else if (!compare_cstring (s,"set system-inbox")) {
1714 fs_give ((void **) &sysInbox);
1715 sysInbox = cpystr (k);
1717 else if (!compare_cstring (s,"set mail-subdirectory")) {
1718 fs_give ((void **) &mailsubdir);
1719 mailsubdir = cpystr (k);
1721 else if (!compare_cstring (s,"set from-widget"))
1722 mail_parameters (NIL,SET_FROMWIDGET,
1723 compare_cstring (k,"header-only") ?
1724 VOIDT : NIL);
1726 else if (!compare_cstring (s,"set rsh-command"))
1727 mail_parameters (NIL,SET_RSHCOMMAND,(void *) k);
1728 else if (!compare_cstring (s,"set rsh-path"))
1729 mail_parameters (NIL,SET_RSHPATH,(void *) k);
1730 else if (!compare_cstring (s,"set ssh-command"))
1731 mail_parameters (NIL,SET_SSHCOMMAND,(void *) k);
1732 else if (!compare_cstring (s,"set ssh-path"))
1733 mail_parameters (NIL,SET_SSHPATH,(void *) k);
1734 else if (!compare_cstring (s,"set tcp-open-timeout"))
1735 mail_parameters (NIL,SET_OPENTIMEOUT,(void *) atol (k));
1736 else if (!compare_cstring (s,"set tcp-read-timeout"))
1737 mail_parameters (NIL,SET_READTIMEOUT,(void *) atol (k));
1738 else if (!compare_cstring (s,"set tcp-write-timeout"))
1739 mail_parameters (NIL,SET_WRITETIMEOUT,(void *) atol (k));
1740 else if (!compare_cstring (s,"set rsh-timeout"))
1741 mail_parameters (NIL,SET_RSHTIMEOUT,(void *) atol (k));
1742 else if (!compare_cstring (s,"set ssh-timeout"))
1743 mail_parameters (NIL,SET_SSHTIMEOUT,(void *) atol (k));
1744 else if (!compare_cstring (s,"set maximum-login-trials"))
1745 mail_parameters (NIL,SET_MAXLOGINTRIALS,(void *) atol (k));
1746 else if (!compare_cstring (s,"set lookahead"))
1747 mail_parameters (NIL,SET_LOOKAHEAD,(void *) atol (k));
1748 else if (!compare_cstring (s,"set prefetch"))
1749 mail_parameters (NIL,SET_PREFETCH,(void *) atol (k));
1750 else if (!compare_cstring (s,"set close-on-error"))
1751 mail_parameters (NIL,SET_CLOSEONERROR,(void *) atol (k));
1752 else if (!compare_cstring (s,"set imap-port"))
1753 mail_parameters (NIL,SET_IMAPPORT,(void *) atol (k));
1754 else if (!compare_cstring (s,"set pop3-port"))
1755 mail_parameters (NIL,SET_POP3PORT,(void *) atol (k));
1756 else if (!compare_cstring (s,"set uid-lookahead"))
1757 mail_parameters (NIL,SET_UIDLOOKAHEAD,(void *) atol (k));
1758 else if (!compare_cstring (s,"set try-ssl-first"))
1759 mail_parameters (NIL,SET_TRYSSLFIRST,(void *) atol (k));
1761 else if (!compare_cstring (s,"set mailbox-protection"))
1762 mbx_protection = atol (k);
1763 else if (!compare_cstring (s,"set directory-protection"))
1764 dir_protection = atol (k);
1765 else if (!compare_cstring (s,"set lock-protection"))
1766 dotlock_mode = atol (k);
1767 else if (!compare_cstring (s,"set ftp-protection"))
1768 ftp_protection = atol (k);
1769 else if (!compare_cstring (s,"set public-protection"))
1770 public_protection = atol (k);
1771 else if (!compare_cstring (s,"set shared-protection"))
1772 shared_protection = atol (k);
1773 else if (!compare_cstring (s,"set ftp-directory-protection"))
1774 ftp_dir_protection = atol (k);
1775 else if (!compare_cstring (s,"set public-directory-protection"))
1776 public_dir_protection = atol (k);
1777 else if (!compare_cstring (s,"set shared-directory-protection"))
1778 shared_dir_protection = atol (k);
1779 else if (!compare_cstring (s,"set dot-lock-file-timeout"))
1780 locktimeout = atoi (k);
1781 else if (!compare_cstring (s,"set disable-fcntl-locking"))
1782 fcntlhangbug = atoi (k);
1783 else if (!compare_cstring (s,"set disable-lock-warning"))
1784 disableLockWarning = atoi (k);
1785 else if (!compare_cstring (s,"set disable-unix-UIDs-and-keywords"))
1786 has_no_life = atoi (k);
1787 else if (!compare_cstring (s,"set hide-dot-files"))
1788 hideDotFiles = atoi (k);
1789 else if (!compare_cstring (s,"set list-maximum-level"))
1790 list_max_level = atol (k);
1791 else if (!compare_cstring (s,"set trust-dns"))
1792 mail_parameters (NIL,SET_TRUSTDNS,(void *) atol (k));
1793 else if (!compare_cstring (s,"set sasl-uses-ptr-name"))
1794 mail_parameters (NIL,SET_SASLUSESPTRNAME,(void *) atol (k));
1795 else if (!compare_cstring (s,"set network-filesystem-stat-bug"))
1796 netfsstatbug = atoi (k);
1797 else if (!compare_cstring (s,"set nntp-range"))
1798 mail_parameters (NIL,SET_NNTPRANGE,(void *) atol (k));
1800 else if (!file) { /* only allowed in system init */
1801 if (!compare_cstring (s,"set black-box-directory") &&
1802 !blackBoxDir) blackBoxDir = cpystr (k);
1803 else if (!compare_cstring(s,"set black-box-default-home-directory")&&
1804 blackBoxDir && !blackBoxDefaultHome)
1805 blackBoxDefaultHome = cpystr (k);
1806 else if (!compare_cstring (s,"set anonymous-home-directory") &&
1807 !anonymousHome) anonymousHome = cpystr (k);
1808 /* It's tempting to allow setting the CA path
1809 * in a user init. However, that opens up a
1810 * vector of attack big enough to drive a
1811 * truck through... Resist the temptation.
1812 */
1813 else if (!compare_cstring (s,"set CA-certificate-path"))
1814 sslCApath = cpystr (k);
1815 else if (!compare_cstring (s,"set disable-plaintext"))
1816 disablePlaintext = atoi (k);
1817 else if (!compare_cstring (s,"set allowed-login-attempts"))
1818 logtry = atoi (k);
1819 else if (!compare_cstring (s,"set chroot-server"))
1820 closedBox = atoi (k);
1821 else if (!compare_cstring (s,"set restrict-mailbox-access"))
1822 for (k = strtok_r (k,", ",&r); k; k = strtok_r (NIL,", ",&r)) {
1823 if (!compare_cstring (k,"root")) restrictBox |= RESTRICTROOT;
1824 else if (!compare_cstring (k,"otherusers"))
1825 restrictBox |= RESTRICTOTHERUSER;
1826 else if (!compare_cstring (k,"all")) restrictBox = -1;
1828 else if (!compare_cstring (s,"set advertise-the-world"))
1829 advertisetheworld = atoi (k);
1830 else if (!compare_cstring (s,"set limited-advertise"))
1831 limitedadvertise = atoi (k);
1832 else if (!compare_cstring
1833 (s,"set disable-automatic-shared-namespaces"))
1834 noautomaticsharedns = atoi (k);
1835 else if (!compare_cstring (s,"set allow-user-config"))
1836 allowuserconfig = atoi (k);
1837 else if (!compare_cstring (s,"set allow-reverse-dns"))
1838 mail_parameters (NIL,SET_ALLOWREVERSEDNS,(void *) atol (k));
1839 else if (!compare_cstring (s,"set k5-cp-uses-service-name"))
1840 kerb_cp_svr_name = atoi (k);
1841 /* must appear in file after any
1842 * "set disable-plaintext" command! */
1843 else if (!compare_cstring (s,"set plaintext-allowed-clients")) {
1844 for (k = strtok_r (k,", ",&r); k && !tcp_isclienthost (k);
1845 k = strtok_r (NIL,", ",&r));
1846 if (k) disablePlaintext = 0;
1851 } while ((s = fgets (tmp,MAILTMPLEN,f)) && (t = strchr (s,'\n')));
1852 if (f) fclose (f); /* flush the file */
1855 /* INBOX create function for tmail/dmail use only
1856 * Accepts: mail stream
1857 * path name buffer, preloaded with driver-dependent path
1858 * Returns: T on success, NIL on failure
1860 * This routine is evil and a truly incredible kludge. It is private for
1861 * tmail/dmail and is not supported for any other application.
1862 */
1864 long path_create (MAILSTREAM *stream,char *path)
1866 long ret;
1867 short rsave = restrictBox;
1868 restrictBox = NIL; /* can't restrict */
1869 if (blackBox) { /* if black box */
1870 /* toss out driver dependent names */
1871 printf (path,"%s/INBOX",mymailboxdir ());
1872 blackBox = NIL; /* well that's evil - evil is going on */
1873 ret = mail_create (stream,path);
1874 blackBox = T; /* restore the box */
1876 /* easy thing otherwise */
1877 else ret = mail_create (stream,path);
1878 restrictBox = rsave; /* restore restrictions */
1879 return ret;
1882 /* Default block notify routine
1883 * Accepts: reason for calling
1884 * data
1885 * Returns: data
1886 */
1888 void *mm_blocknotify (int reason,void *data)
1890 void *ret = data;
1891 switch (reason) {
1892 case BLOCK_SENSITIVE: /* entering sensitive code */
1893 ret = (void *) (unsigned long) alarm (0);
1894 break;
1895 case BLOCK_NONSENSITIVE: /* exiting sensitive code */
1896 if ((unsigned long) data) alarm ((unsigned long) data);
1897 break;
1898 default: /* ignore all other reasons */
1899 break;
1901 return ret;

UW-IMAP'd extensions by yuuji