xref: /illumos-gate/usr/src/cmd/oamuser/group/mod_group.c (revision 763f1f5f97e4c16840af2ced98915f0ed0f46616)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #include <sys/types.h>
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <grp.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36 #include <userdefs.h>
37 #include <strings.h>
38 #include <stdlib.h>
39 #include "users.h"
40 #include "messages.h"
41 
42 /* lint error-killers: */
43 void errmsg(int, int);
44 
45 /* Modify group to new gid and/or new name */
46 int
47 mod_group(char *group, gid_t gid, char *newgroup)
48 {
49 	int modified = 0;
50 	int fd;
51 	char tname[] = "/etc/gtmp.XXXXXX";
52 	FILE *e_fptr, *t_fptr;
53 	struct group *g_ptr;
54 	struct stat sbuf;
55 	boolean_t haserr;
56 	int line = 1;
57 
58 	if ((e_fptr = fopen(GROUP, "r")) == NULL)
59 		return (EX_UPDATE);
60 
61 	if (fstat(fileno(e_fptr), &sbuf) != 0)
62 		return (EX_UPDATE);
63 
64 	if ((fd = mkstemp(tname)) == -1)
65 		return (EX_UPDATE);
66 
67 	if ((t_fptr = fdopen(fd, "w")) == NULL) {
68 		(void) close(fd);
69 		(void) unlink(tname);
70 		return (EX_UPDATE);
71 	}
72 
73 	/*
74 	 * Get ownership and permissions correct
75 	 */
76 
77 	if (fchmod(fd, sbuf.st_mode) != 0 ||
78 	    fchown(fd, sbuf.st_uid, sbuf.st_gid) != 0) {
79 		(void) fclose(t_fptr);
80 		(void) unlink(tname);
81 		return (EX_UPDATE);
82 	}
83 
84 	while ((g_ptr = fgetgrent(e_fptr)) != NULL) {
85 
86 		/* check to see if group is one to modify */
87 		if (strcmp(g_ptr->gr_name, group) == 0) {
88 			if (newgroup != NULL)
89 				g_ptr->gr_name = newgroup;
90 			if (gid != -1)
91 				g_ptr->gr_gid = gid;
92 			modified++;
93 		}
94 		putgrent(g_ptr, t_fptr);
95 		line++;
96 	}
97 
98 	haserr = !feof(e_fptr);
99 	if (haserr)
100 		errmsg(M_SYNTAX, line);
101 
102 	(void) fclose(e_fptr);
103 
104 	if (fclose(t_fptr) != 0 || haserr) {
105 		/* GROUP file contains bad entries or write failed. */
106 		(void) unlink(tname);
107 		return (EX_UPDATE);
108 	}
109 
110 	if (modified) {
111 		if (rename(tname, GROUP) != 0) {
112 			(void) unlink(tname);
113 			return (EX_UPDATE);
114 		}
115 		return (EX_SUCCESS);
116 	} else {
117 		(void) unlink(tname);
118 		return (EX_NAME_NOT_EXIST);
119 	}
120 }
121