Completely rebuild the commandline processing.
[captive.git] / src / client / fuse / main.c
1 /* $Id$
2  * client FUSE interface for libcaptive
3  * Copyright (C) 2005 Jan Kratochvil <project-captive@jankratochvil.net>
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; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include <fuse.h>
23 #include <popt.h>
24 #include <locale.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <syslog.h>
28
29 #include <captive/client-vfs.h>
30 #include <captive/macros.h>
31 #include <captive/client.h>
32 #include <captive/captivemodid.h>
33
34 #include "main.h"       /* self */
35 #include "op_statfs.h"
36 #include "op_fsync.h"
37 #include "op_fsyncdir.h"
38 #include "op_opendir.h"
39 #include "op_readdir.h"
40 #include "op_releasedir.h"
41 #include "op_open.h"
42 #include "op_read.h"
43 #include "op_release.h"
44 #include "op_getattr.h"
45 #include "op_mknod.h"
46 #include "op_unlink.h"
47 #include "op_mkdir.h"
48 #include "op_rmdir.h"
49 #include "op_chmod.h"
50 #include "op_truncate.h"
51 #include "op_write.h"
52 #include "op_rename.h"
53 #include "op_utime.h"
54
55
56 /* Config: */
57 /* FIXME: Dupe with libcaptive/client/options.c */
58 #define DEFAULT_SYSLOG_FACILITY LOG_DAEMON
59 /* Each element must be preceded by a comma (',')! */
60 #define LIBFUSE_ADDONS ",default_permissions,kernel_cache"
61
62
63 CaptiveVfsObject *capfuse_captive_vfs_object;
64
65 static const struct poptOption popt_table[]={
66         CAPTIVE_POPT_INCLUDE,
67         POPT_AUTOHELP
68         POPT_TABLEEND
69         };
70
71 static const struct fuse_operations capfuse_operations={
72         statfs:     op_statfs,
73         fsync:      op_fsync,
74         fsyncdir:   op_fsyncdir,
75         opendir:    op_opendir,
76         readdir:    op_readdir,
77         releasedir: op_releasedir,
78         open:       op_open,
79         read:       op_read,
80         release:    op_release,
81         getattr:    op_getattr,
82         mknod:      op_mknod,
83         unlink:     op_unlink,
84         mkdir:      op_mkdir,
85         rmdir:      op_rmdir,
86         chmod:      op_chmod,
87         truncate:   op_truncate,
88         write:      op_write,
89         rename:     op_rename,
90         utime:      op_utime,
91         };
92
93 /* argv[0] expected as the program name. */
94 /* Only options and mountpoint expected here. */
95 static void capfuse_run(int argc,const char **argv)
96 {
97 char *capfuse_mountpoint;
98 int capfuse_multithreaded,capfuse_fd;
99 struct fuse *capfuse_fuse;
100
101         if (!(capfuse_fuse=fuse_setup(
102                                 argc,   /* argc */
103                                 (/*de-const; broken fuset_setup()*/char **)argv,        /* argv */
104                                 &capfuse_operations,    /* op */
105                                 sizeof(capfuse_operations),     /* op_size */
106                         &capfuse_mountpoint,    /* mountpoint */
107                         &capfuse_multithreaded, /* multithreaded */
108                         &capfuse_fd)))  /* fd */
109                 g_error(_("FUSE fuse_setup() failed"));
110         if (fuse_loop(capfuse_fuse)) {
111                 /* Do not: g_error(_("FUSE fuse_loop() error"));
112                  * as it is caused on each umount(8).
113                  * FIXME: Why?
114                  */
115                 }
116         fuse_teardown(capfuse_fuse,capfuse_fd,capfuse_mountpoint);
117 }
118
119 int main(int argc,char **argv)
120 {
121 poptContext context=NULL;       /* Valid if: opt_o; '= NULL' to shut up GCC. */
122 int rest_argc;
123 const char **rest_argv,**csp;
124 struct captive_options options;
125 int capfuse_argc;
126 const char **capfuse_argv;
127 const char *program_name=argv[0];
128 const char *sandbox_server_argv0=G_STRINGIFY(LIBEXECDIR) "/captive-sandbox-server";
129 const char *image_filename=NULL;
130 gboolean opt_n=FALSE;
131 gboolean opt_v=FALSE;
132 const char *opt_o=NULL;
133 const char *mountpoint=NULL;
134 char **argv_sp;
135 int opt_o_argc; /* Valid if: opt_o */
136 const char **opt_o_argv=NULL;   /* Valid if: opt_o */
137
138 #if 0
139 {
140 char **sp;
141         for (sp=argv;*sp;sp++)
142                 fprintf(stderr,"argv: \"%s\"\n",*sp);
143 }
144 #endif
145
146         g_log_set_always_fatal(~(0
147                         |G_LOG_LEVEL_MESSAGE
148                         |G_LOG_LEVEL_INFO
149                         |G_LOG_LEVEL_DEBUG
150                         ));
151
152         captive_standalone_init();
153
154         argv_sp=argv+1;
155         if (*argv_sp && (*argv_sp)[0]!='-')
156                 image_filename=*argv_sp++;
157         if (*argv_sp && (*argv_sp)[0]!='-')
158                 mountpoint=*argv_sp++;
159         if (*argv_sp && !strcmp(*argv_sp,"-n")) {
160                 opt_n=TRUE;
161                 argv_sp++;
162                 }
163         if (*argv_sp && !strcmp(*argv_sp,"-v")) {
164                 opt_v=TRUE;
165                 argv_sp++;
166                 }
167         if (argv_sp[0] && argv_sp[1] && !strcmp(argv_sp[0],"-o")) {
168                 argv_sp++;
169                 opt_o=*argv_sp++;
170                 }
171         if (*argv_sp) {
172                 /* Lethal path but still give chance for "--help" etc. */
173                 /* Do not: POPT_CONTEXT_POSIXMEHARDER
174                  * as mount(8) puts there first un-pre-dashed "ro"/"rw" etc. */
175                 if ((context=poptGetContext(
176                                 PACKAGE,        /* name */
177                                 argc,(/*en-const*/const char **)argv,   /* argc,argv */
178                                 popt_table,     /* options */
179                                 0)))    /* flags; && !POPT_CONTEXT_KEEP_FIRST */
180                         while (0<=poptGetNextOpt(context));
181                 g_error(_("Excessive argument: %s"),*argv_sp);
182                 }
183
184         if (opt_o) {
185 char *opt_o_spaced=(char */* de-const; length is not changed */)captive_strdup_alloca(opt_o);
186 char *s,quote;
187
188                 quote=0;
189                 for (s=opt_o_spaced;*s;s++) {
190                         /* Be compatible with: poptParseArgvString()
191                          * which also does not differentiate '"' and "'" */
192                         if (*s=='\\' && s[1]) {
193                                 s++;
194                                 continue;
195                                 }
196                         if (quote) {
197                                 if (*s==quote)
198                                         quote=0;
199                                 continue;
200                                 }
201                         switch (*s) {
202                                 case '"':
203                                 case '\'':
204                                         quote=*s;
205                                         continue;
206                                 case ',':
207                                         *s=' ';
208                                         continue;
209                                 }
210                         }
211                 if (poptParseArgvString(opt_o_spaced,&opt_o_argc,&opt_o_argv))
212                         g_error(_("Error splitting arguments of the space-reparsed '-o': %s"),opt_o_spaced);
213                 /* Pre-dash "ro"/"rw" to let libcaptive to be aware of the mode. */
214                 for (csp=opt_o_argv;*csp;csp++) {
215                         if (!strcmp(*csp,"ro"))
216                                 *csp="--ro";
217                         if (!strcmp(*csp,"rw"))
218                                 *csp="--rw";
219                         }
220                 }
221
222         captive_options_init(&options);
223         captive_options=&options;       /* for parsing by 'CAPTIVE_POPT_INCLUDE' */
224
225         g_assert(!options.sandbox_server_argv);
226         g_assert(!options.sandbox_server_ior);
227         /* captive_options_free(&options) will: g_free(options.sandbox_server_argv); */
228         /* Allocation is so terrible to be compatible with: captive_options_copy() */
229         options.sandbox_server_argv=g_malloc(2*sizeof(*options.sandbox_server_argv)+strlen(sandbox_server_argv0)+1);
230         options.sandbox_server_argv[0]=(char *)(options.sandbox_server_argv+2);
231         options.sandbox_server_argv[1]=NULL;
232         strcpy(options.sandbox_server_argv[0],sandbox_server_argv0);
233         options.sandbox=TRUE;
234
235         g_assert(!options.bug_pathname);
236         options.bug_pathname=g_strdup(G_STRINGIFY(VARLIBCAPTIVEDIR) "/bug-%FT%T.captivebug.xml.gz");
237
238         options.syslog_facility=DEFAULT_SYSLOG_FACILITY;
239
240         if (opt_o) {
241                 /* Do not: POPT_CONTEXT_POSIXMEHARDER
242                  * as mount(8) puts there first un-pre-dashed "ro"/"rw" etc. */
243                 if (!(context=poptGetContext(
244                                 PACKAGE,        /* name */
245                                 opt_o_argc,opt_o_argv,  /* argc,argv */
246                                 popt_table,     /* options */
247                                 POPT_CONTEXT_KEEP_FIRST)))      /* flags */
248                         g_error(_("Error parsing command-line arguments from pre-parsed '-o': %s"),opt_o);
249                 if (poptReadDefaultConfig(context,
250                                 TRUE))  /* useEnv */
251                         g_warning(_("Error reading default popt configuration"));
252                 while (0<=poptGetNextOpt(context));
253                 rest_argv=poptGetArgs(context);
254                 }
255         else
256                 rest_argv=NULL;
257         rest_argc=0;
258         for (csp=rest_argv;csp && *csp;csp++)
259                 rest_argc++;
260
261         if (!image_filename || !mountpoint)
262                 g_error(_("File/device disk image pathname and mountpoint command-line arguments required"));
263
264         /* image_iochannel */
265         g_assert(options.image_iochannel==NULL);
266         if (!(options.image_iochannel=g_io_channel_new_file(
267                         image_filename, /* filename */
268                         (options.rwmode==CAPTIVE_OPTION_RWMODE_RW ? "r+" : "r"),        /* mode */
269                         NULL))) {       /* error */
270                 g_error(_("image_iochannel failed open of: %s"),image_filename);
271                 return EXIT_FAILURE;
272                 }
273         
274         if (!options.captivemodid)
275                 options.captivemodid=captive_captivemodid_load_default(FALSE);
276
277         if (options.filesystem.type==CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY) {
278 const char *self_prefix="mount.captive-";
279 size_t self_prefix_len=strlen(self_prefix);
280 const char *fsname;
281
282                 if ((fsname=strrchr(program_name,'/')))
283                         fsname++;
284                 else
285                         fsname=program_name;
286                 if (strncmp(fsname,self_prefix,self_prefix_len))
287                         g_error(_("Cannot detected default filesystem name from my basename: %s"),fsname);
288                 fsname+=self_prefix_len;
289                 if (!captive_options_module_load(&options.filesystem,
290                                 captive_printf_alloca("%s/%s.sys",G_STRINGIFY(VARLIBCAPTIVEDIR),fsname)))
291                         g_error(_("'--filesystem' option requires valid pathname ('ntfs.sys' suggested)"));
292                 g_assert(options.filesystem.type!=CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY);
293                 }
294         if (!options.load_module) {
295 struct captive_options_module *options_module;
296
297                 captive_new(options_module);
298                 if (!captive_options_module_load(options_module,G_STRINGIFY(VARLIBCAPTIVEDIR) "/ntoskrnl.exe"))
299                         g_error(_("'--load-module' option requires valid pathname ('ntoskrnl.exe' suggested)"));
300
301                 options.load_module=g_list_append(options.load_module,options_module);
302                 }
303
304         /* It is still required for: captive_options_module_load() */
305         captive_options=NULL;   /* already parsed by 'CAPTIVE_POPT_INCLUDE' */
306
307         if (GNOME_VFS_OK!=captive_vfs_new(&capfuse_captive_vfs_object,&options)) {
308                 g_error(_("captive_vfs_new() failed"));
309                 return EXIT_FAILURE;
310                 }
311         captive_options_free(&options);
312
313         /* Simulate argv[0] there as it got cut by popt. */
314         capfuse_argc=4+2*rest_argc;
315         captive_newn_alloca(capfuse_argv,capfuse_argc+1);
316         csp=capfuse_argv;
317         *csp++=argv[0];
318         *csp++=mountpoint;
319         *csp++="-o";
320         *csp++=captive_printf_alloca("fsname=%s" LIBFUSE_ADDONS ",%s",
321                         image_filename,(options.rwmode==CAPTIVE_OPTION_RWMODE_RO ? "ro" : "rw"));
322         if (rest_argv)
323                 while (*rest_argv) {
324                         *csp++="-o";
325                         *csp++=*rest_argv++;
326                         }
327         *csp=NULL;
328 #if 0
329 {
330 const char **csp;
331         for (csp=capfuse_argv;*csp;csp++)
332                 fprintf(stderr,"capfuse_argv: \"%s\"\n",*csp);
333 }
334 #endif
335
336         /* FIXFUSE: fuse_main()/fuse_main_real() would be enough for Captive fuse but
337          * the public interface of fuse_main() is too broken.
338          */
339         capfuse_run(capfuse_argc,capfuse_argv);
340
341         /* 'rest_argv' gets cleared by 'poptFreeContext(context);' below */
342         if (opt_o)
343                 poptFreeContext(context);
344
345         if (capfuse_captive_vfs_object) {
346                 g_object_unref(capfuse_captive_vfs_object);
347                 capfuse_captive_vfs_object=NULL;
348                 }
349
350         return EXIT_SUCCESS;
351 }