xref: /illumos-gate/usr/src/cmd/uadmin/uadmin.c (revision 8d483882aa3390058094b043f3d62187b5d1de03)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <fcntl.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <signal.h>
37 #include <sys/uadmin.h>
38 #include <bsm/libbsm.h>
39 
40 #define	SMF_RST "/etc/svc/volatile/resetting"
41 
42 static const char *Usage = "Usage: %s cmd fcn [mdep]\n";
43 
44 extern int audit_uadmin_setup(int, char **);
45 extern int audit_uadmin_success();
46 
47 int
48 main(int argc, char *argv[])
49 {
50 	int cmd, fcn;
51 	uintptr_t mdep = NULL;
52 	sigset_t set;
53 
54 	if (argc < 3 || argc > 4) {
55 		(void) fprintf(stderr, Usage, argv[0]);
56 		return (1);
57 	}
58 
59 	(void) audit_uadmin_setup(argc, argv);
60 
61 	(void) sigfillset(&set);
62 	(void) sigprocmask(SIG_BLOCK, &set, NULL);
63 
64 	cmd = atoi(argv[1]);
65 	fcn = atoi(argv[2]);
66 	if (argc == 4) {	/* mdep argument given */
67 		if (cmd != A_REBOOT && cmd != A_SHUTDOWN && cmd != A_DUMP) {
68 			(void) fprintf(stderr, "%s: mdep argument not "
69 			    "allowed for this cmd value\n", argv[0]);
70 			(void) fprintf(stderr, Usage, argv[0]);
71 			return (1);
72 		} else {
73 			mdep = (uintptr_t)argv[3];
74 		}
75 	}
76 
77 	if (geteuid() == 0) {
78 		if (audit_uadmin_success() == -1)
79 			(void) fprintf(stderr, "%s: can't turn off auditd\n",
80 			    argv[0]);
81 
82 		if (cmd == A_SHUTDOWN || cmd == A_REBOOT)
83 			(void) creat(SMF_RST, 0777);
84 	}
85 
86 	if (uadmin(cmd, fcn, mdep) < 0) {
87 		perror("uadmin");
88 
89 		(void) unlink(SMF_RST);
90 
91 		return (1);
92 	}
93 
94 	return (0);
95 }
96