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