grupd.c revision 287084
11553Srgrimes/*-
21553Srgrimes * Copyright (C) 1996
31553Srgrimes *	David L. Nugent.  All rights reserved.
41553Srgrimes *
51553Srgrimes * Redistribution and use in source and binary forms, with or without
61553Srgrimes * modification, are permitted provided that the following conditions
71553Srgrimes * are met:
81553Srgrimes * 1. Redistributions of source code must retain the above copyright
91553Srgrimes *    notice, this list of conditions and the following disclaimer.
101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111553Srgrimes *    notice, this list of conditions and the following disclaimer in the
121553Srgrimes *    documentation and/or other materials provided with the distribution.
131553Srgrimes *
141553Srgrimes * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
151553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
181553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241553Srgrimes * SUCH DAMAGE.
251553Srgrimes */
261553Srgrimes
271553Srgrimes#ifndef lint
281553Srgrimesstatic const char rcsid[] =
291553Srgrimes  "$FreeBSD: stable/10/usr.sbin/pw/grupd.c 287084 2015-08-23 21:42:27Z bapt $";
301553Srgrimes#endif /* not lint */
3129736Scharnier
321553Srgrimes#include <err.h>
331553Srgrimes#include <grp.h>
341553Srgrimes#include <libutil.h>
351553Srgrimes#include <stdio.h>
361553Srgrimes#include <stdlib.h>
3729736Scharnier
381553Srgrimes#include "pwupd.h"
3929736Scharnier
401553Srgrimeschar *
411553Srgrimesgetgrpath(const char * file)
42246792Scharnier{
43246792Scharnier	static char pathbuf[MAXPATHLEN];
44246792Scharnier
451553Srgrimes	snprintf(pathbuf, sizeof pathbuf, "%s/%s", conf.etcpath, file);
461553Srgrimes
4711937Sphk	return (pathbuf);
481553Srgrimes}
491553Srgrimes
5029736Scharnierstatic int
5129736Scharniergr_update(struct group * grp, char const * group)
521553Srgrimes{
531553Srgrimes	int pfd, tfd;
541553Srgrimes	struct group *gr = NULL;
5529736Scharnier	struct group *old_gr = NULL;
5629736Scharnier
5791009Sbde	if (grp != NULL)
581553Srgrimes		gr = gr_dup(grp);
591553Srgrimes
601553Srgrimes	if (group != NULL)
6129736Scharnier		old_gr = GETGRNAM(group);
621553Srgrimes
631553Srgrimes	if (gr_init(conf.etcpath, NULL))
641553Srgrimes		err(1, "gr_init()");
651553Srgrimes
661553Srgrimes	if ((pfd = gr_lock()) == -1) {
671553Srgrimes		gr_fini();
6829736Scharnier		err(1, "gr_lock()");
691553Srgrimes	}
701553Srgrimes	if ((tfd = gr_tmp(-1)) == -1) {
711553Srgrimes		gr_fini();
721553Srgrimes		err(1, "gr_tmp()");
731553Srgrimes	}
741553Srgrimes	if (gr_copy(pfd, tfd, gr, old_gr) == -1) {
751553Srgrimes		gr_fini();
7613107Sbde		err(1, "gr_copy()");
771553Srgrimes	}
7899800Salfred	if (gr_mkdb() == -1) {
7999800Salfred		gr_fini();
8099800Salfred		err(1, "gr_mkdb()");
81246792Scharnier	}
8299800Salfred	free(gr);
8399800Salfred	gr_fini();
8499800Salfred	return 0;
8599800Salfred}
861553Srgrimes
871553Srgrimes
881553Srgrimesint
891553Srgrimesaddgrent(struct group * grp)
901553Srgrimes{
911553Srgrimes	return gr_update(grp, NULL);
92246792Scharnier}
93246792Scharnier
941553Srgrimesint
95241848Seadlerchggrent(char const * login, struct group * grp)
96241848Seadler{
97241848Seadler	return gr_update(grp, login);
981553Srgrimes}
99246792Scharnier
10024428Simpint
1011553Srgrimesdelgrent(struct group * grp)
1021553Srgrimes{
1031553Srgrimes
1041553Srgrimes	return (gr_update(NULL, grp->gr_name));
1051553Srgrimes}
1061553Srgrimes