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