xref: /illumos-gate/usr/src/cmd/oamuser/user/proj.c (revision 2a8bcb4e)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
28*7c478bd9Sstevel@tonic-gate #include <stdio.h>
29*7c478bd9Sstevel@tonic-gate #include <ctype.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
31*7c478bd9Sstevel@tonic-gate #include <project.h>
32*7c478bd9Sstevel@tonic-gate #include <unistd.h>
33*7c478bd9Sstevel@tonic-gate #include <userdefs.h>
34*7c478bd9Sstevel@tonic-gate #include <errno.h>
35*7c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
36*7c478bd9Sstevel@tonic-gate #include "users.h"
37*7c478bd9Sstevel@tonic-gate #include "messages.h"
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate int
edit_project(char * login,char * new_login,projid_t projids[],int overwrite)40*7c478bd9Sstevel@tonic-gate edit_project(char *login, char *new_login, projid_t projids[], int overwrite)
41*7c478bd9Sstevel@tonic-gate {
42*7c478bd9Sstevel@tonic-gate 	char **memptr;
43*7c478bd9Sstevel@tonic-gate 	char t_name[] = "/etc/projtmp.XXXXXX";
44*7c478bd9Sstevel@tonic-gate 	FILE *e_fptr, *t_fptr;
45*7c478bd9Sstevel@tonic-gate 	struct project *p_ptr;
46*7c478bd9Sstevel@tonic-gate 	struct project p_work;
47*7c478bd9Sstevel@tonic-gate 	char workbuf[NSS_LINELEN_PROJECT];
48*7c478bd9Sstevel@tonic-gate 	int i, modified = 0;
49*7c478bd9Sstevel@tonic-gate 	struct stat sbuf;
50*7c478bd9Sstevel@tonic-gate 	int p_length;
51*7c478bd9Sstevel@tonic-gate 	char p_string[NSS_LINELEN_PROJECT];
52*7c478bd9Sstevel@tonic-gate 	long p_curr = 0;
53*7c478bd9Sstevel@tonic-gate 	int exist;
54*7c478bd9Sstevel@tonic-gate 	int fd;
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 	if ((e_fptr = fopen(PROJF_PATH, "r")) == NULL) {
57*7c478bd9Sstevel@tonic-gate 		return (EX_UPDATE);
58*7c478bd9Sstevel@tonic-gate 	}
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 	if (fstat(fileno(e_fptr), &sbuf) != 0)
61*7c478bd9Sstevel@tonic-gate 		return (EX_UPDATE);
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	if ((fd = mkstemp(t_name)) < 0) {
64*7c478bd9Sstevel@tonic-gate 		return (EX_UPDATE);
65*7c478bd9Sstevel@tonic-gate 	}
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	if ((t_fptr = fdopen(fd, "w+")) == NULL) {
68*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
69*7c478bd9Sstevel@tonic-gate 		(void) unlink(t_name);
70*7c478bd9Sstevel@tonic-gate 		return (EX_UPDATE);
71*7c478bd9Sstevel@tonic-gate 	}
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	/*
74*7c478bd9Sstevel@tonic-gate 	 * Get ownership and permissions correct
75*7c478bd9Sstevel@tonic-gate 	 */
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	if (fchmod(fd, sbuf.st_mode) != 0 ||
78*7c478bd9Sstevel@tonic-gate 	    fchown(fd, sbuf.st_uid, sbuf.st_gid) != 0) {
79*7c478bd9Sstevel@tonic-gate 		(void) fclose(t_fptr);
80*7c478bd9Sstevel@tonic-gate 		(void) unlink(t_name);
81*7c478bd9Sstevel@tonic-gate 		return (EX_UPDATE);
82*7c478bd9Sstevel@tonic-gate 	}
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	p_curr = ftell(e_fptr);
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	/* Make TMP file look like we want project file to look */
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	while (fgets(p_string, NSS_LINELEN_PROJECT - 1, e_fptr)) {
89*7c478bd9Sstevel@tonic-gate 		/* While there is another group string */
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 		p_length = strlen(p_string);
92*7c478bd9Sstevel@tonic-gate 		(void) fseek(e_fptr, p_curr, SEEK_SET);
93*7c478bd9Sstevel@tonic-gate 		p_ptr = fgetprojent(e_fptr, &p_work, &workbuf,
94*7c478bd9Sstevel@tonic-gate 		    sizeof (workbuf));
95*7c478bd9Sstevel@tonic-gate 		p_curr = ftell(e_fptr);
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 		if (p_ptr == NULL) {
98*7c478bd9Sstevel@tonic-gate 			/*
99*7c478bd9Sstevel@tonic-gate 			 * tried to parse a proj string over
100*7c478bd9Sstevel@tonic-gate 			 * NSS_LINELEN_PROJECT chars
101*7c478bd9Sstevel@tonic-gate 			 */
102*7c478bd9Sstevel@tonic-gate 			errmsg(M_PROJ_ENTRY_OVF, NSS_LINELEN_PROJECT);
103*7c478bd9Sstevel@tonic-gate 			modified = 0; /* bad project file: cannot rebuild */
104*7c478bd9Sstevel@tonic-gate 			break;
105*7c478bd9Sstevel@tonic-gate 		}
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 		/* first delete the login from the project, if it's there */
108*7c478bd9Sstevel@tonic-gate 		if (overwrite || !projids) {
109*7c478bd9Sstevel@tonic-gate 			if (p_ptr->pj_users != NULL) {
110*7c478bd9Sstevel@tonic-gate 				for (memptr = p_ptr->pj_users; *memptr;
111*7c478bd9Sstevel@tonic-gate 				    memptr++) {
112*7c478bd9Sstevel@tonic-gate 					if (strcmp(*memptr, login) == 0) {
113*7c478bd9Sstevel@tonic-gate 						/* Delete this one */
114*7c478bd9Sstevel@tonic-gate 						char **from = memptr + 1;
115*7c478bd9Sstevel@tonic-gate 						p_length -= (strlen(*memptr)+1);
116*7c478bd9Sstevel@tonic-gate 						do {
117*7c478bd9Sstevel@tonic-gate 							*(from - 1) = *from;
118*7c478bd9Sstevel@tonic-gate 						} while (*from++);
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 						modified++;
121*7c478bd9Sstevel@tonic-gate 						break;
122*7c478bd9Sstevel@tonic-gate 					}
123*7c478bd9Sstevel@tonic-gate 				}
124*7c478bd9Sstevel@tonic-gate 			}
125*7c478bd9Sstevel@tonic-gate 		}
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 		/* now check to see if project is one to add to */
128*7c478bd9Sstevel@tonic-gate 		if (projids) {
129*7c478bd9Sstevel@tonic-gate 			for (i = 0; projids[i] != -1; i++) {
130*7c478bd9Sstevel@tonic-gate 				if (p_ptr->pj_projid == projids[i]) {
131*7c478bd9Sstevel@tonic-gate 					/* Scan for dups */
132*7c478bd9Sstevel@tonic-gate 					exist = 0;
133*7c478bd9Sstevel@tonic-gate 					for (memptr = p_ptr->pj_users; *memptr;
134*7c478bd9Sstevel@tonic-gate 					    memptr++) {
135*7c478bd9Sstevel@tonic-gate 						if (strncmp(*memptr, new_login ?
136*7c478bd9Sstevel@tonic-gate 						    new_login : login,
137*7c478bd9Sstevel@tonic-gate 						    strlen(*memptr)) == 0)
138*7c478bd9Sstevel@tonic-gate 							exist++;
139*7c478bd9Sstevel@tonic-gate 					}
140*7c478bd9Sstevel@tonic-gate 					p_length += strlen(new_login ?
141*7c478bd9Sstevel@tonic-gate 					    new_login : login) + 1;
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 					if (p_length >=
144*7c478bd9Sstevel@tonic-gate 					    NSS_LINELEN_PROJECT - 1) {
145*7c478bd9Sstevel@tonic-gate 						errmsg(M_PROJ_ENTRY_OVF,
146*7c478bd9Sstevel@tonic-gate 						    NSS_LINELEN_PROJECT);
147*7c478bd9Sstevel@tonic-gate 						break;
148*7c478bd9Sstevel@tonic-gate 					} else {
149*7c478bd9Sstevel@tonic-gate 						if (!exist) {
150*7c478bd9Sstevel@tonic-gate 						*memptr++ = new_login ?
151*7c478bd9Sstevel@tonic-gate 						    new_login : login;
152*7c478bd9Sstevel@tonic-gate 						*memptr = NULL;
153*7c478bd9Sstevel@tonic-gate 						modified++;
154*7c478bd9Sstevel@tonic-gate 						}
155*7c478bd9Sstevel@tonic-gate 					}
156*7c478bd9Sstevel@tonic-gate 				}
157*7c478bd9Sstevel@tonic-gate 			}
158*7c478bd9Sstevel@tonic-gate 		}
159*7c478bd9Sstevel@tonic-gate 		putprojent(p_ptr, t_fptr);
160*7c478bd9Sstevel@tonic-gate 	}
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	(void) fclose(e_fptr);
163*7c478bd9Sstevel@tonic-gate 	(void) fclose(t_fptr);
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	/* Now, update project file, if it was modified */
166*7c478bd9Sstevel@tonic-gate 	if (modified && rename(t_name, PROJF_PATH) < 0) {
167*7c478bd9Sstevel@tonic-gate 		(void) unlink(t_name);
168*7c478bd9Sstevel@tonic-gate 		return (EX_UPDATE);
169*7c478bd9Sstevel@tonic-gate 	}
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 	(void) unlink(t_name);
172*7c478bd9Sstevel@tonic-gate 	return (EX_SUCCESS);
173*7c478bd9Sstevel@tonic-gate }
174