17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
237c478bd9Sstevel@tonic-gate /* All Rights Reserved */
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate * Copyright (c) 1996-1998, 2001 by Sun Microsystems, Inc.
287c478bd9Sstevel@tonic-gate * All rights reserved.
297c478bd9Sstevel@tonic-gate */
30*4656d474SGarrett D'Amore /*
31*4656d474SGarrett D'Amore * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
32*4656d474SGarrett D'Amore */
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <ctype.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <limits.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/stat.h>
427c478bd9Sstevel@tonic-gate #include "valtools.h"
437c478bd9Sstevel@tonic-gate #include <stdlib.h>
447c478bd9Sstevel@tonic-gate #include <fcntl.h>
457c478bd9Sstevel@tonic-gate #include <unistd.h>
467c478bd9Sstevel@tonic-gate #include "libadm.h"
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate #define E_SYNTAX "does not meet suggested filename syntax standard"
497c478bd9Sstevel@tonic-gate #define E_READ "is not readable"
507c478bd9Sstevel@tonic-gate #define E_WRITE "is not writable"
517c478bd9Sstevel@tonic-gate #define E_EXEC "is not executable"
527c478bd9Sstevel@tonic-gate #define E_CREAT "cannot be created"
537c478bd9Sstevel@tonic-gate #define E_ABSOLUTE "must begin with a slash (/)"
547c478bd9Sstevel@tonic-gate #define E_RELATIVE "must not begin with a slash (/)"
557c478bd9Sstevel@tonic-gate #define E_EXIST "does not exist"
567c478bd9Sstevel@tonic-gate #define E_NEXIST "must not already exist"
577c478bd9Sstevel@tonic-gate #define E_BLK "must specify a block special device"
587c478bd9Sstevel@tonic-gate #define E_CHR "must specify a character special device"
597c478bd9Sstevel@tonic-gate #define E_DIR "must specify a directory"
607c478bd9Sstevel@tonic-gate #define E_REG "must be a regular file"
617c478bd9Sstevel@tonic-gate #define E_NONZERO "must be a file of non-zero length"
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate #define H_READ "must be readable"
647c478bd9Sstevel@tonic-gate #define H_WRITE "must be writable"
657c478bd9Sstevel@tonic-gate #define H_EXEC "must be executable"
667c478bd9Sstevel@tonic-gate #define H_CREAT "will be created if it does not exist"
677c478bd9Sstevel@tonic-gate #define H_ABSOLUTE E_ABSOLUTE
687c478bd9Sstevel@tonic-gate #define H_RELATIVE E_RELATIVE
697c478bd9Sstevel@tonic-gate #define H_EXIST "must already exist"
707c478bd9Sstevel@tonic-gate #define H_NEXIST "must not already exist"
717c478bd9Sstevel@tonic-gate #define H_BLK E_BLK
727c478bd9Sstevel@tonic-gate #define H_CHR E_CHR
737c478bd9Sstevel@tonic-gate #define H_DIR E_DIR
747c478bd9Sstevel@tonic-gate #define H_REG E_REG
757c478bd9Sstevel@tonic-gate #define H_NONZERO E_NONZERO
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate #define MSGSIZ 1024
787c478bd9Sstevel@tonic-gate #define STDHELP \
797c478bd9Sstevel@tonic-gate "A pathname is a filename, optionally preceded by parent directories."
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate static char *errstr;
827c478bd9Sstevel@tonic-gate static char *badset = "*?[]{}()<> \t'`\"\\|^";
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate static void
addhlp(char * msg,char * text)857c478bd9Sstevel@tonic-gate addhlp(char *msg, char *text)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate static int count;
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate if (text == NULL) {
907c478bd9Sstevel@tonic-gate count = 0;
917c478bd9Sstevel@tonic-gate return;
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate if (!count++)
947c478bd9Sstevel@tonic-gate (void) strcat(msg, " The pathname you enter:");
957c478bd9Sstevel@tonic-gate (void) strcat(msg, "\\n\\t-\\ ");
967c478bd9Sstevel@tonic-gate (void) strcat(msg, text);
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate static char *
sethlp(int pflags)1007c478bd9Sstevel@tonic-gate sethlp(int pflags)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate char *msg;
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate msg = calloc(MSGSIZ, sizeof (char));
1057c478bd9Sstevel@tonic-gate addhlp(msg, NULL); /* initialize count */
1067c478bd9Sstevel@tonic-gate (void) strcpy(msg, STDHELP);
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate if (pflags & P_EXIST)
1097c478bd9Sstevel@tonic-gate addhlp(msg, H_EXIST);
1107c478bd9Sstevel@tonic-gate else if (pflags & P_NEXIST)
1117c478bd9Sstevel@tonic-gate addhlp(msg, H_NEXIST);
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate if (pflags & P_ABSOLUTE)
1147c478bd9Sstevel@tonic-gate addhlp(msg, H_ABSOLUTE);
1157c478bd9Sstevel@tonic-gate else if (pflags & P_RELATIVE)
1167c478bd9Sstevel@tonic-gate addhlp(msg, H_RELATIVE);
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate if (pflags & P_READ)
1197c478bd9Sstevel@tonic-gate addhlp(msg, H_READ);
1207c478bd9Sstevel@tonic-gate if (pflags & P_WRITE)
1217c478bd9Sstevel@tonic-gate addhlp(msg, H_WRITE);
1227c478bd9Sstevel@tonic-gate if (pflags & P_EXEC)
1237c478bd9Sstevel@tonic-gate addhlp(msg, H_EXEC);
1247c478bd9Sstevel@tonic-gate if (pflags & P_CREAT)
1257c478bd9Sstevel@tonic-gate addhlp(msg, H_CREAT);
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate if (pflags & P_BLK)
1287c478bd9Sstevel@tonic-gate addhlp(msg, H_BLK);
1297c478bd9Sstevel@tonic-gate else if (pflags & P_CHR)
1307c478bd9Sstevel@tonic-gate addhlp(msg, H_CHR);
1317c478bd9Sstevel@tonic-gate else if (pflags & P_DIR)
1327c478bd9Sstevel@tonic-gate addhlp(msg, H_DIR);
1337c478bd9Sstevel@tonic-gate else if (pflags & P_REG)
1347c478bd9Sstevel@tonic-gate addhlp(msg, H_REG);
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate if (pflags & P_NONZERO)
1377c478bd9Sstevel@tonic-gate addhlp(msg, H_NONZERO);
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate return (msg);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate int
ckpath_stx(int pflags)1437c478bd9Sstevel@tonic-gate ckpath_stx(int pflags)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate if (((pflags & P_ABSOLUTE) && (pflags & P_RELATIVE)) ||
1467c478bd9Sstevel@tonic-gate ((pflags & P_NEXIST) && (pflags &
1477c478bd9Sstevel@tonic-gate (P_EXIST|P_NONZERO|P_READ|P_WRITE|P_EXEC))) ||
1487c478bd9Sstevel@tonic-gate ((pflags & P_CREAT) && (pflags & (P_EXIST|P_NEXIST|P_BLK|P_CHR))) ||
1497c478bd9Sstevel@tonic-gate ((pflags & P_BLK) && (pflags & (P_CHR|P_REG|P_DIR|P_NONZERO))) ||
1507c478bd9Sstevel@tonic-gate ((pflags & P_CHR) && (pflags & (P_REG|P_DIR|P_NONZERO))) ||
1517c478bd9Sstevel@tonic-gate ((pflags & P_DIR) && (pflags & P_REG))) {
1527c478bd9Sstevel@tonic-gate return (1);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate return (0);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate int
ckpath_val(char * path,int pflags)1587c478bd9Sstevel@tonic-gate ckpath_val(char *path, int pflags)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate struct stat64 status;
1617c478bd9Sstevel@tonic-gate int fd;
1627c478bd9Sstevel@tonic-gate char *pt;
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate if ((pflags & P_RELATIVE) && (*path == '/')) {
1657c478bd9Sstevel@tonic-gate errstr = E_RELATIVE;
1667c478bd9Sstevel@tonic-gate return (1);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate if ((pflags & P_ABSOLUTE) && (*path != '/')) {
1697c478bd9Sstevel@tonic-gate errstr = E_ABSOLUTE;
1707c478bd9Sstevel@tonic-gate return (1);
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate if (stat64(path, &status)) {
1737c478bd9Sstevel@tonic-gate if (pflags & P_EXIST) {
1747c478bd9Sstevel@tonic-gate errstr = E_EXIST;
1757c478bd9Sstevel@tonic-gate return (1);
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate for (pt = path; *pt; pt++) {
1787c478bd9Sstevel@tonic-gate if (!isprint((unsigned char)*pt) ||
1797c478bd9Sstevel@tonic-gate strchr(badset, *pt)) {
1807c478bd9Sstevel@tonic-gate errstr = E_SYNTAX;
1817c478bd9Sstevel@tonic-gate return (1);
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate if (pflags & P_CREAT) {
1857c478bd9Sstevel@tonic-gate if (pflags & P_DIR) {
1867c478bd9Sstevel@tonic-gate if ((mkdir(path, 0755)) != 0) {
1877c478bd9Sstevel@tonic-gate errstr = E_CREAT;
1887c478bd9Sstevel@tonic-gate return (1);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate } else {
1917c478bd9Sstevel@tonic-gate if ((fd = creat(path, 0644)) < 0) {
1927c478bd9Sstevel@tonic-gate errstr = E_CREAT;
1937c478bd9Sstevel@tonic-gate return (1);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate (void) close(fd);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate return (0);
1997c478bd9Sstevel@tonic-gate } else if (pflags & P_NEXIST) {
2007c478bd9Sstevel@tonic-gate errstr = E_NEXIST;
2017c478bd9Sstevel@tonic-gate return (1);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate if ((status.st_mode & S_IFMT) == S_IFREG) {
2047c478bd9Sstevel@tonic-gate /* check non zero status */
2057c478bd9Sstevel@tonic-gate if ((pflags & P_NONZERO) && (status.st_size < 1)) {
2067c478bd9Sstevel@tonic-gate errstr = E_NONZERO;
2077c478bd9Sstevel@tonic-gate return (1);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate if ((pflags & P_CHR) && ((status.st_mode & S_IFMT) != S_IFCHR)) {
2117c478bd9Sstevel@tonic-gate errstr = E_CHR;
2127c478bd9Sstevel@tonic-gate return (1);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate if ((pflags & P_BLK) && ((status.st_mode & S_IFMT) != S_IFBLK)) {
2157c478bd9Sstevel@tonic-gate errstr = E_BLK;
2167c478bd9Sstevel@tonic-gate return (1);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate if ((pflags & P_DIR) && ((status.st_mode & S_IFMT) != S_IFDIR)) {
2197c478bd9Sstevel@tonic-gate errstr = E_DIR;
2207c478bd9Sstevel@tonic-gate return (1);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate if ((pflags & P_REG) && ((status.st_mode & S_IFMT) != S_IFREG)) {
2237c478bd9Sstevel@tonic-gate errstr = E_REG;
2247c478bd9Sstevel@tonic-gate return (1);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate if ((pflags & P_READ) && !(status.st_mode & S_IREAD)) {
2277c478bd9Sstevel@tonic-gate errstr = E_READ;
2287c478bd9Sstevel@tonic-gate return (1);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate if ((pflags & P_WRITE) && !(status.st_mode & S_IWRITE)) {
2317c478bd9Sstevel@tonic-gate errstr = E_WRITE;
2327c478bd9Sstevel@tonic-gate return (1);
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate if ((pflags & P_EXEC) && !(status.st_mode & S_IEXEC)) {
2357c478bd9Sstevel@tonic-gate errstr = E_EXEC;
2367c478bd9Sstevel@tonic-gate return (1);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate return (0);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate
2417c478bd9Sstevel@tonic-gate void
ckpath_err(int pflags,char * error,char * input)2427c478bd9Sstevel@tonic-gate ckpath_err(int pflags, char *error, char *input)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate char buffer[2048];
2457c478bd9Sstevel@tonic-gate char *defhlp;
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate if (input) {
2487c478bd9Sstevel@tonic-gate if (ckpath_val(input, pflags)) {
249*4656d474SGarrett D'Amore (void) snprintf(buffer, sizeof (buffer),
250*4656d474SGarrett D'Amore "Pathname %s.", errstr);
2517c478bd9Sstevel@tonic-gate puterror(stdout, buffer, error);
2527c478bd9Sstevel@tonic-gate return;
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate defhlp = sethlp(pflags);
2567c478bd9Sstevel@tonic-gate puterror(stdout, defhlp, error);
2577c478bd9Sstevel@tonic-gate free(defhlp);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate void
ckpath_hlp(int pflags,char * help)2617c478bd9Sstevel@tonic-gate ckpath_hlp(int pflags, char *help)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate char *defhlp;
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate defhlp = sethlp(pflags);
2667c478bd9Sstevel@tonic-gate puthelp(stdout, defhlp, help);
2677c478bd9Sstevel@tonic-gate free(defhlp);
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate int
ckpath(char * pathval,int pflags,char * defstr,char * error,char * help,char * prompt)2717c478bd9Sstevel@tonic-gate ckpath(char *pathval, int pflags, char *defstr, char *error, char *help,
2727c478bd9Sstevel@tonic-gate char *prompt)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate char *defhlp,
2757c478bd9Sstevel@tonic-gate input[MAX_INPUT],
2767c478bd9Sstevel@tonic-gate buffer[256];
2777c478bd9Sstevel@tonic-gate
2787c478bd9Sstevel@tonic-gate if ((pathval == NULL) || ckpath_stx(pflags))
2797c478bd9Sstevel@tonic-gate return (2); /* usage error */
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate if (!prompt) {
2827c478bd9Sstevel@tonic-gate if (pflags & P_ABSOLUTE)
2837c478bd9Sstevel@tonic-gate prompt = "Enter an absolute pathname";
2847c478bd9Sstevel@tonic-gate else if (pflags & P_RELATIVE)
2857c478bd9Sstevel@tonic-gate prompt = "Enter a relative pathname";
2867c478bd9Sstevel@tonic-gate else
2877c478bd9Sstevel@tonic-gate prompt = "Enter a pathname";
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate defhlp = sethlp(pflags);
2907c478bd9Sstevel@tonic-gate
2917c478bd9Sstevel@tonic-gate start:
2927c478bd9Sstevel@tonic-gate putprmpt(stderr, prompt, NULL, defstr);
2937c478bd9Sstevel@tonic-gate if (getinput(input)) {
2947c478bd9Sstevel@tonic-gate free(defhlp);
2957c478bd9Sstevel@tonic-gate return (1);
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate if (strlen(input) == 0) {
2997c478bd9Sstevel@tonic-gate if (defstr) {
3007c478bd9Sstevel@tonic-gate (void) strcpy(pathval, defstr);
3017c478bd9Sstevel@tonic-gate free(defhlp);
3027c478bd9Sstevel@tonic-gate return (0);
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate puterror(stderr, NULL, "Input is required.");
3057c478bd9Sstevel@tonic-gate goto start;
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate if (strcmp(input, "?") == 0) {
3087c478bd9Sstevel@tonic-gate puthelp(stderr, defhlp, help);
3097c478bd9Sstevel@tonic-gate goto start;
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate if (ckquit && (strcmp(input, "q") == 0)) {
3127c478bd9Sstevel@tonic-gate free(defhlp);
3137c478bd9Sstevel@tonic-gate return (3);
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate if (ckpath_val(input, pflags)) {
317*4656d474SGarrett D'Amore (void) snprintf(buffer, sizeof (buffer),
318*4656d474SGarrett D'Amore "Pathname %s.", errstr);
3197c478bd9Sstevel@tonic-gate puterror(stderr, buffer, error);
3207c478bd9Sstevel@tonic-gate goto start;
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate (void) strcpy(pathval, input);
3237c478bd9Sstevel@tonic-gate free(defhlp);
3247c478bd9Sstevel@tonic-gate return (0);
3257c478bd9Sstevel@tonic-gate }
326