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