xref: /illumos-gate/usr/src/cmd/acct/accton.c (revision 2a8bcb4e)
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 
30 /*
31  *	accton - calls syscall with super-user privileges
32  */
33 
34 #include <stdio.h>
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include "acctdef.h"
38 #include <errno.h>
39 #include <sys/stat.h>
40 #include <pwd.h>
41 #include <fcntl.h>
42 #include <stdlib.h>
43 
44 uid_t	admuid;
45 struct	passwd *pwd;
46 
47 void ckfile(char *);
48 
49 int
main(int argc,char ** argv)50 main(int argc, char **argv)
51 {
52 	uid_t	uid;
53 
54 	uid = getuid();
55 	if ((pwd = getpwnam("adm")) == NULL) {
56 		perror("cannot determine adm's uid"), exit(1);
57 	}
58 	admuid = pwd->pw_uid;
59 	if (uid == ROOT || uid == admuid) {
60 		if (setuid(ROOT) == ERR) {
61 			perror("cannot setuid (check command mode and owner)");
62 			exit(1);
63 		}
64 		if (argv[1])
65 			ckfile(argv[1]);
66 		if (acct(argc > 1 ? argv[1] : 0) < 0) {
67 			perror(argv[1]), exit(1);
68 		}
69 		exit(0);
70 
71 	}
72 	fprintf(stderr, "%s: permission denied\n", argv[0]);
73 	exit(1);
74 }
75 
76 void
ckfile(char * admfile)77 ckfile(char *admfile)
78 {
79 	struct stat		stbuf;
80 	struct stat	*s = &stbuf;
81 	int fd;
82 
83 	if ((fd = open(admfile, O_RDONLY|O_CREAT, 0644)) == ERR) {
84 		perror("creat"), exit(1);
85 	}
86 
87 	if (fstat(fd, s) == ERR) {
88 		perror("fstat");
89 		exit(1);
90 	}
91 
92 	if (s->st_uid != admuid || s->st_gid != (gid_t)admuid)
93 		if (fchown(fd, admuid, (gid_t)admuid) == ERR) {
94 			perror("cannot change owner"), exit(1);
95 		}
96 
97 	/* was if(s->st_mode & 0777 != 0664) */
98 	if ((s->st_mode & S_IAMB) != S_IRUSR|S_IWUSR|S_IRGRP|S_IWUSR|S_IROTH)
99 	    if (fchmod(fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IWUSR|S_IROTH) == ERR) {
100 			perror("cannot chmod"), exit(1);
101 	    }
102 
103 	(void) close(fd);
104 }
105