1 /*-
2 * SPDX-License-Identifier: BSD-4-Clause
3 *
4 * Copyright (c) 1995, 1996
5 * Bill Paul <[email protected]>. 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
xlate_passwd(struct x_master_passwd * xpwd,struct passwd * pwd)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
copy_yp_pass(char * p,int x,int m)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
validchars(char * arg)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
validate_master(struct passwd * opw __unused,struct x_master_passwd * npw)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
validate(struct passwd * opw,struct x_passwd * npw)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 *
find_domain(struct x_passwd * pw)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
update_inplace(struct passwd * pw,char * domain)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;
327 char keybuf[20];
328 int i;
329 char *ptr = NULL;
330 static char yp_last[] = "YP_LAST_MODIFIED";
331 char yplastbuf[64];
332
333 snprintf(yplastbuf, sizeof yplastbuf, "%llu",
334 (unsigned long long)time(NULL));
335 pwbuf = NULL;
336
337 for (i = 0; i < 4; i++) {
338
339 if (i % 2) {
340 snprintf(keybuf, sizeof keybuf,
341 "%llu", (unsigned long long)pw->pw_uid);
342 key.data = &keybuf;
343 key.size = strlen(keybuf);
344 } else {
345 key.data = pw->pw_name;
346 key.size = strlen(pw->pw_name);
347 }
348
349 /*
350 * XXX The passwd.byname and passwd.byuid maps come in
351 * two flavors: secure and insecure. The secure version
352 * has a '*' in the password field whereas the insecure one
353 * has a real crypted password. The maps will be insecure
354 * if they were built with 'unsecure = TRUE' enabled in
355 * /var/yp/Makefile, but we'd have no way of knowing if
356 * this has been done unless we were to try parsing the
357 * Makefile, which is a disgusting thought. Instead, we
358 * read the records from the maps, skip to the first ':'
359 * in them, and then look at the character immediately
360 * following it. If it's an '*' then the map is 'secure'
361 * and we must not insert a real password into the pw_passwd
362 * field. If it's not an '*', then we put the real crypted
363 * password in.
364 */
365 if (yp_get_record(domain,maps[i],&key,&data,1) != YP_TRUE) {
366 yp_error("couldn't read %s/%s: %s", domain,
367 maps[i], strerror(errno));
368 goto ret1;
369 }
370
371 if ((ptr = strchr(data.data, ':')) == NULL) {
372 yp_error("no colon in passwd record?!");
373 goto ret1;
374 }
375
376 /*
377 * XXX Supposing we have more than one user with the same
378 * UID? (Or more than one user with the same name?) We could
379 * end up modifying the wrong record if were not careful.
380 */
381 if (i % 2) {
382 if (strncmp(data.data, pw->pw_name,
383 strlen(pw->pw_name))) {
384 yp_error("warning: found entry for UID %d \
385 in map %s@%s with wrong name (%.*s)", pw->pw_uid, maps[i], domain,
386 (int)(ptr - (char *)data.data),
387 (char *)data.data);
388 yp_error("there may be more than one user \
389 with the same UID - continuing");
390 continue;
391 }
392 } else {
393 /*
394 * We're really being ultra-paranoid here.
395 * This is generally a 'can't happen' condition.
396 */
397 free(pwbuf);
398 asprintf(&pwbuf, ":%d:%d:", pw->pw_uid, pw->pw_gid);
399 if (pwbuf == NULL) {
400 yp_error("no memory");
401 goto ret1;
402 }
403 if (!strstr(data.data, pwbuf)) {
404 yp_error("warning: found entry for user %s \
405 in map %s@%s with wrong UID", pw->pw_name, maps[i], domain);
406 yp_error("there may be more than one user \
407 with the same name - continuing");
408 continue;
409 }
410 }
411
412 if (i < 2) {
413 free(pwbuf);
414 asprintf(&pwbuf, formats[i],
415 pw->pw_name, pw->pw_passwd, pw->pw_uid,
416 pw->pw_gid, pw->pw_class, pw->pw_change,
417 pw->pw_expire, pw->pw_gecos, pw->pw_dir,
418 pw->pw_shell);
419 } else {
420 free(pwbuf);
421 asprintf(&pwbuf, formats[i],
422 pw->pw_name, *(ptr+1) == '*' ? "*" : pw->pw_passwd,
423 pw->pw_uid, pw->pw_gid, pw->pw_gecos, pw->pw_dir,
424 pw->pw_shell);
425 }
426 if (pwbuf == NULL) {
427 yp_error("no memory");
428 goto ret1;
429 }
430
431 #define FLAGS O_RDWR|O_CREAT
432
433 if ((dbp = yp_open_db_rw(domain, maps[i], FLAGS)) == NULL) {
434 yp_error("couldn't open %s/%s r/w: %s",domain,
435 maps[i],strerror(errno));
436 goto ret1;
437 }
438
439 data.data = pwbuf;
440 data.size = strlen(pwbuf);
441
442 if (yp_put_record(dbp, &key, &data, 1) != YP_TRUE) {
443 yp_error("failed to update record in %s/%s", domain,
444 maps[i]);
445 (void)(dbp->close)(dbp);
446 goto ret1;
447 }
448
449 key.data = yp_last;
450 key.size = strlen(yp_last);
451 data.data = (char *)&yplastbuf;
452 data.size = strlen(yplastbuf);
453
454 if (yp_put_record(dbp, &key, &data, 1) != YP_TRUE) {
455 yp_error("failed to update timestamp in %s/%s", domain,
456 maps[i]);
457 (void)(dbp->close)(dbp);
458 goto ret1;
459 }
460
461 (void)(dbp->close)(dbp);
462 }
463
464 free(pwbuf);
465 return (0);
466 ret1:
467 free(pwbuf);
468 return (1);
469 }
470
471 int *
yppasswdproc_update_1_svc(yppasswd * argp,struct svc_req * rqstp)472 yppasswdproc_update_1_svc(yppasswd *argp, struct svc_req *rqstp)
473 {
474 static int result;
475 struct sockaddr_in *rqhost;
476 DBT key, data;
477 int rval = 0;
478 int pfd, tfd;
479 int pid;
480 int passwd_changed = 0;
481 int shell_changed = 0;
482 int gecos_changed = 0;
483 char *cryptpw;
484 char *oldshell = NULL;
485 char *oldgecos = NULL;
486 char *passdir;
487 char *passfile_hold;
488 char passdir_buf[MAXPATHLEN + 2];
489 char passfile_buf[MAXPATHLEN + 2];
490 char passfile_hold_buf[MAXPATHLEN + 2];
491 char *domain = yppasswd_domain;
492 static struct sockaddr_in clntaddr;
493 static struct timeval t_saved, t_test;
494
495 /*
496 * Normal user updates always use the 'default' master.passwd file.
497 */
498
499 passfile = passfile_default;
500 result = 1;
501
502 rqhost = svc_getcaller(rqstp->rq_xprt);
503
504 gettimeofday(&t_test, NULL);
505 if (!bcmp(rqhost, &clntaddr, sizeof *rqhost) &&
506 t_test.tv_sec > t_saved.tv_sec &&
507 t_test.tv_sec - t_saved.tv_sec < 300) {
508
509 bzero(&clntaddr, sizeof clntaddr);
510 bzero(&t_saved, sizeof t_saved);
511 return(NULL);
512 }
513
514 bcopy(rqhost, &clntaddr, sizeof clntaddr);
515 gettimeofday(&t_saved, NULL);
516
517 if (yp_access(resvport ? "master.passwd.byname" : NULL, rqstp)) {
518 yp_error("rejected update request from unauthorized host");
519 svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED);
520 return(&result);
521 }
522
523 /*
524 * Step one: find the user. (It's kinda pointless to
525 * proceed if the user doesn't exist.) We look for the
526 * user in the master.passwd.byname database, _NOT_ by
527 * using getpwent() and friends! We can't use getpwent()
528 * since the NIS master server is not guaranteed to be
529 * configured as an NIS client.
530 */
531
532 if (multidomain) {
533 if ((domain = find_domain(&argp->newpw)) == NULL) {
534 yp_error("multidomain lookup failed - aborting update");
535 return(&result);
536 } else
537 yp_error("updating user %s in domain %s",
538 argp->newpw.pw_name, domain);
539 }
540
541 key.data = argp->newpw.pw_name;
542 key.size = strlen(argp->newpw.pw_name);
543
544 if ((rval = yp_get_record(domain,"master.passwd.byname",
545 &key, &data, 0)) != YP_TRUE) {
546 if (rval == YP_NOKEY) {
547 yp_error("user %s not found in passwd database",
548 argp->newpw.pw_name);
549 } else {
550 yp_error("database access error: %s",
551 yperr_string(rval));
552 }
553 return(&result);
554 }
555
556 /* Nul terminate, please. */
557 *((char *)data.data + data.size) = '\0';
558
559 copy_yp_pass(data.data, 1, data.size);
560
561 /* Step 2: check that the supplied oldpass is valid. */
562
563 cryptpw = crypt(argp->oldpass, yp_password.pw_passwd);
564 if (cryptpw == NULL || strcmp(cryptpw, yp_password.pw_passwd)) {
565 yp_error("rejected change attempt -- bad password");
566 yp_error("client address: %s username: %s",
567 inet_ntoa(rqhost->sin_addr),
568 argp->newpw.pw_name);
569 return(&result);
570 }
571
572 /* Step 3: validate the arguments passed to us by the client. */
573
574 if (validate(&yp_password, &argp->newpw)) {
575 yp_error("rejecting change attempt: bad arguments");
576 yp_error("client address: %s username: %s",
577 inet_ntoa(rqhost->sin_addr),
578 argp->newpw.pw_name);
579 svcerr_decode(rqstp->rq_xprt);
580 return(&result);
581 }
582
583 /* Step 4: update the user's passwd structure. */
584
585 if (!no_chsh && strcmp(argp->newpw.pw_shell, yp_password.pw_shell)) {
586 oldshell = yp_password.pw_shell;
587 yp_password.pw_shell = argp->newpw.pw_shell;
588 shell_changed++;
589 }
590
591
592 if (!no_chfn && strcmp(argp->newpw.pw_gecos, yp_password.pw_gecos)) {
593 oldgecos = yp_password.pw_gecos;
594 yp_password.pw_gecos = argp->newpw.pw_gecos;
595 gecos_changed++;
596 }
597
598 if (strcmp(argp->newpw.pw_passwd, yp_password.pw_passwd)) {
599 yp_password.pw_passwd = argp->newpw.pw_passwd;
600 yp_password.pw_change = 0;
601 passwd_changed++;
602 }
603
604 /*
605 * If the caller specified a domain other than our 'default'
606 * domain, change the path to master.passwd accordingly.
607 */
608
609 if (strcmp(domain, yppasswd_domain)) {
610 snprintf(passfile_buf, sizeof(passfile_buf),
611 "%s/%s/master.passwd", yp_dir, domain);
612 passfile = (char *)&passfile_buf;
613 }
614
615 /*
616 * Create a filename to hold the original master.passwd
617 * so if our call to yppwupdate fails we can roll back
618 */
619 snprintf(passfile_hold_buf, sizeof(passfile_hold_buf),
620 "%s.hold", passfile);
621 passfile_hold = (char *)&passfile_hold_buf;
622
623
624 /* Step 5: make a new password file with the updated info. */
625
626 snprintf(passdir_buf, sizeof(passdir_buf), "%s", passfile);
627 passdir = dirname(passdir_buf);
628
629 if (pw_init(passdir, passfile)) {
630 yp_error("pw_init() failed");
631 return &result;
632 }
633 if ((pfd = pw_lock()) == -1) {
634 pw_fini();
635 yp_error("pw_lock() failed");
636 return &result;
637 }
638 if ((tfd = pw_tmp(-1)) == -1) {
639 pw_fini();
640 yp_error("pw_tmp() failed");
641 return &result;
642 }
643 if (pw_copy(pfd, tfd, &yp_password, NULL) == -1) {
644 pw_fini();
645 yp_error("pw_copy() failed");
646 return &result;
647 }
648 if (rename(passfile, passfile_hold) == -1) {
649 pw_fini();
650 yp_error("rename of %s to %s failed", passfile,
651 passfile_hold);
652 return &result;
653 }
654
655 if (strcmp(passfile, _PATH_MASTERPASSWD) == 0) {
656 /*
657 * NIS server is exporting the system's master.passwd.
658 * Call pw_mkdb to rebuild passwd and the .db files
659 */
660 if (pw_mkdb(yp_password.pw_name) == -1) {
661 pw_fini();
662 yp_error("pw_mkdb() failed");
663 rename(passfile_hold, passfile);
664 return &result;
665 }
666 } else {
667 /*
668 * NIS server is exporting a private master.passwd.
669 * Rename tempfile into final location
670 */
671 if (rename(pw_tempname(), passfile) == -1) {
672 pw_fini();
673 yp_error("rename of %s to %s failed",
674 pw_tempname(), passfile);
675 rename(passfile_hold, passfile);
676 return &result;
677 }
678 }
679
680 pw_fini();
681
682 if (inplace) {
683 if ((rval = update_inplace(&yp_password, domain))) {
684 yp_error("inplace update failed -- rebuilding maps");
685 }
686 }
687
688 switch ((pid = fork())) {
689 case 0:
690 if (inplace && !rval) {
691 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
692 yppasswd_domain, "pushpw", (char *)NULL);
693 } else {
694 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
695 yppasswd_domain, (char *)NULL);
696 }
697 yp_error("couldn't exec map update process: %s",
698 strerror(errno));
699 unlink(passfile);
700 rename(passfile_hold, passfile);
701 exit(1);
702 break;
703 case -1:
704 yp_error("fork() failed: %s", strerror(errno));
705 unlink(passfile);
706 rename(passfile_hold, passfile);
707 return(&result);
708 break;
709 default:
710 unlink(passfile_hold);
711 break;
712 }
713
714 if (verbose) {
715 yp_error("update completed for user %s (uid %d) in %s:",
716 argp->newpw.pw_name, argp->newpw.pw_uid, passfile);
717
718 if (passwd_changed)
719 yp_error("password changed");
720
721 if (gecos_changed)
722 yp_error("gecos changed ('%s' -> '%s')",
723 oldgecos, argp->newpw.pw_gecos);
724
725 if (shell_changed)
726 yp_error("shell changed ('%s' -> '%s')",
727 oldshell, argp->newpw.pw_shell);
728 }
729
730 result = 0;
731 return (&result);
732 }
733
734 /*
735 * Note that this function performs a little less sanity checking
736 * than the last one. Since only the superuser is allowed to use it,
737 * it is assumed that the caller knows what he's doing.
738 */
739 int *
yppasswdproc_update_master_1_svc(master_yppasswd * argp,struct svc_req * rqstp)740 yppasswdproc_update_master_1_svc(master_yppasswd *argp,
741 struct svc_req *rqstp)
742 {
743 static int result;
744 int pfd, tfd;
745 int pid;
746 uid_t uid;
747 int rval = 0;
748 DBT key, data;
749 char *passdir;
750 char *passfile_hold;
751 char passdir_buf[MAXPATHLEN + 2];
752 char passfile_buf[MAXPATHLEN + 2];
753 char passfile_hold_buf[MAXPATHLEN + 2];
754 struct sockaddr_in *rqhost;
755 SVCXPRT *transp;
756 struct passwd newpasswd;
757
758 result = 1;
759 transp = rqstp->rq_xprt;
760
761 /*
762 * NO AF_INET CONNETCIONS ALLOWED!
763 */
764 rqhost = svc_getcaller(transp);
765 if (rqhost->sin_family != AF_UNIX) {
766 yp_error("Alert! %s/%d attempted to use superuser-only \
767 procedure!\n", inet_ntoa(rqhost->sin_addr), rqhost->sin_port);
768 svcerr_auth(transp, AUTH_BADCRED);
769 return(&result);
770 }
771
772 if (rqstp->rq_cred.oa_flavor != AUTH_SYS) {
773 yp_error("caller didn't send proper credentials");
774 svcerr_auth(transp, AUTH_BADCRED);
775 return(&result);
776 }
777
778 if (__rpc_get_local_uid(transp, &uid) < 0) {
779 yp_error("caller didn't send proper credentials");
780 svcerr_auth(transp, AUTH_BADCRED);
781 return(&result);
782 }
783
784 if (uid) {
785 yp_error("caller euid is %d, expecting 0 -- rejecting request",
786 uid);
787 svcerr_auth(rqstp->rq_xprt, AUTH_BADCRED);
788 return(&result);
789 }
790
791 passfile = passfile_default;
792
793 key.data = argp->newpw.pw_name;
794 key.size = strlen(argp->newpw.pw_name);
795
796 /*
797 * The superuser may add entries to the passwd maps if
798 * rpc.yppasswdd is started with the -a flag. Paranoia
799 * prevents me from allowing additions by default.
800 */
801 if ((rval = yp_get_record(argp->domain, "master.passwd.byname",
802 &key, &data, 0)) != YP_TRUE) {
803 if (rval == YP_NOKEY) {
804 yp_error("user %s not found in passwd database",
805 argp->newpw.pw_name);
806 if (allow_additions)
807 yp_error("notice: adding user %s to \
808 master.passwd database for domain %s", argp->newpw.pw_name, argp->domain);
809 else
810 yp_error("restart rpc.yppasswdd with the -a flag to \
811 allow additions to be made to the password database");
812 } else {
813 yp_error("database access error: %s",
814 yperr_string(rval));
815 }
816 if (!allow_additions)
817 return(&result);
818 } else {
819
820 /* Nul terminate, please. */
821 *((char *)data.data + data.size) = '\0';
822
823 copy_yp_pass(data.data, 1, data.size);
824 }
825
826 /*
827 * Perform a small bit of sanity checking.
828 */
829 if (validate_master(rval == YP_TRUE ? &yp_password:NULL,&argp->newpw)){
830 yp_error("rejecting update attempt for %s: bad arguments",
831 argp->newpw.pw_name);
832 return(&result);
833 }
834
835 /*
836 * If the caller specified a domain other than our 'default'
837 * domain, change the path to master.passwd accordingly.
838 */
839
840 if (strcmp(argp->domain, yppasswd_domain)) {
841 snprintf(passfile_buf, sizeof(passfile_buf),
842 "%s/%s/master.passwd", yp_dir, argp->domain);
843 passfile = (char *)&passfile_buf;
844 }
845
846 /*
847 * Create a filename to hold the original master.passwd
848 * so if our call to yppwupdate fails we can roll back
849 */
850 snprintf(passfile_hold_buf, sizeof(passfile_hold_buf),
851 "%s.hold", passfile);
852 passfile_hold = (char *)&passfile_hold_buf;
853
854 snprintf(passdir_buf, sizeof(passdir_buf), "%s", passfile);
855 passdir = dirname(passdir_buf);
856
857 if (pw_init(passdir, passfile)) {
858 yp_error("pw_init() failed");
859 return &result;
860 }
861 if ((pfd = pw_lock()) == -1) {
862 pw_fini();
863 yp_error("pw_lock() failed");
864 return &result;
865 }
866 if ((tfd = pw_tmp(-1)) == -1) {
867 pw_fini();
868 yp_error("pw_tmp() failed");
869 return &result;
870 }
871 xlate_passwd(&argp->newpw, &newpasswd);
872 if (pw_copy(pfd, tfd, &newpasswd, NULL) == -1) {
873 pw_fini();
874 yp_error("pw_copy() failed");
875 return &result;
876 }
877 if (rename(passfile, passfile_hold) == -1) {
878 pw_fini();
879 yp_error("rename of %s to %s failed", passfile,
880 passfile_hold);
881 return &result;
882 }
883 if (strcmp(passfile, _PATH_MASTERPASSWD) == 0) {
884 /*
885 * NIS server is exporting the system's master.passwd.
886 * Call pw_mkdb to rebuild passwd and the .db files
887 */
888 if (pw_mkdb(argp->newpw.pw_name) == -1) {
889 pw_fini();
890 yp_error("pw_mkdb() failed");
891 rename(passfile_hold, passfile);
892 return &result;
893 }
894 } else {
895 /*
896 * NIS server is exporting a private master.passwd.
897 * Rename tempfile into final location
898 */
899 if (rename(pw_tempname(), passfile) == -1) {
900 pw_fini();
901 yp_error("rename of %s to %s failed",
902 pw_tempname(), passfile);
903 rename(passfile_hold, passfile);
904 return &result;
905 }
906 }
907 pw_fini();
908
909 if (inplace) {
910 xlate_passwd(&argp->newpw, &newpasswd);
911 if ((rval = update_inplace(&newpasswd, argp->domain))) {
912 yp_error("inplace update failed -- rebuilding maps");
913 }
914 }
915
916 switch ((pid = fork())) {
917 case 0:
918 if (inplace && !rval) {
919 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
920 argp->domain, "pushpw", (char *)NULL);
921 } else {
922 execlp(MAP_UPDATE_PATH, MAP_UPDATE, passfile,
923 argp->domain, (char *)NULL);
924 }
925 yp_error("couldn't exec map update process: %s",
926 strerror(errno));
927 unlink(passfile);
928 rename(passfile_hold, passfile);
929 exit(1);
930 break;
931 case -1:
932 yp_error("fork() failed: %s", strerror(errno));
933 unlink(passfile);
934 rename(passfile_hold, passfile);
935 return(&result);
936 break;
937 default:
938 unlink(passfile_hold);
939 break;
940 }
941
942 yp_error("performed update of user %s (uid %d) domain %s",
943 argp->newpw.pw_name,
944 argp->newpw.pw_uid,
945 argp->domain);
946
947 result = 0;
948 return(&result);
949 }
950