1 /*
2    cadaver, command-line DAV client
3    Copyright (C) 1999-2001, Joe Orton <joe@manyfish.co.uk>
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 
20 #include "config.h"
21 
22 #ifdef HAVE_STDLIB_H
23 #include <stdlib.h>
24 #endif
25 #include <stdio.h>
26 
27 #ifdef HAVE_STRING_H
28 #include <string.h>
29 #endif
30 
31 #include <sys/stat.h>
32 #include <dirent.h>
33 #include <errno.h>
34 #include <signal.h>
35 
36 #include <ne_string.h>
37 #include <ne_alloc.h>
38 
39 #include "i18n.h"
40 #include "glob.h"
41 
42 #include "common.h"
43 #include "commands.h"
44 #include "cadaver.h"
45 #include "cmdline.h"
46 #include "utils.h"
47 
has_glob_pattern(const char * str)48 static int has_glob_pattern(const char *str) {
49     const char *pnt;
50     for (pnt = str; *pnt != '\0'; pnt++)
51 	if (*pnt=='*' || *pnt=='[' || *pnt=='?')
52 	    return 1;
53     return 0;
54 }
55 
56 /* Gets the next token for parse_command...
57  * Starts at position in line.
58  * DFA states:
59  *   0: chewing leading whitespace
60  *   1: chewing characters, normally
61  *   2: chewing characters in a quote
62  *   3: just got a backslash in normal chew
63  *   4: just got a backslash in a quoted chew
64  *   8: ignoring comment
65  *   9: acceptance state
66  *
67  * State diagram is left as an exercise to the reader, since
68  * I'm not going to draw it in ASCII. ;)
69  *
70  * Returns the token, malloc()-allocated, or NULL on end-of-line.
71  * Position updated to point after token.
72  */
gettoken(const char * line,const char ** pointer)73 static char *gettoken(const char *line, const char **pointer)
74 {
75     const char *pnt;
76     int state = 0, pos = 0;
77     char buf[BUFSIZ], quote = 0; /* = 0 to keep gcc -Wall happy */
78 
79     pnt = *pointer;
80 
81 #define ISQUOTE(x) (x=='\'' || x=='\"')
82 #define ISWHITE(x) (x==' ' || x=='\t')
83 
84      while (*pnt != '\0' && state != 9 && pos < BUFSIZ) {
85 	 switch (state) {
86 	 case 0: /* leading whitespace chew */
87 	     if (ISQUOTE(*pnt)) {
88 		 state = 2;
89 		 quote = *pnt;
90 	     } else if (*pnt == '#') {
91 		 state = 8;
92 	     } else if (!ISWHITE(*pnt)) {
93 		 buf[pos++] = *pnt;
94 		 state = 1;
95 	     }
96 	     break;
97 	 case 1: /* normal chew */
98 	     if (ISWHITE(*pnt)) {
99 		 state = 9;
100 	     } else if (*pnt == '#') {
101 		 state = 8;
102 	     } else if (*pnt == '\\') {
103 		 state = 3;
104 	     } else {
105 		 buf[pos++] = *pnt;
106 	     }
107 	     break;
108 	 case 2: /* quoted chew */
109 	     if (*pnt == quote) {
110 		 state = 9;
111 	     } else if (*pnt == '\\') {
112                 state = 4;
113 	     } else {
114 		 buf[pos++] = *pnt;
115 	     }
116 	     break;
117 	 case 3: /* chew an escaped literal */
118 	     buf[pos++] = *pnt;
119 	     state = 1;
120 	     break;
121 	 case 4: /* backslash in quoted chew... like 3 except
122 		  * we switch back to state 2 afterwards */
123 	     buf[pos++] = *pnt;
124 	     state = 2;
125 	     break;
126 	 case 5: /* comment chew */
127 	     break;
128 	 }
129 	 pnt++;
130      }
131 
132 #undef ISQUOTE
133 #undef ISWHITE
134 
135      /* overflowed the buffer */
136      if (pos == BUFSIZ) return NULL;
137 
138      buf[pos] = '\0';
139      *pointer = pnt;
140      if (pos > 0) {
141 #ifdef I_AM_A_LUMBERJACK
142 	 /* a little hack; does env. var expansion...
143 	  * 1) is this really useful?
144 	  * 2) this should be done in parse_command not gettoken
145 	  */
146 	 if (buf[0] == '$') {
147 	     char *val = getenv(&buf[1]);
148 	     if (val) return ne_strdup(val);
149 	 }
150 #endif
151 	 return ne_strdup(buf);
152      } else {
153 	 return NULL;
154      }
155 }
156 
157 /* This is about as efficient as painting a wall with a toothbrush.
158  * And it will go just as fast, if you have a slow link to your
159  * server.
160  */
161 
162 /*
163    Okay, and this was a real swine to get working.
164    The glob() in glibc-2.1.2 (I haven't checked other versions)
165    has a REAL_DIR_ENTRY() macro which checks whether the d_ino
166    field is set. If it isn't, the entry is IGNORED!
167 
168    Actually, the glob() in glib-2.1.2 is broken, so we supply
169    a fixed version in lib/.
170 */
171 
172 volatile int interrupt_state; /* for glob */
173 
174 struct dg_ctx {
175     int rootlen;
176     struct resource *files;
177     struct resource *current;
178 };
179 
davglob_opendir(const char * dir)180 static void *davglob_opendir(const char *dir)
181 {
182     struct dg_ctx *ctx = NULL;
183     struct resource *files;
184     char *real_path = resolve_path(session.uri.path, dir, 1);
185     NE_DEBUG(DEBUG_FILES, "opendir: %s\n", dir);
186     switch (fetch_resource_list(session.sess, real_path, 1, 0, &files)) {
187     case NE_OK:
188 	ctx = ne_calloc(sizeof *ctx);
189 	ctx->rootlen = strlen(real_path);
190 	ctx->files = files;
191 	ctx->current = ctx->files;
192 	break;
193     case NE_AUTH:
194 	errno = EACCES;
195 	break;
196     default:
197 	/* Let them know it doesn't exist. */
198 	errno = ENOENT;
199 	break;
200     }
201     free(real_path);
202     return (void *)ctx;
203 }
204 
205 /* Mocks up a dummy dirent structure */
davglob_readdir(void * dir)206 static struct dirent *davglob_readdir(void *dir)
207 {
208     static struct dirent ent;
209     struct dg_ctx *ctx = dir;
210     NE_DEBUG(DEBUG_FILES, "readdir:");
211     if (!ctx->current) {
212 	NE_DEBUG(DEBUG_FILES, "none.\n");
213 	return NULL;
214     } else {
215 	memset(&ent, 0, sizeof(struct dirent));
216 	ne_strnzcpy(ent.d_name, ctx->current->uri + ctx->rootlen, 255);
217 	/* FIXME: non-portable, there's an autoconf
218 	 * test for this, "d-ino.m4" */
219 	ent.d_ino = 1;
220 	NE_DEBUG(DEBUG_FILES, "%s\n", ent.d_name);
221 	ctx->current = ctx->current->next;
222 	return &ent;
223     }
224 }
225 
davglob_closedir(void * dir)226 static void davglob_closedir(void *dir)
227 {
228     struct dg_ctx *ctx = dir;
229     NE_DEBUG(DEBUG_FILES, "closedir\n");
230     free_resource_list(ctx->files);
231     free(ctx);
232 }
233 
davglob_stat(const char * filename,struct stat * st)234 static int davglob_stat(const char *filename, struct stat *st) {
235     /* presumption: all glob needs to know is whether it's a directory
236      * or not. I think this is true for the glob in glibc2 */
237     char *dir;
238     NE_DEBUG(DEBUG_FILES, "stat %s\n", filename);
239     dir = resolve_path(session.uri.path, filename, 1);
240     if (getrestype(dir) == resr_collection) {
241 	st->st_mode = S_IFDIR;
242     } else {
243 	st->st_mode = S_IFREG;
244     }
245     free(dir);
246     return 0;
247 }
248 
davglob_interrupt(int sig)249 static void davglob_interrupt(int sig) {
250     interrupt_state = 1;
251 }
252 
davglob_errfunc(const char * filename,int errcode)253 static int davglob_errfunc(const char *filename, int errcode)
254 {
255     output(o_finish, "Error on %s: %s\n", filename, strerror(errcode));
256     return 0;
257 }
258 
parse_command(const char * line,int * count)259 char **parse_command(const char *line, int *count)
260 {
261     char *token, **tokens = NULL;
262     const char *pnt = line;
263     int numtokens = 0, matches = 0;
264     const struct command *cmd = NULL;
265 
266 #define ADDTOK(x) 						\
267 do {								\
268     tokens = realloc(tokens, ++numtokens*sizeof(char *));	\
269     tokens[numtokens-1] = x;					\
270 } while (0)
271 
272     while ((token = gettoken(line, &pnt)) != NULL) {
273 	if (!numtokens) {
274 	    /* The first token: get the command */
275 	    cmd = get_command(token);
276 	    ADDTOK(token);
277 	} else if (has_glob_pattern(token) && cmd &&
278 		   ((cmd->scope == parmscope_remote && session.connected) ||
279 		     (cmd->scope == parmscope_local))) {
280 	    /* Let us Glob */
281 	    glob_t gl = {0};
282 	    int ret, flags = GLOB_NOSORT;
283 	    void (*oldhand)(int sig);
284 
285 	    output(o_start, _("[Matching..."));
286 	    interrupt_state = 0;
287 
288 	    /* The glob() we have compiled here allows us to set a
289 	     * global variable 'interrupt_state' to 1 when the user
290 	     * wants to interrupt the glob expansion (particularly
291 	     * relevant for long slow remote globs)... here's the
292 	     * handler */
293 	    oldhand = signal(SIGINT, davglob_interrupt);
294 
295 	    /* This is nice. We expand the glob in the same way
296 	     * whether it is a local or a remote glob, except we lob
297 	     * in the remote-glob handlers here if the command
298 	     * requires remote globs to be expanded */
299 	    if (cmd->scope == parmscope_remote) {
300 		gl.gl_closedir = davglob_closedir;
301 		gl.gl_opendir = davglob_opendir;
302 		gl.gl_readdir = davglob_readdir;
303 		gl.gl_stat = gl.gl_lstat = davglob_stat;
304 		flags |= GLOB_ALTDIRFUNC;
305 	    }
306 	    /* Do the Glob Thang */
307 	    ret = glob(token, flags, davglob_errfunc, &gl);
308 	    switch (ret) {
309 	    case 0: {
310 		unsigned int n;
311 		if (gl.gl_pathc > 1) {
312 		    output(o_finish, _("%ld matches.]\n"), (long)gl.gl_pathc);
313 		} else {
314 		    output(o_finish, _("1 match.]\n"));
315 		}
316 		for (n = 0; n < gl.gl_pathc; n++) {
317 		    ADDTOK(ne_strdup(gl.gl_pathv[n]));
318 		}
319 		matches++;
320 	    } break;
321 	    case GLOB_NOSPACE:
322 		output(o_finish, "failed: out of memory.]\n");
323 		break;
324 	    case GLOB_ABORTED:
325 		output(o_finish, "aborted]\n");
326 		break;
327 	    case GLOB_NOMATCH:
328 		output(o_finish, "no matches.]\n");
329 		break;
330 	    default:
331 		output(o_finish, "failed.]\n");
332 	    }
333 	    if (ret) {
334 		/* For all the failure cases, put in the glob instead.
335 		 * Perhaps we should fail here instead... but, this is
336 		 * what bash does, so we stick with consistent
337 		 * behaviour. TODO: this could be an option.
338 		 */
339 		ADDTOK(token);
340 	    } else {
341 		/* Otherwise, we don't use the actual token */
342 		free(token);
343 	    }
344 	    globfree(&gl);
345 	    signal(SIGINT, oldhand);
346 	} else {
347 	    ADDTOK(token);
348 	}
349     }
350 
351     *count = numtokens;
352     /* add a NULL at the end of the list */
353     ADDTOK(NULL);
354 
355     return tokens;
356 }
357