1 /* $NetBSD: yppasswdd_mkpw.c,v 1.17 2009/10/20 00:51:14 snj Exp $ */ 2 3 /* 4 * Copyright (c) 1996 Jason R. Thorpe <thorpej@NetBSD.org> 5 * All rights reserved. 6 * 7 * Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se> 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 __RCSID("$NetBSD: yppasswdd_mkpw.c,v 1.17 2009/10/20 00:51:14 snj Exp $"); 35 #endif /* not lint */ 36 37 #include <sys/types.h> 38 #include <sys/param.h> 39 #include <sys/stat.h> 40 #include <sys/time.h> 41 #include <sys/resource.h> 42 #include <sys/wait.h> 43 44 #include <err.h> 45 #include <errno.h> 46 #include <fcntl.h> 47 #include <stdio.h> 48 #include <pwd.h> 49 #include <signal.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <unistd.h> 53 #include <util.h> 54 #include <limits.h> 55 56 #include <rpc/rpc.h> 57 #include <rpc/xdr.h> 58 #include <rpcsvc/yppasswd.h> 59 60 #include "extern.h" 61 62 int handling_request; /* simple mutex */ 63 64 void 65 make_passwd(yppasswd *argp, struct svc_req *rqstp, SVCXPRT *transp) 66 { 67 struct passwd pw; 68 int pfd, tfd; 69 char mpwd[MAXPATHLEN]; 70 char buf[8192]; /* from libutil */ 71 char *p; 72 int lineno; 73 FILE *fpw; 74 75 #define REPLY(val) do { \ 76 int res = (val); \ 77 if (!svc_sendreply(transp, xdr_int, (caddr_t)&res)) \ 78 svcerr_systemerr(transp); \ 79 } while (0) 80 81 #define RETURN(val) do { \ 82 REPLY((val)); \ 83 handling_request = 0; \ 84 return; \ 85 } while (0) 86 87 if (handling_request) { 88 warnx("already handling request; try again later"); 89 REPLY(1); 90 return; 91 } 92 handling_request = 1; 93 94 (void)strlcpy(mpwd, pw_getprefix(), sizeof(mpwd)); 95 (void)strlcat(mpwd, _PATH_MASTERPASSWD, sizeof(mpwd)); 96 fpw = fopen(mpwd, "r"); 97 if (fpw == NULL) { 98 warnx("%s", mpwd); 99 RETURN(1); 100 } 101 for(lineno = 1; ; lineno++) { 102 if (fgets(buf, sizeof(buf), fpw) == NULL) { 103 if (feof(fpw)) 104 warnx("%s: %s not found", mpwd, 105 argp->newpw.pw_name); 106 else 107 warnx("%s: %s", mpwd, strerror(errno)); 108 (void)fclose(fpw); 109 RETURN(1); 110 } 111 if ((p = strchr(buf, '\n')) == NULL) { 112 warnx("line %d too long", lineno); 113 (void)fclose(fpw); 114 RETURN(1); 115 } 116 /* get rid of trailing \n */ 117 *p = '\0'; 118 if (pw_scan(buf, &pw, NULL) == 0) 119 continue; 120 if (strncmp(argp->newpw.pw_name, pw.pw_name, MAXLOGNAME) == 0) 121 break; 122 } 123 fclose(fpw); 124 if (*pw.pw_passwd && 125 strcmp(crypt(argp->oldpass, pw.pw_passwd), pw.pw_passwd) != 0) 126 RETURN(1); 127 128 pw_init(); 129 tfd = pw_lock(0); 130 if (tfd < 0) { 131 warnx("the passwd file is busy."); 132 RETURN(1); 133 } 134 pfd = open(mpwd, O_RDONLY, 0); 135 if (pfd < 0) { 136 pw_abort(); 137 warnx("%s", mpwd); 138 RETURN(1); 139 } 140 141 /* 142 * Get the new password. Reset passwd change time to zero; when 143 * classes are implemented, go and get the "offset" value for this 144 * class and reset the timer. 145 */ 146 if (!nopw) { 147 pw.pw_passwd = argp->newpw.pw_passwd; 148 pw.pw_change = 0; 149 } 150 if (!nogecos) 151 pw.pw_gecos = argp->newpw.pw_gecos; 152 if (!noshell) 153 pw.pw_shell = argp->newpw.pw_shell; 154 155 pw_copy(pfd, tfd, &pw, NULL); 156 157 if (pw_mkdb(pw.pw_name, 0) < 0) { 158 warnx("pw_mkdb failed"); 159 pw_abort(); 160 RETURN(1); 161 } 162 163 /* XXX RESTORE SIGNAL STATE? XXX */ 164 165 /* Notify caller we succeeded. */ 166 REPLY(0); 167 168 /* Update the YP maps. */ 169 if (chdir("/var/yp")) 170 err(EXIT_FAILURE, "/var/yp"); 171 (void) umask(022); 172 (void) system(make_arg); 173 174 handling_request = 0; 175 } 176