120253Sjoerg/*- 2330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3330449Seadler * 420302Sjoerg * Copyright (C) 1996 520302Sjoerg * David L. Nugent. All rights reserved. 620253Sjoerg * 720253Sjoerg * Redistribution and use in source and binary forms, with or without 820253Sjoerg * modification, are permitted provided that the following conditions 920253Sjoerg * are met: 1020253Sjoerg * 1. Redistributions of source code must retain the above copyright 1120302Sjoerg * notice, this list of conditions and the following disclaimer. 1220253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright 1320253Sjoerg * notice, this list of conditions and the following disclaimer in the 1420253Sjoerg * documentation and/or other materials provided with the distribution. 1520253Sjoerg * 1620302Sjoerg * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND 1720253Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1820253Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1920302Sjoerg * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE 2020253Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2120253Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2220253Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2320253Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2420253Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2520253Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2620253Sjoerg * SUCH DAMAGE. 2720253Sjoerg */ 2820253Sjoerg 2930259Scharnier#ifndef lint 3030259Scharnierstatic const char rcsid[] = 3150479Speter "$FreeBSD: stable/11/usr.sbin/pw/grupd.c 330449 2018-03-05 07:26:05Z eadler $"; 3230259Scharnier#endif /* not lint */ 3330259Scharnier 34286201Sbapt#include <err.h> 35242349Sbapt#include <grp.h> 36242349Sbapt#include <libutil.h> 3720253Sjoerg#include <stdio.h> 3820253Sjoerg#include <stdlib.h> 39308815Sasomers#include <unistd.h> 4020253Sjoerg 4120253Sjoerg#include "pwupd.h" 4220253Sjoerg 4344229Sdavidnchar * 4444229Sdavidngetgrpath(const char * file) 4544229Sdavidn{ 4644229Sdavidn static char pathbuf[MAXPATHLEN]; 4744229Sdavidn 48284118Sbapt snprintf(pathbuf, sizeof pathbuf, "%s/%s", conf.etcpath, file); 49284118Sbapt 50284118Sbapt return (pathbuf); 5144229Sdavidn} 5244229Sdavidn 53242349Sbaptstatic int 54242349Sbaptgr_update(struct group * grp, char const * group) 5544229Sdavidn{ 56242349Sbapt int pfd, tfd; 57242349Sbapt struct group *gr = NULL; 58242349Sbapt struct group *old_gr = NULL; 5944229Sdavidn 60242349Sbapt if (grp != NULL) 61242349Sbapt gr = gr_dup(grp); 6220253Sjoerg 63242349Sbapt if (group != NULL) 64242349Sbapt old_gr = GETGRNAM(group); 6520747Sdavidn 66284118Sbapt if (gr_init(conf.etcpath, NULL)) 67242349Sbapt err(1, "gr_init()"); 6820747Sdavidn 69242349Sbapt if ((pfd = gr_lock()) == -1) { 70242349Sbapt gr_fini(); 71242349Sbapt err(1, "gr_lock()"); 7220747Sdavidn } 73242349Sbapt if ((tfd = gr_tmp(-1)) == -1) { 74242349Sbapt gr_fini(); 75242349Sbapt err(1, "gr_tmp()"); 7644229Sdavidn } 77242349Sbapt if (gr_copy(pfd, tfd, gr, old_gr) == -1) { 78242349Sbapt gr_fini(); 79308815Sasomers close(tfd); 80242349Sbapt err(1, "gr_copy()"); 81242349Sbapt } 82310176Sasomers fsync(tfd); 83308815Sasomers close(tfd); 84242349Sbapt if (gr_mkdb() == -1) { 85242349Sbapt gr_fini(); 86242349Sbapt err(1, "gr_mkdb()"); 87242349Sbapt } 88242349Sbapt free(gr); 89242349Sbapt gr_fini(); 90242349Sbapt return 0; 9120253Sjoerg} 9220253Sjoerg 9320253Sjoerg 9420253Sjoergint 9520253Sjoergaddgrent(struct group * grp) 9620253Sjoerg{ 97242349Sbapt return gr_update(grp, NULL); 9820253Sjoerg} 9920253Sjoerg 10020253Sjoergint 10120253Sjoergchggrent(char const * login, struct group * grp) 10220253Sjoerg{ 103242349Sbapt return gr_update(grp, login); 10420253Sjoerg} 10520253Sjoerg 10620253Sjoergint 10720253Sjoergdelgrent(struct group * grp) 10820253Sjoerg{ 109242349Sbapt 110283810Sbapt return (gr_update(NULL, grp->gr_name)); 11120253Sjoerg} 112