1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * rmf_main.c :
31*0Sstevel@tonic-gate  *	The file containing main() for rmformat. The command line
32*0Sstevel@tonic-gate  *	options are parsed in this file.
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include <priv.h>
37*0Sstevel@tonic-gate #include "rmformat.h"
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate int32_t b_flag = 0;
40*0Sstevel@tonic-gate int32_t c_flag = 0;
41*0Sstevel@tonic-gate int32_t D_flag = 0;
42*0Sstevel@tonic-gate int32_t e_flag = 0;
43*0Sstevel@tonic-gate int32_t F_flag = 0;
44*0Sstevel@tonic-gate int32_t H_flag = 0;
45*0Sstevel@tonic-gate int32_t l_flag = 0;
46*0Sstevel@tonic-gate int32_t p_flag = 0;
47*0Sstevel@tonic-gate int32_t R_flag = 0;
48*0Sstevel@tonic-gate int32_t s_flag = 0;
49*0Sstevel@tonic-gate int32_t U_flag = 0;
50*0Sstevel@tonic-gate int32_t V_flag = 0;
51*0Sstevel@tonic-gate int32_t W_flag = 0;
52*0Sstevel@tonic-gate int32_t w_flag = 0;
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate static char *myname;
55*0Sstevel@tonic-gate char *slice_file = NULL;
56*0Sstevel@tonic-gate char *label;
57*0Sstevel@tonic-gate uint32_t repair_blk_no;
58*0Sstevel@tonic-gate int32_t quick_format = 0;
59*0Sstevel@tonic-gate int32_t long_format = 0;
60*0Sstevel@tonic-gate int32_t force_format = 0;
61*0Sstevel@tonic-gate int32_t rw_protect_enable = 0;
62*0Sstevel@tonic-gate int32_t rw_protect_disable = 0;
63*0Sstevel@tonic-gate int32_t wp_enable_passwd = 0;
64*0Sstevel@tonic-gate int32_t wp_disable_passwd = 0;
65*0Sstevel@tonic-gate int32_t wp_enable = 0;
66*0Sstevel@tonic-gate int32_t wp_disable = 0;
67*0Sstevel@tonic-gate int32_t verify_write = 0;
68*0Sstevel@tonic-gate char *dev_name = NULL;
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate static void usage(char *);
71*0Sstevel@tonic-gate void check_invalid_combinations();
72*0Sstevel@tonic-gate void check_invalid_combinations_again(int32_t);
73*0Sstevel@tonic-gate extern int64_t my_atoll(char *ptr);
74*0Sstevel@tonic-gate extern void my_perror(char *err_string);
75*0Sstevel@tonic-gate void process_options();
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate main(int32_t argc, char **argv)
79*0Sstevel@tonic-gate {
80*0Sstevel@tonic-gate 	char i;
81*0Sstevel@tonic-gate 	char *tmp_ptr;
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate 	/*
84*0Sstevel@tonic-gate 	 * This program requires file_dac_read, file_dac_write,
85*0Sstevel@tonic-gate 	 * proc_fork, and proc_exec privileges
86*0Sstevel@tonic-gate 	 *
87*0Sstevel@tonic-gate 	 * child processes require the sys_mount privilege
88*0Sstevel@tonic-gate 	 */
89*0Sstevel@tonic-gate 	(void) priv_set(PRIV_SET, PRIV_LIMIT, PRIV_FILE_DAC_READ,
90*0Sstevel@tonic-gate 	    PRIV_FILE_DAC_WRITE, PRIV_PROC_FORK, PRIV_PROC_EXEC,
91*0Sstevel@tonic-gate 	    PRIV_SYS_MOUNT, (char *)NULL);
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	/* Turn privileges off until needed */
94*0Sstevel@tonic-gate 	(void) priv_set(PRIV_OFF, PRIV_EFFECTIVE, PRIV_FILE_DAC_READ,
95*0Sstevel@tonic-gate 	    PRIV_FILE_DAC_WRITE, PRIV_PROC_FORK, PRIV_PROC_EXEC,
96*0Sstevel@tonic-gate 	    PRIV_SYS_MOUNT, (char *)NULL);
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	/* Become who we really are */
99*0Sstevel@tonic-gate 	if (seteuid(getuid()) < 0) {
100*0Sstevel@tonic-gate 		PERROR("Can't set effective user id");
101*0Sstevel@tonic-gate 		exit(1);
102*0Sstevel@tonic-gate 	}
103*0Sstevel@tonic-gate 	if (setegid(getgid()) < 0) {
104*0Sstevel@tonic-gate 		PERROR("Can't set effective group id");
105*0Sstevel@tonic-gate 		exit(1);
106*0Sstevel@tonic-gate 	}
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
111*0Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
112*0Sstevel@tonic-gate #endif
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	myname = argv[0];
117*0Sstevel@tonic-gate 	DPRINTF1("myname %s\n", myname);
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	while ((i = getopt(argc, argv, "b:c:DeF:HlpR:s:tUV:W:w:"))
120*0Sstevel@tonic-gate 							!= -1) {
121*0Sstevel@tonic-gate 		DPRINTF1("arg %c\n", i);
122*0Sstevel@tonic-gate 		switch (i) {
123*0Sstevel@tonic-gate 		case 'b' :
124*0Sstevel@tonic-gate 			b_flag++;
125*0Sstevel@tonic-gate 			label = strdup(optarg);
126*0Sstevel@tonic-gate 			if (strlen(label) > 8) {
127*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("Label is \
128*0Sstevel@tonic-gate restricted to 8 characters.\n"));
129*0Sstevel@tonic-gate 				exit(1);
130*0Sstevel@tonic-gate 			}
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 			break;
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 		case 'c' :
135*0Sstevel@tonic-gate 			c_flag++;
136*0Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
137*0Sstevel@tonic-gate 			errno = 0;
138*0Sstevel@tonic-gate 			repair_blk_no = (uint32_t)my_atoll(tmp_ptr);
139*0Sstevel@tonic-gate 			if (repair_blk_no == (uint32_t)(-1)) {
140*0Sstevel@tonic-gate 				free(tmp_ptr);
141*0Sstevel@tonic-gate 				usage("invalid block number");
142*0Sstevel@tonic-gate 			}
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 			DPRINTF1(" block no. %x\n", repair_blk_no);
145*0Sstevel@tonic-gate 			free(tmp_ptr);
146*0Sstevel@tonic-gate 			break;
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 		case 'D' :
149*0Sstevel@tonic-gate 			D_flag++;
150*0Sstevel@tonic-gate 			break;
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 		case 'e' :
153*0Sstevel@tonic-gate 			e_flag++;
154*0Sstevel@tonic-gate 			break;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 		case 'F' :
157*0Sstevel@tonic-gate 			F_flag++;
158*0Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
159*0Sstevel@tonic-gate 			if (strcmp(tmp_ptr, "quick") == 0) {
160*0Sstevel@tonic-gate 				DPRINTF("q");
161*0Sstevel@tonic-gate 				quick_format = 1;
162*0Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "long") == 0) {
163*0Sstevel@tonic-gate 				DPRINTF("l");
164*0Sstevel@tonic-gate 				long_format = 1;
165*0Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "force") == 0) {
166*0Sstevel@tonic-gate 				DPRINTF("f");
167*0Sstevel@tonic-gate 				force_format = 1;
168*0Sstevel@tonic-gate 			} else {
169*0Sstevel@tonic-gate 				free(tmp_ptr);
170*0Sstevel@tonic-gate 				usage("invalid argument for option -F");
171*0Sstevel@tonic-gate 			}
172*0Sstevel@tonic-gate 			free(tmp_ptr);
173*0Sstevel@tonic-gate 			break;
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 		case 'H' :
176*0Sstevel@tonic-gate 			H_flag++;
177*0Sstevel@tonic-gate 			break;
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 		case 'l' :
180*0Sstevel@tonic-gate 			l_flag++;
181*0Sstevel@tonic-gate 			break;
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 		case 'p' :
184*0Sstevel@tonic-gate 			p_flag++;
185*0Sstevel@tonic-gate 			break;
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 		case 'R' :
188*0Sstevel@tonic-gate 			R_flag++;
189*0Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
190*0Sstevel@tonic-gate 			if (strcmp(tmp_ptr, "enable") == 0) {
191*0Sstevel@tonic-gate 				rw_protect_enable++;
192*0Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "disable") == 0) {
193*0Sstevel@tonic-gate 				rw_protect_disable++;
194*0Sstevel@tonic-gate 			} else {
195*0Sstevel@tonic-gate 				usage("Invalid argument for -R option");
196*0Sstevel@tonic-gate 			}
197*0Sstevel@tonic-gate 			free(tmp_ptr);
198*0Sstevel@tonic-gate 			break;
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 		case 's' :
201*0Sstevel@tonic-gate 			s_flag++;
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 			slice_file = strdup(optarg);
204*0Sstevel@tonic-gate 			break;
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 		case 'U' :
207*0Sstevel@tonic-gate 			U_flag++;
208*0Sstevel@tonic-gate 			break;
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 		case 'V' :
211*0Sstevel@tonic-gate 			V_flag++;
212*0Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
213*0Sstevel@tonic-gate 			if (strcmp(tmp_ptr, "read") == 0) {
214*0Sstevel@tonic-gate 				verify_write = 0;
215*0Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "write") == 0) {
216*0Sstevel@tonic-gate 				verify_write = 1;
217*0Sstevel@tonic-gate 			} else {
218*0Sstevel@tonic-gate 				usage("Invalid argument for -V option");
219*0Sstevel@tonic-gate 			}
220*0Sstevel@tonic-gate 			free(tmp_ptr);
221*0Sstevel@tonic-gate 			break;
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 		case 'W' :
224*0Sstevel@tonic-gate 			W_flag++;
225*0Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
226*0Sstevel@tonic-gate 			if (strcmp(tmp_ptr, "enable") == 0) {
227*0Sstevel@tonic-gate 				wp_enable_passwd++;
228*0Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "disable") == 0) {
229*0Sstevel@tonic-gate 				wp_disable_passwd++;
230*0Sstevel@tonic-gate 			} else {
231*0Sstevel@tonic-gate 				usage("Invalid argument for -W option");
232*0Sstevel@tonic-gate 			}
233*0Sstevel@tonic-gate 			free(tmp_ptr);
234*0Sstevel@tonic-gate 			break;
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 		case 'w' :
237*0Sstevel@tonic-gate 			w_flag++;
238*0Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
239*0Sstevel@tonic-gate 			if (strcmp(tmp_ptr, "enable") == 0) {
240*0Sstevel@tonic-gate 				wp_enable++;
241*0Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "disable") == 0) {
242*0Sstevel@tonic-gate 				wp_disable++;
243*0Sstevel@tonic-gate 			} else {
244*0Sstevel@tonic-gate 				usage("Invalid arguments for -w option");
245*0Sstevel@tonic-gate 			}
246*0Sstevel@tonic-gate 			free(tmp_ptr);
247*0Sstevel@tonic-gate 			break;
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 		default:
250*0Sstevel@tonic-gate 			usage("");
251*0Sstevel@tonic-gate 			break;
252*0Sstevel@tonic-gate 		}
253*0Sstevel@tonic-gate 	}
254*0Sstevel@tonic-gate 	if (optind < argc -1) {
255*0Sstevel@tonic-gate 		usage("more than one device name argument");
256*0Sstevel@tonic-gate 		/* NOTREACHED */
257*0Sstevel@tonic-gate 	}
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate 	if (optind == argc -1) {
260*0Sstevel@tonic-gate 		dev_name = argv[optind];
261*0Sstevel@tonic-gate 	} else if (optind == 1) {
262*0Sstevel@tonic-gate 		/* list devices by default */
263*0Sstevel@tonic-gate 		l_flag++;
264*0Sstevel@tonic-gate 	} else if ((optind == argc) && !l_flag) {
265*0Sstevel@tonic-gate 		(void) fprintf(stderr,
266*0Sstevel@tonic-gate 		    gettext("No device specified.\n"));
267*0Sstevel@tonic-gate 		exit(1);
268*0Sstevel@tonic-gate #if 0
269*0Sstevel@tonic-gate 		(void) printf("Using floppy device\n");
270*0Sstevel@tonic-gate 		dev_name = "/dev/rdiskette";
271*0Sstevel@tonic-gate #endif /* 0 */
272*0Sstevel@tonic-gate 	}
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate 	process_options();
275*0Sstevel@tonic-gate 	return (0);
276*0Sstevel@tonic-gate }
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate static void
279*0Sstevel@tonic-gate usage(char *str)
280*0Sstevel@tonic-gate {
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	if (strlen(str)) {
283*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s : ", myname);
284*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(str));
285*0Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
286*0Sstevel@tonic-gate 	}
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 	(void) fprintf(stderr, "Usage:\n");
289*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t%s \
290*0Sstevel@tonic-gate [ -DeHpU ] [ -b label ] [ -c blockno ] [ -F quick|long|force ] \
291*0Sstevel@tonic-gate [ -R enable|disable ] [ -s filename ] [ -V read|write ] \
292*0Sstevel@tonic-gate [ -w enable|disable ] [ -W enable|disable ] devname \n"), myname);
293*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t%s -l [ devname ]\n"),
294*0Sstevel@tonic-gate 	    myname);
295*0Sstevel@tonic-gate 	exit(1);
296*0Sstevel@tonic-gate }
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate void
299*0Sstevel@tonic-gate check_invalid_combinations()
300*0Sstevel@tonic-gate {
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 	/* Inherited from FLOPPY */
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 	if (D_flag && H_flag) {
305*0Sstevel@tonic-gate 		usage("Options -D and -H incompatible");
306*0Sstevel@tonic-gate 	}
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 	if (D_flag && F_flag) {
309*0Sstevel@tonic-gate 		usage("Options -D and -F incompatible");
310*0Sstevel@tonic-gate 	}
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 	if (H_flag && F_flag) {
313*0Sstevel@tonic-gate 		usage("Options -H and -F incompatible");
314*0Sstevel@tonic-gate 	}
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 	/* rmformat additions */
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate 	if ((w_flag && W_flag) || (w_flag && R_flag) || (W_flag && R_flag)) {
319*0Sstevel@tonic-gate 		usage("Options -w, -W and -R incompatible");
320*0Sstevel@tonic-gate 	}
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate 	if (c_flag && F_flag) {
323*0Sstevel@tonic-gate 		usage("Options -c, -F incompatible");
324*0Sstevel@tonic-gate 	}
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate 	/* l_flag is mutually exclusive of these flags */
327*0Sstevel@tonic-gate 	if (l_flag && (D_flag + e_flag + H_flag + p_flag + U_flag +
328*0Sstevel@tonic-gate 	    b_flag + c_flag + F_flag + R_flag + s_flag + V_flag +
329*0Sstevel@tonic-gate 	    w_flag + W_flag)) {
330*0Sstevel@tonic-gate 		usage("Options incompatible");
331*0Sstevel@tonic-gate 	}
332*0Sstevel@tonic-gate }
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate void
336*0Sstevel@tonic-gate check_invalid_combinations_again(int32_t medium_type)
337*0Sstevel@tonic-gate {
338*0Sstevel@tonic-gate 	if ((medium_type != SM_FLOPPY) &&
339*0Sstevel@tonic-gate 			(medium_type != SM_PCMCIA_MEM)) {
340*0Sstevel@tonic-gate 		if (D_flag || H_flag) {
341*0Sstevel@tonic-gate 			usage("-D, -H  options are compatible with floppy and \
342*0Sstevel@tonic-gate PCMCIA memory cards only.");
343*0Sstevel@tonic-gate 		}
344*0Sstevel@tonic-gate 	}
345*0Sstevel@tonic-gate }
346