xref: /illumos-gate/usr/src/cmd/uadmin/uadmin.c (revision bb57d1f5164aca913cbd286ae1b61c896167cfa7)
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 2007 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/adt.h>
39 #include <bsm/adt_event.h>
40 #include <libscf.h>
41 #include <strings.h>
42 
43 #define	SMF_RST	"/etc/svc/volatile/resetting"
44 #define	AUDITD_FMRI	"svc:/system/auditd:default"
45 
46 static const char *Usage = "Usage: %s cmd fcn [mdep]\n";
47 
48 static int turnoff_auditd();
49 static void wait_for_auqueue();
50 
51 int
52 main(int argc, char *argv[])
53 {
54 	int cmd, fcn;
55 	uintptr_t mdep = NULL;
56 	sigset_t set;
57 	adt_session_data_t *ah;  /* audit session handle */
58 	adt_event_data_t *event = NULL; /* event to be generated */
59 	au_event_t event_id;
60 	enum adt_uadmin_fcn fcn_id;
61 
62 	if (argc < 3 || argc > 4) {
63 		(void) fprintf(stderr, Usage, argv[0]);
64 		return (1);
65 	}
66 
67 	(void) sigfillset(&set);
68 	(void) sigprocmask(SIG_BLOCK, &set, NULL);
69 
70 	cmd = atoi(argv[1]);
71 	fcn = atoi(argv[2]);
72 	if (argc == 4) {	/* mdep argument given */
73 		if (cmd != A_REBOOT && cmd != A_SHUTDOWN && cmd != A_DUMP &&
74 		    cmd != A_FREEZE) {
75 			(void) fprintf(stderr, "%s: mdep argument not "
76 			    "allowed for this cmd value\n", argv[0]);
77 			(void) fprintf(stderr, Usage, argv[0]);
78 			return (1);
79 		} else {
80 			mdep = (uintptr_t)argv[3];
81 		}
82 	}
83 
84 	/* set up audit session and event */
85 	if (adt_start_session(&ah, NULL, ADT_USE_PROC_DATA) != 0) {
86 		(void) fprintf(stderr, "%s: can't start audit session\n",
87 		    argv[0]);
88 	}
89 	switch (cmd) {
90 	case A_SHUTDOWN:
91 		event_id = ADT_uadmin_shutdown;
92 		break;
93 	case A_REBOOT:
94 		event_id = ADT_uadmin_reboot;
95 		break;
96 	case A_DUMP:
97 		event_id = ADT_uadmin_dump;
98 		break;
99 	case A_REMOUNT:
100 		event_id = ADT_uadmin_remount;
101 		break;
102 	case A_FREEZE:
103 		event_id = ADT_uadmin_freeze;
104 		break;
105 	case A_FTRACE:
106 		event_id = ADT_uadmin_ftrace;
107 		break;
108 	case A_SWAPCTL:
109 		event_id = ADT_uadmin_swapctl;
110 		break;
111 	default:
112 		event_id = 0;
113 	}
114 	if ((event_id != 0) &&
115 	    (event = adt_alloc_event(ah, event_id)) == NULL) {
116 		(void) fprintf(stderr, "%s: can't allocate audit event\n",
117 		    argv[0]);
118 	}
119 	switch (fcn) {
120 	case AD_HALT:
121 		fcn_id = ADT_UADMIN_FCN_AD_HALT;
122 		break;
123 	case AD_POWEROFF:
124 		fcn_id = ADT_UADMIN_FCN_AD_POWEROFF;
125 		break;
126 	case AD_BOOT:
127 		fcn_id = ADT_UADMIN_FCN_AD_BOOT;
128 		break;
129 	case AD_IBOOT:
130 		fcn_id = ADT_UADMIN_FCN_AD_IBOOT;
131 		break;
132 	case AD_SBOOT:
133 		fcn_id = ADT_UADMIN_FCN_AD_SBOOT;
134 		break;
135 	case AD_SIBOOT:
136 		fcn_id = ADT_UADMIN_FCN_AD_SIBOOT;
137 		break;
138 	case AD_NOSYNC:
139 		fcn_id = ADT_UADMIN_FCN_AD_NOSYNC;
140 		break;
141 	default:
142 		fcn_id = 0;
143 	}
144 	if (cmd == A_FREEZE) {
145 		switch (fcn) {
146 		case AD_SUSPEND_TO_DISK:
147 			fcn_id = ADT_UADMIN_FCN_AD_SUSPEND_TO_DISK;
148 			break;
149 		case AD_CHECK_SUSPEND_TO_DISK:
150 			fcn_id = ADT_UADMIN_FCN_AD_CHECK_SUSPEND_TO_DISK;
151 			break;
152 		case AD_FORCE:
153 			fcn_id = ADT_UADMIN_FCN_AD_FORCE;
154 			break;
155 		case AD_SUSPEND_TO_RAM:
156 			fcn_id = ADT_UADMIN_FCN_AD_SUSPEND_TO_RAM;
157 			break;
158 		case AD_CHECK_SUSPEND_TO_RAM:
159 			fcn_id = ADT_UADMIN_FCN_AD_CHECK_SUSPEND_TO_RAM;
160 			break;
161 		case AD_REUSEINIT:
162 			fcn_id = ADT_UADMIN_FCN_AD_REUSEINIT;
163 			break;
164 		case AD_REUSABLE:
165 			fcn_id = ADT_UADMIN_FCN_AD_REUSABLE;
166 			break;
167 		case AD_REUSEFINI:
168 			fcn_id = ADT_UADMIN_FCN_AD_REUSEFINI;
169 			break;
170 		}
171 	} else if (cmd == A_FTRACE) {
172 		switch (fcn) {
173 		case AD_FTRACE_START:
174 			fcn_id = ADT_UADMIN_FCN_AD_FTRACE_START;
175 			break;
176 		case AD_FTRACE_STOP:
177 			fcn_id = ADT_UADMIN_FCN_AD_FTRACE_STOP;
178 			break;
179 		}
180 	}
181 
182 	if (geteuid() == 0) {
183 		if (event != NULL) {
184 			switch (cmd) {
185 			case A_SHUTDOWN:
186 				event->adt_uadmin_shutdown.fcn = fcn_id;
187 				event->adt_uadmin_shutdown.mdep = (char *)mdep;
188 				break;
189 			case A_REBOOT:
190 				event->adt_uadmin_reboot.fcn = fcn_id;
191 				event->adt_uadmin_reboot.mdep = (char *)mdep;
192 				break;
193 			case A_DUMP:
194 				event->adt_uadmin_dump.fcn = fcn_id;
195 				event->adt_uadmin_dump.mdep = (char *)mdep;
196 				break;
197 			case A_REMOUNT:
198 				/* no parameters */
199 				break;
200 			case A_FREEZE:
201 				event->adt_uadmin_freeze.fcn = fcn_id;
202 				event->adt_uadmin_freeze.mdep = (char *)mdep;
203 				break;
204 			case A_FTRACE:
205 				event->adt_uadmin_ftrace.fcn = fcn_id;
206 				break;
207 			case A_SWAPCTL:
208 				event->adt_uadmin_swapctl.fcn = fcn_id;
209 				break;
210 			}
211 
212 			if (adt_put_event(event, ADT_SUCCESS, 0) != 0) {
213 				(void) fprintf(stderr,
214 				    "%s: can't put audit event\n", argv[0]);
215 			}
216 			/*
217 			 * allow audit record to be processed in the kernel
218 			 * audit queue
219 			 */
220 			wait_for_auqueue();
221 		}
222 
223 		if (turnoff_auditd() == -1)
224 			(void) fprintf(stderr, "%s: can't turn off auditd\n",
225 			    argv[0]);
226 
227 		if (cmd == A_SHUTDOWN || cmd == A_REBOOT)
228 			(void) creat(SMF_RST, 0777);
229 	}
230 
231 	(void) adt_free_event(event);
232 	(void) adt_end_session(ah);
233 
234 	if (uadmin(cmd, fcn, mdep) < 0) {
235 		perror("uadmin");
236 
237 		(void) unlink(SMF_RST);
238 
239 		return (1);
240 	}
241 
242 	return (0);
243 }
244 
245 static int
246 turnoff_auditd()
247 {
248 	char	*smf_state;
249 	int	rc = -1;
250 	int	retries = 15;
251 
252 	if (smf_disable_instance(AUDITD_FMRI, SMF_TEMPORARY) != 0) {
253 		(void) fprintf(stderr, "error disabling auditd: %s\n",
254 		    scf_strerror(scf_error()));
255 		return (-1);
256 	}
257 
258 	/* wait for auditd to finish its work */
259 	do {
260 		if ((smf_state = smf_get_state(AUDITD_FMRI)) == NULL) {
261 			(void) fprintf(stderr,
262 			    "getting state of auditd failed: %s\n",
263 			    scf_strerror(scf_error()));
264 			return (-1);
265 		}
266 
267 		if (strcmp(smf_state, SCF_STATE_STRING_DISABLED)) {
268 			retries--;
269 			(void) sleep(1);
270 		} else {
271 			rc = 0;
272 		}
273 		free(smf_state);
274 	} while (rc && retries);
275 
276 	return (rc);
277 }
278 
279 static void
280 wait_for_auqueue()
281 {
282 	au_stat_t	au_stat;
283 	int		retries = 10;
284 
285 	while (retries-- && auditon(A_GETSTAT, (caddr_t)&au_stat, NULL) == 0) {
286 		if (au_stat.as_enqueue == au_stat.as_written) {
287 			break;
288 		}
289 		(void) sleep(1);
290 	}
291 }
292