grupd.c revision 30259
160812Sps/*- 260786Sps * Copyright (C) 1996 3195941Sdelphij * David L. Nugent. All rights reserved. 460786Sps * 560786Sps * Redistribution and use in source and binary forms, with or without 660786Sps * modification, are permitted provided that the following conditions 760786Sps * are met: 860786Sps * 1. Redistributions of source code must retain the above copyright 960786Sps * notice, this list of conditions and the following disclaimer. 1060786Sps * 2. Redistributions in binary form must reproduce the above copyright 1160786Sps * notice, this list of conditions and the following disclaimer in the 1260786Sps * documentation and/or other materials provided with the distribution. 1360786Sps * 1460786Sps * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND 1560786Sps * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1660786Sps * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1760786Sps * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE 18195941Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1960786Sps * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20172471Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2160786Sps * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2260786Sps * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2360786Sps * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2460786Sps * SUCH DAMAGE. 2560786Sps */ 2660786Sps 2760786Sps#ifndef lint 2860786Spsstatic const char rcsid[] = 2960786Sps "$Id$"; 3060786Sps#endif /* not lint */ 3160786Sps 32170259Sdelphij#include <stdio.h> 33128348Stjr#include <stdlib.h> 3463131Sps#include <string.h> 35170259Sdelphij#include <unistd.h> 3660786Sps#include <stdarg.h> 3760786Sps#include <sys/types.h> 38191930Sdelphij#include <sys/stat.h> 39191930Sdelphij 4060786Sps#include "pwupd.h" 4160786Sps 4260786Spsint 4360786Spsfmtgrentry(char **buf, int * buflen, struct group * grp, int type) 4460786Sps{ 4560786Sps int i, l; 4660786Sps 4760786Sps /* 48195941Sdelphij * Since a group line is of arbitrary length, 49195941Sdelphij * we need to calculate up-front just how long 5060786Sps * it will need to be... 5160786Sps */ 5260786Sps /* groupname : password : gid : */ 5360786Sps l = strlen(grp->gr_name) + 1 + strlen(grp->gr_passwd) + 1 + 5 + 1; 5460786Sps /* group members + comma separator */ 5560786Sps for (i = 0; grp->gr_mem[i] != NULL; i++) { 5660786Sps l += strlen(grp->gr_mem[i]) + 1; 5760786Sps } 58191930Sdelphij l += 2; /* For newline & NUL */ 5960786Sps if (extendline(buf, buflen, l) == -1) 6060786Sps l = -1; 6160786Sps else{ 6260786Sps /* 6360786Sps * Now we can safely format 64195941Sdelphij */ 6560786Sps if (type == PWF_STANDARD) 66195941Sdelphij l = sprintf(*buf, "%s:*:%ld:", grp->gr_name, (long) grp->gr_gid); 67195941Sdelphij else 68195941Sdelphij l = sprintf(*buf, "%s:%s:%ld:", grp->gr_name, grp->gr_passwd, (long) grp->gr_gid); 69195941Sdelphij 70195941Sdelphij /* 71195941Sdelphij * List members 72195941Sdelphij */ 73195941Sdelphij for (i = 0; grp->gr_mem[i] != NULL; i++) { 7460786Sps l += sprintf(*buf + l, "%s%s", i ? "," : "", grp->gr_mem[i]); 75173685Sdelphij } 76195941Sdelphij 77173685Sdelphij (*buf)[l++] = '\n'; 78173685Sdelphij (*buf)[l] = '\0'; 79195941Sdelphij } 80195941Sdelphij return l; 81195941Sdelphij} 82195941Sdelphij 83173685Sdelphij 84195941Sdelphijint 85195941Sdelphijfmtgrent(char **buf, int * buflen, struct group * grp) 86195941Sdelphij{ 87195941Sdelphij return fmtgrentry(buf, buflen, grp, PWF_STANDARD); 88195941Sdelphij} 89195941Sdelphij 90195941Sdelphij 91195941Sdelphijstatic int 92195941Sdelphijgr_update(struct group * grp, char const * group, int mode) 93195941Sdelphij{ 94195941Sdelphij int l; 95195941Sdelphij char pfx[64]; 96195941Sdelphij int grbuflen = 0; 97195941Sdelphij char *grbuf = NULL; 98195941Sdelphij 99173685Sdelphij endgrent(); 100173685Sdelphij l = snprintf(pfx, sizeof pfx, "%s:", group); 101173685Sdelphij 102195941Sdelphij /* 103173685Sdelphij * Update the group file 10460786Sps */ 105195941Sdelphij if (grp != NULL && fmtgrentry(&grbuf, &grbuflen, grp, PWF_PASSWD) == -1) 106195941Sdelphij l = -1; 10760786Sps else 108195941Sdelphij l = fileupdate(_PATH_GROUP, 0644, grbuf, pfx, l, mode); 109195941Sdelphij if (grbuf != NULL) 110195941Sdelphij free(grbuf); 111195941Sdelphij return l; 112195941Sdelphij} 11360786Sps 114195941Sdelphij 115195941Sdelphijint 116195941Sdelphijaddgrent(struct group * grp) 117195941Sdelphij{ 118195941Sdelphij return gr_update(grp, grp->gr_name, UPD_CREATE); 119195941Sdelphij} 120195941Sdelphij 121195941Sdelphijint 122195941Sdelphijchggrent(char const * login, struct group * grp) 123195941Sdelphij{ 124195941Sdelphij return gr_update(grp, login, UPD_REPLACE); 125170259Sdelphij} 126195941Sdelphij 127195941Sdelphijint 128195941Sdelphijdelgrent(struct group * grp) 129195941Sdelphij{ 130195941Sdelphij return gr_update(NULL, grp->gr_name, UPD_DELETE); 131195941Sdelphij} 132195941Sdelphij