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