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
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.
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]
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <sys/types.h>
36 #include <nss_dbdefs.h>
41 edit_project(char *login
, char *new_login
, projid_t projids
[], int overwrite
)
44 char t_name
[] = "/etc/projtmp.XXXXXX";
45 FILE *e_fptr
, *t_fptr
;
46 struct project
*p_ptr
;
47 struct project p_work
;
48 char workbuf
[NSS_LINELEN_PROJECT
];
52 char p_string
[NSS_LINELEN_PROJECT
];
57 if ((e_fptr
= fopen(PROJF_PATH
, "r")) == NULL
) {
61 if (fstat(fileno(e_fptr
), &sbuf
) != 0)
64 if ((fd
= mkstemp(t_name
)) < 0) {
68 if ((t_fptr
= fdopen(fd
, "w+")) == NULL
) {
70 (void) unlink(t_name
);
75 * Get ownership and permissions correct
78 if (fchmod(fd
, sbuf
.st_mode
) != 0 ||
79 fchown(fd
, sbuf
.st_uid
, sbuf
.st_gid
) != 0) {
80 (void) fclose(t_fptr
);
81 (void) unlink(t_name
);
85 p_curr
= ftell(e_fptr
);
87 /* Make TMP file look like we want project file to look */
89 while (fgets(p_string
, NSS_LINELEN_PROJECT
- 1, e_fptr
)) {
90 /* While there is another group string */
92 p_length
= strlen(p_string
);
93 (void) fseek(e_fptr
, p_curr
, SEEK_SET
);
94 p_ptr
= fgetprojent(e_fptr
, &p_work
, &workbuf
,
96 p_curr
= ftell(e_fptr
);
100 * tried to parse a proj string over
101 * NSS_LINELEN_PROJECT chars
103 errmsg(M_PROJ_ENTRY_OVF
, NSS_LINELEN_PROJECT
);
104 modified
= 0; /* bad project file: cannot rebuild */
108 /* first delete the login from the project, if it's there */
109 if (overwrite
|| !projids
) {
110 if (p_ptr
->pj_users
!= NULL
) {
111 for (memptr
= p_ptr
->pj_users
; *memptr
;
113 if (strcmp(*memptr
, login
) == 0) {
114 /* Delete this one */
115 char **from
= memptr
+ 1;
116 p_length
-= (strlen(*memptr
)+1);
128 /* now check to see if project is one to add to */
130 for (i
= 0; projids
[i
] != -1; i
++) {
131 if (p_ptr
->pj_projid
== projids
[i
]) {
134 for (memptr
= p_ptr
->pj_users
; *memptr
;
136 if (strncmp(*memptr
, new_login
?
138 strlen(*memptr
)) == 0)
141 p_length
+= strlen(new_login
?
142 new_login
: login
) + 1;
145 NSS_LINELEN_PROJECT
- 1) {
146 errmsg(M_PROJ_ENTRY_OVF
,
147 NSS_LINELEN_PROJECT
);
151 *memptr
++ = new_login
?
160 putprojent(p_ptr
, t_fptr
);
163 (void) fclose(e_fptr
);
164 (void) fclose(t_fptr
);
166 /* Now, update project file, if it was modified */
167 if (modified
&& rename(t_name
, PROJF_PATH
) < 0) {
168 (void) unlink(t_name
);
172 (void) unlink(t_name
);