cmd/oamuser: drop -Wno-implicit-function-declaration
[unleashed.git] / usr / src / cmd / oamuser / user / proj.c
blob71a430e4f2ca946526f06035e7a10d6ce443401b
1 /*
2 * CDDL HEADER START
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.
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]
20 * CDDL HEADER END
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>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <sys/stat.h>
34 #include <project.h>
35 #include <unistd.h>
36 #include <userdefs.h>
37 #include <errno.h>
38 #include <nss_dbdefs.h>
39 #include "users.h"
40 #include "messages.h"
42 int
43 edit_project(char *login, char *new_login, projid_t projids[], int overwrite)
45 char **memptr;
46 char t_name[] = "/etc/projtmp.XXXXXX";
47 FILE *e_fptr, *t_fptr;
48 struct project *p_ptr;
49 struct project p_work;
50 char workbuf[NSS_LINELEN_PROJECT];
51 int i, modified = 0;
52 struct stat sbuf;
53 int p_length;
54 char p_string[NSS_LINELEN_PROJECT];
55 long p_curr = 0;
56 int exist;
57 int fd;
59 if ((e_fptr = fopen(PROJF_PATH, "r")) == NULL) {
60 return (EX_UPDATE);
63 if (fstat(fileno(e_fptr), &sbuf) != 0)
64 return (EX_UPDATE);
66 if ((fd = mkstemp(t_name)) < 0) {
67 return (EX_UPDATE);
70 if ((t_fptr = fdopen(fd, "w+")) == NULL) {
71 (void) close(fd);
72 (void) unlink(t_name);
73 return (EX_UPDATE);
77 * Get ownership and permissions correct
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(t_name);
84 return (EX_UPDATE);
87 p_curr = ftell(e_fptr);
89 /* Make TMP file look like we want project file to look */
91 while (fgets(p_string, NSS_LINELEN_PROJECT - 1, e_fptr)) {
92 /* While there is another group string */
94 p_length = strlen(p_string);
95 (void) fseek(e_fptr, p_curr, SEEK_SET);
96 p_ptr = fgetprojent(e_fptr, &p_work, &workbuf,
97 sizeof (workbuf));
98 p_curr = ftell(e_fptr);
100 if (p_ptr == NULL) {
102 * tried to parse a proj string over
103 * NSS_LINELEN_PROJECT chars
105 errmsg(M_PROJ_ENTRY_OVF, NSS_LINELEN_PROJECT);
106 modified = 0; /* bad project file: cannot rebuild */
107 break;
110 /* first delete the login from the project, if it's there */
111 if (overwrite || !projids) {
112 if (p_ptr->pj_users != NULL) {
113 for (memptr = p_ptr->pj_users; *memptr;
114 memptr++) {
115 if (strcmp(*memptr, login) == 0) {
116 /* Delete this one */
117 char **from = memptr + 1;
118 p_length -= (strlen(*memptr)+1);
119 do {
120 *(from - 1) = *from;
121 } while (*from++);
123 modified++;
124 break;
130 /* now check to see if project is one to add to */
131 if (projids) {
132 for (i = 0; projids[i] != -1; i++) {
133 if (p_ptr->pj_projid == projids[i]) {
134 /* Scan for dups */
135 exist = 0;
136 for (memptr = p_ptr->pj_users; *memptr;
137 memptr++) {
138 if (strncmp(*memptr, new_login ?
139 new_login : login,
140 strlen(*memptr)) == 0)
141 exist++;
143 p_length += strlen(new_login ?
144 new_login : login) + 1;
146 if (p_length >=
147 NSS_LINELEN_PROJECT - 1) {
148 errmsg(M_PROJ_ENTRY_OVF,
149 NSS_LINELEN_PROJECT);
150 break;
151 } else {
152 if (!exist) {
153 *memptr++ = new_login ?
154 new_login : login;
155 *memptr = NULL;
156 modified++;
162 putprojent(p_ptr, t_fptr);
165 (void) fclose(e_fptr);
166 (void) fclose(t_fptr);
168 /* Now, update project file, if it was modified */
169 if (modified && rename(t_name, PROJF_PATH) < 0) {
170 (void) unlink(t_name);
171 return (EX_UPDATE);
174 (void) unlink(t_name);
175 return (EX_SUCCESS);