3ed51e8cd02d8682c031635d6c5a3b0a4634953b
[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,allow_other,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(_(
110                                 "FUSE fuse_setup() failed!\n"
111                                 "You may need Linux kernel 2.6.14 or higher with its 'fuse.ko' enabled.\n"
112                                 "You may also need to install or upgrade 'fuse' package to your system."));
113         if (fuse_loop(capfuse_fuse)) {
114                 /* Do not: g_error(_("FUSE fuse_loop() error"));
115                  * as it is caused on each umount(8).
116                  * FIXME: Why?
117                  */
118                 }
119         fuse_teardown(capfuse_fuse,capfuse_fd,capfuse_mountpoint);
120 }
121
122 int main(int argc,char **argv)
123 {
124 poptContext context=NULL;       /* Valid if: opt_o; '= NULL' to shut up GCC. */
125 int rest_argc;
126 const char **rest_argv,**csp;
127 struct captive_options options;
128 int capfuse_argc;
129 const char **capfuse_argv;
130 const char *program_name=argv[0];
131 const char *sandbox_server_argv0=G_STRINGIFY(LIBEXECDIR) "/captive-sandbox-server";
132 const char *image_filename=NULL;
133 gboolean opt_n=FALSE;
134 gboolean opt_v=FALSE;
135 const char *opt_o=NULL;
136 const char *mountpoint=NULL;
137 char **argv_sp;
138 int opt_o_argc; /* Valid if: opt_o */
139 const char **opt_o_argv=NULL;   /* Valid if: opt_o */
140
141 #if 0
142 {
143 char **sp;
144         for (sp=argv;*sp;sp++)
145                 fprintf(stderr,"argv: \"%s\"\n",*sp);
146 }
147 #endif
148
149         g_log_set_always_fatal(~(0
150                         |G_LOG_LEVEL_MESSAGE
151                         |G_LOG_LEVEL_INFO
152                         |G_LOG_LEVEL_DEBUG
153                         ));
154
155         captive_standalone_init();
156
157         /* poptGetNextOpt() below requires valid: captive_options */
158         captive_options_init(&options);
159         captive_options=&options;       /* for parsing by 'CAPTIVE_POPT_INCLUDE' */
160
161         argv_sp=argv+1;
162         if (*argv_sp && (*argv_sp)[0]!='-')
163                 image_filename=*argv_sp++;
164         if (*argv_sp && (*argv_sp)[0]!='-')
165                 mountpoint=*argv_sp++;
166         if (*argv_sp && !strcmp(*argv_sp,"-n")) {
167                 opt_n=TRUE;
168                 argv_sp++;
169                 }
170         if (*argv_sp && !strcmp(*argv_sp,"-v")) {
171                 opt_v=TRUE;
172                 argv_sp++;
173                 }
174         if (argv_sp[0] && argv_sp[1] && !strcmp(argv_sp[0],"-o")) {
175                 argv_sp++;
176                 opt_o=*argv_sp++;
177                 }
178         if (*argv_sp) {
179                 /* Lethal path but still give chance for "--help" etc. */
180                 /* Do not: POPT_CONTEXT_POSIXMEHARDER
181                  * as mount(8) puts there first un-pre-dashed "ro"/"rw" etc. */
182                 /* poptGetNextOpt() requires valid: captive_options */
183                 if ((context=poptGetContext(
184                                 PACKAGE,        /* name */
185                                 argc,(/*en-const*/const char **)argv,   /* argc,argv */
186                                 popt_table,     /* options */
187                                 0)))    /* flags; && !POPT_CONTEXT_KEEP_FIRST */
188                         while (0<=poptGetNextOpt(context));
189                 g_error(_("Excessive argument: %s"),*argv_sp);
190                 }
191
192         if (opt_o) {
193 char *opt_o_spaced=(char */* de-const; length is not changed */)captive_strdup_alloca(opt_o);
194 char *s,quote;
195
196                 quote=0;
197                 for (s=opt_o_spaced;*s;s++) {
198                         /* Be compatible with: poptParseArgvString()
199                          * which also does not differentiate '"' and "'" */
200                         if (*s=='\\' && s[1]) {
201                                 s++;
202                                 continue;
203                                 }
204                         if (quote) {
205                                 if (*s==quote)
206                                         quote=0;
207                                 continue;
208                                 }
209                         switch (*s) {
210                                 case '"':
211                                 case '\'':
212                                         quote=*s;
213                                         continue;
214                                 case ',':
215                                         *s=' ';
216                                         continue;
217                                 }
218                         }
219                 if (poptParseArgvString(opt_o_spaced,&opt_o_argc,&opt_o_argv))
220                         g_error(_("Error splitting arguments of the space-reparsed '-o': %s"),opt_o_spaced);
221                 /* Adjust the options for all the subsystems eating the same string vector. */
222                 for (csp=opt_o_argv;*csp;csp++) {
223                         /* Unsupported mount(8) options not valid for fusermount(8) and causing:
224                          *      fusermount: mount failed: Invalid argument */
225                         if (0
226                                         || !strcmp(*csp,"defaults")
227                                         || !strcmp(*csp,  "auto")
228                                         || !strcmp(*csp,"noauto")) {
229                                 memmove(csp,csp+1,(opt_o_argv+opt_o_argc+1-(csp+1))*sizeof(*csp));
230                                 opt_o_argc--;
231                                 csp--;
232                                 continue;
233                                 }
234                         /* Pre-dash "ro"/"rw" to let libcaptive to be aware of the mode.
235                          * We will put the final "ro"/"rw" there again from 'captive_options.rwmode' later. */
236                         if (!strcmp(*csp,"ro"))
237                                 *csp="--ro";
238                         if (!strcmp(*csp,"rw"))
239                                 *csp="--rw";
240                         /* LUFS "uid" and "gid" options should map to FUSE well, I hope. */
241 #define STRNCMP_CONST(mem,string) strncmp((mem),(string),strlen((string)))
242                         if (0
243                                         || !STRNCMP_CONST(*csp,"fmask=")
244                                         || !STRNCMP_CONST(*csp,"dmask=")
245                                         || !STRNCMP_CONST(*csp,"channels=")
246                                         || !STRNCMP_CONST(*csp,"root=")
247                                         || !strcmp(*csp,"own_fs")
248                                         || !strcmp(*csp,"quiet")
249                                         || !STRNCMP_CONST(*csp,"dir_cache_ttl=")
250                                         )
251                                 g_error(_("Seen obsolete LUFS option \"%s\" - please update your \"/etc/fstab\" for FUSE options instead"),
252                                                 *csp);
253 #undef STRNCMP_CONST
254                         }
255                 }
256
257         g_assert(!options.sandbox_server_argv);
258         g_assert(!options.sandbox_server_ior);
259         /* captive_options_free(&options) will: g_free(options.sandbox_server_argv); */
260         /* Allocation is so terrible to be compatible with: captive_options_copy() */
261         options.sandbox_server_argv=g_malloc(2*sizeof(*options.sandbox_server_argv)+strlen(sandbox_server_argv0)+1);
262         options.sandbox_server_argv[0]=(char *)(options.sandbox_server_argv+2);
263         options.sandbox_server_argv[1]=NULL;
264         strcpy(options.sandbox_server_argv[0],sandbox_server_argv0);
265         options.sandbox=TRUE;
266
267         g_assert(!options.bug_pathname);
268         options.bug_pathname=g_strdup(G_STRINGIFY(VARLIBCAPTIVEDIR) "/bug-%FT%T.captivebug.xml.gz");
269
270         options.syslog_facility=DEFAULT_SYSLOG_FACILITY;
271
272         if (opt_o) {
273                 /* Do not: POPT_CONTEXT_POSIXMEHARDER
274                  * as mount(8) puts there first un-pre-dashed "ro"/"rw" etc. */
275                 if (!(context=poptGetContext(
276                                 PACKAGE,        /* name */
277                                 opt_o_argc,opt_o_argv,  /* argc,argv */
278                                 popt_table,     /* options */
279                                 POPT_CONTEXT_KEEP_FIRST)))      /* flags */
280                         g_error(_("Error parsing command-line arguments from pre-parsed '-o': %s"),opt_o);
281                 if (poptReadDefaultConfig(context,
282                                 TRUE))  /* useEnv */
283                         g_warning(_("Error reading default popt configuration"));
284                 while (0<=poptGetNextOpt(context));
285                 rest_argv=poptGetArgs(context);
286                 }
287         else
288                 rest_argv=NULL;
289         rest_argc=0;
290         for (csp=rest_argv;csp && *csp;csp++)
291                 rest_argc++;
292
293         if (!image_filename || !mountpoint)
294                 g_error(_("File/device disk image pathname and mountpoint command-line arguments required"));
295
296         /* image_iochannel */
297         g_assert(options.image_iochannel==NULL);
298         if (!(options.image_iochannel=g_io_channel_new_file(
299                         image_filename, /* filename */
300                         (options.rwmode==CAPTIVE_OPTION_RWMODE_RW ? "r+" : "r"),        /* mode */
301                         NULL))) {       /* error */
302                 g_error(_("image_iochannel failed open of: %s"),image_filename);
303                 return EXIT_FAILURE;
304                 }
305         
306         if (!options.captivemodid)
307                 options.captivemodid=captive_captivemodid_load_default(FALSE);
308
309         if (options.filesystem.type==CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY) {
310 const char *self_prefix="mount.captive-";
311 size_t self_prefix_len=strlen(self_prefix);
312 const char *fsname;
313
314                 if ((fsname=strrchr(program_name,'/')))
315                         fsname++;
316                 else
317                         fsname=program_name;
318                 if (strncmp(fsname,self_prefix,self_prefix_len))
319                         g_error(_("Cannot detected default filesystem name from my basename: %s"),fsname);
320                 fsname+=self_prefix_len;
321                 if (!captive_options_module_load(&options.filesystem,
322                                 captive_printf_alloca("%s/%s.sys",G_STRINGIFY(VARLIBCAPTIVEDIR),fsname)))
323                         g_error(_("'--filesystem' option requires valid pathname ('ntfs.sys' suggested)"));
324                 g_assert(options.filesystem.type!=CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY);
325                 }
326         if (!options.load_module) {
327 struct captive_options_module *options_module;
328
329                 captive_new(options_module);
330                 if (!captive_options_module_load(options_module,G_STRINGIFY(VARLIBCAPTIVEDIR) "/ntoskrnl.exe"))
331                         g_error(_("'--load-module' option requires valid pathname ('ntoskrnl.exe' suggested)"));
332
333                 options.load_module=g_list_append(options.load_module,options_module);
334                 }
335
336         /* It is still required for: captive_options_module_load() */
337         captive_options=NULL;   /* already parsed by 'CAPTIVE_POPT_INCLUDE' */
338
339         if (GNOME_VFS_OK!=captive_vfs_new(&capfuse_captive_vfs_object,&options)) {
340                 g_error(_("captive_vfs_new() failed"));
341                 return EXIT_FAILURE;
342                 }
343         captive_options_free(&options);
344
345         /* Simulate argv[0] there as it got cut by popt. */
346         capfuse_argc=4+2*rest_argc;
347         captive_newn_alloca(capfuse_argv,capfuse_argc+1);
348         csp=capfuse_argv;
349         *csp++=argv[0];
350         *csp++=mountpoint;
351         *csp++="-o";
352         *csp++=captive_printf_alloca("fsname=%s" LIBFUSE_ADDONS ",%s",
353                         image_filename,(options.rwmode==CAPTIVE_OPTION_RWMODE_RO ? "ro" : "rw"));
354         if (rest_argv)
355                 while (*rest_argv) {
356                         *csp++="-o";
357                         *csp++=*rest_argv++;
358                         }
359         *csp=NULL;
360 #if 0
361 {
362 const char **csp;
363         for (csp=capfuse_argv;*csp;csp++)
364                 fprintf(stderr,"capfuse_argv: \"%s\"\n",*csp);
365 }
366 #endif
367
368         /* FIXFUSE: fuse_main()/fuse_main_real() would be enough for Captive fuse but
369          * the public interface of fuse_main() is too broken.
370          */
371         capfuse_run(capfuse_argc,capfuse_argv);
372
373         /* 'rest_argv' gets cleared by 'poptFreeContext(context);' below */
374         if (opt_o)
375                 poptFreeContext(context);
376
377         if (capfuse_captive_vfs_object) {
378                 g_object_unref(capfuse_captive_vfs_object);
379                 capfuse_captive_vfs_object=NULL;
380                 }
381
382         return EXIT_SUCCESS;
383 }