1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 24*7c478bd9Sstevel@tonic-gate 25*7c478bd9Sstevel@tonic-gate 26*7c478bd9Sstevel@tonic-gate /* 27*7c478bd9Sstevel@tonic-gate * Copyright (c) 1996-1998, 2001 by Sun Microsystems, Inc. 28*7c478bd9Sstevel@tonic-gate * All rights reserved. 29*7c478bd9Sstevel@tonic-gate */ 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.3 */ 32*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/ 33*7c478bd9Sstevel@tonic-gate 34*7c478bd9Sstevel@tonic-gate #include <stdio.h> 35*7c478bd9Sstevel@tonic-gate #include <ctype.h> 36*7c478bd9Sstevel@tonic-gate #include <string.h> 37*7c478bd9Sstevel@tonic-gate #include <limits.h> 38*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 39*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 40*7c478bd9Sstevel@tonic-gate #include "valtools.h" 41*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 42*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 43*7c478bd9Sstevel@tonic-gate #include <unistd.h> 44*7c478bd9Sstevel@tonic-gate #include "libadm.h" 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate #define E_SYNTAX "does not meet suggested filename syntax standard" 47*7c478bd9Sstevel@tonic-gate #define E_READ "is not readable" 48*7c478bd9Sstevel@tonic-gate #define E_WRITE "is not writable" 49*7c478bd9Sstevel@tonic-gate #define E_EXEC "is not executable" 50*7c478bd9Sstevel@tonic-gate #define E_CREAT "cannot be created" 51*7c478bd9Sstevel@tonic-gate #define E_ABSOLUTE "must begin with a slash (/)" 52*7c478bd9Sstevel@tonic-gate #define E_RELATIVE "must not begin with a slash (/)" 53*7c478bd9Sstevel@tonic-gate #define E_EXIST "does not exist" 54*7c478bd9Sstevel@tonic-gate #define E_NEXIST "must not already exist" 55*7c478bd9Sstevel@tonic-gate #define E_BLK "must specify a block special device" 56*7c478bd9Sstevel@tonic-gate #define E_CHR "must specify a character special device" 57*7c478bd9Sstevel@tonic-gate #define E_DIR "must specify a directory" 58*7c478bd9Sstevel@tonic-gate #define E_REG "must be a regular file" 59*7c478bd9Sstevel@tonic-gate #define E_NONZERO "must be a file of non-zero length" 60*7c478bd9Sstevel@tonic-gate 61*7c478bd9Sstevel@tonic-gate #define H_READ "must be readable" 62*7c478bd9Sstevel@tonic-gate #define H_WRITE "must be writable" 63*7c478bd9Sstevel@tonic-gate #define H_EXEC "must be executable" 64*7c478bd9Sstevel@tonic-gate #define H_CREAT "will be created if it does not exist" 65*7c478bd9Sstevel@tonic-gate #define H_ABSOLUTE E_ABSOLUTE 66*7c478bd9Sstevel@tonic-gate #define H_RELATIVE E_RELATIVE 67*7c478bd9Sstevel@tonic-gate #define H_EXIST "must already exist" 68*7c478bd9Sstevel@tonic-gate #define H_NEXIST "must not already exist" 69*7c478bd9Sstevel@tonic-gate #define H_BLK E_BLK 70*7c478bd9Sstevel@tonic-gate #define H_CHR E_CHR 71*7c478bd9Sstevel@tonic-gate #define H_DIR E_DIR 72*7c478bd9Sstevel@tonic-gate #define H_REG E_REG 73*7c478bd9Sstevel@tonic-gate #define H_NONZERO E_NONZERO 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate #define MSGSIZ 1024 76*7c478bd9Sstevel@tonic-gate #define STDHELP \ 77*7c478bd9Sstevel@tonic-gate "A pathname is a filename, optionally preceded by parent directories." 78*7c478bd9Sstevel@tonic-gate 79*7c478bd9Sstevel@tonic-gate static char *errstr; 80*7c478bd9Sstevel@tonic-gate static char *badset = "*?[]{}()<> \t'`\"\\|^"; 81*7c478bd9Sstevel@tonic-gate 82*7c478bd9Sstevel@tonic-gate static void 83*7c478bd9Sstevel@tonic-gate addhlp(char *msg, char *text) 84*7c478bd9Sstevel@tonic-gate { 85*7c478bd9Sstevel@tonic-gate static int count; 86*7c478bd9Sstevel@tonic-gate 87*7c478bd9Sstevel@tonic-gate if (text == NULL) { 88*7c478bd9Sstevel@tonic-gate count = 0; 89*7c478bd9Sstevel@tonic-gate return; 90*7c478bd9Sstevel@tonic-gate } 91*7c478bd9Sstevel@tonic-gate if (!count++) 92*7c478bd9Sstevel@tonic-gate (void) strcat(msg, " The pathname you enter:"); 93*7c478bd9Sstevel@tonic-gate (void) strcat(msg, "\\n\\t-\\ "); 94*7c478bd9Sstevel@tonic-gate (void) strcat(msg, text); 95*7c478bd9Sstevel@tonic-gate } 96*7c478bd9Sstevel@tonic-gate 97*7c478bd9Sstevel@tonic-gate static char * 98*7c478bd9Sstevel@tonic-gate sethlp(int pflags) 99*7c478bd9Sstevel@tonic-gate { 100*7c478bd9Sstevel@tonic-gate char *msg; 101*7c478bd9Sstevel@tonic-gate 102*7c478bd9Sstevel@tonic-gate msg = calloc(MSGSIZ, sizeof (char)); 103*7c478bd9Sstevel@tonic-gate addhlp(msg, NULL); /* initialize count */ 104*7c478bd9Sstevel@tonic-gate (void) strcpy(msg, STDHELP); 105*7c478bd9Sstevel@tonic-gate 106*7c478bd9Sstevel@tonic-gate if (pflags & P_EXIST) 107*7c478bd9Sstevel@tonic-gate addhlp(msg, H_EXIST); 108*7c478bd9Sstevel@tonic-gate else if (pflags & P_NEXIST) 109*7c478bd9Sstevel@tonic-gate addhlp(msg, H_NEXIST); 110*7c478bd9Sstevel@tonic-gate 111*7c478bd9Sstevel@tonic-gate if (pflags & P_ABSOLUTE) 112*7c478bd9Sstevel@tonic-gate addhlp(msg, H_ABSOLUTE); 113*7c478bd9Sstevel@tonic-gate else if (pflags & P_RELATIVE) 114*7c478bd9Sstevel@tonic-gate addhlp(msg, H_RELATIVE); 115*7c478bd9Sstevel@tonic-gate 116*7c478bd9Sstevel@tonic-gate if (pflags & P_READ) 117*7c478bd9Sstevel@tonic-gate addhlp(msg, H_READ); 118*7c478bd9Sstevel@tonic-gate if (pflags & P_WRITE) 119*7c478bd9Sstevel@tonic-gate addhlp(msg, H_WRITE); 120*7c478bd9Sstevel@tonic-gate if (pflags & P_EXEC) 121*7c478bd9Sstevel@tonic-gate addhlp(msg, H_EXEC); 122*7c478bd9Sstevel@tonic-gate if (pflags & P_CREAT) 123*7c478bd9Sstevel@tonic-gate addhlp(msg, H_CREAT); 124*7c478bd9Sstevel@tonic-gate 125*7c478bd9Sstevel@tonic-gate if (pflags & P_BLK) 126*7c478bd9Sstevel@tonic-gate addhlp(msg, H_BLK); 127*7c478bd9Sstevel@tonic-gate else if (pflags & P_CHR) 128*7c478bd9Sstevel@tonic-gate addhlp(msg, H_CHR); 129*7c478bd9Sstevel@tonic-gate else if (pflags & P_DIR) 130*7c478bd9Sstevel@tonic-gate addhlp(msg, H_DIR); 131*7c478bd9Sstevel@tonic-gate else if (pflags & P_REG) 132*7c478bd9Sstevel@tonic-gate addhlp(msg, H_REG); 133*7c478bd9Sstevel@tonic-gate 134*7c478bd9Sstevel@tonic-gate if (pflags & P_NONZERO) 135*7c478bd9Sstevel@tonic-gate addhlp(msg, H_NONZERO); 136*7c478bd9Sstevel@tonic-gate 137*7c478bd9Sstevel@tonic-gate return (msg); 138*7c478bd9Sstevel@tonic-gate } 139*7c478bd9Sstevel@tonic-gate 140*7c478bd9Sstevel@tonic-gate int 141*7c478bd9Sstevel@tonic-gate ckpath_stx(int pflags) 142*7c478bd9Sstevel@tonic-gate { 143*7c478bd9Sstevel@tonic-gate if (((pflags & P_ABSOLUTE) && (pflags & P_RELATIVE)) || 144*7c478bd9Sstevel@tonic-gate ((pflags & P_NEXIST) && (pflags & 145*7c478bd9Sstevel@tonic-gate (P_EXIST|P_NONZERO|P_READ|P_WRITE|P_EXEC))) || 146*7c478bd9Sstevel@tonic-gate ((pflags & P_CREAT) && (pflags & (P_EXIST|P_NEXIST|P_BLK|P_CHR))) || 147*7c478bd9Sstevel@tonic-gate ((pflags & P_BLK) && (pflags & (P_CHR|P_REG|P_DIR|P_NONZERO))) || 148*7c478bd9Sstevel@tonic-gate ((pflags & P_CHR) && (pflags & (P_REG|P_DIR|P_NONZERO))) || 149*7c478bd9Sstevel@tonic-gate ((pflags & P_DIR) && (pflags & P_REG))) { 150*7c478bd9Sstevel@tonic-gate return (1); 151*7c478bd9Sstevel@tonic-gate } 152*7c478bd9Sstevel@tonic-gate return (0); 153*7c478bd9Sstevel@tonic-gate } 154*7c478bd9Sstevel@tonic-gate 155*7c478bd9Sstevel@tonic-gate int 156*7c478bd9Sstevel@tonic-gate ckpath_val(char *path, int pflags) 157*7c478bd9Sstevel@tonic-gate { 158*7c478bd9Sstevel@tonic-gate struct stat64 status; 159*7c478bd9Sstevel@tonic-gate int fd; 160*7c478bd9Sstevel@tonic-gate char *pt; 161*7c478bd9Sstevel@tonic-gate 162*7c478bd9Sstevel@tonic-gate if ((pflags & P_RELATIVE) && (*path == '/')) { 163*7c478bd9Sstevel@tonic-gate errstr = E_RELATIVE; 164*7c478bd9Sstevel@tonic-gate return (1); 165*7c478bd9Sstevel@tonic-gate } 166*7c478bd9Sstevel@tonic-gate if ((pflags & P_ABSOLUTE) && (*path != '/')) { 167*7c478bd9Sstevel@tonic-gate errstr = E_ABSOLUTE; 168*7c478bd9Sstevel@tonic-gate return (1); 169*7c478bd9Sstevel@tonic-gate } 170*7c478bd9Sstevel@tonic-gate if (stat64(path, &status)) { 171*7c478bd9Sstevel@tonic-gate if (pflags & P_EXIST) { 172*7c478bd9Sstevel@tonic-gate errstr = E_EXIST; 173*7c478bd9Sstevel@tonic-gate return (1); 174*7c478bd9Sstevel@tonic-gate } 175*7c478bd9Sstevel@tonic-gate for (pt = path; *pt; pt++) { 176*7c478bd9Sstevel@tonic-gate if (!isprint((unsigned char)*pt) || 177*7c478bd9Sstevel@tonic-gate strchr(badset, *pt)) { 178*7c478bd9Sstevel@tonic-gate errstr = E_SYNTAX; 179*7c478bd9Sstevel@tonic-gate return (1); 180*7c478bd9Sstevel@tonic-gate } 181*7c478bd9Sstevel@tonic-gate } 182*7c478bd9Sstevel@tonic-gate if (pflags & P_CREAT) { 183*7c478bd9Sstevel@tonic-gate if (pflags & P_DIR) { 184*7c478bd9Sstevel@tonic-gate if ((mkdir(path, 0755)) != 0) { 185*7c478bd9Sstevel@tonic-gate errstr = E_CREAT; 186*7c478bd9Sstevel@tonic-gate return (1); 187*7c478bd9Sstevel@tonic-gate } 188*7c478bd9Sstevel@tonic-gate } else { 189*7c478bd9Sstevel@tonic-gate if ((fd = creat(path, 0644)) < 0) { 190*7c478bd9Sstevel@tonic-gate errstr = E_CREAT; 191*7c478bd9Sstevel@tonic-gate return (1); 192*7c478bd9Sstevel@tonic-gate } 193*7c478bd9Sstevel@tonic-gate (void) close(fd); 194*7c478bd9Sstevel@tonic-gate } 195*7c478bd9Sstevel@tonic-gate } 196*7c478bd9Sstevel@tonic-gate return (0); 197*7c478bd9Sstevel@tonic-gate } else if (pflags & P_NEXIST) { 198*7c478bd9Sstevel@tonic-gate errstr = E_NEXIST; 199*7c478bd9Sstevel@tonic-gate return (1); 200*7c478bd9Sstevel@tonic-gate } 201*7c478bd9Sstevel@tonic-gate if ((status.st_mode & S_IFMT) == S_IFREG) { 202*7c478bd9Sstevel@tonic-gate /* check non zero status */ 203*7c478bd9Sstevel@tonic-gate if ((pflags & P_NONZERO) && (status.st_size < 1)) { 204*7c478bd9Sstevel@tonic-gate errstr = E_NONZERO; 205*7c478bd9Sstevel@tonic-gate return (1); 206*7c478bd9Sstevel@tonic-gate } 207*7c478bd9Sstevel@tonic-gate } 208*7c478bd9Sstevel@tonic-gate if ((pflags & P_CHR) && ((status.st_mode & S_IFMT) != S_IFCHR)) { 209*7c478bd9Sstevel@tonic-gate errstr = E_CHR; 210*7c478bd9Sstevel@tonic-gate return (1); 211*7c478bd9Sstevel@tonic-gate } 212*7c478bd9Sstevel@tonic-gate if ((pflags & P_BLK) && ((status.st_mode & S_IFMT) != S_IFBLK)) { 213*7c478bd9Sstevel@tonic-gate errstr = E_BLK; 214*7c478bd9Sstevel@tonic-gate return (1); 215*7c478bd9Sstevel@tonic-gate } 216*7c478bd9Sstevel@tonic-gate if ((pflags & P_DIR) && ((status.st_mode & S_IFMT) != S_IFDIR)) { 217*7c478bd9Sstevel@tonic-gate errstr = E_DIR; 218*7c478bd9Sstevel@tonic-gate return (1); 219*7c478bd9Sstevel@tonic-gate } 220*7c478bd9Sstevel@tonic-gate if ((pflags & P_REG) && ((status.st_mode & S_IFMT) != S_IFREG)) { 221*7c478bd9Sstevel@tonic-gate errstr = E_REG; 222*7c478bd9Sstevel@tonic-gate return (1); 223*7c478bd9Sstevel@tonic-gate } 224*7c478bd9Sstevel@tonic-gate if ((pflags & P_READ) && !(status.st_mode & S_IREAD)) { 225*7c478bd9Sstevel@tonic-gate errstr = E_READ; 226*7c478bd9Sstevel@tonic-gate return (1); 227*7c478bd9Sstevel@tonic-gate } 228*7c478bd9Sstevel@tonic-gate if ((pflags & P_WRITE) && !(status.st_mode & S_IWRITE)) { 229*7c478bd9Sstevel@tonic-gate errstr = E_WRITE; 230*7c478bd9Sstevel@tonic-gate return (1); 231*7c478bd9Sstevel@tonic-gate } 232*7c478bd9Sstevel@tonic-gate if ((pflags & P_EXEC) && !(status.st_mode & S_IEXEC)) { 233*7c478bd9Sstevel@tonic-gate errstr = E_EXEC; 234*7c478bd9Sstevel@tonic-gate return (1); 235*7c478bd9Sstevel@tonic-gate } 236*7c478bd9Sstevel@tonic-gate return (0); 237*7c478bd9Sstevel@tonic-gate } 238*7c478bd9Sstevel@tonic-gate 239*7c478bd9Sstevel@tonic-gate void 240*7c478bd9Sstevel@tonic-gate ckpath_err(int pflags, char *error, char *input) 241*7c478bd9Sstevel@tonic-gate { 242*7c478bd9Sstevel@tonic-gate char buffer[2048]; 243*7c478bd9Sstevel@tonic-gate char *defhlp; 244*7c478bd9Sstevel@tonic-gate 245*7c478bd9Sstevel@tonic-gate if (input) { 246*7c478bd9Sstevel@tonic-gate if (ckpath_val(input, pflags)) { 247*7c478bd9Sstevel@tonic-gate (void) sprintf(buffer, "Pathname %s.", errstr); 248*7c478bd9Sstevel@tonic-gate puterror(stdout, buffer, error); 249*7c478bd9Sstevel@tonic-gate return; 250*7c478bd9Sstevel@tonic-gate } 251*7c478bd9Sstevel@tonic-gate } 252*7c478bd9Sstevel@tonic-gate defhlp = sethlp(pflags); 253*7c478bd9Sstevel@tonic-gate puterror(stdout, defhlp, error); 254*7c478bd9Sstevel@tonic-gate free(defhlp); 255*7c478bd9Sstevel@tonic-gate } 256*7c478bd9Sstevel@tonic-gate 257*7c478bd9Sstevel@tonic-gate void 258*7c478bd9Sstevel@tonic-gate ckpath_hlp(int pflags, char *help) 259*7c478bd9Sstevel@tonic-gate { 260*7c478bd9Sstevel@tonic-gate char *defhlp; 261*7c478bd9Sstevel@tonic-gate 262*7c478bd9Sstevel@tonic-gate defhlp = sethlp(pflags); 263*7c478bd9Sstevel@tonic-gate puthelp(stdout, defhlp, help); 264*7c478bd9Sstevel@tonic-gate free(defhlp); 265*7c478bd9Sstevel@tonic-gate } 266*7c478bd9Sstevel@tonic-gate 267*7c478bd9Sstevel@tonic-gate int 268*7c478bd9Sstevel@tonic-gate ckpath(char *pathval, int pflags, char *defstr, char *error, char *help, 269*7c478bd9Sstevel@tonic-gate char *prompt) 270*7c478bd9Sstevel@tonic-gate { 271*7c478bd9Sstevel@tonic-gate char *defhlp, 272*7c478bd9Sstevel@tonic-gate input[MAX_INPUT], 273*7c478bd9Sstevel@tonic-gate buffer[256]; 274*7c478bd9Sstevel@tonic-gate 275*7c478bd9Sstevel@tonic-gate if ((pathval == NULL) || ckpath_stx(pflags)) 276*7c478bd9Sstevel@tonic-gate return (2); /* usage error */ 277*7c478bd9Sstevel@tonic-gate 278*7c478bd9Sstevel@tonic-gate if (!prompt) { 279*7c478bd9Sstevel@tonic-gate if (pflags & P_ABSOLUTE) 280*7c478bd9Sstevel@tonic-gate prompt = "Enter an absolute pathname"; 281*7c478bd9Sstevel@tonic-gate else if (pflags & P_RELATIVE) 282*7c478bd9Sstevel@tonic-gate prompt = "Enter a relative pathname"; 283*7c478bd9Sstevel@tonic-gate else 284*7c478bd9Sstevel@tonic-gate prompt = "Enter a pathname"; 285*7c478bd9Sstevel@tonic-gate } 286*7c478bd9Sstevel@tonic-gate defhlp = sethlp(pflags); 287*7c478bd9Sstevel@tonic-gate 288*7c478bd9Sstevel@tonic-gate start: 289*7c478bd9Sstevel@tonic-gate putprmpt(stderr, prompt, NULL, defstr); 290*7c478bd9Sstevel@tonic-gate if (getinput(input)) { 291*7c478bd9Sstevel@tonic-gate free(defhlp); 292*7c478bd9Sstevel@tonic-gate return (1); 293*7c478bd9Sstevel@tonic-gate } 294*7c478bd9Sstevel@tonic-gate 295*7c478bd9Sstevel@tonic-gate if (strlen(input) == 0) { 296*7c478bd9Sstevel@tonic-gate if (defstr) { 297*7c478bd9Sstevel@tonic-gate (void) strcpy(pathval, defstr); 298*7c478bd9Sstevel@tonic-gate free(defhlp); 299*7c478bd9Sstevel@tonic-gate return (0); 300*7c478bd9Sstevel@tonic-gate } 301*7c478bd9Sstevel@tonic-gate puterror(stderr, NULL, "Input is required."); 302*7c478bd9Sstevel@tonic-gate goto start; 303*7c478bd9Sstevel@tonic-gate } 304*7c478bd9Sstevel@tonic-gate if (strcmp(input, "?") == 0) { 305*7c478bd9Sstevel@tonic-gate puthelp(stderr, defhlp, help); 306*7c478bd9Sstevel@tonic-gate goto start; 307*7c478bd9Sstevel@tonic-gate } 308*7c478bd9Sstevel@tonic-gate if (ckquit && (strcmp(input, "q") == 0)) { 309*7c478bd9Sstevel@tonic-gate free(defhlp); 310*7c478bd9Sstevel@tonic-gate return (3); 311*7c478bd9Sstevel@tonic-gate } 312*7c478bd9Sstevel@tonic-gate 313*7c478bd9Sstevel@tonic-gate if (ckpath_val(input, pflags)) { 314*7c478bd9Sstevel@tonic-gate (void) sprintf(buffer, "Pathname %s.", errstr); 315*7c478bd9Sstevel@tonic-gate puterror(stderr, buffer, error); 316*7c478bd9Sstevel@tonic-gate goto start; 317*7c478bd9Sstevel@tonic-gate } 318*7c478bd9Sstevel@tonic-gate (void) strcpy(pathval, input); 319*7c478bd9Sstevel@tonic-gate free(defhlp); 320*7c478bd9Sstevel@tonic-gate return (0); 321*7c478bd9Sstevel@tonic-gate } 322