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