xref: /illumos-gate/usr/src/cmd/bnu/in.uucpd.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5f48205beScasper  * Common Development and Distribution License (the "License").
6f48205beScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22f48205beScasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * 4.2BSD, 2.9BSD, or ATTSVR4 TCP/IP server for uucico
287c478bd9Sstevel@tonic-gate  * uucico's TCP channel causes this server to be run at the remote end.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include "uucp.h"
327c478bd9Sstevel@tonic-gate #include <netdb.h>
337c478bd9Sstevel@tonic-gate #ifdef BSD2_9
347c478bd9Sstevel@tonic-gate #include <sys/localopts.h>
357c478bd9Sstevel@tonic-gate #include <sys/file.h>
36462be471Sceastha #endif	/* BSD2_9 */
377c478bd9Sstevel@tonic-gate #include <signal.h>
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate #include <sys/socket.h>
407c478bd9Sstevel@tonic-gate #include <netinet/in.h>
417c478bd9Sstevel@tonic-gate #include <sys/wait.h>
427c478bd9Sstevel@tonic-gate #ifdef ATTSVTTY
437c478bd9Sstevel@tonic-gate #include <sys/termio.h>
447c478bd9Sstevel@tonic-gate #else
457c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
467c478bd9Sstevel@tonic-gate #endif
477c478bd9Sstevel@tonic-gate #include <pwd.h>
487c478bd9Sstevel@tonic-gate #ifdef ATTSVR4
497c478bd9Sstevel@tonic-gate #include <shadow.h>
507c478bd9Sstevel@tonic-gate #endif
51*2de0a7d6SDan McDonald #include <lastlog.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include <security/pam_appl.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static int uucp_conv();
567c478bd9Sstevel@tonic-gate struct pam_conv conv = {uucp_conv, NULL };
577c478bd9Sstevel@tonic-gate pam_handle_t    *pamh;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #if !defined(BSD4_2) && !defined(BSD2_9) && !defined(ATTSVR4)
607c478bd9Sstevel@tonic-gate --- You must have either BSD4_2, BSD2_9, or ATTSVR4 defined for this to work
61462be471Sceastha #endif	/* !BSD4_2 && !BSD2_9 */
627c478bd9Sstevel@tonic-gate #if defined(BSD4_2) && defined(BSD2_9)
637c478bd9Sstevel@tonic-gate --- You may not have both BSD4_2 and BSD2_9 defined for this to work
647c478bd9Sstevel@tonic-gate #endif	/* check for stupidity */
657c478bd9Sstevel@tonic-gate 
66*2de0a7d6SDan McDonald char lastlog[] = "/var/adm/lastlog";
67f48205beScasper struct	passwd nouser = {
68e5af821aScasper 	"", "nope", (uid_t)-1, (gid_t)-1, "", "", "", "", "" };
697c478bd9Sstevel@tonic-gate #ifdef ATTSVR4
707c478bd9Sstevel@tonic-gate struct	spwd noupass = { "", "nope" };
717c478bd9Sstevel@tonic-gate #endif
727c478bd9Sstevel@tonic-gate struct	sockaddr_in hisctladdr;
737c478bd9Sstevel@tonic-gate socklen_t hisaddrlen = (socklen_t)sizeof (hisctladdr);
747c478bd9Sstevel@tonic-gate struct	sockaddr_in myctladdr;
757c478bd9Sstevel@tonic-gate int nolog;		/* don't log in utmp or wtmp */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate char Username[64];
787c478bd9Sstevel@tonic-gate char Loginname[64];
797c478bd9Sstevel@tonic-gate char *nenv[] = {
807c478bd9Sstevel@tonic-gate 	Username,
817c478bd9Sstevel@tonic-gate 	Loginname,
827c478bd9Sstevel@tonic-gate 	NULL,
837c478bd9Sstevel@tonic-gate };
847c478bd9Sstevel@tonic-gate extern char **environ;
857c478bd9Sstevel@tonic-gate 
86462be471Sceastha static void doit(struct sockaddr_in *);
87462be471Sceastha static void dologout(void);
88462be471Sceastha 
89462be471Sceastha int
main(argc,argv)907c478bd9Sstevel@tonic-gate main(argc, argv)
917c478bd9Sstevel@tonic-gate int argc;
927c478bd9Sstevel@tonic-gate char **argv;
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate #ifndef BSDINETD
95462be471Sceastha 	int s, tcp_socket;
967c478bd9Sstevel@tonic-gate 	struct servent *sp;
97462be471Sceastha #endif	/* !BSDINETD */
987c478bd9Sstevel@tonic-gate 	extern int errno;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if (argc > 1 && strcmp(argv[1], "-n") == 0)
1017c478bd9Sstevel@tonic-gate 		nolog = 1;
1027c478bd9Sstevel@tonic-gate 	environ = nenv;
1037c478bd9Sstevel@tonic-gate #ifdef BSDINETD
1047c478bd9Sstevel@tonic-gate 	close(1); close(2);
1057c478bd9Sstevel@tonic-gate 	dup(0); dup(0);
1067c478bd9Sstevel@tonic-gate 	hisaddrlen = (socklen_t)sizeof (hisctladdr);
1077c478bd9Sstevel@tonic-gate 	if (getpeername(0, (struct sockaddr *)&hisctladdr, &hisaddrlen) < 0) {
1087c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: ", argv[0]);
1097c478bd9Sstevel@tonic-gate 		perror("getpeername");
1107c478bd9Sstevel@tonic-gate 		_exit(1);
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 	if (fork() == 0)
1137c478bd9Sstevel@tonic-gate 		doit(&hisctladdr);
1147c478bd9Sstevel@tonic-gate 	dologout();
1157c478bd9Sstevel@tonic-gate 	exit(1);
116462be471Sceastha #else	/* !BSDINETD */
1177c478bd9Sstevel@tonic-gate 	sp = getservbyname("uucp", "tcp");
1187c478bd9Sstevel@tonic-gate 	if (sp == NULL) {
1197c478bd9Sstevel@tonic-gate 		perror("uucpd: getservbyname");
1207c478bd9Sstevel@tonic-gate 		exit(1);
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 	if (fork())
1237c478bd9Sstevel@tonic-gate 		exit(0);
1247c478bd9Sstevel@tonic-gate #ifdef ATTSVR4
1257c478bd9Sstevel@tonic-gate 	setsid();
1267c478bd9Sstevel@tonic-gate #else
1277c478bd9Sstevel@tonic-gate 	if ((s = open("/dev/tty", 2)) >= 0) {
1287c478bd9Sstevel@tonic-gate 		ioctl(s, TIOCNOTTY, (char *)0);
1297c478bd9Sstevel@tonic-gate 		close(s);
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate #endif
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate #ifdef ATTSVR4
1347c478bd9Sstevel@tonic-gate 	memset((void *)&myctladdr, 0, sizeof (myctladdr));
1357c478bd9Sstevel@tonic-gate #else
1367c478bd9Sstevel@tonic-gate 	bzero((char *)&myctladdr, sizeof (myctladdr));
1377c478bd9Sstevel@tonic-gate #endif
1387c478bd9Sstevel@tonic-gate 	myctladdr.sin_family = AF_INET;
1397c478bd9Sstevel@tonic-gate 	myctladdr.sin_port = sp->s_port;
1407c478bd9Sstevel@tonic-gate #if defined(BSD4_2) || defined(ATTSVR4)
1417c478bd9Sstevel@tonic-gate 	tcp_socket = socket(AF_INET, SOCK_STREAM, 0);
1427c478bd9Sstevel@tonic-gate 	if (tcp_socket < 0) {
1437c478bd9Sstevel@tonic-gate 		perror("uucpd: socket");
1447c478bd9Sstevel@tonic-gate 		exit(1);
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 	if (bind(tcp_socket, (char *)&myctladdr, sizeof (myctladdr)) < 0) {
1477c478bd9Sstevel@tonic-gate 		perror("uucpd: bind");
1487c478bd9Sstevel@tonic-gate 		exit(1);
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 	listen(tcp_socket, 3);	/* at most 3 simultaneuos uucp connections */
1517c478bd9Sstevel@tonic-gate 	signal(SIGCHLD, dologout);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	for (;;) {
1547c478bd9Sstevel@tonic-gate 		s = accept(tcp_socket, &hisctladdr, &hisaddrlen);
1557c478bd9Sstevel@tonic-gate 		if (s < 0) {
1567c478bd9Sstevel@tonic-gate 			if (errno == EINTR)
1577c478bd9Sstevel@tonic-gate 				continue;
1587c478bd9Sstevel@tonic-gate 			perror("uucpd: accept");
1597c478bd9Sstevel@tonic-gate 			exit(1);
1607c478bd9Sstevel@tonic-gate 		}
1617c478bd9Sstevel@tonic-gate 		if (fork() == 0) {
1627c478bd9Sstevel@tonic-gate 			close(0); close(1); close(2);
1637c478bd9Sstevel@tonic-gate 			dup(s); dup(s); dup(s);
1647c478bd9Sstevel@tonic-gate 			close(tcp_socket); close(s);
1657c478bd9Sstevel@tonic-gate 			doit(&hisctladdr);
1667c478bd9Sstevel@tonic-gate 			exit(1);
1677c478bd9Sstevel@tonic-gate 		}
1687c478bd9Sstevel@tonic-gate 		close(s);
1697c478bd9Sstevel@tonic-gate 	}
170462be471Sceastha #endif	/* BSD4_2 */
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate #ifdef BSD2_9
1737c478bd9Sstevel@tonic-gate 	for (;;) {
1747c478bd9Sstevel@tonic-gate 		signal(SIGCHLD, dologout);
1757c478bd9Sstevel@tonic-gate 		s = socket(SOCK_STREAM, 0,  &myctladdr,
1767c478bd9Sstevel@tonic-gate 			SO_ACCEPTCONN|SO_KEEPALIVE);
1777c478bd9Sstevel@tonic-gate 		if (s < 0) {
1787c478bd9Sstevel@tonic-gate 			perror("uucpd: socket");
1797c478bd9Sstevel@tonic-gate 			exit(1);
1807c478bd9Sstevel@tonic-gate 		}
1817c478bd9Sstevel@tonic-gate 		if (accept(s, &hisctladdr) < 0) {
1827c478bd9Sstevel@tonic-gate 			if (errno == EINTR) {
1837c478bd9Sstevel@tonic-gate 				close(s);
1847c478bd9Sstevel@tonic-gate 				continue;
1857c478bd9Sstevel@tonic-gate 			}
1867c478bd9Sstevel@tonic-gate 			perror("uucpd: accept");
1877c478bd9Sstevel@tonic-gate 			exit(1);
1887c478bd9Sstevel@tonic-gate 		}
1897c478bd9Sstevel@tonic-gate 		if (fork() == 0) {
1907c478bd9Sstevel@tonic-gate 			close(0); close(1); close(2);
1917c478bd9Sstevel@tonic-gate 			dup(s); dup(s); dup(s);
1927c478bd9Sstevel@tonic-gate 			close(s);
1937c478bd9Sstevel@tonic-gate 			doit(&hisctladdr);
1947c478bd9Sstevel@tonic-gate 			exit(1);
1957c478bd9Sstevel@tonic-gate 		}
1967c478bd9Sstevel@tonic-gate 	}
197462be471Sceastha #endif	/* BSD2_9 */
198462be471Sceastha #endif	/* !BSDINETD */
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate 
203462be471Sceastha static void
doit(sinp)2047c478bd9Sstevel@tonic-gate doit(sinp)
2057c478bd9Sstevel@tonic-gate struct sockaddr_in *sinp;
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	char user[64], passwd[64];
2087c478bd9Sstevel@tonic-gate 	struct passwd *pw, *getpwnam();
2097c478bd9Sstevel@tonic-gate 	int error;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	alarm(60);
2127c478bd9Sstevel@tonic-gate 	printf("login: "); fflush(stdout);
2137c478bd9Sstevel@tonic-gate 	if (readline(user, sizeof (user)) < 0) {
2147c478bd9Sstevel@tonic-gate 		fprintf(stderr, "user read\n");
2157c478bd9Sstevel@tonic-gate 		return;
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	/*
2197c478bd9Sstevel@tonic-gate 	 * Call pam_start to initiate a PAM authentication operation
2207c478bd9Sstevel@tonic-gate 	 */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	if ((pam_start("uucp", user, &conv, &pamh)) != PAM_SUCCESS)
2237c478bd9Sstevel@tonic-gate 		return;
2247c478bd9Sstevel@tonic-gate 	if ((pam_set_item(pamh, PAM_TTY, ttyname(0))) != PAM_SUCCESS)
2257c478bd9Sstevel@tonic-gate 		return;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	if (pam_authenticate(pamh, PAM_SILENT) != PAM_SUCCESS) {
2287c478bd9Sstevel@tonic-gate 		/* force a delay if passwd bad */
2297c478bd9Sstevel@tonic-gate 		sleep(4);
2307c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Login incorrect.");
2317c478bd9Sstevel@tonic-gate 		pam_end(pamh, PAM_ABORT);
2327c478bd9Sstevel@tonic-gate 		return;
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	if ((error = pam_acct_mgmt(pamh, PAM_SILENT)) != PAM_SUCCESS) {
2367c478bd9Sstevel@tonic-gate 		switch (error) {
2377c478bd9Sstevel@tonic-gate 		case PAM_NEW_AUTHTOK_REQD:
2387c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Password Expired.");
2397c478bd9Sstevel@tonic-gate 			break;
2407c478bd9Sstevel@tonic-gate 		case PAM_PERM_DENIED:
2417c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Account Expired.");
2427c478bd9Sstevel@tonic-gate 			break;
2437c478bd9Sstevel@tonic-gate 		case PAM_AUTHTOK_EXPIRED:
2447c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Password Expired.");
2457c478bd9Sstevel@tonic-gate 			break;
2467c478bd9Sstevel@tonic-gate 		default:
2477c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Login incorrect.");
2487c478bd9Sstevel@tonic-gate 			break;
2497c478bd9Sstevel@tonic-gate 		}
2507c478bd9Sstevel@tonic-gate 		pam_end(pamh, PAM_ABORT);
2517c478bd9Sstevel@tonic-gate 		return;
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	if ((pw = getpwnam(user)) == NULL || strcmp(pw->pw_shell, UUCICO)) {
2557c478bd9Sstevel@tonic-gate 		/* force a delay if user bad */
2567c478bd9Sstevel@tonic-gate 		sleep(4);
2577c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Login incorrect.");
2587c478bd9Sstevel@tonic-gate 		pam_end(pamh, PAM_USER_UNKNOWN);
2597c478bd9Sstevel@tonic-gate 		return;
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	alarm(0);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	sprintf(Username, "USER=%s", user);
2657c478bd9Sstevel@tonic-gate 	sprintf(Loginname, "LOGNAME=%s", user);
2667c478bd9Sstevel@tonic-gate 	if (!nolog)
2677c478bd9Sstevel@tonic-gate 		if (dologin(pw, sinp)) {
2687c478bd9Sstevel@tonic-gate 			pam_end(pamh, PAM_ABORT);
2697c478bd9Sstevel@tonic-gate 			_exit(1);
2707c478bd9Sstevel@tonic-gate 		}
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	/* set the real (and effective) GID */
2737c478bd9Sstevel@tonic-gate 	if (setgid(pw->pw_gid) == -1) {
2747c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Login incorrect.");
2757c478bd9Sstevel@tonic-gate 		pam_end(pamh, PAM_PERM_DENIED);
2767c478bd9Sstevel@tonic-gate 		return;
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	/*
2807c478bd9Sstevel@tonic-gate 	 * Initialize the supplementary group access list.
2817c478bd9Sstevel@tonic-gate 	 */
2827c478bd9Sstevel@tonic-gate 	if (initgroups(user, pw->pw_gid) == -1) {
2837c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Login incorrect.");
2847c478bd9Sstevel@tonic-gate 		pam_end(pamh, PAM_PERM_DENIED);
2857c478bd9Sstevel@tonic-gate 		return;
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	if (pam_setcred(pamh, PAM_ESTABLISH_CRED) != PAM_SUCCESS) {
2897c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Login incorrect.");
2907c478bd9Sstevel@tonic-gate 		pam_end(pamh, PAM_CRED_INSUFFICIENT);
2917c478bd9Sstevel@tonic-gate 		return;
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	/* set the real (and effective) UID */
2957c478bd9Sstevel@tonic-gate 	if (setuid(pw->pw_uid) == -1) {
2967c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Login incorrect.");
2977c478bd9Sstevel@tonic-gate 		pam_end(pamh, PAM_CRED_ERR);
2987c478bd9Sstevel@tonic-gate 		return;
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	chdir(pw->pw_dir);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	pam_end(pamh, PAM_SUCCESS);
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate #if defined(BSD4_2) || defined(ATTSVR4)
3067c478bd9Sstevel@tonic-gate 	execl(UUCICO, "uucico", "-u", user, (char *)0);
307462be471Sceastha #endif	/* BSD4_2 */
3087c478bd9Sstevel@tonic-gate #ifdef BSD2_9
3097c478bd9Sstevel@tonic-gate 	sprintf(passwd, "-h%s", inet_ntoa(sinp->sin_addr));
3107c478bd9Sstevel@tonic-gate 	execl(UUCICO, "uucico", passwd, (char *)0);
311462be471Sceastha #endif	/* BSD2_9 */
3127c478bd9Sstevel@tonic-gate 	perror("uucico server: execl");
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate 
315462be471Sceastha int
readline(p,n)3167c478bd9Sstevel@tonic-gate readline(p, n)
317462be471Sceastha char *p;
318462be471Sceastha int n;
3197c478bd9Sstevel@tonic-gate {
3207c478bd9Sstevel@tonic-gate 	char c;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	while (n-- > 0) {
3237c478bd9Sstevel@tonic-gate 		if (read(0, &c, 1) <= 0)
3247c478bd9Sstevel@tonic-gate 			return (-1);
3257c478bd9Sstevel@tonic-gate 		c &= 0177;
3267c478bd9Sstevel@tonic-gate 		if (c == '\n' || c == '\r') {
3277c478bd9Sstevel@tonic-gate 			*p = '\0';
3287c478bd9Sstevel@tonic-gate 			return (0);
3297c478bd9Sstevel@tonic-gate 		}
3307c478bd9Sstevel@tonic-gate 		*p++ = c;
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate 	return (-1);
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate #ifdef ATTSVR4
3367c478bd9Sstevel@tonic-gate #include <sac.h>	/* for SC_WILDC */
3377c478bd9Sstevel@tonic-gate #include <utmpx.h>
338462be471Sceastha #else	/* !ATTSVR4 */
3397c478bd9Sstevel@tonic-gate #include <utmp.h>
340462be471Sceastha #endif	/* !ATTSVR4 */
3417c478bd9Sstevel@tonic-gate #if defined(BSD4_2) || defined(ATTSVR4)
3427c478bd9Sstevel@tonic-gate #include <fcntl.h>
343462be471Sceastha #endif	/* BSD4_2 */
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate #ifdef BSD2_9
3467c478bd9Sstevel@tonic-gate #define	O_APPEND	0 /* kludge */
3477c478bd9Sstevel@tonic-gate #define	wait3(a, b, c)	wait2(a, b)
348462be471Sceastha #endif	/* BSD2_9 */
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate #define	SCPYN(a, b)	strncpy(a, b, sizeof (a))
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate #ifdef ATTSVR4
3537c478bd9Sstevel@tonic-gate struct	utmpx utmp;
354462be471Sceastha #else	/* !ATTSVR4 */
3557c478bd9Sstevel@tonic-gate struct	utmp utmp;
356462be471Sceastha #endif	/* !ATTSVR4 */
3577c478bd9Sstevel@tonic-gate 
358462be471Sceastha static void
dologout(void)359462be471Sceastha dologout(void)
3607c478bd9Sstevel@tonic-gate {
3617c478bd9Sstevel@tonic-gate #ifdef ATTSVR4
3627c478bd9Sstevel@tonic-gate 	int status;
363462be471Sceastha #else	/* !ATTSVR4 */
3647c478bd9Sstevel@tonic-gate 	union wait status;
365462be471Sceastha #endif	/* !ATSVR4 */
3667c478bd9Sstevel@tonic-gate 	int pid, wtmp;
3677c478bd9Sstevel@tonic-gate 	/* the following 2 variables are needed for utmp mgmt */
3687c478bd9Sstevel@tonic-gate 	struct utmpx	ut;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate #ifdef BSDINETD
3717c478bd9Sstevel@tonic-gate 	while ((pid = wait(&status)) > 0) {
372462be471Sceastha #else	/* !BSDINETD */
3737c478bd9Sstevel@tonic-gate 	while ((pid = wait3(&status, WNOHANG, 0)) > 0) {
374462be471Sceastha #endif	/* !BSDINETD */
3757c478bd9Sstevel@tonic-gate 		if (nolog)
3767c478bd9Sstevel@tonic-gate 			continue;
3777c478bd9Sstevel@tonic-gate #ifdef ATTSVR4
3787c478bd9Sstevel@tonic-gate 		/* clear out any residue from utmpx buffer */
3797c478bd9Sstevel@tonic-gate 		(void) memset((char *)&ut, 0, sizeof (ut));
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 		SCPYN(utmp.ut_user, "");
3827c478bd9Sstevel@tonic-gate 		ut.ut_id[0] = 'u';
3837c478bd9Sstevel@tonic-gate 		ut.ut_id[1] = 'u';
3847c478bd9Sstevel@tonic-gate 		ut.ut_id[2] = SC_WILDC;
3857c478bd9Sstevel@tonic-gate 		ut.ut_id[3] = SC_WILDC;
3867c478bd9Sstevel@tonic-gate 		sprintf(ut.ut_line, "uucp%.4d", pid);
3877c478bd9Sstevel@tonic-gate 		ut.ut_pid  = getpid();
3887c478bd9Sstevel@tonic-gate 		ut.ut_type = DEAD_PROCESS;
3897c478bd9Sstevel@tonic-gate 		ut.ut_exit.e_termination = status & 0xFF;
3907c478bd9Sstevel@tonic-gate 		ut.ut_exit.e_exit = WEXITSTATUS(status);
3917c478bd9Sstevel@tonic-gate 		SCPYN(ut.ut_host, "");
3927c478bd9Sstevel@tonic-gate 		ut.ut_syslen = 1;
3937c478bd9Sstevel@tonic-gate 		(void) gettimeofday(&ut.ut_tv, NULL);
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 		/*
3967c478bd9Sstevel@tonic-gate 		 * XXX: UUCPD does not do any pam session management.
3977c478bd9Sstevel@tonic-gate 		 *	There is no way for the parent process to close
3987c478bd9Sstevel@tonic-gate 		 *	the pam session after a child has exited.
3997c478bd9Sstevel@tonic-gate 		 */
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 		updwtmpx(WTMPX_FILE, &ut);
402462be471Sceastha #else	/* !ATTSVR4 */
4037c478bd9Sstevel@tonic-gate 		wtmp = open("/usr/adm/wtmp", O_WRONLY|O_APPEND);
4047c478bd9Sstevel@tonic-gate 		if (wtmp >= 0) {
4057c478bd9Sstevel@tonic-gate 			sprintf(utmp.ut_line, "uucp%.4d", pid);
4067c478bd9Sstevel@tonic-gate 			SCPYN(utmp.ut_name, "");
4077c478bd9Sstevel@tonic-gate 			SCPYN(utmp.ut_host, "");
4087c478bd9Sstevel@tonic-gate 			(void) time(&utmp.ut_time);
4097c478bd9Sstevel@tonic-gate #ifdef BSD2_9
4107c478bd9Sstevel@tonic-gate 			(void) lseek(wtmp, 0L, 2);
411462be471Sceastha #endif	/* BSD2_9 */
4127c478bd9Sstevel@tonic-gate 			(void) write(wtmp, (char *)&utmp, sizeof (utmp));
4137c478bd9Sstevel@tonic-gate 			(void) close(wtmp);
4147c478bd9Sstevel@tonic-gate 		}
415462be471Sceastha #endif	/* !ATTSVR4 */
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate /*
4207c478bd9Sstevel@tonic-gate  * Record login in wtmp file.
4217c478bd9Sstevel@tonic-gate  */
422462be471Sceastha int
4237c478bd9Sstevel@tonic-gate dologin(pw, sin)
4247c478bd9Sstevel@tonic-gate struct passwd *pw;
4257c478bd9Sstevel@tonic-gate struct sockaddr_in *sin;
4267c478bd9Sstevel@tonic-gate {
4277c478bd9Sstevel@tonic-gate 	char line[32];
4287c478bd9Sstevel@tonic-gate 	char remotehost[32];
4297c478bd9Sstevel@tonic-gate 	int wtmp;
4307c478bd9Sstevel@tonic-gate 	struct hostent *hp = gethostbyaddr((const char *)&sin->sin_addr,
4317c478bd9Sstevel@tonic-gate 		sizeof (struct in_addr), AF_INET);
4327c478bd9Sstevel@tonic-gate 	struct utmpx	ut;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	if (hp) {
4357c478bd9Sstevel@tonic-gate 		strncpy(remotehost, hp->h_name, sizeof (remotehost));
4367c478bd9Sstevel@tonic-gate 		endhostent();
4377c478bd9Sstevel@tonic-gate 	} else
4387c478bd9Sstevel@tonic-gate 		strncpy(remotehost, (char *)inet_ntoa(sin->sin_addr),
4397c478bd9Sstevel@tonic-gate 		    sizeof (remotehost));
4407c478bd9Sstevel@tonic-gate #ifdef ATTSVR4
4417c478bd9Sstevel@tonic-gate 	/* clear wtmpx entry */
4427c478bd9Sstevel@tonic-gate 	(void) memset((void *)&ut, 0, sizeof (ut));
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	SCPYN(ut.ut_user, pw->pw_name);
4457c478bd9Sstevel@tonic-gate 	ut.ut_id[0] = 'u';
4467c478bd9Sstevel@tonic-gate 	ut.ut_id[1] = 'u';
4477c478bd9Sstevel@tonic-gate 	ut.ut_id[2] = SC_WILDC;
4487c478bd9Sstevel@tonic-gate 	ut.ut_id[3] = SC_WILDC;
4497c478bd9Sstevel@tonic-gate 	/* hack, but must be unique and no tty line */
4507c478bd9Sstevel@tonic-gate 	sprintf(line, "uucp%.4d", getpid());
4517c478bd9Sstevel@tonic-gate 	SCPYN(ut.ut_line, line);
4527c478bd9Sstevel@tonic-gate 	ut.ut_pid = getpid();
4537c478bd9Sstevel@tonic-gate 	ut.ut_type = USER_PROCESS;
4547c478bd9Sstevel@tonic-gate 	ut.ut_exit.e_termination = 0;
4557c478bd9Sstevel@tonic-gate 	ut.ut_exit.e_exit = 0;
4567c478bd9Sstevel@tonic-gate 	SCPYN(ut.ut_host, remotehost);
4577c478bd9Sstevel@tonic-gate 	ut.ut_syslen = strlen(remotehost) + 1;
4587c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&ut.ut_tv, 0);
4597c478bd9Sstevel@tonic-gate 	updwtmpx(WTMPX_FILE, &ut);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	/*
4627c478bd9Sstevel@tonic-gate 	 * XXX:
4637c478bd9Sstevel@tonic-gate 	 * 	We no longer do session management in uucpd because
4647c478bd9Sstevel@tonic-gate 	 *	there is no way to do the "pam_close_session()".
4657c478bd9Sstevel@tonic-gate 	 *
4667c478bd9Sstevel@tonic-gate 	 *	Processes like "init" can do a pam_close_session()
467da6c28aaSamw 	 *	because they can use the utmp entry to retrieve
4687c478bd9Sstevel@tonic-gate 	 *	the proper username, ttyname, etc. --
4697c478bd9Sstevel@tonic-gate 	 *	uucpd only writes to the wtmp file.
4707c478bd9Sstevel@tonic-gate 	 *
4717c478bd9Sstevel@tonic-gate 	 *	ftpd (which also only writes to the wtmp file)
4727c478bd9Sstevel@tonic-gate 	 *	can do a pam_close_session() because it doesn't fork().
4737c478bd9Sstevel@tonic-gate 	 *
4747c478bd9Sstevel@tonic-gate 	 *	if (pam_set_item(pamh, PAM_RHOST, remotehost) != PAM_SUCCESS)
4757c478bd9Sstevel@tonic-gate 	 *		return (1);
4767c478bd9Sstevel@tonic-gate 	 *	if (pam_set_item(pamh, PAM_TTY, line) != PAM_SUCCESS)
4777c478bd9Sstevel@tonic-gate 	 *		return (1);
4787c478bd9Sstevel@tonic-gate 	 *	if (pam_open_session(pamh, 0) != PAM_SUCCESS) {
4797c478bd9Sstevel@tonic-gate 	 *		return (1);
4807c478bd9Sstevel@tonic-gate 	 *	}
4817c478bd9Sstevel@tonic-gate 	 */
4827c478bd9Sstevel@tonic-gate 
483462be471Sceastha #else	/* !ATTSVR4 */
4847c478bd9Sstevel@tonic-gate 	wtmp = open("/usr/adm/wtmp", O_WRONLY|O_APPEND);
4857c478bd9Sstevel@tonic-gate 	if (wtmp >= 0) {
4867c478bd9Sstevel@tonic-gate 		/* hack, but must be unique and no tty line */
4877c478bd9Sstevel@tonic-gate 		sprintf(line, "uucp%.4d", getpid());
4887c478bd9Sstevel@tonic-gate 		SCPYN(utmp.ut_line, line);
4897c478bd9Sstevel@tonic-gate 		SCPYN(utmp.ut_name, pw->pw_name);
4907c478bd9Sstevel@tonic-gate 		SCPYN(utmp.ut_host, remotehost);
4917c478bd9Sstevel@tonic-gate 		time(&utmp.ut_time);
4927c478bd9Sstevel@tonic-gate #ifdef BSD2_9
4937c478bd9Sstevel@tonic-gate 		(void) lseek(wtmp, 0L, 2);
494462be471Sceastha #endif	/* BSD2_9 */
4957c478bd9Sstevel@tonic-gate 		(void) write(wtmp, (char *)&utmp, sizeof (utmp));
4967c478bd9Sstevel@tonic-gate 		(void) close(wtmp);
4977c478bd9Sstevel@tonic-gate 	}
498462be471Sceastha #endif	/* !ATTSVR4 */
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	return (0);
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate /*
5047c478bd9Sstevel@tonic-gate  * uucp_conv	- This is the conv (conversation) function called from
5057c478bd9Sstevel@tonic-gate  *		a PAM authentication module to print error messages
5067c478bd9Sstevel@tonic-gate  *		or garner information from the user.
5077c478bd9Sstevel@tonic-gate  */
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate static int
5107c478bd9Sstevel@tonic-gate uucp_conv(num_msg, msg, response, appdata_ptr)
5117c478bd9Sstevel@tonic-gate 	int num_msg;
5127c478bd9Sstevel@tonic-gate 	struct pam_message **msg;
5137c478bd9Sstevel@tonic-gate 	struct pam_response **response;
5147c478bd9Sstevel@tonic-gate 	void *appdata_ptr;
5157c478bd9Sstevel@tonic-gate {
5167c478bd9Sstevel@tonic-gate 	struct pam_message	*m;
5177c478bd9Sstevel@tonic-gate 	struct pam_response	*r;
5187c478bd9Sstevel@tonic-gate 	char			*temp;
5197c478bd9Sstevel@tonic-gate 	static char		passwd[64];
5207c478bd9Sstevel@tonic-gate 	int			k, i;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	if (num_msg <= 0)
5237c478bd9Sstevel@tonic-gate 		return (PAM_CONV_ERR);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	*response = (struct pam_response *)calloc(num_msg,
5267c478bd9Sstevel@tonic-gate 			sizeof (struct pam_response));
5277c478bd9Sstevel@tonic-gate 	if (*response == NULL)
5287c478bd9Sstevel@tonic-gate 		return (PAM_BUF_ERR);
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	k = num_msg;
5317c478bd9Sstevel@tonic-gate 	m = *msg;
5327c478bd9Sstevel@tonic-gate 	r = *response;
5337c478bd9Sstevel@tonic-gate 	while (k--) {
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 		switch (m->msg_style) {
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 		case PAM_PROMPT_ECHO_OFF:
5387c478bd9Sstevel@tonic-gate 			/*
5397c478bd9Sstevel@tonic-gate 			 * we do this instead of using passed in message
5407c478bd9Sstevel@tonic-gate 			 * to prevent possible breakage of uucp protocol.
5417c478bd9Sstevel@tonic-gate 			 */
5427c478bd9Sstevel@tonic-gate 			printf("Password: "); fflush(stdout);
5437c478bd9Sstevel@tonic-gate 			if (readline(passwd, sizeof (passwd)) < 0) {
5447c478bd9Sstevel@tonic-gate 				fprintf(stderr, "passwd read\n");
5457c478bd9Sstevel@tonic-gate 				return (PAM_SUCCESS);
5467c478bd9Sstevel@tonic-gate 			}
5477c478bd9Sstevel@tonic-gate 			temp = passwd;
5487c478bd9Sstevel@tonic-gate 			if (temp != NULL) {
5497c478bd9Sstevel@tonic-gate 				r->resp = strdup(temp);
5507c478bd9Sstevel@tonic-gate 				if (r->resp == NULL) {
5517c478bd9Sstevel@tonic-gate 					/* free responses */
5527c478bd9Sstevel@tonic-gate 					r = *response;
5537c478bd9Sstevel@tonic-gate 					for (i = 0; i < num_msg; i++, r++) {
5547c478bd9Sstevel@tonic-gate 						if (r->resp)
5557c478bd9Sstevel@tonic-gate 							free(r->resp);
5567c478bd9Sstevel@tonic-gate 					}
5577c478bd9Sstevel@tonic-gate 					free(*response);
5587c478bd9Sstevel@tonic-gate 					*response = NULL;
5597c478bd9Sstevel@tonic-gate 					return (PAM_BUF_ERR);
5607c478bd9Sstevel@tonic-gate 				}
5617c478bd9Sstevel@tonic-gate 			}
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 			m++;
5647c478bd9Sstevel@tonic-gate 			r++;
5657c478bd9Sstevel@tonic-gate 			break;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 		case PAM_PROMPT_ECHO_ON:
5687c478bd9Sstevel@tonic-gate 			if (m->msg != NULL) {
5697c478bd9Sstevel@tonic-gate 				fputs(m->msg, stdout);
5707c478bd9Sstevel@tonic-gate 				fflush(stdout);
5717c478bd9Sstevel@tonic-gate 			}
5727c478bd9Sstevel@tonic-gate 			r->resp = (char *)malloc(PAM_MAX_RESP_SIZE);
5737c478bd9Sstevel@tonic-gate 			if (r->resp == NULL) {
5747c478bd9Sstevel@tonic-gate 				/* free the response */
5757c478bd9Sstevel@tonic-gate 				r = *response;
5767c478bd9Sstevel@tonic-gate 				for (i = 0; i < num_msg; i++, r++) {
5777c478bd9Sstevel@tonic-gate 					if (r->resp)
5787c478bd9Sstevel@tonic-gate 						free(r->resp);
5797c478bd9Sstevel@tonic-gate 				}
5807c478bd9Sstevel@tonic-gate 				free(*response);
5817c478bd9Sstevel@tonic-gate 				*response = NULL;
5827c478bd9Sstevel@tonic-gate 				return (PAM_BUF_ERR);
5837c478bd9Sstevel@tonic-gate 			}
5847c478bd9Sstevel@tonic-gate 			(void) fgets(r->resp, PAM_MAX_RESP_SIZE, stdin);
5857c478bd9Sstevel@tonic-gate 			m++;
5867c478bd9Sstevel@tonic-gate 			r++;
5877c478bd9Sstevel@tonic-gate 			break;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		case PAM_ERROR_MSG:
5907c478bd9Sstevel@tonic-gate 			if (m->msg != NULL) {
5917c478bd9Sstevel@tonic-gate 				fputs(m->msg, stderr);
5927c478bd9Sstevel@tonic-gate 				fputs("\n", stderr);
5937c478bd9Sstevel@tonic-gate 			}
5947c478bd9Sstevel@tonic-gate 			m++;
5957c478bd9Sstevel@tonic-gate 			r++;
5967c478bd9Sstevel@tonic-gate 			break;
5977c478bd9Sstevel@tonic-gate 		case PAM_TEXT_INFO:
5987c478bd9Sstevel@tonic-gate 			if (m->msg != NULL) {
5997c478bd9Sstevel@tonic-gate 				fputs(m->msg, stdout);
6007c478bd9Sstevel@tonic-gate 				fputs("\n", stdout);
6017c478bd9Sstevel@tonic-gate 			}
6027c478bd9Sstevel@tonic-gate 			m++;
6037c478bd9Sstevel@tonic-gate 			r++;
6047c478bd9Sstevel@tonic-gate 			break;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 		default:
6077c478bd9Sstevel@tonic-gate 			break;
6087c478bd9Sstevel@tonic-gate 		}
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate 	return (PAM_SUCCESS);
6117c478bd9Sstevel@tonic-gate }
612