grupd.c revision 20747
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 * $Id: grupd.c,v 1.1.1.2 1996/12/10 23:58:56 joerg Exp $ 2730259Scharnier */ 2830259Scharnier 2930259Scharnier#include <stdio.h> 3030259Scharnier#include <stdlib.h> 3130259Scharnier#include <string.h> 3220253Sjoerg#include <unistd.h> 3320253Sjoerg#include <stdarg.h> 3420253Sjoerg#include <errno.h> 3520253Sjoerg#include <sys/types.h> 3620253Sjoerg#include <sys/stat.h> 3720253Sjoerg 3820253Sjoerg#include "pwupd.h" 3920253Sjoerg 4020253Sjoergint 4120253Sjoergfmtgrentry(char **buf, int * buflen, struct group * grp, int type) 4220253Sjoerg{ 4320747Sdavidn int i, l; 4420253Sjoerg 4520253Sjoerg /* 4620253Sjoerg * Since a group line is of arbitrary length, 4720253Sjoerg * we need to calculate up-front just how long 4820747Sdavidn * it will need to be... 4920747Sdavidn */ 5020747Sdavidn /* groupname : password : gid : */ 5120253Sjoerg l = strlen(grp->gr_name) + 1 + strlen(grp->gr_passwd) + 1 + 5 + 1; 5220747Sdavidn /* group members + comma separator */ 5320747Sdavidn for (i = 0; grp->gr_mem[i] != NULL; i++) { 5420747Sdavidn l += strlen(grp->gr_mem[i]) + 1; 5520747Sdavidn } 5620747Sdavidn l += 2; /* For newline & NUL */ 5720747Sdavidn if (extendline(buf, buflen, l) == -1) 5820747Sdavidn l = -1; 5920747Sdavidn else{ 6020747Sdavidn /* 6120747Sdavidn * Now we can safely format 6220747Sdavidn */ 6320747Sdavidn if (type == PWF_STANDARD) 6420747Sdavidn l = sprintf(*buf, "%s:*:%ld:", grp->gr_name, (long) grp->gr_gid); 6520747Sdavidn else 6620747Sdavidn l = sprintf(*buf, "%s:%s:%ld:", grp->gr_name, grp->gr_passwd, (long) grp->gr_gid); 6720747Sdavidn 6820747Sdavidn /* 6920747Sdavidn * List members 7020747Sdavidn */ 7120747Sdavidn for (i = 0; grp->gr_mem[i] != NULL; i++) { 7220747Sdavidn l += sprintf(*buf + l, "%s%s", i ? "," : "", grp->gr_mem[i]); 7320747Sdavidn } 7420747Sdavidn 7520747Sdavidn (*buf)[l++] = '\n'; 7620747Sdavidn (*buf)[l] = '\0'; 7720747Sdavidn } 7820747Sdavidn return l; 7920747Sdavidn} 8020253Sjoerg 8120253Sjoerg 8220253Sjoergint 8320253Sjoergfmtgrent(char **buf, int * buflen, struct group * grp) 8420253Sjoerg{ 8520747Sdavidn return fmtgrentry(buf, buflen, grp, PWF_STANDARD); 8620253Sjoerg} 8720747Sdavidn 8820253Sjoerg 8920253Sjoergstatic int 9020253Sjoerggr_update(struct group * grp, char const * group, int mode) 9120253Sjoerg{ 9220253Sjoerg int l; 9320253Sjoerg char pfx[64]; 9420253Sjoerg int grbuflen = 0; 9520747Sdavidn char *grbuf = NULL; 9620747Sdavidn 9720747Sdavidn endgrent(); 9820253Sjoerg l = snprintf(pfx, sizeof pfx, "%s:", group); 9920253Sjoerg 10020747Sdavidn /* 10120253Sjoerg * Update the group file 10220253Sjoerg */ 10320253Sjoerg if (grp != NULL && fmtgrentry(&grbuf, &grbuflen, grp, PWF_PASSWD) == -1) 10420253Sjoerg l = -1; 10520747Sdavidn else 10620747Sdavidn l = fileupdate(_PATH_GROUP, 0644, grbuf, pfx, l, mode); 10720253Sjoerg if (grbuf != NULL) 10820747Sdavidn free(grbuf); 10920747Sdavidn return l; 11020747Sdavidn} 11120747Sdavidn 11220253Sjoerg 11320253Sjoergint 11420253Sjoergaddgrent(struct group * grp) 11520253Sjoerg{ 11620253Sjoerg return gr_update(grp, grp->gr_name, UPD_CREATE); 11720253Sjoerg} 11820253Sjoerg 11920253Sjoergint 12020253Sjoergchggrent(char const * login, struct group * grp) 12120253Sjoerg{ 12220253Sjoerg return gr_update(grp, login, UPD_REPLACE); 12320253Sjoerg} 12420253Sjoerg 12520253Sjoergint 12620253Sjoergdelgrent(struct group * grp) 12720253Sjoerg{ 12820253Sjoerg return gr_update(NULL, grp->gr_name, UPD_DELETE); 12920253Sjoerg} 13020253Sjoerg