xref: /onnv-gate/usr/src/cmd/oamuser/group/add_group.c (revision 4321:a8930ec16e52)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*4321Scasper  * Common Development and Distribution License (the "License").
6*4321Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*4321Scasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	All Rights Reserved	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.3 */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include	<sys/types.h>
330Sstevel@tonic-gate #include	<sys/stat.h>
340Sstevel@tonic-gate #include	<stdio.h>
350Sstevel@tonic-gate #include	<unistd.h>
360Sstevel@tonic-gate #include	<userdefs.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #define	GRPTMP		"/etc/gtmp"
390Sstevel@tonic-gate #define	GRPBUFSIZ	5120
400Sstevel@tonic-gate 
410Sstevel@tonic-gate int
add_group(group,gid)420Sstevel@tonic-gate add_group(group, gid)
430Sstevel@tonic-gate char *group;	/* name of group to add */
440Sstevel@tonic-gate gid_t gid;		/* gid of group to add */
450Sstevel@tonic-gate {
460Sstevel@tonic-gate 	FILE *etcgrp;		/* /etc/group file */
470Sstevel@tonic-gate 	FILE *etctmp;		/* temp file */
480Sstevel@tonic-gate 	int o_mask;		/* old umask value */
490Sstevel@tonic-gate 	int newdone = 0;	/* set true when new entry done */
500Sstevel@tonic-gate 	struct stat sb;		/* stat buf to copy modes */
510Sstevel@tonic-gate 	char buf[GRPBUFSIZ];
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 	if ((etcgrp = fopen(GROUP, "r")) == NULL) {
540Sstevel@tonic-gate 		return (EX_UPDATE);
550Sstevel@tonic-gate 	}
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	if (fstat(fileno(etcgrp), &sb) < 0) {
580Sstevel@tonic-gate 		/* If we can't get mode, take a default */
590Sstevel@tonic-gate 		sb.st_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH;
600Sstevel@tonic-gate 	}
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	o_mask = umask(077);
630Sstevel@tonic-gate 	etctmp = fopen(GRPTMP, "w+");
640Sstevel@tonic-gate 	(void) umask(o_mask);
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	if (etctmp == NULL) {
670Sstevel@tonic-gate 		fclose(etcgrp);
680Sstevel@tonic-gate 		return (EX_UPDATE);
690Sstevel@tonic-gate 	}
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	if (fchmod(fileno(etctmp), sb.st_mode) != 0 ||
720Sstevel@tonic-gate 	    fchown(fileno(etctmp), sb.st_uid, sb.st_gid) != 0 ||
730Sstevel@tonic-gate 	    lockf(fileno(etctmp), F_LOCK, 0) != 0) {
740Sstevel@tonic-gate 		fclose(etcgrp);
750Sstevel@tonic-gate 		fclose(etctmp);
760Sstevel@tonic-gate 		unlink(GRPTMP);
770Sstevel@tonic-gate 		return (EX_UPDATE);
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	while (fgets(buf, GRPBUFSIZ, etcgrp) != NULL) {
810Sstevel@tonic-gate 		/* Check for NameService reference */
820Sstevel@tonic-gate 		if (!newdone && (buf[0] == '+' || buf[0] == '-')) {
83*4321Scasper 			(void) fprintf(etctmp, "%s::%u:\n", group, gid);
840Sstevel@tonic-gate 			newdone = 1;
850Sstevel@tonic-gate 		}
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 		fputs(buf, etctmp);
880Sstevel@tonic-gate 	}
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	(void) fclose(etcgrp);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	if (!newdone) {
94*4321Scasper 		(void) fprintf(etctmp, "%s::%u:\n", group, gid);
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	if (rename(GRPTMP, GROUP) < 0) {
980Sstevel@tonic-gate 		fclose(etctmp);
990Sstevel@tonic-gate 		unlink(GRPTMP);
1000Sstevel@tonic-gate 		return (EX_UPDATE);
1010Sstevel@tonic-gate 	}
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	(void) fclose(etctmp);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	return (EX_SUCCESS);
1070Sstevel@tonic-gate }
108