xref: /onnv-gate/usr/src/cmd/oamuser/user/groups.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include <sys/types.h>
33*0Sstevel@tonic-gate #include <stdio.h>
34*0Sstevel@tonic-gate #include <stdlib.h>
35*0Sstevel@tonic-gate #include <string.h>
36*0Sstevel@tonic-gate #include <ctype.h>
37*0Sstevel@tonic-gate #include <sys/stat.h>
38*0Sstevel@tonic-gate #include <grp.h>
39*0Sstevel@tonic-gate #include <unistd.h>
40*0Sstevel@tonic-gate #include <userdefs.h>
41*0Sstevel@tonic-gate #include <errno.h>
42*0Sstevel@tonic-gate #include <limits.h>
43*0Sstevel@tonic-gate #include "users.h"
44*0Sstevel@tonic-gate #include "messages.h"
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate #define	MYBUFSIZE (LINE_MAX)
47*0Sstevel@tonic-gate 	/* Corresponds to MYBUFSIZE in grpck.c, BUFCONST in nss_dbdefs.c */
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate int
edit_group(char * login,char * new_login,gid_t gids[],int overwrite)50*0Sstevel@tonic-gate edit_group(char *login, char *new_login, gid_t gids[], int overwrite)
51*0Sstevel@tonic-gate {
52*0Sstevel@tonic-gate 	char **memptr;
53*0Sstevel@tonic-gate 	char t_name[] = "/etc/gtmp.XXXXXX";
54*0Sstevel@tonic-gate 	int fd;
55*0Sstevel@tonic-gate 	FILE *e_fptr, *t_fptr;
56*0Sstevel@tonic-gate 	struct group *g_ptr;	/* group structure from fgetgrent */
57*0Sstevel@tonic-gate 	int i;
58*0Sstevel@tonic-gate 	int modified = 0;
59*0Sstevel@tonic-gate 	struct stat sbuf;
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 	int bufsize, g_length, sav_errno;
62*0Sstevel@tonic-gate 	long g_curr = 0L;
63*0Sstevel@tonic-gate 	char *g_string, *new_g_string, *gstr_off;
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	if ((e_fptr = fopen(GROUP, "r")) == NULL)
66*0Sstevel@tonic-gate 		return (EX_UPDATE);
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 	if (fstat(fileno(e_fptr), &sbuf) != 0) {
69*0Sstevel@tonic-gate 		(void) fclose(e_fptr);
70*0Sstevel@tonic-gate 		return (EX_UPDATE);
71*0Sstevel@tonic-gate 	}
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 	if ((fd = mkstemp(t_name)) == -1) {
74*0Sstevel@tonic-gate 		(void) fclose(e_fptr);
75*0Sstevel@tonic-gate 		return (EX_UPDATE);
76*0Sstevel@tonic-gate 	}
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	if ((t_fptr = fdopen(fd, "w")) == NULL) {
79*0Sstevel@tonic-gate 		(void) close(fd);
80*0Sstevel@tonic-gate 		(void) unlink(t_name);
81*0Sstevel@tonic-gate 		(void) fclose(e_fptr);
82*0Sstevel@tonic-gate 		return (EX_UPDATE);
83*0Sstevel@tonic-gate 	}
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	/*
86*0Sstevel@tonic-gate 	 * Get ownership and permissions correct
87*0Sstevel@tonic-gate 	 */
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 	if (fchmod(fd, sbuf.st_mode) != 0 ||
90*0Sstevel@tonic-gate 	    fchown(fd, sbuf.st_uid, sbuf.st_gid) != 0) {
91*0Sstevel@tonic-gate 		(void) fclose(t_fptr);
92*0Sstevel@tonic-gate 		(void) fclose(e_fptr);
93*0Sstevel@tonic-gate 		(void) unlink(t_name);
94*0Sstevel@tonic-gate 		return (EX_UPDATE);
95*0Sstevel@tonic-gate 	}
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	g_curr = ftell(e_fptr);
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	/* Make TMP file look like we want GROUP file to look */
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	bufsize = MYBUFSIZE;
102*0Sstevel@tonic-gate 	if ((g_string = malloc(bufsize)) == NULL) {
103*0Sstevel@tonic-gate 		(void) fclose(t_fptr);
104*0Sstevel@tonic-gate 		(void) fclose(e_fptr);
105*0Sstevel@tonic-gate 		(void) unlink(t_name);
106*0Sstevel@tonic-gate 		return (EX_UPDATE);
107*0Sstevel@tonic-gate 	}
108*0Sstevel@tonic-gate 	/*
109*0Sstevel@tonic-gate 	 * bufsize contains the size of the currently allocated buffer
110*0Sstevel@tonic-gate 	 * buffer size, which is initially MYBUFSIZE but when a line
111*0Sstevel@tonic-gate 	 * greater than MYBUFSIZE is encountered then bufsize gets increased
112*0Sstevel@tonic-gate 	 * by MYBUFSIZE.
113*0Sstevel@tonic-gate 	 * g_string always points to the beginning of the buffer (even after
114*0Sstevel@tonic-gate 	 * realloc()).
115*0Sstevel@tonic-gate 	 * gstr_off = g_string + MYBUFSIZE * (n), where n >= 0.
116*0Sstevel@tonic-gate 	 */
117*0Sstevel@tonic-gate 	while (!feof(e_fptr) && !ferror(e_fptr)) {
118*0Sstevel@tonic-gate 		g_length = 0;
119*0Sstevel@tonic-gate 		gstr_off = g_string;
120*0Sstevel@tonic-gate 		while (fgets(gstr_off, (bufsize - g_length), e_fptr) != NULL) {
121*0Sstevel@tonic-gate 			g_length += strlen(gstr_off);
122*0Sstevel@tonic-gate 			if (g_string[g_length - 1] == '\n' || feof(e_fptr))
123*0Sstevel@tonic-gate 				break;
124*0Sstevel@tonic-gate 			new_g_string = realloc(g_string, (bufsize + MYBUFSIZE));
125*0Sstevel@tonic-gate 			if (new_g_string == NULL) {
126*0Sstevel@tonic-gate 				free(g_string);
127*0Sstevel@tonic-gate 				(void) fclose(t_fptr);
128*0Sstevel@tonic-gate 				(void) fclose(e_fptr);
129*0Sstevel@tonic-gate 				(void) unlink(t_name);
130*0Sstevel@tonic-gate 				return (EX_UPDATE);
131*0Sstevel@tonic-gate 			}
132*0Sstevel@tonic-gate 			bufsize += MYBUFSIZE;
133*0Sstevel@tonic-gate 			g_string = new_g_string;
134*0Sstevel@tonic-gate 			gstr_off = g_string + g_length;
135*0Sstevel@tonic-gate 		}
136*0Sstevel@tonic-gate 		if (g_length == 0) {
137*0Sstevel@tonic-gate 			continue;
138*0Sstevel@tonic-gate 		}
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 		/* While there is another group string */
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 		(void) fseek(e_fptr, g_curr, SEEK_SET);
143*0Sstevel@tonic-gate 		errno = 0;
144*0Sstevel@tonic-gate 		g_ptr = fgetgrent(e_fptr);
145*0Sstevel@tonic-gate 		sav_errno = errno;
146*0Sstevel@tonic-gate 		g_curr = ftell(e_fptr);
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 		if (g_ptr == NULL) {
149*0Sstevel@tonic-gate 			/* tried to parse a group string over MYBUFSIZ char */
150*0Sstevel@tonic-gate 			if (sav_errno == ERANGE)
151*0Sstevel@tonic-gate 				errmsg(M_GROUP_ENTRY_OVF);
152*0Sstevel@tonic-gate 			else
153*0Sstevel@tonic-gate 				errmsg(M_READ_ERROR);
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 			modified = 0; /* bad group file: cannot rebuild */
156*0Sstevel@tonic-gate 			break;
157*0Sstevel@tonic-gate 		}
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 		/* first delete the login from the group, if it's there */
160*0Sstevel@tonic-gate 		if (overwrite || !gids) {
161*0Sstevel@tonic-gate 			if (g_ptr->gr_mem != NULL) {
162*0Sstevel@tonic-gate 				for (memptr = g_ptr->gr_mem; *memptr;
163*0Sstevel@tonic-gate 				    memptr++) {
164*0Sstevel@tonic-gate 					if (strcmp(*memptr, login) == 0) {
165*0Sstevel@tonic-gate 						/* Delete this one */
166*0Sstevel@tonic-gate 						char **from = memptr + 1;
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 						g_length -= (strlen(*memptr)+1);
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 						do {
171*0Sstevel@tonic-gate 							*(from - 1) = *from;
172*0Sstevel@tonic-gate 						} while (*from++);
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 						modified++;
175*0Sstevel@tonic-gate 						break;
176*0Sstevel@tonic-gate 					}
177*0Sstevel@tonic-gate 				}
178*0Sstevel@tonic-gate 			}
179*0Sstevel@tonic-gate 		}
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 		/* now check to see if group is one to add to */
182*0Sstevel@tonic-gate 		if (gids) {
183*0Sstevel@tonic-gate 			for (i = 0; gids[i] != -1; i++) {
184*0Sstevel@tonic-gate 				if (g_ptr->gr_gid == gids[i]) {
185*0Sstevel@tonic-gate 					/* Find end */
186*0Sstevel@tonic-gate 					for (memptr = g_ptr->gr_mem; *memptr;
187*0Sstevel@tonic-gate 						memptr++)
188*0Sstevel@tonic-gate 						;
189*0Sstevel@tonic-gate 					g_length += strlen(new_login ?
190*0Sstevel@tonic-gate 							new_login : login)+1;
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 					*memptr++ = new_login ?
193*0Sstevel@tonic-gate 						new_login : login;
194*0Sstevel@tonic-gate 					*memptr = NULL;
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 					modified++;
197*0Sstevel@tonic-gate 				}
198*0Sstevel@tonic-gate 			}
199*0Sstevel@tonic-gate 		}
200*0Sstevel@tonic-gate 		putgrent(g_ptr, t_fptr);
201*0Sstevel@tonic-gate 	}
202*0Sstevel@tonic-gate 	free(g_string);
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	(void) fclose(e_fptr);
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	if (fclose(t_fptr) != 0) {
207*0Sstevel@tonic-gate 		(void) unlink(t_name);
208*0Sstevel@tonic-gate 		return (EX_UPDATE);
209*0Sstevel@tonic-gate 	}
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	/* Now, update GROUP file, if it was modified */
212*0Sstevel@tonic-gate 	if (modified) {
213*0Sstevel@tonic-gate 		if (rename(t_name, GROUP) != 0) {
214*0Sstevel@tonic-gate 			(void) unlink(t_name);
215*0Sstevel@tonic-gate 			return (EX_UPDATE);
216*0Sstevel@tonic-gate 		}
217*0Sstevel@tonic-gate 		return (EX_SUCCESS);
218*0Sstevel@tonic-gate 	} else {
219*0Sstevel@tonic-gate 		(void) unlink(t_name);
220*0Sstevel@tonic-gate 		return (EX_SUCCESS);
221*0Sstevel@tonic-gate 	}
222*0Sstevel@tonic-gate }
223