xref: /illumos-gate/usr/src/cmd/acct/accton.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 /*
27  * Copyright 1984-2002 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 /*
34  *	accton - calls syscall with super-user privileges
35  */
36 
37 #include <stdio.h>
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include	"acctdef.h"
41 #include	<errno.h>
42 #include	<sys/stat.h>
43 #include	<pwd.h>
44 #include	<fcntl.h>
45 
46 uid_t	admuid;
47 struct	passwd *pwd, *getpwnam();
48 
49 main(argc, argv)
50 int argc;
51 char **argv;
52 {
53 	register 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 ckfile(admfile)
78 register char	*admfile;
79 {
80 	struct stat		stbuf;
81 	register 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