8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / acct / accton.c
blob023f4d4c7aa08a7820b0527e6eb8555e3e30a584
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
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
26 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
29 #pragma ident "%Z%%M% %I% %E% SMI"
32 * accton - calls syscall with super-user privileges
35 #include <stdio.h>
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include "acctdef.h"
39 #include <errno.h>
40 #include <sys/stat.h>
41 #include <pwd.h>
42 #include <fcntl.h>
43 #include <stdlib.h>
45 uid_t admuid;
46 struct passwd *pwd;
48 void ckfile(char *);
50 int
51 main(int argc, char **argv)
53 uid_t uid;
55 uid = getuid();
56 if ((pwd = getpwnam("adm")) == NULL) {
57 perror("cannot determine adm's uid"), exit(1);
59 admuid = pwd->pw_uid;
60 if (uid == ROOT || uid == admuid) {
61 if (setuid(ROOT) == ERR) {
62 perror("cannot setuid (check command mode and owner)");
63 exit(1);
65 if (argv[1])
66 ckfile(argv[1]);
67 if (acct(argc > 1 ? argv[1] : 0) < 0) {
68 perror(argv[1]), exit(1);
70 exit(0);
73 fprintf(stderr, "%s: permission denied\n", argv[0]);
74 exit(1);
77 void
78 ckfile(char *admfile)
80 struct stat stbuf;
81 struct stat *s = &stbuf;
82 int fd;
84 if ((fd = open(admfile, O_RDONLY|O_CREAT, 0644)) == ERR) {
85 perror("creat"), exit(1);
88 if (fstat(fd, s) == ERR) {
89 perror("fstat");
90 exit(1);
93 if (s->st_uid != admuid || s->st_gid != (gid_t)admuid)
94 if (fchown(fd, admuid, (gid_t)admuid) == ERR) {
95 perror("cannot change owner"), exit(1);
98 /* was if(s->st_mode & 0777 != 0664) */
99 if ((s->st_mode & S_IAMB) != S_IRUSR|S_IWUSR|S_IRGRP|S_IWUSR|S_IROTH)
100 if (fchmod(fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IWUSR|S_IROTH) == ERR) {
101 perror("cannot chmod"), exit(1);
104 (void) close(fd);