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