1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1990, 1993, 1994\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)mail.local.c	8.18 (Berkeley) 01/25/95";
16 #endif /* not lint */
17 
18 /*
19  * This is not intended to compile on System V derived systems
20  * such as Solaris or HP-UX, since they use a totally different
21  * approach to mailboxes (essentially, they have a setgid program
22  * rather than setuid, and they rely on the ability to "give away"
23  * files to do their work).  IT IS NOT A BUG that this doesn't
24  * compile on such architectures.
25  */
26 
27 #include <sys/param.h>
28 #include <sys/stat.h>
29 #include <sys/socket.h>
30 
31 #include <netinet/in.h>
32 
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <netdb.h>
36 #include <pwd.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sysexits.h>
41 #include <syslog.h>
42 #include <time.h>
43 #include <unistd.h>
44 #include <ctype.h>
45 
46 #if __STDC__
47 #include <stdarg.h>
48 #else
49 #include <varargs.h>
50 #endif
51 
52 #ifndef LOCK_EX
53 # include <sys/file.h>
54 #endif
55 
56 #ifdef BSD4_4
57 # include "pathnames.h"
58 #endif
59 
60 #ifndef __P
61 # ifdef __STDC__
62 #  define __P(protos)	protos
63 # else
64 #  define __P(protos)	()
65 #  define const
66 # endif
67 #endif
68 #ifndef __dead
69 # if defined(__GNUC__) && (__GNUC__ < 2 || __GNUC_MINOR__ < 5) && !defined(__STRICT_ANSI__)
70 #  define __dead	__volatile
71 # else
72 #  define __dead
73 # endif
74 #endif
75 
76 #ifndef BSD4_4
77 # define _BSD_VA_LIST_	va_list
78 extern char	*strerror __P((int));
79 #endif
80 
81 #ifndef _PATH_LOCTMP
82 # define _PATH_LOCTMP	"/tmp/local.XXXXXX"
83 #endif
84 #ifndef _PATH_MAILDIR
85 # define _PATH_MAILDIR	"/var/spool/mail"
86 #endif
87 
88 #ifndef S_ISREG
89 # define S_ISREG(mode)	(((mode) & _S_IFMT) == S_IFREG)
90 #endif
91 
92 int eval = EX_OK;			/* sysexits.h error value. */
93 
94 void		deliver __P((int, char *));
95 void		e_to_sys __P((int));
96 __dead void	err __P((const char *, ...));
97 void		notifybiff __P((char *));
98 int		store __P((char *));
99 void		usage __P((void));
100 void		vwarn __P((const char *, _BSD_VA_LIST_));
101 void		warn __P((const char *, ...));
102 
103 int
104 main(argc, argv)
105 	int argc;
106 	char *argv[];
107 {
108 	struct passwd *pw;
109 	int ch, fd;
110 	uid_t uid;
111 	char *from;
112 	extern char *optarg;
113 	extern int optind;
114 
115 	/* make sure we have some open file descriptors */
116 	for (fd = 10; fd < 30; fd++)
117 		(void) close(fd);
118 
119 	/* use a reasonable umask */
120 	(void) umask(0077);
121 
122 #ifdef LOG_MAIL
123 	openlog("mail.local", 0, LOG_MAIL);
124 #else
125 	openlog("mail.local", 0);
126 #endif
127 
128 	from = NULL;
129 	while ((ch = getopt(argc, argv, "df:r:")) != EOF)
130 		switch(ch) {
131 		case 'd':		/* Backward compatible. */
132 			break;
133 		case 'f':
134 		case 'r':		/* Backward compatible. */
135 			if (from != NULL) {
136 				warn("multiple -f options");
137 				usage();
138 			}
139 			from = optarg;
140 			break;
141 		case '?':
142 		default:
143 			usage();
144 		}
145 	argc -= optind;
146 	argv += optind;
147 
148 	if (!*argv)
149 		usage();
150 
151 	/*
152 	 * If from not specified, use the name from getlogin() if the
153 	 * uid matches, otherwise, use the name from the password file
154 	 * corresponding to the uid.
155 	 */
156 	uid = getuid();
157 	if (!from && (!(from = getlogin()) ||
158 	    !(pw = getpwnam(from)) || pw->pw_uid != uid))
159 		from = (pw = getpwuid(uid)) ? pw->pw_name : "???";
160 
161 	/*
162 	 * There is no way to distinguish the error status of one delivery
163 	 * from the rest of the deliveries.  So, if we failed hard on one
164 	 * or more deliveries, but had no failures on any of the others, we
165 	 * return a hard failure.  If we failed temporarily on one or more
166 	 * deliveries, we return a temporary failure regardless of the other
167 	 * failures.  This results in the delivery being reattempted later
168 	 * at the expense of repeated failures and multiple deliveries.
169 	 */
170 	for (fd = store(from); *argv; ++argv)
171 		deliver(fd, *argv);
172 	exit(eval);
173 }
174 
175 int
176 store(from)
177 	char *from;
178 {
179 	FILE *fp;
180 	time_t tval;
181 	int fd, eline;
182 	char line[2048];
183 	char tmpbuf[sizeof _PATH_LOCTMP + 1];
184 
185 	strcpy(tmpbuf, _PATH_LOCTMP);
186 	if ((fd = mkstemp(tmpbuf)) == -1 || (fp = fdopen(fd, "w+")) == NULL) {
187 		e_to_sys(errno);
188 		err("unable to open temporary file");
189 	}
190 	(void)unlink(tmpbuf);
191 
192 	(void)time(&tval);
193 	(void)fprintf(fp, "From %s %s", from, ctime(&tval));
194 
195 	line[0] = '\0';
196 	for (eline = 1; fgets(line, sizeof(line), stdin);) {
197 		if (line[0] == '\n')
198 			eline = 1;
199 		else {
200 			if (eline && line[0] == 'F' &&
201 			    !memcmp(line, "From ", 5))
202 				(void)putc('>', fp);
203 			eline = 0;
204 		}
205 		(void)fprintf(fp, "%s", line);
206 		if (ferror(fp)) {
207 			e_to_sys(errno);
208 			err("temporary file write error");
209 		}
210 	}
211 
212 	/* If message not newline terminated, need an extra. */
213 	if (!strchr(line, '\n'))
214 		(void)putc('\n', fp);
215 	/* Output a newline; note, empty messages are allowed. */
216 	(void)putc('\n', fp);
217 
218 	if (fflush(fp) == EOF || ferror(fp)) {
219 		e_to_sys(errno);
220 		err("temporary file write error");
221 	}
222 	return (fd);
223 }
224 
225 void
226 deliver(fd, name)
227 	int fd;
228 	char *name;
229 {
230 	struct stat fsb, sb;
231 	struct passwd *pw;
232 	int mbfd, nr, nw, off;
233 	char *p;
234 	char biffmsg[100], buf[8*1024], path[MAXPATHLEN];
235 	off_t curoff;
236 
237 	/*
238 	 * Disallow delivery to unknown names -- special mailboxes can be
239 	 * handled in the sendmail aliases file.
240 	 */
241 	if (!(pw = getpwnam(name))) {
242 		if (eval != EX_TEMPFAIL)
243 			eval = EX_UNAVAILABLE;
244 		warn("unknown name: %s", name);
245 		return;
246 	}
247 
248 	/*
249 	 * Keep name reasonably short to avoid buffer overruns.
250 	 *	This isn't necessary on BSD because of the proper
251 	 *	definition of snprintf(), but it can cause problems
252 	 *	on other systems.
253 	 * Also, clear out any bogus characters.
254 	 */
255 
256 	if (strlen(name) > 40)
257 		name[40] = '\0';
258 	for (p = name; *p != '\0'; p++)
259 	{
260 		if (!isascii(*p))
261 			*p &= 0x7f;
262 		else if (!isprint(*p))
263 			*p = '.';
264 	}
265 
266 	(void)snprintf(path, sizeof(path), "%s/%s", _PATH_MAILDIR, name);
267 
268 	/*
269 	 * If the mailbox is linked or a symlink, fail.  There's an obvious
270 	 * race here, that the file was replaced with a symbolic link after
271 	 * the lstat returned, but before the open.  We attempt to detect
272 	 * this by comparing the original stat information and information
273 	 * returned by an fstat of the file descriptor returned by the open.
274 	 *
275 	 * NB: this is a symptom of a larger problem, that the mail spooling
276 	 * directory is writeable by the wrong users.  If that directory is
277 	 * writeable, system security is compromised for other reasons, and
278 	 * it cannot be fixed here.
279 	 *
280 	 * If we created the mailbox, set the owner/group.  If that fails,
281 	 * just return.  Another process may have already opened it, so we
282 	 * can't unlink it.  Historically, binmail set the owner/group at
283 	 * each mail delivery.  We no longer do this, assuming that if the
284 	 * ownership or permissions were changed there was a reason.
285 	 *
286 	 * XXX
287 	 * open(2) should support flock'ing the file.
288 	 */
289 tryagain:
290 	lockmbox(path);
291 	if (lstat(path, &sb)) {
292 		mbfd = open(path,
293 		    O_APPEND|O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR);
294 		if (mbfd == -1) {
295 			if (errno == EEXIST)
296 				goto tryagain;
297 		} else if (fchown(mbfd, pw->pw_uid, pw->pw_gid)) {
298 			e_to_sys(errno);
299 			warn("chown %u.%u: %s", pw->pw_uid, pw->pw_gid, name);
300 			unlockmbox();
301 			return;
302 		}
303 	} else if (sb.st_nlink != 1 || !S_ISREG(sb.st_mode)) {
304 		e_to_sys(errno);
305 		warn("%s: irregular file", path);
306 		unlockmbox();
307 		return;
308 	} else if (sb.st_uid != pw->pw_uid) {
309 		warn("%s: wrong ownership (%d)", path, sb.st_uid);
310 		unlockmbox();
311 		return;
312 	} else {
313 		mbfd = open(path, O_APPEND|O_WRONLY, 0);
314 		if (mbfd != -1 &&
315 		    (fstat(mbfd, &fsb) || fsb.st_nlink != 1 ||
316 		    !S_ISREG(fsb.st_mode) || sb.st_dev != fsb.st_dev ||
317 		    sb.st_ino != fsb.st_ino || sb.st_uid != fsb.st_uid)) {
318 			warn("%s: file changed after open", path);
319 			(void)close(mbfd);
320 			unlockmbox();
321 			return;
322 		}
323 	}
324 
325 	if (mbfd == -1) {
326 		e_to_sys(errno);
327 		warn("%s: %s", path, strerror(errno));
328 		unlockmbox();
329 		return;
330 	}
331 
332 	/* Wait until we can get a lock on the file. */
333 	if (flock(mbfd, LOCK_EX)) {
334 		e_to_sys(errno);
335 		warn("%s: %s", path, strerror(errno));
336 		unlockmbox();
337 		goto err1;
338 	}
339 
340 	/* Get the starting offset of the new message for biff. */
341 	curoff = lseek(mbfd, (off_t)0, SEEK_END);
342 	(void)snprintf(biffmsg, sizeof(biffmsg),
343 		sizeof curoff > sizeof(long) ? "%s@%qd\n" : "%s@%ld\n",
344 		name, curoff);
345 
346 	/* Copy the message into the file. */
347 	if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
348 		e_to_sys(errno);
349 		warn("temporary file: %s", strerror(errno));
350 		goto err1;
351 	}
352 	while ((nr = read(fd, buf, sizeof(buf))) > 0)
353 		for (off = 0; off < nr; off += nw)
354 			if ((nw = write(mbfd, buf + off, nr - off)) < 0) {
355 				e_to_sys(errno);
356 				warn("%s: %s", path, strerror(errno));
357 				goto err2;;
358 			}
359 	if (nr < 0) {
360 		e_to_sys(errno);
361 		warn("temporary file: %s", strerror(errno));
362 		goto err2;;
363 	}
364 
365 	/* Flush to disk, don't wait for update. */
366 	if (fsync(mbfd)) {
367 		e_to_sys(errno);
368 		warn("%s: %s", path, strerror(errno));
369 err2:		(void)ftruncate(mbfd, curoff);
370 err1:		(void)close(mbfd);
371 		unlockmbox();
372 		return;
373 	}
374 
375 	/* Close and check -- NFS doesn't write until the close. */
376 	if (close(mbfd)) {
377 		e_to_sys(errno);
378 		warn("%s: %s", path, strerror(errno));
379 		unlockmbox();
380 		return;
381 	}
382 
383 	unlockmbox();
384 	notifybiff(biffmsg);
385 }
386 
387 /*
388  * user.lock files are necessary for compatibility with other
389  * systems, e.g., when the mail spool file is NFS exported.
390  * Alas, mailbox locking is more than just a local matter.
391  * EPA 11/94.
392  */
393 
394 char	lockname[MAXPATHLEN];
395 int	locked = 0;
396 
397 lockmbox(path)
398 	char *path;
399 {
400 	int statfailed = 0;
401 
402 	if (locked)
403 		return;
404 	sprintf(lockname, "%s.lock", path);
405 	for (;; sleep(5)) {
406 		int fd;
407 		struct stat st;
408 		time_t now;
409 
410 		fd = open(lockname, O_WRONLY|O_EXCL|O_CREAT, 0);
411 		if (fd >= 0) {
412 			locked = 1;
413 			close(fd);
414 			return;
415 		}
416 		if (stat(lockname, &st) < 0) {
417 			if (statfailed++ > 5)
418 				return;
419 			continue;
420 		}
421 		statfailed = 0;
422 		time(&now);
423 		if (now < st.st_ctime + 300)
424 			continue;
425 		unlink(lockname);
426 	}
427 }
428 
429 unlockmbox()
430 {
431 	if (!locked)
432 		return;
433 	unlink(lockname);
434 	locked = 0;
435 }
436 
437 void
438 notifybiff(msg)
439 	char *msg;
440 {
441 	static struct sockaddr_in addr;
442 	static int f = -1;
443 	struct hostent *hp;
444 	struct servent *sp;
445 	int len;
446 
447 	if (!addr.sin_family) {
448 		/* Be silent if biff service not available. */
449 		if (!(sp = getservbyname("biff", "udp")))
450 			return;
451 		if (!(hp = gethostbyname("localhost"))) {
452 			warn("localhost: %s", strerror(errno));
453 			return;
454 		}
455 		addr.sin_family = hp->h_addrtype;
456 		memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
457 		addr.sin_port = sp->s_port;
458 	}
459 	if (f < 0 && (f = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
460 		warn("socket: %s", strerror(errno));
461 		return;
462 	}
463 	len = strlen(msg) + 1;
464 	if (sendto(f, msg, len, 0, (struct sockaddr *)&addr, sizeof(addr))
465 	    != len)
466 		warn("sendto biff: %s", strerror(errno));
467 }
468 
469 void
470 usage()
471 {
472 	eval = EX_USAGE;
473 	err("usage: mail.local [-f from] user ...");
474 }
475 
476 #if __STDC__
477 void
478 err(const char *fmt, ...)
479 #else
480 void
481 err(fmt, va_alist)
482 	const char *fmt;
483 	va_dcl
484 #endif
485 {
486 	va_list ap;
487 
488 #if __STDC__
489 	va_start(ap, fmt);
490 #else
491 	va_start(ap);
492 #endif
493 	vwarn(fmt, ap);
494 	va_end(ap);
495 
496 	exit(eval);
497 }
498 
499 void
500 #if __STDC__
501 warn(const char *fmt, ...)
502 #else
503 warn(fmt, va_alist)
504 	const char *fmt;
505 	va_dcl
506 #endif
507 {
508 	va_list ap;
509 
510 #if __STDC__
511 	va_start(ap, fmt);
512 #else
513 	va_start(ap);
514 #endif
515 	vwarn(fmt, ap);
516 	va_end(ap);
517 }
518 
519 void
520 vwarn(fmt, ap)
521 	const char *fmt;
522 	_BSD_VA_LIST_ ap;
523 {
524 	/*
525 	 * Log the message to stderr.
526 	 *
527 	 * Don't use LOG_PERROR as an openlog() flag to do this,
528 	 * it's not portable enough.
529 	 */
530 	if (eval != EX_USAGE)
531 		(void)fprintf(stderr, "mail.local: ");
532 	(void)vfprintf(stderr, fmt, ap);
533 	(void)fprintf(stderr, "\n");
534 
535 #ifndef ultrix
536 	/* Log the message to syslog. */
537 	vsyslog(LOG_ERR, fmt, ap);
538 #else
539 	{
540 		char fmtbuf[10240];
541 
542 		(void) sprintf(fmtbuf, fmt, ap);
543 		syslog(LOG_ERR, "%s", fmtbuf);
544 	}
545 #endif
546 }
547 
548 /*
549  * e_to_sys --
550  *	Guess which errno's are temporary.  Gag me.
551  */
552 void
553 e_to_sys(num)
554 	int num;
555 {
556 	/* Temporary failures override hard errors. */
557 	if (eval == EX_TEMPFAIL)
558 		return;
559 
560 	switch(num) {		/* Hopefully temporary errors. */
561 #ifdef EAGAIN
562 	case EAGAIN:		/* Resource temporarily unavailable */
563 #endif
564 #ifdef EDQUOT
565 	case EDQUOT:		/* Disc quota exceeded */
566 #endif
567 #ifdef EBUSY
568 	case EBUSY:		/* Device busy */
569 #endif
570 #ifdef EPROCLIM
571 	case EPROCLIM:		/* Too many processes */
572 #endif
573 #ifdef EUSERS
574 	case EUSERS:		/* Too many users */
575 #endif
576 #ifdef ECONNABORTED
577 	case ECONNABORTED:	/* Software caused connection abort */
578 #endif
579 #ifdef ECONNREFUSED
580 	case ECONNREFUSED:	/* Connection refused */
581 #endif
582 #ifdef ECONNRESET
583 	case ECONNRESET:	/* Connection reset by peer */
584 #endif
585 #ifdef EDEADLK
586 	case EDEADLK:		/* Resource deadlock avoided */
587 #endif
588 #ifdef EFBIG
589 	case EFBIG:		/* File too large */
590 #endif
591 #ifdef EHOSTDOWN
592 	case EHOSTDOWN:		/* Host is down */
593 #endif
594 #ifdef EHOSTUNREACH
595 	case EHOSTUNREACH:	/* No route to host */
596 #endif
597 #ifdef EMFILE
598 	case EMFILE:		/* Too many open files */
599 #endif
600 #ifdef ENETDOWN
601 	case ENETDOWN:		/* Network is down */
602 #endif
603 #ifdef ENETRESET
604 	case ENETRESET:		/* Network dropped connection on reset */
605 #endif
606 #ifdef ENETUNREACH
607 	case ENETUNREACH:	/* Network is unreachable */
608 #endif
609 #ifdef ENFILE
610 	case ENFILE:		/* Too many open files in system */
611 #endif
612 #ifdef ENOBUFS
613 	case ENOBUFS:		/* No buffer space available */
614 #endif
615 #ifdef ENOMEM
616 	case ENOMEM:		/* Cannot allocate memory */
617 #endif
618 #ifdef ENOSPC
619 	case ENOSPC:		/* No space left on device */
620 #endif
621 #ifdef EROFS
622 	case EROFS:		/* Read-only file system */
623 #endif
624 #ifdef ESTALE
625 	case ESTALE:		/* Stale NFS file handle */
626 #endif
627 #ifdef ETIMEDOUT
628 	case ETIMEDOUT:		/* Connection timed out */
629 #endif
630 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN && EWOULDBLOCK != EDEADLK
631 	case EWOULDBLOCK:	/* Operation would block. */
632 #endif
633 		eval = EX_TEMPFAIL;
634 		break;
635 	default:
636 		eval = EX_UNAVAILABLE;
637 		break;
638 	}
639 }
640 
641 #ifndef BSD4_4
642 
643 char *
644 strerror(eno)
645 	int eno;
646 {
647 	extern int sys_nerr;
648 	extern char *sys_errlist[];
649 	static char ebuf[60];
650 
651 	if (eno >= 0 && eno <= sys_nerr)
652 		return sys_errlist[eno];
653 	(void) sprintf(ebuf, "Error %d", eno);
654 	return ebuf;
655 }
656 
657 #if __STDC__
658 snprintf(char *buf, int bufsiz, const char *fmt, ...)
659 #else
660 snprintf(buf, bufsiz, fmt, va_alist)
661 	char *buf;
662 	int bufsiz;
663 	const char *fmt;
664 	va_dcl
665 #endif
666 {
667 	va_list ap;
668 
669 #if __STDC__
670 	va_start(ap, fmt);
671 #else
672 	va_start(ap);
673 #endif
674 	vsprintf(buf, fmt, ap);
675 	va_end(ap);
676 }
677 
678 #endif
679 
680 #ifdef ultrix
681 
682 /*
683  * Copyright (c) 1987, 1993
684  *	The Regents of the University of California.  All rights reserved.
685  *
686  * Redistribution and use in source and binary forms, with or without
687  * modification, are permitted provided that the following conditions
688  * are met:
689  * 1. Redistributions of source code must retain the above copyright
690  *    notice, this list of conditions and the following disclaimer.
691  * 2. Redistributions in binary form must reproduce the above copyright
692  *    notice, this list of conditions and the following disclaimer in the
693  *    documentation and/or other materials provided with the distribution.
694  * 3. All advertising materials mentioning features or use of this software
695  *    must display the following acknowledgement:
696  *	This product includes software developed by the University of
697  *	California, Berkeley and its contributors.
698  * 4. Neither the name of the University nor the names of its contributors
699  *    may be used to endorse or promote products derived from this software
700  *    without specific prior written permission.
701  *
702  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
703  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
704  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
705  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
706  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
707  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
708  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
709  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
710  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
711  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
712  * SUCH DAMAGE.
713  */
714 
715 #if defined(LIBC_SCCS) && !defined(lint)
716 static char sccsid[] = "@(#)mktemp.c	8.1 (Berkeley) 6/4/93";
717 #endif /* LIBC_SCCS and not lint */
718 
719 #include <sys/types.h>
720 #include <sys/stat.h>
721 #include <fcntl.h>
722 #include <errno.h>
723 #include <stdio.h>
724 #include <ctype.h>
725 
726 static int _gettemp();
727 
728 mkstemp(path)
729 	char *path;
730 {
731 	int fd;
732 
733 	return (_gettemp(path, &fd) ? fd : -1);
734 }
735 
736 /*
737 char *
738 mktemp(path)
739 	char *path;
740 {
741 	return(_gettemp(path, (int *)NULL) ? path : (char *)NULL);
742 }
743 */
744 
745 static
746 _gettemp(path, doopen)
747 	char *path;
748 	register int *doopen;
749 {
750 	extern int errno;
751 	register char *start, *trv;
752 	struct stat sbuf;
753 	u_int pid;
754 
755 	pid = getpid();
756 	for (trv = path; *trv; ++trv);		/* extra X's get set to 0's */
757 	while (*--trv == 'X') {
758 		*trv = (pid % 10) + '0';
759 		pid /= 10;
760 	}
761 
762 	/*
763 	 * check the target directory; if you have six X's and it
764 	 * doesn't exist this runs for a *very* long time.
765 	 */
766 	for (start = trv + 1;; --trv) {
767 		if (trv <= path)
768 			break;
769 		if (*trv == '/') {
770 			*trv = '\0';
771 			if (stat(path, &sbuf))
772 				return(0);
773 			if (!S_ISDIR(sbuf.st_mode)) {
774 				errno = ENOTDIR;
775 				return(0);
776 			}
777 			*trv = '/';
778 			break;
779 		}
780 	}
781 
782 	for (;;) {
783 		if (doopen) {
784 			if ((*doopen =
785 			    open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
786 				return(1);
787 			if (errno != EEXIST)
788 				return(0);
789 		}
790 		else if (stat(path, &sbuf))
791 			return(errno == ENOENT ? 1 : 0);
792 
793 		/* tricky little algorithm for backward compatibility */
794 		for (trv = start;;) {
795 			if (!*trv)
796 				return(0);
797 			if (*trv == 'z')
798 				*trv++ = 'a';
799 			else {
800 				if (isdigit(*trv))
801 					*trv = 'a';
802 				else
803 					++*trv;
804 				break;
805 			}
806 		}
807 	}
808 	/*NOTREACHED*/
809 }
810 
811 #endif
812