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 /*
23*796Smathue  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * rmf_main.c :
310Sstevel@tonic-gate  *	The file containing main() for rmformat. The command line
320Sstevel@tonic-gate  *	options are parsed in this file.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <priv.h>
370Sstevel@tonic-gate #include "rmformat.h"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate int32_t b_flag = 0;
400Sstevel@tonic-gate int32_t c_flag = 0;
410Sstevel@tonic-gate int32_t D_flag = 0;
420Sstevel@tonic-gate int32_t e_flag = 0;
430Sstevel@tonic-gate int32_t F_flag = 0;
440Sstevel@tonic-gate int32_t H_flag = 0;
450Sstevel@tonic-gate int32_t l_flag = 0;
460Sstevel@tonic-gate int32_t p_flag = 0;
470Sstevel@tonic-gate int32_t R_flag = 0;
480Sstevel@tonic-gate int32_t s_flag = 0;
490Sstevel@tonic-gate int32_t U_flag = 0;
500Sstevel@tonic-gate int32_t V_flag = 0;
510Sstevel@tonic-gate int32_t W_flag = 0;
520Sstevel@tonic-gate int32_t w_flag = 0;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate static char *myname;
550Sstevel@tonic-gate char *slice_file = NULL;
560Sstevel@tonic-gate char *label;
570Sstevel@tonic-gate uint32_t repair_blk_no;
580Sstevel@tonic-gate int32_t quick_format = 0;
590Sstevel@tonic-gate int32_t long_format = 0;
600Sstevel@tonic-gate int32_t force_format = 0;
610Sstevel@tonic-gate int32_t rw_protect_enable = 0;
620Sstevel@tonic-gate int32_t rw_protect_disable = 0;
630Sstevel@tonic-gate int32_t wp_enable_passwd = 0;
640Sstevel@tonic-gate int32_t wp_disable_passwd = 0;
650Sstevel@tonic-gate int32_t wp_enable = 0;
660Sstevel@tonic-gate int32_t wp_disable = 0;
670Sstevel@tonic-gate int32_t verify_write = 0;
680Sstevel@tonic-gate char *dev_name = NULL;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate static void usage(char *);
710Sstevel@tonic-gate void check_invalid_combinations();
720Sstevel@tonic-gate void check_invalid_combinations_again(int32_t);
730Sstevel@tonic-gate extern int64_t my_atoll(char *ptr);
740Sstevel@tonic-gate extern void my_perror(char *err_string);
750Sstevel@tonic-gate void process_options();
760Sstevel@tonic-gate 
77*796Smathue int
780Sstevel@tonic-gate main(int32_t argc, char **argv)
790Sstevel@tonic-gate {
800Sstevel@tonic-gate 	char i;
810Sstevel@tonic-gate 	char *tmp_ptr;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	/*
840Sstevel@tonic-gate 	 * This program requires file_dac_read, file_dac_write,
850Sstevel@tonic-gate 	 * proc_fork, and proc_exec privileges
860Sstevel@tonic-gate 	 *
870Sstevel@tonic-gate 	 * child processes require the sys_mount privilege
880Sstevel@tonic-gate 	 */
890Sstevel@tonic-gate 	(void) priv_set(PRIV_SET, PRIV_LIMIT, PRIV_FILE_DAC_READ,
900Sstevel@tonic-gate 	    PRIV_FILE_DAC_WRITE, PRIV_PROC_FORK, PRIV_PROC_EXEC,
910Sstevel@tonic-gate 	    PRIV_SYS_MOUNT, (char *)NULL);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	/* Turn privileges off until needed */
940Sstevel@tonic-gate 	(void) priv_set(PRIV_OFF, PRIV_EFFECTIVE, PRIV_FILE_DAC_READ,
950Sstevel@tonic-gate 	    PRIV_FILE_DAC_WRITE, PRIV_PROC_FORK, PRIV_PROC_EXEC,
960Sstevel@tonic-gate 	    PRIV_SYS_MOUNT, (char *)NULL);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	/* Become who we really are */
990Sstevel@tonic-gate 	if (seteuid(getuid()) < 0) {
1000Sstevel@tonic-gate 		PERROR("Can't set effective user id");
1010Sstevel@tonic-gate 		exit(1);
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 	if (setegid(getgid()) < 0) {
1040Sstevel@tonic-gate 		PERROR("Can't set effective group id");
1050Sstevel@tonic-gate 		exit(1);
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1110Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
1120Sstevel@tonic-gate #endif
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	myname = argv[0];
1170Sstevel@tonic-gate 	DPRINTF1("myname %s\n", myname);
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	while ((i = getopt(argc, argv, "b:c:DeF:HlpR:s:tUV:W:w:"))
1200Sstevel@tonic-gate 							!= -1) {
1210Sstevel@tonic-gate 		DPRINTF1("arg %c\n", i);
1220Sstevel@tonic-gate 		switch (i) {
1230Sstevel@tonic-gate 		case 'b' :
1240Sstevel@tonic-gate 			b_flag++;
1250Sstevel@tonic-gate 			label = strdup(optarg);
1260Sstevel@tonic-gate 			if (strlen(label) > 8) {
1270Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("Label is \
1280Sstevel@tonic-gate restricted to 8 characters.\n"));
1290Sstevel@tonic-gate 				exit(1);
1300Sstevel@tonic-gate 			}
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 			break;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 		case 'c' :
1350Sstevel@tonic-gate 			c_flag++;
1360Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
1370Sstevel@tonic-gate 			errno = 0;
1380Sstevel@tonic-gate 			repair_blk_no = (uint32_t)my_atoll(tmp_ptr);
1390Sstevel@tonic-gate 			if (repair_blk_no == (uint32_t)(-1)) {
1400Sstevel@tonic-gate 				free(tmp_ptr);
1410Sstevel@tonic-gate 				usage("invalid block number");
1420Sstevel@tonic-gate 			}
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 			DPRINTF1(" block no. %x\n", repair_blk_no);
1450Sstevel@tonic-gate 			free(tmp_ptr);
1460Sstevel@tonic-gate 			break;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 		case 'D' :
1490Sstevel@tonic-gate 			D_flag++;
1500Sstevel@tonic-gate 			break;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 		case 'e' :
1530Sstevel@tonic-gate 			e_flag++;
1540Sstevel@tonic-gate 			break;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 		case 'F' :
1570Sstevel@tonic-gate 			F_flag++;
1580Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
1590Sstevel@tonic-gate 			if (strcmp(tmp_ptr, "quick") == 0) {
1600Sstevel@tonic-gate 				DPRINTF("q");
1610Sstevel@tonic-gate 				quick_format = 1;
1620Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "long") == 0) {
1630Sstevel@tonic-gate 				DPRINTF("l");
1640Sstevel@tonic-gate 				long_format = 1;
1650Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "force") == 0) {
1660Sstevel@tonic-gate 				DPRINTF("f");
1670Sstevel@tonic-gate 				force_format = 1;
1680Sstevel@tonic-gate 			} else {
1690Sstevel@tonic-gate 				free(tmp_ptr);
1700Sstevel@tonic-gate 				usage("invalid argument for option -F");
1710Sstevel@tonic-gate 			}
1720Sstevel@tonic-gate 			free(tmp_ptr);
1730Sstevel@tonic-gate 			break;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 		case 'H' :
1760Sstevel@tonic-gate 			H_flag++;
1770Sstevel@tonic-gate 			break;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 		case 'l' :
1800Sstevel@tonic-gate 			l_flag++;
1810Sstevel@tonic-gate 			break;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 		case 'p' :
1840Sstevel@tonic-gate 			p_flag++;
1850Sstevel@tonic-gate 			break;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 		case 'R' :
1880Sstevel@tonic-gate 			R_flag++;
1890Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
1900Sstevel@tonic-gate 			if (strcmp(tmp_ptr, "enable") == 0) {
1910Sstevel@tonic-gate 				rw_protect_enable++;
1920Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "disable") == 0) {
1930Sstevel@tonic-gate 				rw_protect_disable++;
1940Sstevel@tonic-gate 			} else {
1950Sstevel@tonic-gate 				usage("Invalid argument for -R option");
1960Sstevel@tonic-gate 			}
1970Sstevel@tonic-gate 			free(tmp_ptr);
1980Sstevel@tonic-gate 			break;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 		case 's' :
2010Sstevel@tonic-gate 			s_flag++;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 			slice_file = strdup(optarg);
2040Sstevel@tonic-gate 			break;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 		case 'U' :
2070Sstevel@tonic-gate 			U_flag++;
2080Sstevel@tonic-gate 			break;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 		case 'V' :
2110Sstevel@tonic-gate 			V_flag++;
2120Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
2130Sstevel@tonic-gate 			if (strcmp(tmp_ptr, "read") == 0) {
2140Sstevel@tonic-gate 				verify_write = 0;
2150Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "write") == 0) {
2160Sstevel@tonic-gate 				verify_write = 1;
2170Sstevel@tonic-gate 			} else {
2180Sstevel@tonic-gate 				usage("Invalid argument for -V option");
2190Sstevel@tonic-gate 			}
2200Sstevel@tonic-gate 			free(tmp_ptr);
2210Sstevel@tonic-gate 			break;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 		case 'W' :
2240Sstevel@tonic-gate 			W_flag++;
2250Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
2260Sstevel@tonic-gate 			if (strcmp(tmp_ptr, "enable") == 0) {
2270Sstevel@tonic-gate 				wp_enable_passwd++;
2280Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "disable") == 0) {
2290Sstevel@tonic-gate 				wp_disable_passwd++;
2300Sstevel@tonic-gate 			} else {
2310Sstevel@tonic-gate 				usage("Invalid argument for -W option");
2320Sstevel@tonic-gate 			}
2330Sstevel@tonic-gate 			free(tmp_ptr);
2340Sstevel@tonic-gate 			break;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 		case 'w' :
2370Sstevel@tonic-gate 			w_flag++;
2380Sstevel@tonic-gate 			tmp_ptr = strdup(optarg);
2390Sstevel@tonic-gate 			if (strcmp(tmp_ptr, "enable") == 0) {
2400Sstevel@tonic-gate 				wp_enable++;
2410Sstevel@tonic-gate 			} else if (strcmp(tmp_ptr, "disable") == 0) {
2420Sstevel@tonic-gate 				wp_disable++;
2430Sstevel@tonic-gate 			} else {
2440Sstevel@tonic-gate 				usage("Invalid arguments for -w option");
2450Sstevel@tonic-gate 			}
2460Sstevel@tonic-gate 			free(tmp_ptr);
2470Sstevel@tonic-gate 			break;
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 		default:
2500Sstevel@tonic-gate 			usage("");
2510Sstevel@tonic-gate 			break;
2520Sstevel@tonic-gate 		}
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 	if (optind < argc -1) {
2550Sstevel@tonic-gate 		usage("more than one device name argument");
2560Sstevel@tonic-gate 		/* NOTREACHED */
2570Sstevel@tonic-gate 	}
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	if (optind == argc -1) {
2600Sstevel@tonic-gate 		dev_name = argv[optind];
2610Sstevel@tonic-gate 	} else if (optind == 1) {
2620Sstevel@tonic-gate 		/* list devices by default */
2630Sstevel@tonic-gate 		l_flag++;
2640Sstevel@tonic-gate 	} else if ((optind == argc) && !l_flag) {
2650Sstevel@tonic-gate 		(void) fprintf(stderr,
2660Sstevel@tonic-gate 		    gettext("No device specified.\n"));
2670Sstevel@tonic-gate 		exit(1);
2680Sstevel@tonic-gate #if 0
2690Sstevel@tonic-gate 		(void) printf("Using floppy device\n");
2700Sstevel@tonic-gate 		dev_name = "/dev/rdiskette";
2710Sstevel@tonic-gate #endif /* 0 */
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	process_options();
2750Sstevel@tonic-gate 	return (0);
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate static void
2790Sstevel@tonic-gate usage(char *str)
2800Sstevel@tonic-gate {
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	if (strlen(str)) {
2830Sstevel@tonic-gate 		(void) fprintf(stderr, "%s : ", myname);
2840Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(str));
2850Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
2860Sstevel@tonic-gate 	}
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	(void) fprintf(stderr, "Usage:\n");
2890Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t%s \
2900Sstevel@tonic-gate [ -DeHpU ] [ -b label ] [ -c blockno ] [ -F quick|long|force ] \
2910Sstevel@tonic-gate [ -R enable|disable ] [ -s filename ] [ -V read|write ] \
2920Sstevel@tonic-gate [ -w enable|disable ] [ -W enable|disable ] devname \n"), myname);
2930Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\t%s -l [ devname ]\n"),
2940Sstevel@tonic-gate 	    myname);
2950Sstevel@tonic-gate 	exit(1);
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate void
2990Sstevel@tonic-gate check_invalid_combinations()
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	/* Inherited from FLOPPY */
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	if (D_flag && H_flag) {
3050Sstevel@tonic-gate 		usage("Options -D and -H incompatible");
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	if (D_flag && F_flag) {
3090Sstevel@tonic-gate 		usage("Options -D and -F incompatible");
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	if (H_flag && F_flag) {
3130Sstevel@tonic-gate 		usage("Options -H and -F incompatible");
3140Sstevel@tonic-gate 	}
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	/* rmformat additions */
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	if ((w_flag && W_flag) || (w_flag && R_flag) || (W_flag && R_flag)) {
3190Sstevel@tonic-gate 		usage("Options -w, -W and -R incompatible");
3200Sstevel@tonic-gate 	}
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	if (c_flag && F_flag) {
3230Sstevel@tonic-gate 		usage("Options -c, -F incompatible");
3240Sstevel@tonic-gate 	}
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	/* l_flag is mutually exclusive of these flags */
3270Sstevel@tonic-gate 	if (l_flag && (D_flag + e_flag + H_flag + p_flag + U_flag +
3280Sstevel@tonic-gate 	    b_flag + c_flag + F_flag + R_flag + s_flag + V_flag +
3290Sstevel@tonic-gate 	    w_flag + W_flag)) {
3300Sstevel@tonic-gate 		usage("Options incompatible");
3310Sstevel@tonic-gate 	}
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate void
3360Sstevel@tonic-gate check_invalid_combinations_again(int32_t medium_type)
3370Sstevel@tonic-gate {
3380Sstevel@tonic-gate 	if ((medium_type != SM_FLOPPY) &&
3390Sstevel@tonic-gate 			(medium_type != SM_PCMCIA_MEM)) {
3400Sstevel@tonic-gate 		if (D_flag || H_flag) {
3410Sstevel@tonic-gate 			usage("-D, -H  options are compatible with floppy and \
3420Sstevel@tonic-gate PCMCIA memory cards only.");
3430Sstevel@tonic-gate 		}
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate }
346