grupd.c revision 287084
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: stable/10/usr.sbin/pw/grupd.c 287084 2015-08-23 21:42:27Z bapt $";
3030259Scharnier#endif /* not lint */
3130259Scharnier
32287084Sbapt#include <err.h>
33242349Sbapt#include <grp.h>
34242349Sbapt#include <libutil.h>
3520253Sjoerg#include <stdio.h>
3620253Sjoerg#include <stdlib.h>
3720253Sjoerg
3820253Sjoerg#include "pwupd.h"
3920253Sjoerg
4044229Sdavidnchar *
4144229Sdavidngetgrpath(const char * file)
4244229Sdavidn{
4344229Sdavidn	static char pathbuf[MAXPATHLEN];
4444229Sdavidn
45285092Sbapt	snprintf(pathbuf, sizeof pathbuf, "%s/%s", conf.etcpath, file);
46285092Sbapt
47285092Sbapt	return (pathbuf);
4844229Sdavidn}
4944229Sdavidn
50242349Sbaptstatic int
51242349Sbaptgr_update(struct group * grp, char const * group)
5244229Sdavidn{
53242349Sbapt	int pfd, tfd;
54242349Sbapt	struct group *gr = NULL;
55242349Sbapt	struct group *old_gr = NULL;
5644229Sdavidn
57242349Sbapt	if (grp != NULL)
58242349Sbapt		gr = gr_dup(grp);
5920253Sjoerg
60242349Sbapt	if (group != NULL)
61242349Sbapt		old_gr = GETGRNAM(group);
6220747Sdavidn
63285092Sbapt	if (gr_init(conf.etcpath, NULL))
64242349Sbapt		err(1, "gr_init()");
6520747Sdavidn
66242349Sbapt	if ((pfd = gr_lock()) == -1) {
67242349Sbapt		gr_fini();
68242349Sbapt		err(1, "gr_lock()");
6920747Sdavidn	}
70242349Sbapt	if ((tfd = gr_tmp(-1)) == -1) {
71242349Sbapt		gr_fini();
72242349Sbapt		err(1, "gr_tmp()");
7344229Sdavidn	}
74242349Sbapt	if (gr_copy(pfd, tfd, gr, old_gr) == -1) {
75242349Sbapt		gr_fini();
76242349Sbapt		err(1, "gr_copy()");
77242349Sbapt	}
78242349Sbapt	if (gr_mkdb() == -1) {
79242349Sbapt		gr_fini();
80242349Sbapt		err(1, "gr_mkdb()");
81242349Sbapt	}
82242349Sbapt	free(gr);
83242349Sbapt	gr_fini();
84242349Sbapt	return 0;
8520253Sjoerg}
8620253Sjoerg
8720253Sjoerg
8820253Sjoergint
8920253Sjoergaddgrent(struct group * grp)
9020253Sjoerg{
91242349Sbapt	return gr_update(grp, NULL);
9220253Sjoerg}
9320253Sjoerg
9420253Sjoergint
9520253Sjoergchggrent(char const * login, struct group * grp)
9620253Sjoerg{
97242349Sbapt	return gr_update(grp, login);
9820253Sjoerg}
9920253Sjoerg
10020253Sjoergint
10120253Sjoergdelgrent(struct group * grp)
10220253Sjoerg{
103242349Sbapt
104285092Sbapt	return (gr_update(NULL, grp->gr_name));
10520253Sjoerg}
106