1*3cd9f110Splunky /* $NetBSD: yppasswdd_mkpw.c,v 1.18 2011/08/30 17:06:22 plunky Exp $ */
2d3e1a366Sthorpej
3d3e1a366Sthorpej /*
4cacf2d0fSgrant * 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 *
19d3e1a366Sthorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20d3e1a366Sthorpej * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21d3e1a366Sthorpej * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22d3e1a366Sthorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23d3e1a366Sthorpej * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24d3e1a366Sthorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25d3e1a366Sthorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26d3e1a366Sthorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27d3e1a366Sthorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28d3e1a366Sthorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29d3e1a366Sthorpej * SUCH DAMAGE.
30d3e1a366Sthorpej */
31d3e1a366Sthorpej
32308c65aaSad #include <sys/cdefs.h>
33308c65aaSad #ifndef lint
34*3cd9f110Splunky __RCSID("$NetBSD: yppasswdd_mkpw.c,v 1.18 2011/08/30 17:06:22 plunky Exp $");
35308c65aaSad #endif /* not lint */
36308c65aaSad
37d3e1a366Sthorpej #include <sys/types.h>
387ebe3b4aStron #include <sys/param.h>
39d3e1a366Sthorpej #include <sys/stat.h>
40d3e1a366Sthorpej #include <sys/time.h>
41d3e1a366Sthorpej #include <sys/resource.h>
42d3e1a366Sthorpej #include <sys/wait.h>
43308c65aaSad
44a5e58d3dSthorpej #include <err.h>
4509391c11Sdholland #include <errno.h>
46d3e1a366Sthorpej #include <fcntl.h>
47d3e1a366Sthorpej #include <stdio.h>
48d3e1a366Sthorpej #include <pwd.h>
49d3e1a366Sthorpej #include <signal.h>
50d3e1a366Sthorpej #include <stdlib.h>
513ab02a87Smatt #include <string.h>
52d3e1a366Sthorpej #include <unistd.h>
53d3e1a366Sthorpej #include <util.h>
54308c65aaSad #include <limits.h>
55d3e1a366Sthorpej
56d3e1a366Sthorpej #include <rpc/rpc.h>
57d3e1a366Sthorpej #include <rpc/xdr.h>
58d3e1a366Sthorpej #include <rpcsvc/yppasswd.h>
59d3e1a366Sthorpej
60308c65aaSad #include "extern.h"
61d3e1a366Sthorpej
62d3e1a366Sthorpej int handling_request; /* simple mutex */
63d3e1a366Sthorpej
64d3e1a366Sthorpej void
make_passwd(yppasswd * argp,struct svc_req * rqstp,SVCXPRT * transp)65308c65aaSad make_passwd(yppasswd *argp, struct svc_req *rqstp, SVCXPRT *transp)
66d3e1a366Sthorpej {
6727b46820Sbouyer struct passwd pw;
68a5e58d3dSthorpej int pfd, tfd;
697ebe3b4aStron char mpwd[MAXPATHLEN];
7027b46820Sbouyer char buf[8192]; /* from libutil */
710269a124Swennmach char *p;
720269a124Swennmach int lineno;
7327b46820Sbouyer FILE *fpw;
74d3e1a366Sthorpej
75d3e1a366Sthorpej #define REPLY(val) do { \
76d3e1a366Sthorpej int res = (val); \
77*3cd9f110Splunky if (!svc_sendreply(transp, (xdrproc_t)xdr_int, (caddr_t)&res)) \
78d3e1a366Sthorpej svcerr_systemerr(transp); \
79d3e1a366Sthorpej } while (0)
80d3e1a366Sthorpej
81d3e1a366Sthorpej #define RETURN(val) do { \
82d3e1a366Sthorpej REPLY((val)); \
83d3e1a366Sthorpej handling_request = 0; \
84d3e1a366Sthorpej return; \
85d3e1a366Sthorpej } while (0)
86d3e1a366Sthorpej
87d3e1a366Sthorpej if (handling_request) {
88d3e1a366Sthorpej warnx("already handling request; try again later");
89d3e1a366Sthorpej REPLY(1);
90d3e1a366Sthorpej return;
91d3e1a366Sthorpej }
92d3e1a366Sthorpej handling_request = 1;
93d3e1a366Sthorpej
9427b46820Sbouyer (void)strlcpy(mpwd, pw_getprefix(), sizeof(mpwd));
9527b46820Sbouyer (void)strlcat(mpwd, _PATH_MASTERPASSWD, sizeof(mpwd));
9627b46820Sbouyer fpw = fopen(mpwd, "r");
9727b46820Sbouyer if (fpw == NULL) {
9827b46820Sbouyer warnx("%s", mpwd);
99d3e1a366Sthorpej RETURN(1);
10027b46820Sbouyer }
1010269a124Swennmach for(lineno = 1; ; lineno++) {
10227b46820Sbouyer if (fgets(buf, sizeof(buf), fpw) == NULL) {
10327b46820Sbouyer if (feof(fpw))
10427b46820Sbouyer warnx("%s: %s not found", mpwd,
10527b46820Sbouyer argp->newpw.pw_name);
10627b46820Sbouyer else
10727b46820Sbouyer warnx("%s: %s", mpwd, strerror(errno));
108862f4856Schristos (void)fclose(fpw);
10927b46820Sbouyer RETURN(1);
11027b46820Sbouyer }
1110269a124Swennmach if ((p = strchr(buf, '\n')) == NULL) {
1120269a124Swennmach warnx("line %d too long", lineno);
113e4dddba9Schristos (void)fclose(fpw);
1140269a124Swennmach RETURN(1);
1150269a124Swennmach }
1160269a124Swennmach /* get rid of trailing \n */
1170269a124Swennmach *p = '\0';
11827b46820Sbouyer if (pw_scan(buf, &pw, NULL) == 0)
11927b46820Sbouyer continue;
12027b46820Sbouyer if (strncmp(argp->newpw.pw_name, pw.pw_name, MAXLOGNAME) == 0)
12127b46820Sbouyer break;
12227b46820Sbouyer }
12327b46820Sbouyer fclose(fpw);
12427b46820Sbouyer if (*pw.pw_passwd &&
12527b46820Sbouyer strcmp(crypt(argp->oldpass, pw.pw_passwd), pw.pw_passwd) != 0)
126d3e1a366Sthorpej RETURN(1);
127d3e1a366Sthorpej
128d3e1a366Sthorpej pw_init();
129d3e1a366Sthorpej tfd = pw_lock(0);
130d3e1a366Sthorpej if (tfd < 0) {
131d3e1a366Sthorpej warnx("the passwd file is busy.");
132d3e1a366Sthorpej RETURN(1);
133d3e1a366Sthorpej }
1347ebe3b4aStron pfd = open(mpwd, O_RDONLY, 0);
135d3e1a366Sthorpej if (pfd < 0) {
136d3e1a366Sthorpej pw_abort();
1377ebe3b4aStron warnx("%s", mpwd);
138d3e1a366Sthorpej RETURN(1);
139d3e1a366Sthorpej }
140d3e1a366Sthorpej
141d3e1a366Sthorpej /*
142d3e1a366Sthorpej * Get the new password. Reset passwd change time to zero; when
143d3e1a366Sthorpej * classes are implemented, go and get the "offset" value for this
144d3e1a366Sthorpej * class and reset the timer.
145d3e1a366Sthorpej */
146d3e1a366Sthorpej if (!nopw) {
14727b46820Sbouyer pw.pw_passwd = argp->newpw.pw_passwd;
14827b46820Sbouyer pw.pw_change = 0;
149d3e1a366Sthorpej }
150d3e1a366Sthorpej if (!nogecos)
15127b46820Sbouyer pw.pw_gecos = argp->newpw.pw_gecos;
152d3e1a366Sthorpej if (!noshell)
15327b46820Sbouyer pw.pw_shell = argp->newpw.pw_shell;
154d3e1a366Sthorpej
15527b46820Sbouyer pw_copy(pfd, tfd, &pw, NULL);
156d3e1a366Sthorpej
15727b46820Sbouyer if (pw_mkdb(pw.pw_name, 0) < 0) {
158d3e1a366Sthorpej warnx("pw_mkdb failed");
159d3e1a366Sthorpej pw_abort();
160d3e1a366Sthorpej RETURN(1);
161d3e1a366Sthorpej }
162d3e1a366Sthorpej
163d3e1a366Sthorpej /* XXX RESTORE SIGNAL STATE? XXX */
164d3e1a366Sthorpej
165d3e1a366Sthorpej /* Notify caller we succeeded. */
166d3e1a366Sthorpej REPLY(0);
167d3e1a366Sthorpej
168d3e1a366Sthorpej /* Update the YP maps. */
169d3e1a366Sthorpej if (chdir("/var/yp"))
170308c65aaSad err(EXIT_FAILURE, "/var/yp");
171d3e1a366Sthorpej (void) umask(022);
172d3e1a366Sthorpej (void) system(make_arg);
173d3e1a366Sthorpej
174d3e1a366Sthorpej handling_request = 0;
175d3e1a366Sthorpej }
176