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