Added password prompting.
[usmb.git] / usmb.c
blobd3d99be793e3aba8a2e21ddcde6bf55ad1dfa63c
1 /* usmb - mount SMB shares via FUSE and Samba
2 * Copyright (C) 2006-2008 Geoff Johnstone
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include <sys/time.h> // struct timeval needed by libsmbclient.h
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <unistd.h>
21 #include <libsmbclient.h>
22 #include <fuse.h>
23 #include <assert.h>
24 #include <errno.h>
25 #include <stdarg.h>
26 #include <stdbool.h>
27 #include <stddef.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include "conffile.h"
32 #include "options.h"
33 #include "usmb.h"
34 #include "usmb_dir.h"
35 #include "usmb_file.h"
36 #include "utils.h"
37 #include "version.h"
40 SMBCCTX *ctx;
41 static char *server, *share, *mountpoint, *options,
42 *domain, *username, *password;
45 char * make_url (const char *path)
47 assert (NULL != share);
49 if ((NULL == path) || ('\0' == path[0]))
50 return xstrdup (share);
51 else
52 return concat_strings (2, share, path);
56 static inline void do_strncpy (char *to, const char *from, int tolen)
58 strncpy (to, from, tolen);
59 to[tolen - 1] = '\0';
63 static void auth_fn (const char *srv, const char *shr, char *wg, int wglen,
64 char *un, int unlen, char *pw, int pwlen)
66 (void)srv;
67 (void)shr;
68 DEBUG (fprintf (stderr, "Authenticating for \\\\%s\\%s\n", srv, shr));
69 DEBUG (fprintf (stderr, "Domain: %s; User: %s; Password:%s\n",
70 domain, username, password));
72 if (NULL != domain)
73 do_strncpy (wg, domain, wglen);
75 do_strncpy (un, username, unlen);
76 do_strncpy (pw, password, pwlen);
80 static bool create_smb_context (char *domain, char *username, SMBCCTX **pctx)
82 *pctx = smbc_new_context();
84 if (NULL == *pctx)
86 perror ("Cannot create SMB context");
87 return false;
90 (*pctx)->workgroup = domain;
91 (*pctx)->user = username;
92 (*pctx)->timeout = 5000;
93 (*pctx)->callbacks.auth_fn = auth_fn;
95 if (NULL == smbc_init_context (*pctx))
97 perror ("Cannot initialise SMB context");
98 smbc_free_context (*pctx, 1);
99 return false;
102 return true;
106 #if 0
107 static int usmb_statfs (const char *path, struct statvfs *vfs)
109 if ((NULL == path) || (NULL == vfs))
110 return -EINVAL;
112 memset (vfs, 0, sizeof (*vfs));
114 vfs->f_bsize = 32768;
115 vfs->f_blocks = 0x7FFFFFFFl;
116 vfs->f_bfree = 0x7FFFFFFFl;
117 vfs->f_bavail = 0x7FFFFFFFl;
119 return 0;
121 #endif
124 static void * usmb_init (struct fuse_conn_info *conn)
126 DEBUG (fputs ("usmb_init()\n", stderr));
127 (void)conn;
128 return NULL;
132 static void usmb_destroy (void *unused)
134 (void)unused;
135 DEBUG (fputs ("usmb_destroy()\n", stderr));
139 // probably won't (can't ?) implement these:
140 // readlink mknod symlink flush fsync
142 // fuse.h says "Just a placeholder, don't set".
143 // statfs
145 // no easy way of implementing these.
146 // access ftruncate
148 #ifdef __lint
149 #define SET_ELEMENT(name,value) value
150 #else
151 #define SET_ELEMENT(name,value) name = value
152 #endif
153 static struct fuse_operations fuse_ops = {
154 SET_ELEMENT (.getattr, usmb_getattr),
155 SET_ELEMENT (.readlink, NULL),
156 SET_ELEMENT (.getdir, NULL),
157 SET_ELEMENT (.mknod, NULL),
158 SET_ELEMENT (.mkdir, usmb_mkdir),
159 SET_ELEMENT (.unlink, usmb_unlink),
160 SET_ELEMENT (.rmdir, usmb_rmdir),
161 SET_ELEMENT (.symlink, NULL),
162 SET_ELEMENT (.rename, usmb_rename),
163 SET_ELEMENT (.link, NULL),
164 SET_ELEMENT (.chmod, usmb_chmod),
165 SET_ELEMENT (.chown, NULL), // usmb_chown, --not implemented in libsmbclient
166 SET_ELEMENT (.truncate, usmb_truncate),
167 SET_ELEMENT (.utime, usmb_utime),
168 SET_ELEMENT (.open, usmb_open),
169 SET_ELEMENT (.read, usmb_read),
170 SET_ELEMENT (.write, usmb_write),
171 SET_ELEMENT (.statfs, NULL),
172 SET_ELEMENT (.flush, NULL),
173 SET_ELEMENT (.release, usmb_release),
174 SET_ELEMENT (.fsync, NULL),
175 SET_ELEMENT (.setxattr, usmb_setxattr),
176 SET_ELEMENT (.getxattr, usmb_getxattr),
177 SET_ELEMENT (.listxattr, usmb_listxattr),
178 SET_ELEMENT (.removexattr, usmb_removexattr),
179 SET_ELEMENT (.opendir, usmb_opendir),
180 SET_ELEMENT (.readdir, usmb_readdir),
181 SET_ELEMENT (.releasedir, usmb_releasedir),
182 SET_ELEMENT (.fsyncdir, NULL),
183 SET_ELEMENT (.init, usmb_init),
184 SET_ELEMENT (.destroy, usmb_destroy),
185 SET_ELEMENT (.access, NULL),
186 SET_ELEMENT (.create, usmb_create),
187 SET_ELEMENT (.ftruncate, NULL),
188 SET_ELEMENT (.fgetattr, usmb_fgetattr),
189 SET_ELEMENT (.lock, NULL), // TODO: implement
190 SET_ELEMENT (.utimens, NULL), // TODO: implement
191 SET_ELEMENT (.bmap, NULL), // TODO: implement
195 // this should really open() the file and check the fd, but the XML parser
196 // takes a filename, not a file descriptor
197 static bool check_conf_perms (const char *conffile)
199 struct stat buf;
200 if (0 == stat (conffile, &buf))
202 if (getuid() != buf.st_uid)
204 fprintf (stderr, "You do not own the configuration file %s\n",
205 conffile);
206 return false;
209 if (buf.st_mode & (S_IRWXG | S_IRWXO))
211 fprintf (stderr, "Configuration file %s is accessible to non-owner\n",
212 conffile);
213 return false;
216 else
218 fprintf (stderr, "Cannot stat configuration file %s: %s\n",
219 conffile, strerror (errno));
220 return false;
223 return true;
227 static bool create_share_name (const char *server, const char *sharename)
229 size_t len = strlen ("smb:///") +
230 strlen (server) +
231 strlen (sharename) + 1;
233 if (NULL == (share = malloc (len)))
235 perror ("Cannot allocate share name");
236 return false;
239 snprintf (share, len, "smb://%s/%s", server, sharename);
240 DEBUG (fprintf (stderr, "Share URL: %s\n", share));
241 return true;
245 static void free_strings (char *sharename)
247 xfree (sharename);
248 clear_and_free (password);
249 xfree (username);
250 xfree (domain);
251 xfree (options);
252 xfree (mountpoint);
253 xfree (share);
254 xfree (server);
258 int main (int argc, char **argv)
260 const char *conffile, *mountid;
261 char *sharename = NULL;
263 if (sizeof (uint64_t) < sizeof (uintptr_t))
265 fputs ("usmb is not supported on this platform.\n", stderr);
266 return EXIT_FAILURE;
270 static char conf[256];
271 snprintf (conf, sizeof (conf), "%s/.usmb.conf", getenv ("HOME"));
272 conffile = conf;
275 if (!parse_args (&argc, &argv, &mountid, &conffile))
276 return EXIT_FAILURE;
278 show_about (stdout);
280 if (!check_conf_perms (conffile) ||
281 !conffile_get_mount (conffile, mountid,
282 &server, &sharename, &mountpoint, &options,
283 &domain, &username, &password))
284 return EXIT_FAILURE;
286 if (!create_share_name (server, sharename) ||
287 !create_smb_context (domain, username, &ctx))
289 free_strings (sharename);
290 return EXIT_FAILURE;
293 DEBUG (fprintf (stderr, "Username: %s\\%s\n", domain, username));
295 int fuse_argc;
296 char **fuse_argv;
297 build_fuse_args (options, mountpoint, &fuse_argc, &fuse_argv);
298 int ret = fuse_main (fuse_argc, fuse_argv, &fuse_ops, NULL);
300 smbc_free_context (ctx, 1);
301 free_strings (sharename);
303 return ret;