xref: /illumos-gate/usr/src/cmd/acct/accton.c (revision 8b80e8cb6855118d46f605e91b5ed4ce83417395)
1 /*
2  * CDDL HEADER START
3  *
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.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 /*
26  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 /*
32  *	accton - calls syscall with super-user privileges
33  */
34 
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>
44 
45 uid_t	admuid;
46 struct	passwd *pwd;
47 
48 void ckfile(char *);
49 
50 int
51 main(int argc, char **argv)
52 {
53 	uid_t	uid;
54 
55 	uid = getuid();
56 	if ((pwd = getpwnam("adm")) == NULL) {
57 		perror("cannot determine adm's uid"), exit(1);
58 	}
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);
64 		}
65 		if (argv[1])
66 			ckfile(argv[1]);
67 		if (acct(argc > 1 ? argv[1] : 0) < 0) {
68 			perror(argv[1]), exit(1);
69 		}
70 		exit(0);
71 
72 	}
73 	fprintf(stderr, "%s: permission denied\n", argv[0]);
74 	exit(1);
75 }
76 
77 void
78 ckfile(char *admfile)
79 {
80 	struct stat		stbuf;
81 	struct stat	*s = &stbuf;
82 	int fd;
83 
84 	if ((fd = open(admfile, O_RDONLY|O_CREAT, 0644)) == ERR) {
85 		perror("creat"), exit(1);
86 	}
87 
88 	if (fstat(fd, s) == ERR) {
89 		perror("fstat");
90 		exit(1);
91 	}
92 
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);
96 		}
97 
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);
102 	    }
103 
104 	(void) close(fd);
105 }
106