Move /var/svc/log to /var/log/svc
[unleashed/lotheac.git] / usr / src / cmd / oamuser / group / mod_group.c
blob647c3505dfac8da770e70228eec27100f4284b62
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 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI"
33 #include <sys/types.h>
34 #include <stdio.h>
35 #include <ctype.h>
36 #include <grp.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 #include <userdefs.h>
40 #include <strings.h>
41 #include <stdlib.h>
42 #include "users.h"
43 #include "messages.h"
45 extern struct group *fgetgrent(FILE *);
47 /* lint error-killers: */
48 void errmsg(int, int);
50 /* Modify group to new gid and/or new name */
51 int
52 mod_group(char *group, gid_t gid, char *newgroup)
54 int modified = 0;
55 int fd;
56 char tname[] = "/etc/gtmp.XXXXXX";
57 FILE *e_fptr, *t_fptr;
58 struct group *g_ptr;
59 struct stat sbuf;
60 boolean_t haserr;
61 int line = 1;
63 if ((e_fptr = fopen(GROUP, "r")) == NULL)
64 return (EX_UPDATE);
66 if (fstat(fileno(e_fptr), &sbuf) != 0)
67 return (EX_UPDATE);
69 if ((fd = mkstemp(tname)) == -1)
70 return (EX_UPDATE);
72 if ((t_fptr = fdopen(fd, "w")) == NULL) {
73 (void) close(fd);
74 (void) unlink(tname);
75 return (EX_UPDATE);
79 * Get ownership and permissions correct
82 if (fchmod(fd, sbuf.st_mode) != 0 ||
83 fchown(fd, sbuf.st_uid, sbuf.st_gid) != 0) {
84 (void) fclose(t_fptr);
85 (void) unlink(tname);
86 return (EX_UPDATE);
89 while ((g_ptr = fgetgrent(e_fptr)) != NULL) {
91 /* check to see if group is one to modify */
92 if (strcmp(g_ptr->gr_name, group) == 0) {
93 if (newgroup != NULL)
94 g_ptr->gr_name = newgroup;
95 if (gid != -1)
96 g_ptr->gr_gid = gid;
97 modified++;
99 putgrent(g_ptr, t_fptr);
100 line++;
103 haserr = !feof(e_fptr);
104 if (haserr)
105 errmsg(M_SYNTAX, line);
107 (void) fclose(e_fptr);
109 if (fclose(t_fptr) != 0 || haserr) {
110 /* GROUP file contains bad entries or write failed. */
111 (void) unlink(tname);
112 return (EX_UPDATE);
115 if (modified) {
116 if (rename(tname, GROUP) != 0) {
117 (void) unlink(tname);
118 return (EX_UPDATE);
120 return (EX_SUCCESS);
121 } else {
122 (void) unlink(tname);
123 return (EX_NAME_NOT_EXIST);