120253Sjoerg/*-
220302Sjoerg * Copyright (C) 1996
320302Sjoerg *	David L. Nugent.  All rights reserved.
420253Sjoerg *
520253Sjoerg * Redistribution and use in source and binary forms, with or without
620253Sjoerg * modification, are permitted provided that the following conditions
720253Sjoerg * are met:
820253Sjoerg * 1. Redistributions of source code must retain the above copyright
920302Sjoerg *    notice, this list of conditions and the following disclaimer.
1020253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1120253Sjoerg *    notice, this list of conditions and the following disclaimer in the
1220253Sjoerg *    documentation and/or other materials provided with the distribution.
1320253Sjoerg *
1420302Sjoerg * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
1520253Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1620253Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1720302Sjoerg * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
1820253Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1920253Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2020253Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2120253Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2220253Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2320253Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2420253Sjoerg * SUCH DAMAGE.
2520253Sjoerg */
2620253Sjoerg
2730259Scharnier#ifndef lint
2830259Scharnierstatic const char rcsid[] =
2950479Speter  "$FreeBSD$";
3030259Scharnier#endif /* not lint */
3130259Scharnier
32242349Sbapt#include <grp.h>
33242349Sbapt#include <libutil.h>
34242349Sbapt#include <err.h>
3520253Sjoerg#include <stdio.h>
3620253Sjoerg#include <stdlib.h>
3720253Sjoerg#include <string.h>
3820253Sjoerg#include <unistd.h>
3920253Sjoerg#include <stdarg.h>
4020253Sjoerg#include <sys/types.h>
4120253Sjoerg#include <sys/stat.h>
4244229Sdavidn#include <sys/param.h>
4320253Sjoerg
4420253Sjoerg#include "pwupd.h"
4520253Sjoerg
4644229Sdavidnstatic char * grpath = _PATH_PWD;
4744229Sdavidn
4820253Sjoergint
4944229Sdavidnsetgrdir(const char * dir)
5044229Sdavidn{
5144229Sdavidn	if (dir == NULL)
5244229Sdavidn		return -1;
53243898Seadler	else
54243898Seadler		grpath = strdup(dir);
55243898Seadler	if (grpath == NULL)
56243898Seadler		return -1;
57243898Seadler
5844229Sdavidn	return 0;
5944229Sdavidn}
6044229Sdavidn
6144229Sdavidnchar *
6244229Sdavidngetgrpath(const char * file)
6344229Sdavidn{
6444229Sdavidn	static char pathbuf[MAXPATHLEN];
6544229Sdavidn
6644229Sdavidn	snprintf(pathbuf, sizeof pathbuf, "%s/%s", grpath, file);
6744229Sdavidn	return pathbuf;
6844229Sdavidn}
6944229Sdavidn
70242349Sbaptstatic int
71242349Sbaptgr_update(struct group * grp, char const * group)
7244229Sdavidn{
73242349Sbapt	int pfd, tfd;
74242349Sbapt	struct group *gr = NULL;
75242349Sbapt	struct group *old_gr = NULL;
7644229Sdavidn
77242349Sbapt	if (grp != NULL)
78242349Sbapt		gr = gr_dup(grp);
7920253Sjoerg
80242349Sbapt	if (group != NULL)
81242349Sbapt		old_gr = GETGRNAM(group);
8220747Sdavidn
83242349Sbapt	if (gr_init(grpath, NULL))
84242349Sbapt		err(1, "gr_init()");
8520747Sdavidn
86242349Sbapt	if ((pfd = gr_lock()) == -1) {
87242349Sbapt		gr_fini();
88242349Sbapt		err(1, "gr_lock()");
8920747Sdavidn	}
90242349Sbapt	if ((tfd = gr_tmp(-1)) == -1) {
91242349Sbapt		gr_fini();
92242349Sbapt		err(1, "gr_tmp()");
9344229Sdavidn	}
94242349Sbapt	if (gr_copy(pfd, tfd, gr, old_gr) == -1) {
95242349Sbapt		gr_fini();
96242349Sbapt		err(1, "gr_copy()");
97242349Sbapt	}
98242349Sbapt	if (gr_mkdb() == -1) {
99242349Sbapt		gr_fini();
100242349Sbapt		err(1, "gr_mkdb()");
101242349Sbapt	}
102242349Sbapt	free(gr);
103242349Sbapt	gr_fini();
104242349Sbapt	return 0;
10520253Sjoerg}
10620253Sjoerg
10720253Sjoerg
10820253Sjoergint
10920253Sjoergaddgrent(struct group * grp)
11020253Sjoerg{
111242349Sbapt	return gr_update(grp, NULL);
11220253Sjoerg}
11320253Sjoerg
11420253Sjoergint
11520253Sjoergchggrent(char const * login, struct group * grp)
11620253Sjoerg{
117242349Sbapt	return gr_update(grp, login);
11820253Sjoerg}
11920253Sjoerg
12020253Sjoergint
12120253Sjoergdelgrent(struct group * grp)
12220253Sjoerg{
123242349Sbapt	char group[MAXLOGNAME];
124242349Sbapt
125242349Sbapt	strlcpy(group, grp->gr_name, MAXLOGNAME);
126242349Sbapt
127242349Sbapt	return gr_update(NULL, group);
12820253Sjoerg}
129