1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 27 /* All Rights Reserved */ 28 29 30 #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.3 */ 31 32 #include <sys/types.h> 33 #include <sys/stat.h> 34 #include <stdio.h> 35 #include <unistd.h> 36 #include <userdefs.h> 37 38 #define GRPTMP "/etc/gtmp" 39 #define GRPBUFSIZ 5120 40 41 int 42 add_group(group, gid) 43 char *group; /* name of group to add */ 44 gid_t gid; /* gid of group to add */ 45 { 46 FILE *etcgrp; /* /etc/group file */ 47 FILE *etctmp; /* temp file */ 48 int o_mask; /* old umask value */ 49 int newdone = 0; /* set true when new entry done */ 50 struct stat sb; /* stat buf to copy modes */ 51 char buf[GRPBUFSIZ]; 52 53 if ((etcgrp = fopen(GROUP, "r")) == NULL) { 54 return (EX_UPDATE); 55 } 56 57 if (fstat(fileno(etcgrp), &sb) < 0) { 58 /* If we can't get mode, take a default */ 59 sb.st_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH; 60 } 61 62 o_mask = umask(077); 63 etctmp = fopen(GRPTMP, "w+"); 64 (void) umask(o_mask); 65 66 if (etctmp == NULL) { 67 fclose(etcgrp); 68 return (EX_UPDATE); 69 } 70 71 if (fchmod(fileno(etctmp), sb.st_mode) != 0 || 72 fchown(fileno(etctmp), sb.st_uid, sb.st_gid) != 0 || 73 lockf(fileno(etctmp), F_LOCK, 0) != 0) { 74 fclose(etcgrp); 75 fclose(etctmp); 76 unlink(GRPTMP); 77 return (EX_UPDATE); 78 } 79 80 while (fgets(buf, GRPBUFSIZ, etcgrp) != NULL) { 81 /* Check for NameService reference */ 82 if (!newdone && (buf[0] == '+' || buf[0] == '-')) { 83 (void) fprintf(etctmp, "%s::%u:\n", group, gid); 84 newdone = 1; 85 } 86 87 fputs(buf, etctmp); 88 } 89 90 91 (void) fclose(etcgrp); 92 93 if (!newdone) { 94 (void) fprintf(etctmp, "%s::%u:\n", group, gid); 95 } 96 97 if (rename(GRPTMP, GROUP) < 0) { 98 fclose(etctmp); 99 unlink(GRPTMP); 100 return (EX_UPDATE); 101 } 102 103 (void) fclose(etctmp); 104 105 106 return (EX_SUCCESS); 107 } 108