Critical fixes for Samba 3.0 and 3.2.
[usmb.git] / usmb.c
blob798e616d3afba877c6766300aeefd56659b1b952
1 /* usmb - mount SMB shares via FUSE and Samba
2 * Copyright (C) 2006-2009 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 "config.h"
18 #include <sys/time.h> // struct timeval needed by libsmbclient.h
19 #include <unistd.h>
20 #include <libsmbclient.h>
21 #include "samba3x-compat.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 "password.h"
34 #include "usmb.h"
35 #include "usmb_dir.h"
36 #include "usmb_file.h"
37 #include "utils.h"
38 #include "version.h"
41 SMBCCTX *ctx;
42 static char *server, *share, *mountpoint, *options,
43 *domain, *username, *password;
46 char * make_url (const char *path)
48 assert (NULL != share);
50 if ((NULL == path) || ('\0' == path[0]))
51 return xstrdup (share);
52 else
53 return concat_strings (2, share, path);
57 static inline void do_strncpy (char *to, const char *from, int tolen)
59 strncpy (to, from, tolen);
60 to[tolen - 1] = '\0';
64 static void auth_fn (const char *srv UNUSED, const char *shr UNUSED,
65 char *wg, int wglen, char *un, int unlen,
66 char *pw, int pwlen)
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 void destroy_smb_context (SMBCCTX *ctx, int shutdown)
82 // Samba frees the workgroup and user strings but we want to persist them.
83 smbc_setWorkgroup (ctx, NULL);
84 smbc_setUser (ctx, NULL);
85 smbc_free_context (ctx, shutdown);
89 bool create_smb_context (SMBCCTX **pctx)
91 *pctx = smbc_new_context();
93 if (NULL == *pctx)
95 perror ("Cannot create SMB context");
96 return false;
99 smbc_setWorkgroup (*pctx, domain);
100 smbc_setUser (*pctx, username);
101 smbc_setTimeout (*pctx, 5000);
102 smbc_setFunctionAuthData (*pctx, auth_fn);
104 if (NULL == smbc_init_context (*pctx))
106 perror ("Cannot initialise SMB context");
107 destroy_smb_context (*pctx, 1);
108 return false;
111 return true;
115 static void * usmb_init (struct fuse_conn_info *conn UNUSED)
117 DEBUG (fputs ("usmb_init()\n", stderr));
118 return NULL;
122 static void usmb_destroy (void *unused UNUSED)
124 DEBUG (fputs ("usmb_destroy()\n", stderr));
128 // probably won't (can't ?) implement these:
129 // readlink mknod symlink flush fsync
131 // no easy way of implementing these:
132 // access
134 #ifdef __lint
135 #define SET_ELEMENT(name,value) value
136 #else
137 #define SET_ELEMENT(name,value) name = value
138 #endif
139 static struct fuse_operations fuse_ops = {
140 SET_ELEMENT (.getattr, usmb_getattr),
141 SET_ELEMENT (.readlink, NULL),
142 SET_ELEMENT (.getdir, NULL),
143 SET_ELEMENT (.mknod, NULL),
144 SET_ELEMENT (.mkdir, usmb_mkdir),
145 SET_ELEMENT (.unlink, usmb_unlink),
146 SET_ELEMENT (.rmdir, usmb_rmdir),
147 SET_ELEMENT (.symlink, NULL),
148 SET_ELEMENT (.rename, usmb_rename),
149 SET_ELEMENT (.link, NULL),
150 SET_ELEMENT (.chmod, usmb_chmod),
151 SET_ELEMENT (.chown, NULL), // usmb_chown, --not implemented in libsmbclient
152 SET_ELEMENT (.truncate, usmb_truncate),
153 SET_ELEMENT (.utime, usmb_utime),
154 SET_ELEMENT (.open, usmb_open),
155 SET_ELEMENT (.read, usmb_read),
156 SET_ELEMENT (.write, usmb_write),
157 SET_ELEMENT (.statfs, usmb_statfs),
158 SET_ELEMENT (.flush, NULL),
159 SET_ELEMENT (.release, usmb_release),
160 SET_ELEMENT (.fsync, NULL),
161 SET_ELEMENT (.setxattr, usmb_setxattr),
162 SET_ELEMENT (.getxattr, usmb_getxattr),
163 SET_ELEMENT (.listxattr, usmb_listxattr),
164 SET_ELEMENT (.removexattr, usmb_removexattr),
165 SET_ELEMENT (.opendir, usmb_opendir),
166 SET_ELEMENT (.readdir, usmb_readdir),
167 SET_ELEMENT (.releasedir, usmb_releasedir),
168 SET_ELEMENT (.fsyncdir, NULL),
169 SET_ELEMENT (.init, usmb_init),
170 SET_ELEMENT (.destroy, usmb_destroy),
171 SET_ELEMENT (.access, NULL),
172 SET_ELEMENT (.create, usmb_create),
173 SET_ELEMENT (.ftruncate, usmb_ftruncate),
174 SET_ELEMENT (.fgetattr, usmb_fgetattr),
175 SET_ELEMENT (.lock, NULL), // TODO: implement
176 SET_ELEMENT (.utimens, NULL), // TODO: implement
177 SET_ELEMENT (.bmap, NULL), // TODO: implement
181 static bool create_share_name (const char *server, const char *sharename)
183 size_t len = strlen ("smb:///") +
184 strlen (server) +
185 strlen (sharename) + 1;
187 if (NULL == (share = malloc (len)))
189 perror ("Cannot allocate share name");
190 return false;
193 snprintf (share, len, "smb://%s/%s", server, sharename);
194 DEBUG (fprintf (stderr, "Share URL: %s\n", share));
195 return true;
199 static void free_strings (char *sharename)
201 xfree (sharename);
202 clear_and_free (password);
203 xfree (username);
204 xfree (domain);
205 xfree (options);
206 xfree (mountpoint);
207 xfree (share);
208 xfree (server);
212 static bool check_credentials (void)
214 char *url = make_url ("");
215 if (NULL == url)
217 errno = ENOMEM;
218 return false;
221 DEBUG (fprintf (stderr, "URL: %s\n", url));
223 struct stat stat;
224 bool ret = (0 == (smbc_getFunctionStat (ctx) (ctx, url, &stat)));
226 free_errno (url);
227 return ret;
231 static bool get_context (void)
233 ctx = NULL;
235 unsigned attempts = 3;
237 while (0 != attempts--)
239 if ((NULL == password) && !password_read (&password))
240 break;
242 if (!create_smb_context (&ctx))
244 clear_and_free (password);
245 password = NULL;
246 ctx = NULL;
247 break;
250 if (check_credentials())
251 break;
253 perror ("Connection failed");
254 clear_and_free (password);
255 password = NULL;
257 destroy_smb_context (ctx, 1);
258 ctx = NULL;
261 return (NULL != ctx);
265 int main (int argc, char **argv)
267 const char *conffile, *mountid;
268 char *sharename = NULL;
270 if (sizeof (uint64_t) < sizeof (uintptr_t))
272 fputs ("usmb is not supported on this platform.\n", stderr);
273 return EXIT_FAILURE;
277 static char conf[256];
278 snprintf (conf, sizeof (conf), "%s/.usmb.conf", getenv ("HOME"));
279 conffile = conf;
282 if (!parse_args (&argc, &argv, &mountid, &conffile))
283 return EXIT_FAILURE;
285 show_about (stdout);
287 if (!conffile_get_mount (conffile, mountid,
288 &server, &sharename, &mountpoint, &options,
289 &domain, &username, &password))
290 return EXIT_FAILURE;
292 if (!create_share_name (server, sharename) || !get_context())
294 free_strings (sharename);
295 return EXIT_FAILURE;
298 DEBUG (fprintf (stderr, "Username: %s\\%s\n", domain, username));
300 int fuse_argc;
301 char **fuse_argv;
302 build_fuse_args (options, mountpoint, &fuse_argc, &fuse_argv);
303 int ret = fuse_main (fuse_argc, fuse_argv, &fuse_ops, NULL);
305 destroy_smb_context (ctx, 1);
306 free_strings (sharename);
308 return ret;