pw_nis.c revision 21330
121330Sdavidn/*-
221330Sdavidn * Copyright (C) 1996
321330Sdavidn *	David L. Nugent.  All rights reserved.
421330Sdavidn *
521330Sdavidn * Redistribution and use in source and binary forms, with or without
621330Sdavidn * modification, are permitted provided that the following conditions
721330Sdavidn * are met:
821330Sdavidn * 1. Redistributions of source code must retain the above copyright
921330Sdavidn *    notice, this list of conditions and the following disclaimer.
1021330Sdavidn * 2. Redistributions in binary form must reproduce the above copyright
1121330Sdavidn *    notice, this list of conditions and the following disclaimer in the
1221330Sdavidn *    documentation and/or other materials provided with the distribution.
1321330Sdavidn *
1421330Sdavidn * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
1521330Sdavidn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1621330Sdavidn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1721330Sdavidn * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
1821330Sdavidn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1921330Sdavidn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2021330Sdavidn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2121330Sdavidn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2221330Sdavidn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2321330Sdavidn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2421330Sdavidn * SUCH DAMAGE.
2521330Sdavidn *
2621330Sdavidn *	$Id$
2721330Sdavidn */
2821330Sdavidn
2921330Sdavidn#include <stdio.h>
3021330Sdavidn#include <stdlib.h>
3121330Sdavidn#include <string.h>
3221330Sdavidn#include <sys/types.h>
3321330Sdavidn
3421330Sdavidn#include "pwupd.h"
3521330Sdavidn#include "pw.h"
3621330Sdavidn
3721330Sdavidnstatic int
3821330Sdavidnpw_nisupdate(const char * path, struct passwd * pwd, char const * user, int mode)
3921330Sdavidn{
4021330Sdavidn	char            pfx[32];
4121330Sdavidn	char            pwbuf[PWBUFSZ];
4221330Sdavidn	int             l = sprintf(pfx, "%s:", user);
4321330Sdavidn
4421330Sdavidn	/*
4521330Sdavidn	 * Update the passwd file first
4621330Sdavidn	 */
4721330Sdavidn	if (pwd == NULL)
4821330Sdavidn		*pwbuf = '\0';
4921330Sdavidn	else
5021330Sdavidn		fmtpwentry(pwbuf, pwd, PWF_MASTER);
5121330Sdavidn	return fileupdate(path, 0600, pwbuf, pfx, l, mode) != 0;
5221330Sdavidn}
5321330Sdavidn
5421330Sdavidnint
5521330Sdavidnaddnispwent(const char *path, struct passwd * pwd)
5621330Sdavidn{
5721330Sdavidn	return pw_nisupdate(path, pwd, pwd->pw_name, UPD_CREATE);
5821330Sdavidn}
5921330Sdavidn
6021330Sdavidnint
6121330Sdavidnchgnispwent(const char *path, char const * login, struct passwd * pwd)
6221330Sdavidn{
6321330Sdavidn	return pw_nisupdate(path, pwd, login, UPD_REPLACE);
6421330Sdavidn}
6521330Sdavidn
6621330Sdavidnint
6721330Sdavidndelnispwent(const char *path, const char *login)
6821330Sdavidn{
6921330Sdavidn	return pw_nisupdate(path, NULL, login, UPD_DELETE);
7021330Sdavidn}
71