17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
237c478bdstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bdstevel@tonic-gate
287c478bdstevel@tonic-gate#include <sys/types.h>
297c478bdstevel@tonic-gate#include <stdio.h>
307c478bdstevel@tonic-gate#include <ctype.h>
317c478bdstevel@tonic-gate#include <sys/stat.h>
327c478bdstevel@tonic-gate#include <project.h>
337c478bdstevel@tonic-gate#include <unistd.h>
347c478bdstevel@tonic-gate#include <userdefs.h>
357c478bdstevel@tonic-gate#include <errno.h>
367c478bdstevel@tonic-gate#include <nss_dbdefs.h>
377c478bdstevel@tonic-gate#include "users.h"
387c478bdstevel@tonic-gate#include "messages.h"
397c478bdstevel@tonic-gate
407c478bdstevel@tonic-gateint
417c478bdstevel@tonic-gateedit_project(char *login, char *new_login, projid_t projids[], int overwrite)
427c478bdstevel@tonic-gate{
437c478bdstevel@tonic-gate	char **memptr;
447c478bdstevel@tonic-gate	char t_name[] = "/etc/projtmp.XXXXXX";
457c478bdstevel@tonic-gate	FILE *e_fptr, *t_fptr;
467c478bdstevel@tonic-gate	struct project *p_ptr;
477c478bdstevel@tonic-gate	struct project p_work;
487c478bdstevel@tonic-gate	char workbuf[NSS_LINELEN_PROJECT];
497c478bdstevel@tonic-gate	int i, modified = 0;
507c478bdstevel@tonic-gate	struct stat sbuf;
517c478bdstevel@tonic-gate	int p_length;
527c478bdstevel@tonic-gate	char p_string[NSS_LINELEN_PROJECT];
537c478bdstevel@tonic-gate	long p_curr = 0;
547c478bdstevel@tonic-gate	int exist;
557c478bdstevel@tonic-gate	int fd;
567c478bdstevel@tonic-gate
577c478bdstevel@tonic-gate	if ((e_fptr = fopen(PROJF_PATH, "r")) == NULL) {
587c478bdstevel@tonic-gate		return (EX_UPDATE);
597c478bdstevel@tonic-gate	}
607c478bdstevel@tonic-gate
617c478bdstevel@tonic-gate	if (fstat(fileno(e_fptr), &sbuf) != 0)
627c478bdstevel@tonic-gate		return (EX_UPDATE);
637c478bdstevel@tonic-gate
647c478bdstevel@tonic-gate	if ((fd = mkstemp(t_name)) < 0) {
657c478bdstevel@tonic-gate		return (EX_UPDATE);
667c478bdstevel@tonic-gate	}
677c478bdstevel@tonic-gate
687c478bdstevel@tonic-gate	if ((t_fptr = fdopen(fd, "w+")) == NULL) {
697c478bdstevel@tonic-gate		(void) close(fd);
707c478bdstevel@tonic-gate		(void) unlink(t_name);
717c478bdstevel@tonic-gate		return (EX_UPDATE);
727c478bdstevel@tonic-gate	}
737c478bdstevel@tonic-gate
747c478bdstevel@tonic-gate	/*
757c478bdstevel@tonic-gate	 * Get ownership and permissions correct
767c478bdstevel@tonic-gate	 */
777c478bdstevel@tonic-gate
787c478bdstevel@tonic-gate	if (fchmod(fd, sbuf.st_mode) != 0 ||
797c478bdstevel@tonic-gate	    fchown(fd, sbuf.st_uid, sbuf.st_gid) != 0) {
807c478bdstevel@tonic-gate		(void) fclose(t_fptr);
817c478bdstevel@tonic-gate		(void) unlink(t_name);
827c478bdstevel@tonic-gate		return (EX_UPDATE);
837c478bdstevel@tonic-gate	}
847c478bdstevel@tonic-gate
857c478bdstevel@tonic-gate	p_curr = ftell(e_fptr);
867c478bdstevel@tonic-gate
877c478bdstevel@tonic-gate	/* Make TMP file look like we want project file to look */
887c478bdstevel@tonic-gate
897c478bdstevel@tonic-gate	while (fgets(p_string, NSS_LINELEN_PROJECT - 1, e_fptr)) {
907c478bdstevel@tonic-gate		/* While there is another group string */
917c478bdstevel@tonic-gate
927c478bdstevel@tonic-gate		p_length = strlen(p_string);
937c478bdstevel@tonic-gate		(void) fseek(e_fptr, p_curr, SEEK_SET);
947c478bdstevel@tonic-gate		p_ptr = fgetprojent(e_fptr, &p_work, &workbuf,
957c478bdstevel@tonic-gate		    sizeof (workbuf));
967c478bdstevel@tonic-gate		p_curr = ftell(e_fptr);
977c478bdstevel@tonic-gate
987c478bdstevel@tonic-gate		if (p_ptr == NULL) {
997c478bdstevel@tonic-gate			/*
1007c478bdstevel@tonic-gate			 * tried to parse a proj string over
1017c478bdstevel@tonic-gate			 * NSS_LINELEN_PROJECT chars
1027c478bdstevel@tonic-gate			 */
1037c478bdstevel@tonic-gate			errmsg(M_PROJ_ENTRY_OVF, NSS_LINELEN_PROJECT);
1047c478bdstevel@tonic-gate			modified = 0; /* bad project file: cannot rebuild */
1057c478bdstevel@tonic-gate			break;
1067c478bdstevel@tonic-gate		}
1077c478bdstevel@tonic-gate
1087c478bdstevel@tonic-gate		/* first delete the login from the project, if it's there */
1097c478bdstevel@tonic-gate		if (overwrite || !projids) {
1107c478bdstevel@tonic-gate			if (p_ptr->pj_users != NULL) {
1117c478bdstevel@tonic-gate				for (memptr = p_ptr->pj_users; *memptr;
1127c478bdstevel@tonic-gate				    memptr++) {
1137c478bdstevel@tonic-gate					if (strcmp(*memptr, login) == 0) {
1147c478bdstevel@tonic-gate						/* Delete this one */
1157c478bdstevel@tonic-gate						char **from = memptr + 1;
1167c478bdstevel@tonic-gate						p_length -= (strlen(*memptr)+1);
1177c478bdstevel@tonic-gate						do {
1187c478bdstevel@tonic-gate							*(from - 1) = *from;
1197c478bdstevel@tonic-gate						} while (*from++);
1207c478bdstevel@tonic-gate
1217c478bdstevel@tonic-gate						modified++;
1227c478bdstevel@tonic-gate						break;
1237c478bdstevel@tonic-gate					}
1247c478bdstevel@tonic-gate				}
1257c478bdstevel@tonic-gate			}
1267c478bdstevel@tonic-gate		}
1277c478bdstevel@tonic-gate
1287c478bdstevel@tonic-gate		/* now check to see if project is one to add to */
1297c478bdstevel@tonic-gate		if (projids) {
1307c478bdstevel@tonic-gate			for (i = 0; projids[i] != -1; i++) {
1317c478bdstevel@tonic-gate				if (p_ptr->pj_projid == projids[i]) {
1327c478bdstevel@tonic-gate					/* Scan for dups */
1337c478bdstevel@tonic-gate					exist = 0;
1347c478bdstevel@tonic-gate					for (memptr = p_ptr->pj_users; *memptr;
1357c478bdstevel@tonic-gate					    memptr++) {
1367c478bdstevel@tonic-gate						if (strncmp(*memptr, new_login ?
1377c478bdstevel@tonic-gate						    new_login : login,
1387c478bdstevel@tonic-gate						    strlen(*memptr)) == 0)
1397c478bdstevel@tonic-gate							exist++;
1407c478bdstevel@tonic-gate					}
1417c478bdstevel@tonic-gate					p_length += strlen(new_login ?
1427c478bdstevel@tonic-gate					    new_login : login) + 1;
1437c478bdstevel@tonic-gate
1447c478bdstevel@tonic-gate					if (p_length >=
1457c478bdstevel@tonic-gate					    NSS_LINELEN_PROJECT - 1) {
1467c478bdstevel@tonic-gate						errmsg(M_PROJ_ENTRY_OVF,
1477c478bdstevel@tonic-gate						    NSS_LINELEN_PROJECT);
1487c478bdstevel@tonic-gate						break;
1497c478bdstevel@tonic-gate					} else {
1507c478bdstevel@tonic-gate						if (!exist) {
1517c478bdstevel@tonic-gate						*memptr++ = new_login ?
1527c478bdstevel@tonic-gate						    new_login : login;
1537c478bdstevel@tonic-gate						*memptr = NULL;
1547c478bdstevel@tonic-gate						modified++;
1557c478bdstevel@tonic-gate						}
1567c478bdstevel@tonic-gate					}
1577c478bdstevel@tonic-gate				}
1587c478bdstevel@tonic-gate			}
1597c478bdstevel@tonic-gate		}
1607c478bdstevel@tonic-gate		putprojent(p_ptr, t_fptr);
1617c478bdstevel@tonic-gate	}
1627c478bdstevel@tonic-gate
1637c478bdstevel@tonic-gate	(void) fclose(e_fptr);
1647c478bdstevel@tonic-gate	(void) fclose(t_fptr);
1657c478bdstevel@tonic-gate
1667c478bdstevel@tonic-gate	/* Now, update project file, if it was modified */
1677c478bdstevel@tonic-gate	if (modified && rename(t_name, PROJF_PATH) < 0) {
1687c478bdstevel@tonic-gate		(void) unlink(t_name);
1697c478bdstevel@tonic-gate		return (EX_UPDATE);
1707c478bdstevel@tonic-gate	}
1717c478bdstevel@tonic-gate
1727c478bdstevel@tonic-gate	(void) unlink(t_name);
1737c478bdstevel@tonic-gate	return (EX_SUCCESS);
1747c478bdstevel@tonic-gate}
175