]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/rpc.yppasswdd/yppasswdd_server.c
zfs: merge openzfs/zfs@e13538856
[FreeBSD/FreeBSD.git] / usr.sbin / rpc.yppasswdd / yppasswdd_server.c
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1995, 1996
5  *      Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Bill Paul.
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #include <sys/cdefs.h>
36 #include <sys/param.h>
37 #include <sys/fcntl.h>
38 #include <sys/socket.h>
39 #include <sys/stat.h>
40 #include <sys/wait.h>
41
42 #include <arpa/inet.h>
43 #include <netinet/in.h>
44
45 #include <ctype.h>
46 #include <db.h>
47 #include <dirent.h>
48 #include <errno.h>
49 #include <limits.h>
50 #include <pwd.h>
51 #include <signal.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56
57 #include <libgen.h>
58 #include <libutil.h>
59
60 #include <rpc/rpc.h>
61 #include <rpcsvc/yp.h>
62 struct dom_binding;
63 #include <rpcsvc/ypclnt.h>
64 #include "yppasswdd_extern.h"
65 #include "yppasswd.h"
66 #include "yppasswd_private.h"
67 #include "ypxfr_extern.h"
68 #include "yp_extern.h"
69
70 static struct passwd yp_password;
71
72 static void
73 xlate_passwd(struct x_master_passwd *xpwd, struct passwd *pwd)
74 {
75         pwd->pw_name = xpwd->pw_name;
76         pwd->pw_passwd = xpwd->pw_passwd;
77         pwd->pw_uid = xpwd->pw_uid;
78         pwd->pw_gid = xpwd->pw_gid;
79         pwd->pw_change = xpwd->pw_change;
80         pwd->pw_class = xpwd->pw_class;
81         pwd->pw_gecos = xpwd->pw_gecos;
82         pwd->pw_dir = xpwd->pw_dir;
83         pwd->pw_shell = xpwd->pw_shell;
84         pwd->pw_expire = xpwd->pw_expire;
85         pwd->pw_fields = xpwd->pw_fields;
86 }
87
88 static void
89 copy_yp_pass(char *p, int x, int m)
90 {
91         char *t, *s = p;
92         static char *buf;
93
94         yp_password.pw_fields = 0;
95
96         buf = realloc(buf, m + 10);
97         bzero(buf, m + 10);
98
99         /* Turn all colons into NULLs */
100         while (strchr(s, ':')) {
101                 s = (strchr(s, ':') + 1);
102                 *(s - 1)= '\0';
103         }
104
105         t = buf;
106 #define EXPAND(e) do { \
107         e = t; \
108         while ((*t++ = *p++)); \
109 } while (0)
110         EXPAND(yp_password.pw_name);
111         yp_password.pw_fields |= _PWF_NAME;
112         EXPAND(yp_password.pw_passwd);
113         yp_password.pw_fields |= _PWF_PASSWD;
114         yp_password.pw_uid = atoi(p);
115         p += (strlen(p) + 1);
116         yp_password.pw_fields |= _PWF_UID;
117         yp_password.pw_gid = atoi(p);
118         p += (strlen(p) + 1);
119         yp_password.pw_fields |= _PWF_GID;
120         if (x) {
121                 EXPAND(yp_password.pw_class);
122                 yp_password.pw_fields |= _PWF_CLASS;
123                 yp_password.pw_change = atol(p);
124                 p += (strlen(p) + 1);
125                 yp_password.pw_fields |= _PWF_CHANGE;
126                 yp_password.pw_expire = atol(p);
127                 p += (strlen(p) + 1);
128                 yp_password.pw_fields |= _PWF_EXPIRE;
129         }
130         EXPAND(yp_password.pw_gecos);
131         yp_password.pw_fields |= _PWF_GECOS;
132         EXPAND(yp_password.pw_dir);
133         yp_password.pw_fields |= _PWF_DIR;
134         EXPAND(yp_password.pw_shell);
135         yp_password.pw_fields |= _PWF_SHELL;
136 }
137
138 static int
139 validchars(char *arg)
140 {
141         size_t i;
142
143         for (i = 0; i < strlen(arg); i++) {
144                 if (iscntrl(arg[i])) {
145                         yp_error("string contains a control character");
146                         return(1);
147                 }
148                 if (arg[i] == ':') {
149                         yp_error("string contains a colon");
150                         return(1);
151                 }
152                 /* Be evil: truncate strings with \n in them silently. */
153                 if (arg[i] == '\n') {
154                         arg[i] = '\0';
155                         return(0);
156                 }
157         }
158         return(0);
159 }
160
161 static int
162 validate_master(struct passwd *opw __unused, struct x_master_passwd *npw)
163 {
164
165         if (npw->pw_name[0] == '+' || npw->pw_name[0] == '-') {
166                 yp_error("client tried to modify an NIS entry");
167                 return(1);
168         }
169
170         if (validchars(npw->pw_shell)) {
171                 yp_error("specified shell contains invalid characters");
172                 return(1);
173         }
174
175         if (validchars(npw->pw_gecos)) {
176                 yp_error("specified gecos field contains invalid characters");
177                 return(1);
178         }
179
180         if (validchars(npw->pw_passwd)) {
181                 yp_error("specified password contains invalid characters");
182                 return(1);
183         }
184         return(0);
185 }
186
187 static int
188 validate(struct passwd *opw, struct x_passwd *npw)
189 {
190
191         if (npw->pw_name[0] == '+' || npw->pw_name[0] == '-') {
192                 yp_error("client tried to modify an NIS entry");
193                 return(1);
194         }
195
196         if ((uid_t)npw->pw_uid != opw->pw_uid) {
197                 yp_error("UID mismatch: client says user %s has UID %d",
198                          npw->pw_name, npw->pw_uid);
199                 yp_error("database says user %s has UID %d", opw->pw_name,
200                          opw->pw_uid);
201                 return(1);
202         }
203
204         if ((gid_t)npw->pw_gid != opw->pw_gid) {
205                 yp_error("GID mismatch: client says user %s has GID %d",
206                          npw->pw_name, npw->pw_gid);
207                 yp_error("database says user %s has GID %d", opw->pw_name,
208                          opw->pw_gid);
209                 return(1);
210         }
211
212         /*
213          * Don't allow the user to shoot himself in the foot,
214          * even on purpose.
215          */
216         if (!no_chsh && !ok_shell(npw->pw_shell)) {
217                 yp_error("%s is not a valid shell", npw->pw_shell);
218                 return(1);
219         }
220
221         if (!no_chsh && validchars(npw->pw_shell)) {
222                 yp_error("specified shell contains invalid characters");
223                 return(1);
224         }
225
226         if (validchars(npw->pw_gecos)) {
227                 yp_error("specified gecos field contains invalid characters");
228                 return(1);
229         }
230
231         if (validchars(npw->pw_passwd)) {
232                 yp_error("specified password contains invalid characters");
233                 return(1);
234         }
235         return(0);
236 }
237
238 /*
239  * Kludge alert:
240  * In order to have one rpc.yppasswdd support multiple domains,
241  * we have to cheat: we search each directory under /var/yp
242  * and try to match the user in each master.passwd.byname
243  * map that we find. If the user matches (username, uid and gid
244  * all agree), then we use that domain. If we match the user in
245  * more than one database, we must abort.
246  */
247 static char *
248 find_domain(struct x_passwd *pw)
249 {
250         struct stat statbuf;
251         struct dirent *dirp;
252         DIR *dird;
253         char yp_mapdir[MAXPATHLEN + 2];
254         static char domain[YPMAXDOMAIN];
255         char *tmp = NULL;
256         DBT key, data;
257         int hit = 0;
258
259         yp_error("performing multidomain lookup");
260
261         if ((dird = opendir(yp_dir)) == NULL) {
262                 yp_error("opendir(%s) failed: %s", yp_dir, strerror(errno));
263                 return(NULL);
264         }
265
266         while ((dirp = readdir(dird)) != NULL) {
267                 snprintf(yp_mapdir, sizeof yp_mapdir, "%s/%s",
268                                                         yp_dir, dirp->d_name);
269                 if (stat(yp_mapdir, &statbuf) < 0) {
270                         yp_error("stat(%s) failed: %s", yp_mapdir,
271                                                         strerror(errno));
272                         closedir(dird);
273                         return(NULL);
274                 }
275                 if (S_ISDIR(statbuf.st_mode)) {
276                         tmp = (char *)dirp->d_name;
277                         key.data = pw->pw_name;
278                         key.size = strlen(pw->pw_name);
279
280                         if (yp_get_record(tmp,"master.passwd.byname",
281                                         &key, &data, 0) != YP_TRUE) {
282                                 continue;
283                         }
284                         *((char *)data.data + data.size) = '\0';
285                         copy_yp_pass(data.data, 1, data.size);
286                         if (yp_password.pw_uid == (uid_t)pw->pw_uid &&
287                             yp_password.pw_gid == (gid_t)pw->pw_gid) {
288                                 hit++;
289                                 snprintf(domain, YPMAXDOMAIN, "%s", tmp);
290                         }
291                 }
292         }
293
294         closedir(dird);
295         if (hit > 1) {
296                 yp_error("found same user in two different domains");
297                 return(NULL);
298         } else
299                 return((char *)&domain);
300 }
301
302 static const char *maps[] = {
303         "master.passwd.byname",
304         "master.passwd.byuid",
305         "passwd.byname",
306         "passwd.byuid"
307 };
308
309 static const char *formats[] = {
310         "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s",
311         "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s",
312         "%s:%s:%d:%d:%s:%s:%s",
313         "%s:%s:%d:%d:%s:%s:%s"
314 };
315
316 static int
317 update_inplace(struct passwd *pw, char *domain)
318 {
319         DB *dbp = NULL;
320         DBT key = { NULL, 0 };
321         DBT data = { NULL, 0 };
322         char *pwbuf;
323         char keybuf[20];
324         int i;
325         char *ptr = NULL;
326         static char yp_last[] = "YP_LAST_MODIFIED";
327         char yplastbuf[64];
328
329         snprintf(yplastbuf, sizeof yplastbuf, "%llu",
330             (unsigned long long)time(NULL));
331         pwbuf = NULL;
332
333         for (i = 0; i < 4; i++) {
334
335                 if (i % 2) {
336                         snprintf(keybuf, sizeof keybuf,
337                             "%llu", (unsigned long long)pw->pw_uid);
338                         key.data = &keybuf;
339                         key.size = strlen(keybuf);
340                 } else {
341                         key.data = pw->pw_name;
342                         key.size = strlen(pw->pw_name);
343                 }
344
345                 /*
346                  * XXX The passwd.byname and passwd.byuid maps come in
347                  * two flavors: secure and insecure. The secure version
348                  * has a '*' in the password field whereas the insecure one
349                  * has a real crypted password. The maps will be insecure
350                  * if they were built with 'unsecure = TRUE' enabled in
351                  * /var/yp/Makefile, but we'd have no way of knowing if
352                  * this has been done unless we were to try parsing the
353                  * Makefile, which is a disgusting thought. Instead, we
354                  * read the records from the maps, skip to the first ':'
355                  * in them, and then look at the character immediately
356                  * following it. If it's an '*' then the map is 'secure'
357                  * and we must not insert a real password into the pw_passwd
358                  * field. If it's not an '*', then we put the real crypted
359                  * password in.
360                  */
361                 if (yp_get_record(domain,maps[i],&key,&data,1) != YP_TRUE) {
362                         yp_error("couldn't read %s/%s: %s", domain,
363                                                 maps[i], strerror(errno));
364                         goto ret1;
365                 }
366
367                 if ((ptr = strchr(data.data, ':')) == NULL) {
368                         yp_error("no colon in passwd record?!");
369                         goto ret1;
370                 }
371
372                 /*
373                  * XXX Supposing we have more than one user with the same
374                  * UID? (Or more than one user with the same name?) We could
375                  * end up modifying the wrong record if were not careful.
376                  */
377                 if (i % 2) {
378                         if (strncmp(data.data, pw->pw_name,
379                                                         strlen(pw->pw_name))) {
380                                 yp_error("warning: found entry for UID %d \
381 in map %s@%s with wrong name (%.*s)", pw->pw_uid, maps[i], domain,
382                                     (int)(ptr - (char *)data.data),
383                                     (char *)data.data);
384                                 yp_error("there may be more than one user \
385 with the same UID - continuing");
386                                 continue;
387                         }
388                 } else {
389                         /*
390                          * We're really being ultra-paranoid here.
391                          * This is generally a 'can't happen' condition.
392                          */
393                         free(pwbuf);
394                         asprintf(&pwbuf, ":%d:%d:", pw->pw_uid, pw->pw_gid);
395                         if (pwbuf == NULL) {
396                                 yp_error("no memory");
397                                 goto ret1;
398                         }
399                         if (!strstr(data.data, pwbuf)) {
400                                 yp_error("warning: found entry for user %s \
401 in map %s@%s with wrong UID", pw->pw_name, maps[i], domain);
402                                 yp_error("there may be more than one user \
403 with the same name - continuing");
404                                 continue;
405                         }
406                 }
407
408                 if (i < 2) {
409                         free(pwbuf);
410                         asprintf(&pwbuf, formats[i],
411                            pw->pw_name, pw->pw_passwd, pw->pw_uid,
412                            pw->pw_gid, pw->pw_class, pw->pw_change,
413                            pw->pw_expire, pw->pw_gecos, pw->pw_dir,
414                            pw->pw_shell);
415                 } else {
416                         free(pwbuf);
417                         asprintf(&pwbuf, formats[i],
418                            pw->pw_name, *(ptr+1) == '*' ? "*" : pw->pw_passwd,
419                            pw->pw_uid, pw->pw_gid, pw->pw_gecos, pw->pw_dir,
420                            pw->pw_shell);
421                 }
422                 if (pwbuf == NULL) {
423                         yp_error("no memory");
424                         goto ret1;
425                 }
426
427 #define FLAGS O_RDWR|O_CREAT
428
429                 if ((dbp = yp_open_db_rw(domain, maps[i], FLAGS)) == NULL) {
430                         yp_error("couldn't open %s/%s r/w: %s",domain,
431                                                 maps[i],strerror(errno));
432                         goto ret1;
433                 }
434
435                 data.data = pwbuf;
436                 data.size = strlen(pwbuf);
437
438                 if (yp_put_record(dbp, &key, &data, 1) != YP_TRUE) {
439                         yp_error("failed to update record in %s/%s", domain,
440                                                                 maps[i]);
441                         (void)(dbp->close)(dbp);
442                         goto ret1;
443                 }
444
445                 key.data = yp_last;
446                 key.size = strlen(yp_last);
447                 data.data = (char *)&yplastbuf;
448                 data.size = strlen(yplastbuf);
449
450                 if (yp_put_record(dbp, &key, &data, 1) != YP_TRUE) {
451                         yp_error("failed to update timestamp in %s/%s", domain,
452                                                                 maps[i]);
453                         (void)(dbp->close)(dbp);
454                         goto ret1;
455                 }
456
457                 (void)(dbp->close)(dbp);
458         }
459
460         free(pwbuf);
461         return (0);
462 ret1:
463         free(pwbuf);
464         return (1);
465 }
466
467 int *
468 yppasswdproc_update_1_svc(yppasswd *argp, struct svc_req *rqstp)
469 {
470         static int  result;
471         struct sockaddr_in *rqhost;
472         DBT key, data;
473         int rval = 0;
474         int pfd, tfd;
475         int pid;
476         int passwd_changed = 0;
477         int shell_changed = 0;
478         int gecos_changed = 0;
479         char *cryptpw;
480         char *oldshell = NULL;
481         char *oldgecos = NULL;
482         char *passdir;
483         char *passfile_hold;
484         char passdir_buf[MAXPATHLEN + 2];
485         char passfile_buf[MAXPATHLEN + 2];
486         char passfile_hold_buf[MAXPATHLEN + 2];
487         char *domain = yppasswd_domain;
488         static struct sockaddr_in clntaddr;
489         static struct timeval t_saved, t_test;
490
491         /*
492          * Normal user updates always use the 'default' master.passwd file.
493          */
494
495         passfile = passfile_default;
496         result = 1;
497
498         rqhost = svc_getcaller(rqstp->rq_xprt);
499
500         gettimeofday(&t_test, NULL);
501         if (!bcmp(rqhost, &clntaddr, sizeof *rqhost) &&
502                 t_test.tv_sec > t_saved.tv_sec &&
503                 t_test.tv_sec - t_saved.tv_sec < 300) {
504
505                 bzero(&clntaddr, sizeof clntaddr);
506                 bzero(&t_saved, sizeof t_saved);
507                 return(NULL);
508         }
509
510         bcopy(rqhost, &clntaddr, sizeof clntaddr);
511         gettimeofday(&t_saved, NULL);
512
513         if (yp_access(resvport ? "master.passwd.byname" : NULL, rqstp)) {
514                 yp_error("rejected update request from unauthorized host");
515                 svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED);
516                 return(&result);
517         }
518
519         /*
520          * Step one: find the user. (It's kinda pointless to
521          * proceed if the user doesn't exist.) We look for the
522          * user in the master.passwd.byname database, _NOT_ by
523          * using getpwent() and friends! We can't use getpwent()
524          * since the NIS master server is not guaranteed to be
525          * configured as an NIS client.
526          */
527
528         if (multidomain) {
529                 if ((domain = find_domain(&argp->newpw)) == NULL) {
530                         yp_error("multidomain lookup failed - aborting update");
531                         return(&result);
532                 } else
533                         yp_error("updating user %s in domain %s",
534                                         argp->newpw.pw_name, domain);
535         }
536
537         key.data = argp->newpw.pw_name;
538         key.size = strlen(argp->newpw.pw_name);
539
540         if ((rval = yp_get_record(domain,"master.passwd.byname",
541                         &key, &data, 0)) != YP_TRUE) {
542                 if (rval == YP_NOKEY) {
543                         yp_error("user %s not found in passwd database",
544                                 argp->newpw.pw_name);
545                 } else {
546                         yp_error("database access error: %s",
547                                 yperr_string(rval));
548                 }
549                 return(&result);
550         }
551
552         /* Nul terminate, please. */
553         *((char *)data.data + data.size) = '\0';
554
555         copy_yp_pass(data.data, 1, data.size);
556
557         /* Step 2: check that the supplied oldpass is valid. */
558
559         cryptpw = crypt(argp->oldpass, yp_password.pw_passwd);
560         if (cryptpw == NULL || strcmp(cryptpw, yp_password.pw_passwd)) {
561                 yp_error("rejected change attempt -- bad password");
562                 yp_error("client address: %s username: %s",
563                           inet_ntoa(rqhost->sin_addr),
564                           argp->newpw.pw_name);
565                 return(&result);
566         }
567
568         /* Step 3: validate the arguments passed to us by the client. */
569
570         if (validate(&yp_password, &argp->newpw)) {
571                 yp_error("rejecting change attempt: bad arguments");
572                 yp_error("client address: %s username: %s",
573                          inet_ntoa(rqhost->sin_addr),
574                          argp->newpw.pw_name);
575                 svcerr_decode(rqstp->rq_xprt);
576                 return(&result);
577         }
578
579         /* Step 4: update the user's passwd structure. */
580
581         if (!no_chsh && strcmp(argp->newpw.pw_shell, yp_password.pw_shell)) {
582                 oldshell = yp_password.pw_shell;
583                 yp_password.pw_shell = argp->newpw.pw_shell;
584                 shell_changed++;
585         }
586
587
588         if (!no_chfn && strcmp(argp->newpw.pw_gecos, yp_password.pw_gecos)) {
589                 oldgecos = yp_password.pw_gecos;
590                 yp_password.pw_gecos = argp->newpw.pw_gecos;
591                 gecos_changed++;
592         }
593
594         if (strcmp(argp->newpw.pw_passwd, yp_password.pw_passwd)) {
595                 yp_password.pw_passwd = argp->newpw.pw_passwd;
596                 yp_password.pw_change = 0;
597                 passwd_changed++;
598         }
599
600         /*
601          * If the caller specified a domain other than our 'default'
602          * domain, change the path to master.passwd accordingly.
603          */
604
605         if (strcmp(domain, yppasswd_domain)) {
606                 snprintf(passfile_buf, sizeof(passfile_buf),
607                         "%s/%s/master.passwd", yp_dir, domain);
608                 passfile = (char *)&passfile_buf;
609         }
610
611         /*
612          * Create a filename to hold the original master.passwd
613          * so if our call to yppwupdate fails we can roll back
614          */
615         snprintf(passfile_hold_buf, sizeof(passfile_hold_buf),
616             "%s.hold", passfile);
617         passfile_hold = (char *)&passfile_hold_buf;
618
619
620         /* Step 5: make a new password file with the updated info. */
621
622         snprintf(passdir_buf, sizeof(passdir_buf), "%s", passfile);
623         passdir = dirname(passdir_buf);
624
625         if (pw_init(passdir, passfile)) {
626                 yp_error("pw_init() failed");
627                 return &result;
628         }
629         if ((pfd = pw_lock()) == -1) {
630                 pw_fini();
631                 yp_error("pw_lock() failed");
632                 return &result;
633         }
634         if ((tfd = pw_tmp(-1)) == -1) {
635                 pw_fini();
636                 yp_error("pw_tmp() failed");
637                 return &result;
638         }
639         if (pw_copy(pfd, tfd, &yp_password, NULL) == -1) {
640                 pw_fini();
641                 yp_error("pw_copy() failed");
642                 return &result;
643         }
644         if (rename(passfile, passfile_hold) == -1) {
645                 pw_fini();
646                 yp_error("rename of %s to %s failed", passfile,
647                     passfile_hold);
648                 return &result;
649         }
650
651         if (strcmp(passfile, _PATH_MASTERPASSWD) == 0) { 
652                 /*
653                  * NIS server is exporting the system's master.passwd.
654                  * Call pw_mkdb to rebuild passwd and the .db files
655                  */
656                 if (pw_mkdb(yp_password.pw_name) == -1) {
657                         pw_fini();
658                         yp_error("pw_mkdb() failed");
659                         rename(passfile_hold, passfile);
660                         return &result;
661                 }
662         } else {
663                 /*
664                  * NIS server is exporting a private master.passwd.
665                  * Rename tempfile into final location
666                  */
667                 if (rename(pw_tempname(), passfile) == -1) {
668                         pw_fini();
669                         yp_error("rename of %s to %s failed",
670                             pw_tempname(), passfile);
671                         rename(passfile_hold, passfile);
672                         return &result;
673                 }
674         }
675
676         pw_fini();
677
678         if (inplace) {
679                 if ((rval = update_inplace(&yp_password, domain))) {
680                         yp_error("inplace update failed -- rebuilding maps");
681                 }
682         }
683
684         switch ((pid = fork())) {
685         case 0:
686                 if (inplace && !rval) {
687                         execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
688                                 yppasswd_domain, "pushpw", (char *)NULL);
689                 } else {
690                         execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
691                                 yppasswd_domain, (char *)NULL);
692                 }
693                 yp_error("couldn't exec map update process: %s",
694                                         strerror(errno));
695                 unlink(passfile);
696                 rename(passfile_hold, passfile);
697                 exit(1);
698                 break;
699         case -1:
700                 yp_error("fork() failed: %s", strerror(errno));
701                 unlink(passfile);
702                 rename(passfile_hold, passfile);
703                 return(&result);
704                 break;
705         default:
706                 unlink(passfile_hold);
707                 break;
708         }
709
710         if (verbose) {
711                 yp_error("update completed for user %s (uid %d) in %s:",
712                     argp->newpw.pw_name, argp->newpw.pw_uid, passfile);
713
714                 if (passwd_changed)
715                         yp_error("password changed");
716
717                 if (gecos_changed)
718                         yp_error("gecos changed ('%s' -> '%s')",
719                                         oldgecos, argp->newpw.pw_gecos);
720
721                 if (shell_changed)
722                         yp_error("shell changed ('%s' -> '%s')",
723                                         oldshell, argp->newpw.pw_shell);
724         }
725
726         result = 0;
727         return (&result);
728 }
729
730 /*
731  * Note that this function performs a little less sanity checking
732  * than the last one. Since only the superuser is allowed to use it,
733  * it is assumed that the caller knows what he's doing.
734  */
735 int *
736 yppasswdproc_update_master_1_svc(master_yppasswd *argp,
737     struct svc_req *rqstp)
738 {
739         static int result;
740         int pfd, tfd;
741         int pid;
742         uid_t uid;
743         int rval = 0;
744         DBT key, data;
745         char *passdir;
746         char *passfile_hold;
747         char passdir_buf[MAXPATHLEN + 2];
748         char passfile_buf[MAXPATHLEN + 2];
749         char passfile_hold_buf[MAXPATHLEN + 2];
750         struct sockaddr_in *rqhost;
751         SVCXPRT *transp;
752         struct passwd newpasswd;
753
754         result = 1;
755         transp = rqstp->rq_xprt;
756
757         /*
758          * NO AF_INET CONNETCIONS ALLOWED!
759          */
760         rqhost = svc_getcaller(transp);
761         if (rqhost->sin_family != AF_UNIX) {
762                 yp_error("Alert! %s/%d attempted to use superuser-only \
763 procedure!\n", inet_ntoa(rqhost->sin_addr), rqhost->sin_port);
764                 svcerr_auth(transp, AUTH_BADCRED);
765                 return(&result);
766         }
767
768         if (rqstp->rq_cred.oa_flavor != AUTH_SYS) {
769                 yp_error("caller didn't send proper credentials");
770                 svcerr_auth(transp, AUTH_BADCRED);
771                 return(&result);
772         }
773
774         if (__rpc_get_local_uid(transp, &uid) < 0) {
775                 yp_error("caller didn't send proper credentials");
776                 svcerr_auth(transp, AUTH_BADCRED);
777                 return(&result);
778         }
779
780         if (uid) {
781                 yp_error("caller euid is %d, expecting 0 -- rejecting request",
782                     uid);
783                 svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED);
784                 return(&result);
785         }
786
787         passfile = passfile_default;
788
789         key.data = argp->newpw.pw_name;
790         key.size = strlen(argp->newpw.pw_name);
791
792         /*
793          * The superuser may add entries to the passwd maps if
794          * rpc.yppasswdd is started with the -a flag. Paranoia
795          * prevents me from allowing additions by default.
796          */
797         if ((rval = yp_get_record(argp->domain, "master.passwd.byname",
798                           &key, &data, 0)) != YP_TRUE) {
799                 if (rval == YP_NOKEY) {
800                         yp_error("user %s not found in passwd database",
801                                  argp->newpw.pw_name);
802                         if (allow_additions)
803                                 yp_error("notice: adding user %s to \
804 master.passwd database for domain %s", argp->newpw.pw_name, argp->domain);
805                         else
806                                 yp_error("restart rpc.yppasswdd with the -a flag to \
807 allow additions to be made to the password database");
808                 } else {
809                         yp_error("database access error: %s",
810                                  yperr_string(rval));
811                 }
812                 if (!allow_additions)
813                         return(&result);
814         } else {
815
816                 /* Nul terminate, please. */
817                 *((char *)data.data + data.size) = '\0';
818
819                 copy_yp_pass(data.data, 1, data.size);
820         }
821
822         /*
823          * Perform a small bit of sanity checking.
824          */
825         if (validate_master(rval == YP_TRUE ? &yp_password:NULL,&argp->newpw)){
826                 yp_error("rejecting update attempt for %s: bad arguments",
827                          argp->newpw.pw_name);
828                 return(&result);
829         }
830
831         /*
832          * If the caller specified a domain other than our 'default'
833          * domain, change the path to master.passwd accordingly.
834          */
835
836         if (strcmp(argp->domain, yppasswd_domain)) {
837                 snprintf(passfile_buf, sizeof(passfile_buf),
838                         "%s/%s/master.passwd", yp_dir, argp->domain);
839                 passfile = (char *)&passfile_buf;
840         }
841
842         /*
843          * Create a filename to hold the original master.passwd
844          * so if our call to yppwupdate fails we can roll back
845          */
846         snprintf(passfile_hold_buf, sizeof(passfile_hold_buf),
847             "%s.hold", passfile);
848         passfile_hold = (char *)&passfile_hold_buf;
849
850         snprintf(passdir_buf, sizeof(passdir_buf), "%s", passfile);
851         passdir = dirname(passdir_buf);
852
853         if (pw_init(passdir, passfile)) {
854                 yp_error("pw_init() failed");
855                 return &result;
856         }
857         if ((pfd = pw_lock()) == -1) {
858                 pw_fini();
859                 yp_error("pw_lock() failed");
860                 return &result;
861         }
862         if ((tfd = pw_tmp(-1)) == -1) {
863                 pw_fini();
864                 yp_error("pw_tmp() failed");
865                 return &result;
866         }
867         xlate_passwd(&argp->newpw, &newpasswd);
868         if (pw_copy(pfd, tfd, &newpasswd, NULL) == -1) {
869                 pw_fini();
870                 yp_error("pw_copy() failed");
871                 return &result;
872         }
873         if (rename(passfile, passfile_hold) == -1) {
874                 pw_fini();
875                 yp_error("rename of %s to %s failed", passfile,
876                     passfile_hold);
877                 return &result;
878         }
879         if (strcmp(passfile, _PATH_MASTERPASSWD) == 0) {
880                 /*
881                  * NIS server is exporting the system's master.passwd.
882                  * Call pw_mkdb to rebuild passwd and the .db files
883                  */
884                 if (pw_mkdb(argp->newpw.pw_name) == -1) {
885                         pw_fini();
886                         yp_error("pw_mkdb() failed");
887                         rename(passfile_hold, passfile);
888                         return &result;
889                 }
890         } else {
891                 /*
892                  * NIS server is exporting a private master.passwd.
893                  * Rename tempfile into final location
894                  */
895                 if (rename(pw_tempname(), passfile) == -1) {
896                         pw_fini();
897                         yp_error("rename of %s to %s failed",
898                             pw_tempname(), passfile);
899                         rename(passfile_hold, passfile);
900                         return &result;
901                 }
902         }
903         pw_fini();
904
905         if (inplace) {
906                 xlate_passwd(&argp->newpw, &newpasswd);
907                 if ((rval = update_inplace(&newpasswd, argp->domain))) {
908                         yp_error("inplace update failed -- rebuilding maps");
909                 }
910         }
911
912         switch ((pid = fork())) {
913         case 0:
914                 if (inplace && !rval) {
915                         execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
916                                 argp->domain, "pushpw", (char *)NULL);
917                 } else {
918                         execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
919                                 argp->domain, (char *)NULL);
920                 }
921                 yp_error("couldn't exec map update process: %s",
922                                         strerror(errno));
923                 unlink(passfile);
924                 rename(passfile_hold, passfile);
925                 exit(1);
926                 break;
927         case -1:
928                 yp_error("fork() failed: %s", strerror(errno));
929                 unlink(passfile);
930                 rename(passfile_hold, passfile);
931                 return(&result);
932                 break;
933         default:
934                 unlink(passfile_hold);
935                 break;
936         }
937
938         yp_error("performed update of user %s (uid %d) domain %s",
939                                                 argp->newpw.pw_name,
940                                                 argp->newpw.pw_uid,
941                                                 argp->domain);
942
943         result = 0;
944         return(&result);
945 }