2006-05-25 07:17:43 +04:00
|
|
|
/* $NetBSD: yppasswdd_mkpw.c,v 1.15 2006/05/25 03:17:43 christos Exp $ */
|
1996-08-09 14:19:48 +04:00
|
|
|
|
|
|
|
/*
|
2003-11-12 16:31:07 +03:00
|
|
|
* Copyright (c) 1996 Jason R. Thorpe <thorpej@NetBSD.org>
|
1996-08-09 14:19:48 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Mats O Jansson
|
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2000-08-03 12:22:33 +04:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2006-05-25 07:17:43 +04:00
|
|
|
__RCSID("$NetBSD: yppasswdd_mkpw.c,v 1.15 2006/05/25 03:17:43 christos Exp $");
|
2000-08-03 12:22:33 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1996-08-09 14:19:48 +04:00
|
|
|
#include <sys/types.h>
|
2000-12-09 01:23:14 +03:00
|
|
|
#include <sys/param.h>
|
1996-08-09 14:19:48 +04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/wait.h>
|
2000-08-03 12:22:33 +04:00
|
|
|
|
1997-07-18 11:47:26 +04:00
|
|
|
#include <err.h>
|
1996-08-09 14:19:48 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdlib.h>
|
2000-07-05 00:27:35 +04:00
|
|
|
#include <string.h>
|
1996-08-09 14:19:48 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <util.h>
|
2000-08-03 12:22:33 +04:00
|
|
|
#include <limits.h>
|
1996-08-09 14:19:48 +04:00
|
|
|
|
|
|
|
#include <rpc/rpc.h>
|
|
|
|
#include <rpc/xdr.h>
|
|
|
|
#include <rpcsvc/yppasswd.h>
|
|
|
|
|
2000-08-03 12:22:33 +04:00
|
|
|
#include "extern.h"
|
1996-08-09 14:19:48 +04:00
|
|
|
|
|
|
|
int handling_request; /* simple mutex */
|
|
|
|
|
|
|
|
void
|
2000-08-03 12:22:33 +04:00
|
|
|
make_passwd(yppasswd *argp, struct svc_req *rqstp, SVCXPRT *transp)
|
1996-08-09 14:19:48 +04:00
|
|
|
{
|
2002-07-31 18:41:56 +04:00
|
|
|
struct passwd pw;
|
1997-07-18 11:47:26 +04:00
|
|
|
int pfd, tfd;
|
2000-12-09 01:23:14 +03:00
|
|
|
char mpwd[MAXPATHLEN];
|
2002-07-31 18:41:56 +04:00
|
|
|
char buf[8192]; /* from libutil */
|
Problem: rpc.yppasswdd didn't work if invoked with the "-noshell" option.
Reason: In routine make_passwd() in yppasswdd_mkpw.c, fgets() is used
to read master.passwd line-by-line. The lines are then parsed using
pw_scan(), without removing the trailing \n from the line. pw_scan()
fills in the shell name into pw.pw_shell, including the trailing \n.
Subsequently, rpc.yppasswdd calls pw_copy(), which copies master.passwd
to ptmp, updating the entry for pw.pw_name. pw_copy() terminates the
updated entry with an additonal \n, so that there is now an empty line
in ptmp.
Finally, rpc.yppasswdd calls pw_mkdb(3), which exec's /usr/sbin/pwd_mkdb
to install ptmp to master.passwd (and to create the pwd.db and spwd.db
data bases). pwd_mkdb chokes on the empty ptmp line.
Fix: remove the trailing \n from the buffer fgets() returns. As a side
effect, this gets us some additional error checking.
Reviewed by <tron>.
2004-05-12 12:25:53 +04:00
|
|
|
char *p;
|
|
|
|
int lineno;
|
2002-07-31 18:41:56 +04:00
|
|
|
FILE *fpw;
|
1996-08-09 14:19:48 +04:00
|
|
|
|
|
|
|
#define REPLY(val) do { \
|
|
|
|
int res = (val); \
|
|
|
|
if (!svc_sendreply(transp, xdr_int, (caddr_t)&res)) \
|
|
|
|
svcerr_systemerr(transp); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define RETURN(val) do { \
|
|
|
|
REPLY((val)); \
|
|
|
|
handling_request = 0; \
|
|
|
|
return; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
if (handling_request) {
|
|
|
|
warnx("already handling request; try again later");
|
|
|
|
REPLY(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
handling_request = 1;
|
|
|
|
|
2002-07-31 18:41:56 +04:00
|
|
|
(void)strlcpy(mpwd, pw_getprefix(), sizeof(mpwd));
|
|
|
|
(void)strlcat(mpwd, _PATH_MASTERPASSWD, sizeof(mpwd));
|
|
|
|
fpw = fopen(mpwd, "r");
|
|
|
|
if (fpw == NULL) {
|
|
|
|
warnx("%s", mpwd);
|
1996-08-09 14:19:48 +04:00
|
|
|
RETURN(1);
|
2002-07-31 18:41:56 +04:00
|
|
|
}
|
Problem: rpc.yppasswdd didn't work if invoked with the "-noshell" option.
Reason: In routine make_passwd() in yppasswdd_mkpw.c, fgets() is used
to read master.passwd line-by-line. The lines are then parsed using
pw_scan(), without removing the trailing \n from the line. pw_scan()
fills in the shell name into pw.pw_shell, including the trailing \n.
Subsequently, rpc.yppasswdd calls pw_copy(), which copies master.passwd
to ptmp, updating the entry for pw.pw_name. pw_copy() terminates the
updated entry with an additonal \n, so that there is now an empty line
in ptmp.
Finally, rpc.yppasswdd calls pw_mkdb(3), which exec's /usr/sbin/pwd_mkdb
to install ptmp to master.passwd (and to create the pwd.db and spwd.db
data bases). pwd_mkdb chokes on the empty ptmp line.
Fix: remove the trailing \n from the buffer fgets() returns. As a side
effect, this gets us some additional error checking.
Reviewed by <tron>.
2004-05-12 12:25:53 +04:00
|
|
|
for(lineno = 1; ; lineno++) {
|
2002-07-31 18:41:56 +04:00
|
|
|
if (fgets(buf, sizeof(buf), fpw) == NULL) {
|
|
|
|
if (feof(fpw))
|
|
|
|
warnx("%s: %s not found", mpwd,
|
|
|
|
argp->newpw.pw_name);
|
|
|
|
else
|
|
|
|
warnx("%s: %s", mpwd, strerror(errno));
|
2006-05-25 07:17:26 +04:00
|
|
|
(void)fclose(fpw);
|
2002-07-31 18:41:56 +04:00
|
|
|
RETURN(1);
|
|
|
|
}
|
Problem: rpc.yppasswdd didn't work if invoked with the "-noshell" option.
Reason: In routine make_passwd() in yppasswdd_mkpw.c, fgets() is used
to read master.passwd line-by-line. The lines are then parsed using
pw_scan(), without removing the trailing \n from the line. pw_scan()
fills in the shell name into pw.pw_shell, including the trailing \n.
Subsequently, rpc.yppasswdd calls pw_copy(), which copies master.passwd
to ptmp, updating the entry for pw.pw_name. pw_copy() terminates the
updated entry with an additonal \n, so that there is now an empty line
in ptmp.
Finally, rpc.yppasswdd calls pw_mkdb(3), which exec's /usr/sbin/pwd_mkdb
to install ptmp to master.passwd (and to create the pwd.db and spwd.db
data bases). pwd_mkdb chokes on the empty ptmp line.
Fix: remove the trailing \n from the buffer fgets() returns. As a side
effect, this gets us some additional error checking.
Reviewed by <tron>.
2004-05-12 12:25:53 +04:00
|
|
|
if ((p = strchr(buf, '\n')) == NULL) {
|
|
|
|
warnx("line %d too long", lineno);
|
2006-05-25 07:17:43 +04:00
|
|
|
(void)fclose(fpw);
|
Problem: rpc.yppasswdd didn't work if invoked with the "-noshell" option.
Reason: In routine make_passwd() in yppasswdd_mkpw.c, fgets() is used
to read master.passwd line-by-line. The lines are then parsed using
pw_scan(), without removing the trailing \n from the line. pw_scan()
fills in the shell name into pw.pw_shell, including the trailing \n.
Subsequently, rpc.yppasswdd calls pw_copy(), which copies master.passwd
to ptmp, updating the entry for pw.pw_name. pw_copy() terminates the
updated entry with an additonal \n, so that there is now an empty line
in ptmp.
Finally, rpc.yppasswdd calls pw_mkdb(3), which exec's /usr/sbin/pwd_mkdb
to install ptmp to master.passwd (and to create the pwd.db and spwd.db
data bases). pwd_mkdb chokes on the empty ptmp line.
Fix: remove the trailing \n from the buffer fgets() returns. As a side
effect, this gets us some additional error checking.
Reviewed by <tron>.
2004-05-12 12:25:53 +04:00
|
|
|
RETURN(1);
|
|
|
|
}
|
|
|
|
/* get rid of trailing \n */
|
|
|
|
*p = '\0';
|
2002-07-31 18:41:56 +04:00
|
|
|
if (pw_scan(buf, &pw, NULL) == 0)
|
|
|
|
continue;
|
|
|
|
if (strncmp(argp->newpw.pw_name, pw.pw_name, MAXLOGNAME) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fclose(fpw);
|
|
|
|
if (*pw.pw_passwd &&
|
|
|
|
strcmp(crypt(argp->oldpass, pw.pw_passwd), pw.pw_passwd) != 0)
|
1996-08-09 14:19:48 +04:00
|
|
|
RETURN(1);
|
|
|
|
|
|
|
|
pw_init();
|
|
|
|
tfd = pw_lock(0);
|
|
|
|
if (tfd < 0) {
|
|
|
|
warnx("the passwd file is busy.");
|
|
|
|
RETURN(1);
|
|
|
|
}
|
2000-12-09 01:23:14 +03:00
|
|
|
pfd = open(mpwd, O_RDONLY, 0);
|
1996-08-09 14:19:48 +04:00
|
|
|
if (pfd < 0) {
|
|
|
|
pw_abort();
|
2000-12-09 01:23:14 +03:00
|
|
|
warnx("%s", mpwd);
|
1996-08-09 14:19:48 +04:00
|
|
|
RETURN(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the new password. Reset passwd change time to zero; when
|
|
|
|
* classes are implemented, go and get the "offset" value for this
|
|
|
|
* class and reset the timer.
|
|
|
|
*/
|
|
|
|
if (!nopw) {
|
2002-07-31 18:41:56 +04:00
|
|
|
pw.pw_passwd = argp->newpw.pw_passwd;
|
|
|
|
pw.pw_change = 0;
|
1996-08-09 14:19:48 +04:00
|
|
|
}
|
|
|
|
if (!nogecos)
|
2002-07-31 18:41:56 +04:00
|
|
|
pw.pw_gecos = argp->newpw.pw_gecos;
|
1996-08-09 14:19:48 +04:00
|
|
|
if (!noshell)
|
2002-07-31 18:41:56 +04:00
|
|
|
pw.pw_shell = argp->newpw.pw_shell;
|
1996-08-09 14:19:48 +04:00
|
|
|
|
2002-07-31 18:41:56 +04:00
|
|
|
pw_copy(pfd, tfd, &pw, NULL);
|
1996-08-09 14:19:48 +04:00
|
|
|
|
2002-07-31 18:41:56 +04:00
|
|
|
if (pw_mkdb(pw.pw_name, 0) < 0) {
|
1996-08-09 14:19:48 +04:00
|
|
|
warnx("pw_mkdb failed");
|
|
|
|
pw_abort();
|
|
|
|
RETURN(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX RESTORE SIGNAL STATE? XXX */
|
|
|
|
|
|
|
|
/* Notify caller we succeeded. */
|
|
|
|
REPLY(0);
|
|
|
|
|
|
|
|
/* Update the YP maps. */
|
|
|
|
if (chdir("/var/yp"))
|
2000-08-03 12:22:33 +04:00
|
|
|
err(EXIT_FAILURE, "/var/yp");
|
1996-08-09 14:19:48 +04:00
|
|
|
(void) umask(022);
|
|
|
|
(void) system(make_arg);
|
|
|
|
|
|
|
|
handling_request = 0;
|
|
|
|
}
|