xref: /titanic_50/usr/src/cmd/oamuser/group/mod_group.c (revision 2df1fe9ca32bb227b9158c67f5c00b54c20b10fd)
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 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <sys/types.h>
34 #include <stdio.h>
35 #include <ctype.h>
36 #include <grp.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 #include <userdefs.h>
40 #include <strings.h>
41 #include <stdlib.h>
42 #include "users.h"
43 #include "messages.h"
44 
45 /* lint error-killers: */
46 void errmsg(int, int);
47 
48 /* Modify group to new gid and/or new name */
49 int
50 mod_group(char *group, gid_t gid, char *newgroup)
51 {
52 	int modified = 0;
53 	int fd;
54 	char tname[] = "/etc/gtmp.XXXXXX";
55 	FILE *e_fptr, *t_fptr;
56 	struct group *g_ptr;
57 	struct stat sbuf;
58 	boolean_t haserr;
59 	int line = 1;
60 
61 	if ((e_fptr = fopen(GROUP, "r")) == NULL)
62 		return (EX_UPDATE);
63 
64 	if (fstat(fileno(e_fptr), &sbuf) != 0)
65 		return (EX_UPDATE);
66 
67 	if ((fd = mkstemp(tname)) == -1)
68 		return (EX_UPDATE);
69 
70 	if ((t_fptr = fdopen(fd, "w")) == NULL) {
71 		(void) close(fd);
72 		(void) unlink(tname);
73 		return (EX_UPDATE);
74 	}
75 
76 	/*
77 	 * Get ownership and permissions correct
78 	 */
79 
80 	if (fchmod(fd, sbuf.st_mode) != 0 ||
81 	    fchown(fd, sbuf.st_uid, sbuf.st_gid) != 0) {
82 		(void) fclose(t_fptr);
83 		(void) unlink(tname);
84 		return (EX_UPDATE);
85 	}
86 
87 	while ((g_ptr = fgetgrent(e_fptr)) != NULL) {
88 
89 		/* check to see if group is one to modify */
90 		if (strcmp(g_ptr->gr_name, group) == 0) {
91 			if (newgroup != NULL)
92 				g_ptr->gr_name = newgroup;
93 			if (gid != -1)
94 				g_ptr->gr_gid = gid;
95 			modified++;
96 		}
97 		putgrent(g_ptr, t_fptr);
98 		line++;
99 	}
100 
101 	haserr = !feof(e_fptr);
102 	if (haserr)
103 		errmsg(M_SYNTAX, line);
104 
105 	(void) fclose(e_fptr);
106 
107 	if (fclose(t_fptr) != 0 || haserr) {
108 		/* GROUP file contains bad entries or write failed. */
109 		(void) unlink(tname);
110 		return (EX_UPDATE);
111 	}
112 
113 	if (modified) {
114 		if (rename(tname, GROUP) != 0) {
115 			(void) unlink(tname);
116 			return (EX_UPDATE);
117 		}
118 		return (EX_SUCCESS);
119 	} else {
120 		(void) unlink(tname);
121 		return (EX_NAME_NOT_EXIST);
122 	}
123 }
124