xref: /onnv-gate/usr/src/cmd/acct/accton.c (revision 428:a261479c3dbd)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
230Sstevel@tonic-gate /*	  All Rights Reserved  	*/
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
26*428Ssl108498  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
270Sstevel@tonic-gate  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  *	accton - calls syscall with super-user privileges
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include <stdio.h>
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <sys/param.h>
38*428Ssl108498 #include "acctdef.h"
39*428Ssl108498 #include <errno.h>
40*428Ssl108498 #include <sys/stat.h>
41*428Ssl108498 #include <pwd.h>
42*428Ssl108498 #include <fcntl.h>
43*428Ssl108498 #include <stdlib.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate uid_t	admuid;
46*428Ssl108498 struct	passwd *pwd;
47*428Ssl108498 
48*428Ssl108498 void ckfile(char *);
490Sstevel@tonic-gate 
50*428Ssl108498 int
main(int argc,char ** argv)51*428Ssl108498 main(int argc, char **argv)
520Sstevel@tonic-gate {
53*428Ssl108498 	uid_t	uid;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 	uid = getuid();
560Sstevel@tonic-gate 	if ((pwd = getpwnam("adm")) == NULL) {
570Sstevel@tonic-gate 		perror("cannot determine adm's uid"), exit(1);
580Sstevel@tonic-gate 	}
590Sstevel@tonic-gate 	admuid = pwd->pw_uid;
600Sstevel@tonic-gate 	if (uid == ROOT || uid == admuid) {
610Sstevel@tonic-gate 		if (setuid(ROOT) == ERR) {
620Sstevel@tonic-gate 			perror("cannot setuid (check command mode and owner)");
630Sstevel@tonic-gate 			exit(1);
640Sstevel@tonic-gate 		}
650Sstevel@tonic-gate 		if (argv[1])
660Sstevel@tonic-gate 			ckfile(argv[1]);
670Sstevel@tonic-gate 		if (acct(argc > 1 ? argv[1] : 0) < 0) {
680Sstevel@tonic-gate 			perror(argv[1]), exit(1);
690Sstevel@tonic-gate 		}
700Sstevel@tonic-gate 		exit(0);
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	}
730Sstevel@tonic-gate 	fprintf(stderr, "%s: permission denied\n", argv[0]);
740Sstevel@tonic-gate 	exit(1);
750Sstevel@tonic-gate }
760Sstevel@tonic-gate 
77*428Ssl108498 void
ckfile(char * admfile)78*428Ssl108498 ckfile(char *admfile)
790Sstevel@tonic-gate {
800Sstevel@tonic-gate 	struct stat		stbuf;
81*428Ssl108498 	struct stat	*s = &stbuf;
820Sstevel@tonic-gate 	int fd;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	if ((fd = open(admfile, O_RDONLY|O_CREAT, 0644)) == ERR) {
850Sstevel@tonic-gate 		perror("creat"), exit(1);
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	if (fstat(fd, s) == ERR) {
890Sstevel@tonic-gate 		perror("fstat");
900Sstevel@tonic-gate 		exit(1);
910Sstevel@tonic-gate 	}
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	if (s->st_uid != admuid || s->st_gid != (gid_t)admuid)
940Sstevel@tonic-gate 		if (fchown(fd, admuid, (gid_t)admuid) == ERR) {
950Sstevel@tonic-gate 			perror("cannot change owner"), exit(1);
960Sstevel@tonic-gate 		}
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	/* was if(s->st_mode & 0777 != 0664) */
990Sstevel@tonic-gate 	if ((s->st_mode & S_IAMB) != S_IRUSR|S_IWUSR|S_IRGRP|S_IWUSR|S_IROTH)
1000Sstevel@tonic-gate 	    if (fchmod(fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IWUSR|S_IROTH) == ERR) {
1010Sstevel@tonic-gate 			perror("cannot chmod"), exit(1);
1020Sstevel@tonic-gate 	    }
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	(void) close(fd);
1050Sstevel@tonic-gate }
106