xref: /onnv-gate/usr/src/cmd/oamuser/user/proj.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 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #include <sys/types.h>
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <ctype.h>
31*0Sstevel@tonic-gate #include <sys/stat.h>
32*0Sstevel@tonic-gate #include <project.h>
33*0Sstevel@tonic-gate #include <unistd.h>
34*0Sstevel@tonic-gate #include <userdefs.h>
35*0Sstevel@tonic-gate #include <errno.h>
36*0Sstevel@tonic-gate #include <nss_dbdefs.h>
37*0Sstevel@tonic-gate #include "users.h"
38*0Sstevel@tonic-gate #include "messages.h"
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate int
edit_project(char * login,char * new_login,projid_t projids[],int overwrite)41*0Sstevel@tonic-gate edit_project(char *login, char *new_login, projid_t projids[], int overwrite)
42*0Sstevel@tonic-gate {
43*0Sstevel@tonic-gate 	char **memptr;
44*0Sstevel@tonic-gate 	char t_name[] = "/etc/projtmp.XXXXXX";
45*0Sstevel@tonic-gate 	FILE *e_fptr, *t_fptr;
46*0Sstevel@tonic-gate 	struct project *p_ptr;
47*0Sstevel@tonic-gate 	struct project p_work;
48*0Sstevel@tonic-gate 	char workbuf[NSS_LINELEN_PROJECT];
49*0Sstevel@tonic-gate 	int i, modified = 0;
50*0Sstevel@tonic-gate 	struct stat sbuf;
51*0Sstevel@tonic-gate 	int p_length;
52*0Sstevel@tonic-gate 	char p_string[NSS_LINELEN_PROJECT];
53*0Sstevel@tonic-gate 	long p_curr = 0;
54*0Sstevel@tonic-gate 	int exist;
55*0Sstevel@tonic-gate 	int fd;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 	if ((e_fptr = fopen(PROJF_PATH, "r")) == NULL) {
58*0Sstevel@tonic-gate 		return (EX_UPDATE);
59*0Sstevel@tonic-gate 	}
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 	if (fstat(fileno(e_fptr), &sbuf) != 0)
62*0Sstevel@tonic-gate 		return (EX_UPDATE);
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 	if ((fd = mkstemp(t_name)) < 0) {
65*0Sstevel@tonic-gate 		return (EX_UPDATE);
66*0Sstevel@tonic-gate 	}
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 	if ((t_fptr = fdopen(fd, "w+")) == NULL) {
69*0Sstevel@tonic-gate 		(void) close(fd);
70*0Sstevel@tonic-gate 		(void) unlink(t_name);
71*0Sstevel@tonic-gate 		return (EX_UPDATE);
72*0Sstevel@tonic-gate 	}
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 	/*
75*0Sstevel@tonic-gate 	 * Get ownership and permissions correct
76*0Sstevel@tonic-gate 	 */
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	if (fchmod(fd, sbuf.st_mode) != 0 ||
79*0Sstevel@tonic-gate 	    fchown(fd, sbuf.st_uid, sbuf.st_gid) != 0) {
80*0Sstevel@tonic-gate 		(void) fclose(t_fptr);
81*0Sstevel@tonic-gate 		(void) unlink(t_name);
82*0Sstevel@tonic-gate 		return (EX_UPDATE);
83*0Sstevel@tonic-gate 	}
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	p_curr = ftell(e_fptr);
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	/* Make TMP file look like we want project file to look */
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 	while (fgets(p_string, NSS_LINELEN_PROJECT - 1, e_fptr)) {
90*0Sstevel@tonic-gate 		/* While there is another group string */
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 		p_length = strlen(p_string);
93*0Sstevel@tonic-gate 		(void) fseek(e_fptr, p_curr, SEEK_SET);
94*0Sstevel@tonic-gate 		p_ptr = fgetprojent(e_fptr, &p_work, &workbuf,
95*0Sstevel@tonic-gate 		    sizeof (workbuf));
96*0Sstevel@tonic-gate 		p_curr = ftell(e_fptr);
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 		if (p_ptr == NULL) {
99*0Sstevel@tonic-gate 			/*
100*0Sstevel@tonic-gate 			 * tried to parse a proj string over
101*0Sstevel@tonic-gate 			 * NSS_LINELEN_PROJECT chars
102*0Sstevel@tonic-gate 			 */
103*0Sstevel@tonic-gate 			errmsg(M_PROJ_ENTRY_OVF, NSS_LINELEN_PROJECT);
104*0Sstevel@tonic-gate 			modified = 0; /* bad project file: cannot rebuild */
105*0Sstevel@tonic-gate 			break;
106*0Sstevel@tonic-gate 		}
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 		/* first delete the login from the project, if it's there */
109*0Sstevel@tonic-gate 		if (overwrite || !projids) {
110*0Sstevel@tonic-gate 			if (p_ptr->pj_users != NULL) {
111*0Sstevel@tonic-gate 				for (memptr = p_ptr->pj_users; *memptr;
112*0Sstevel@tonic-gate 				    memptr++) {
113*0Sstevel@tonic-gate 					if (strcmp(*memptr, login) == 0) {
114*0Sstevel@tonic-gate 						/* Delete this one */
115*0Sstevel@tonic-gate 						char **from = memptr + 1;
116*0Sstevel@tonic-gate 						p_length -= (strlen(*memptr)+1);
117*0Sstevel@tonic-gate 						do {
118*0Sstevel@tonic-gate 							*(from - 1) = *from;
119*0Sstevel@tonic-gate 						} while (*from++);
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 						modified++;
122*0Sstevel@tonic-gate 						break;
123*0Sstevel@tonic-gate 					}
124*0Sstevel@tonic-gate 				}
125*0Sstevel@tonic-gate 			}
126*0Sstevel@tonic-gate 		}
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 		/* now check to see if project is one to add to */
129*0Sstevel@tonic-gate 		if (projids) {
130*0Sstevel@tonic-gate 			for (i = 0; projids[i] != -1; i++) {
131*0Sstevel@tonic-gate 				if (p_ptr->pj_projid == projids[i]) {
132*0Sstevel@tonic-gate 					/* Scan for dups */
133*0Sstevel@tonic-gate 					exist = 0;
134*0Sstevel@tonic-gate 					for (memptr = p_ptr->pj_users; *memptr;
135*0Sstevel@tonic-gate 					    memptr++) {
136*0Sstevel@tonic-gate 						if (strncmp(*memptr, new_login ?
137*0Sstevel@tonic-gate 						    new_login : login,
138*0Sstevel@tonic-gate 						    strlen(*memptr)) == 0)
139*0Sstevel@tonic-gate 							exist++;
140*0Sstevel@tonic-gate 					}
141*0Sstevel@tonic-gate 					p_length += strlen(new_login ?
142*0Sstevel@tonic-gate 					    new_login : login) + 1;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 					if (p_length >=
145*0Sstevel@tonic-gate 					    NSS_LINELEN_PROJECT - 1) {
146*0Sstevel@tonic-gate 						errmsg(M_PROJ_ENTRY_OVF,
147*0Sstevel@tonic-gate 						    NSS_LINELEN_PROJECT);
148*0Sstevel@tonic-gate 						break;
149*0Sstevel@tonic-gate 					} else {
150*0Sstevel@tonic-gate 						if (!exist) {
151*0Sstevel@tonic-gate 						*memptr++ = new_login ?
152*0Sstevel@tonic-gate 						    new_login : login;
153*0Sstevel@tonic-gate 						*memptr = NULL;
154*0Sstevel@tonic-gate 						modified++;
155*0Sstevel@tonic-gate 						}
156*0Sstevel@tonic-gate 					}
157*0Sstevel@tonic-gate 				}
158*0Sstevel@tonic-gate 			}
159*0Sstevel@tonic-gate 		}
160*0Sstevel@tonic-gate 		putprojent(p_ptr, t_fptr);
161*0Sstevel@tonic-gate 	}
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	(void) fclose(e_fptr);
164*0Sstevel@tonic-gate 	(void) fclose(t_fptr);
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	/* Now, update project file, if it was modified */
167*0Sstevel@tonic-gate 	if (modified && rename(t_name, PROJF_PATH) < 0) {
168*0Sstevel@tonic-gate 		(void) unlink(t_name);
169*0Sstevel@tonic-gate 		return (EX_UPDATE);
170*0Sstevel@tonic-gate 	}
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	(void) unlink(t_name);
173*0Sstevel@tonic-gate 	return (EX_SUCCESS);
174*0Sstevel@tonic-gate }
175