1*308c65aaSad /*	$NetBSD: yppasswdd_mkpw.c,v 1.7 2000/08/03 08:22:34 ad Exp $	*/
2d3e1a366Sthorpej 
3d3e1a366Sthorpej /*
4d3e1a366Sthorpej  * Copyright (c) 1996 Jason R. Thorpe <thorpej@NetBSD.ORG>
5d3e1a366Sthorpej  * All rights reserved.
6d3e1a366Sthorpej  *
7d3e1a366Sthorpej  * Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se>
8d3e1a366Sthorpej  * All rights reserved.
9d3e1a366Sthorpej  *
10d3e1a366Sthorpej  * Redistribution and use in source and binary forms, with or without
11d3e1a366Sthorpej  * modification, are permitted provided that the following conditions
12d3e1a366Sthorpej  * are met:
13d3e1a366Sthorpej  * 1. Redistributions of source code must retain the above copyright
14d3e1a366Sthorpej  *    notice, this list of conditions and the following disclaimer.
15d3e1a366Sthorpej  * 2. Redistributions in binary form must reproduce the above copyright
16d3e1a366Sthorpej  *    notice, this list of conditions and the following disclaimer in the
17d3e1a366Sthorpej  *    documentation and/or other materials provided with the distribution.
18d3e1a366Sthorpej  * 3. All advertising materials mentioning features or use of this software
19d3e1a366Sthorpej  *    must display the following acknowledgement:
20d3e1a366Sthorpej  *	This product includes software developed by Mats O Jansson
21d3e1a366Sthorpej  * 4. The name of the author may not be used to endorse or promote products
22d3e1a366Sthorpej  *    derived from this software without specific prior written permission.
23d3e1a366Sthorpej  *
24d3e1a366Sthorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
25d3e1a366Sthorpej  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26d3e1a366Sthorpej  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27d3e1a366Sthorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
28d3e1a366Sthorpej  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29d3e1a366Sthorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30d3e1a366Sthorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31d3e1a366Sthorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32d3e1a366Sthorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33d3e1a366Sthorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34d3e1a366Sthorpej  * SUCH DAMAGE.
35d3e1a366Sthorpej  */
36d3e1a366Sthorpej 
37*308c65aaSad #include <sys/cdefs.h>
38*308c65aaSad #ifndef lint
39*308c65aaSad __RCSID("$NetBSD: yppasswdd_mkpw.c,v 1.7 2000/08/03 08:22:34 ad Exp $");
40*308c65aaSad #endif /* not lint */
41*308c65aaSad 
42d3e1a366Sthorpej #include <sys/types.h>
43d3e1a366Sthorpej #include <sys/stat.h>
44d3e1a366Sthorpej #include <sys/time.h>
45d3e1a366Sthorpej #include <sys/resource.h>
46d3e1a366Sthorpej #include <sys/wait.h>
47*308c65aaSad 
48a5e58d3dSthorpej #include <err.h>
49d3e1a366Sthorpej #include <fcntl.h>
50d3e1a366Sthorpej #include <stdio.h>
51d3e1a366Sthorpej #include <pwd.h>
52d3e1a366Sthorpej #include <signal.h>
53d3e1a366Sthorpej #include <stdlib.h>
543ab02a87Smatt #include <string.h>
55d3e1a366Sthorpej #include <unistd.h>
56d3e1a366Sthorpej #include <util.h>
57*308c65aaSad #include <limits.h>
58d3e1a366Sthorpej 
59d3e1a366Sthorpej #include <rpc/rpc.h>
60d3e1a366Sthorpej #include <rpc/xdr.h>
61d3e1a366Sthorpej #include <rpcsvc/yppasswd.h>
62d3e1a366Sthorpej 
63*308c65aaSad #include "extern.h"
64d3e1a366Sthorpej 
65d3e1a366Sthorpej int	handling_request;		/* simple mutex */
66d3e1a366Sthorpej 
67d3e1a366Sthorpej void
68*308c65aaSad make_passwd(yppasswd *argp, struct svc_req *rqstp, SVCXPRT *transp)
69d3e1a366Sthorpej {
70d3e1a366Sthorpej 	struct passwd *pw;
71a5e58d3dSthorpej 	int pfd, tfd;
72d3e1a366Sthorpej 
73d3e1a366Sthorpej #define REPLY(val)	do { \
74d3e1a366Sthorpej 		int res = (val); \
75d3e1a366Sthorpej 		if (!svc_sendreply(transp, xdr_int, (caddr_t)&res)) \
76d3e1a366Sthorpej 			svcerr_systemerr(transp); \
77d3e1a366Sthorpej 	} while (0)
78d3e1a366Sthorpej 
79d3e1a366Sthorpej #define RETURN(val)	do { \
80d3e1a366Sthorpej 		REPLY((val)); \
81d3e1a366Sthorpej 		handling_request = 0; \
82d3e1a366Sthorpej 		return; \
83d3e1a366Sthorpej 	} while (0)
84d3e1a366Sthorpej 
85d3e1a366Sthorpej 	if (handling_request) {
86d3e1a366Sthorpej 		warnx("already handling request; try again later");
87d3e1a366Sthorpej 		REPLY(1);
88d3e1a366Sthorpej 		return;
89d3e1a366Sthorpej 	}
90d3e1a366Sthorpej 	handling_request = 1;
91d3e1a366Sthorpej 
92d3e1a366Sthorpej 	pw = getpwnam(argp->newpw.pw_name);
93d3e1a366Sthorpej 	if (!pw)
94d3e1a366Sthorpej 		RETURN(1);
95d3e1a366Sthorpej 
96f12b956eSis 	if (*pw->pw_passwd &&
97f12b956eSis 	    strcmp(crypt(argp->oldpass, pw->pw_passwd), pw->pw_passwd) != 0)
98d3e1a366Sthorpej 		RETURN(1);
99d3e1a366Sthorpej 
100d3e1a366Sthorpej 	pw_init();
101d3e1a366Sthorpej 	tfd = pw_lock(0);
102d3e1a366Sthorpej 	if (tfd < 0) {
103d3e1a366Sthorpej 		warnx("the passwd file is busy.");
104d3e1a366Sthorpej 		RETURN(1);
105d3e1a366Sthorpej 	}
106d3e1a366Sthorpej 	pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
107d3e1a366Sthorpej 	if (pfd < 0) {
108d3e1a366Sthorpej 		pw_abort();
109bbef2fbaSitojun 		warnx("%s", _PATH_MASTERPASSWD);
110d3e1a366Sthorpej 		RETURN(1);
111d3e1a366Sthorpej 	}
112d3e1a366Sthorpej 
113d3e1a366Sthorpej 	/*
114d3e1a366Sthorpej 	 * Get the new password.  Reset passwd change time to zero; when
115d3e1a366Sthorpej 	 * classes are implemented, go and get the "offset" value for this
116d3e1a366Sthorpej 	 * class and reset the timer.
117d3e1a366Sthorpej 	 */
118d3e1a366Sthorpej 	if (!nopw) {
119d3e1a366Sthorpej 		pw->pw_passwd = argp->newpw.pw_passwd;
120d3e1a366Sthorpej 		pw->pw_change = 0;
121d3e1a366Sthorpej 	}
122d3e1a366Sthorpej 	if (!nogecos)
123d3e1a366Sthorpej 		pw->pw_gecos = argp->newpw.pw_gecos;
124d3e1a366Sthorpej 	if (!noshell)
125d3e1a366Sthorpej 		pw->pw_shell = argp->newpw.pw_shell;
126d3e1a366Sthorpej 
127ffdae814Sphil 	pw_copy(pfd, tfd, pw, NULL);
128d3e1a366Sthorpej 
129d3e1a366Sthorpej 	if (pw_mkdb() < 0) {
130d3e1a366Sthorpej 		warnx("pw_mkdb failed");
131d3e1a366Sthorpej 		pw_abort();
132d3e1a366Sthorpej 		RETURN(1);
133d3e1a366Sthorpej 	}
134d3e1a366Sthorpej 
135d3e1a366Sthorpej 	/* XXX RESTORE SIGNAL STATE? XXX */
136d3e1a366Sthorpej 
137d3e1a366Sthorpej 	/* Notify caller we succeeded. */
138d3e1a366Sthorpej 	REPLY(0);
139d3e1a366Sthorpej 
140d3e1a366Sthorpej 	/* Update the YP maps. */
141d3e1a366Sthorpej 	if (chdir("/var/yp"))
142*308c65aaSad 		err(EXIT_FAILURE, "/var/yp");
143d3e1a366Sthorpej 	(void) umask(022);
144d3e1a366Sthorpej 	(void) system(make_arg);
145d3e1a366Sthorpej 
146d3e1a366Sthorpej 	handling_request = 0;
147d3e1a366Sthorpej }
148