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 310173 2016-12-16 20:10:55Z asomers $"; 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> 37308814Sasomers#include <unistd.h> 3820253Sjoerg 3920253Sjoerg#include "pwupd.h" 4020253Sjoerg 4144229Sdavidnchar * 4244229Sdavidngetgrpath(const char * file) 4344229Sdavidn{ 4444229Sdavidn static char pathbuf[MAXPATHLEN]; 4544229Sdavidn 46285092Sbapt snprintf(pathbuf, sizeof pathbuf, "%s/%s", conf.etcpath, file); 47285092Sbapt 48285092Sbapt return (pathbuf); 4944229Sdavidn} 5044229Sdavidn 51242349Sbaptstatic int 52242349Sbaptgr_update(struct group * grp, char const * group) 5344229Sdavidn{ 54242349Sbapt int pfd, tfd; 55242349Sbapt struct group *gr = NULL; 56242349Sbapt struct group *old_gr = NULL; 5744229Sdavidn 58242349Sbapt if (grp != NULL) 59242349Sbapt gr = gr_dup(grp); 6020253Sjoerg 61242349Sbapt if (group != NULL) 62242349Sbapt old_gr = GETGRNAM(group); 6320747Sdavidn 64285092Sbapt if (gr_init(conf.etcpath, NULL)) 65242349Sbapt err(1, "gr_init()"); 6620747Sdavidn 67242349Sbapt if ((pfd = gr_lock()) == -1) { 68242349Sbapt gr_fini(); 69242349Sbapt err(1, "gr_lock()"); 7020747Sdavidn } 71242349Sbapt if ((tfd = gr_tmp(-1)) == -1) { 72242349Sbapt gr_fini(); 73242349Sbapt err(1, "gr_tmp()"); 7444229Sdavidn } 75242349Sbapt if (gr_copy(pfd, tfd, gr, old_gr) == -1) { 76242349Sbapt gr_fini(); 77308814Sasomers close(tfd); 78242349Sbapt err(1, "gr_copy()"); 79242349Sbapt } 80310173Sasomers fsync(tfd); 81308814Sasomers close(tfd); 82242349Sbapt if (gr_mkdb() == -1) { 83242349Sbapt gr_fini(); 84242349Sbapt err(1, "gr_mkdb()"); 85242349Sbapt } 86242349Sbapt free(gr); 87242349Sbapt gr_fini(); 88242349Sbapt return 0; 8920253Sjoerg} 9020253Sjoerg 9120253Sjoerg 9220253Sjoergint 9320253Sjoergaddgrent(struct group * grp) 9420253Sjoerg{ 95242349Sbapt return gr_update(grp, NULL); 9620253Sjoerg} 9720253Sjoerg 9820253Sjoergint 9920253Sjoergchggrent(char const * login, struct group * grp) 10020253Sjoerg{ 101242349Sbapt return gr_update(grp, login); 10220253Sjoerg} 10320253Sjoerg 10420253Sjoergint 10520253Sjoergdelgrent(struct group * grp) 10620253Sjoerg{ 107242349Sbapt 108285092Sbapt return (gr_update(NULL, grp->gr_name)); 10920253Sjoerg} 110