Disallow --without-samba. Fix dist target.
[usmb.git] / usmb.c
blob6dfb4734563fb780ea7193e965390c5f2aecbe88
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 "samba30-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 #if 0
116 static int usmb_statfs (const char *path, struct statvfs *vfs)
118 if ((NULL == path) || (NULL == vfs))
119 return -EINVAL;
121 memset (vfs, 0, sizeof (*vfs));
123 vfs->f_bsize = 32768;
124 vfs->f_blocks = 0x7FFFFFFFl;
125 vfs->f_bfree = 0x7FFFFFFFl;
126 vfs->f_bavail = 0x7FFFFFFFl;
128 return 0;
130 #endif
133 static void * usmb_init (struct fuse_conn_info *conn UNUSED)
135 DEBUG (fputs ("usmb_init()\n", stderr));
136 return NULL;
140 static void usmb_destroy (void *unused UNUSED)
142 DEBUG (fputs ("usmb_destroy()\n", stderr));
146 // probably won't (can't ?) implement these:
147 // readlink mknod symlink flush fsync statfs
149 // no easy way of implementing these:
150 // access ftruncate
152 #ifdef __lint
153 #define SET_ELEMENT(name,value) value
154 #else
155 #define SET_ELEMENT(name,value) name = value
156 #endif
157 static struct fuse_operations fuse_ops = {
158 SET_ELEMENT (.getattr, usmb_getattr),
159 SET_ELEMENT (.readlink, NULL),
160 SET_ELEMENT (.getdir, NULL),
161 SET_ELEMENT (.mknod, NULL),
162 SET_ELEMENT (.mkdir, usmb_mkdir),
163 SET_ELEMENT (.unlink, usmb_unlink),
164 SET_ELEMENT (.rmdir, usmb_rmdir),
165 SET_ELEMENT (.symlink, NULL),
166 SET_ELEMENT (.rename, usmb_rename),
167 SET_ELEMENT (.link, NULL),
168 SET_ELEMENT (.chmod, usmb_chmod),
169 SET_ELEMENT (.chown, NULL), // usmb_chown, --not implemented in libsmbclient
170 SET_ELEMENT (.truncate, usmb_truncate),
171 SET_ELEMENT (.utime, usmb_utime),
172 SET_ELEMENT (.open, usmb_open),
173 SET_ELEMENT (.read, usmb_read),
174 SET_ELEMENT (.write, usmb_write),
175 SET_ELEMENT (.statfs, NULL),
176 SET_ELEMENT (.flush, NULL),
177 SET_ELEMENT (.release, usmb_release),
178 SET_ELEMENT (.fsync, NULL),
179 SET_ELEMENT (.setxattr, usmb_setxattr),
180 SET_ELEMENT (.getxattr, usmb_getxattr),
181 SET_ELEMENT (.listxattr, usmb_listxattr),
182 SET_ELEMENT (.removexattr, usmb_removexattr),
183 SET_ELEMENT (.opendir, usmb_opendir),
184 SET_ELEMENT (.readdir, usmb_readdir),
185 SET_ELEMENT (.releasedir, usmb_releasedir),
186 SET_ELEMENT (.fsyncdir, NULL),
187 SET_ELEMENT (.init, usmb_init),
188 SET_ELEMENT (.destroy, usmb_destroy),
189 SET_ELEMENT (.access, NULL),
190 SET_ELEMENT (.create, usmb_create),
191 SET_ELEMENT (.ftruncate, NULL),
192 SET_ELEMENT (.fgetattr, usmb_fgetattr),
193 SET_ELEMENT (.lock, NULL), // TODO: implement
194 SET_ELEMENT (.utimens, NULL), // TODO: implement
195 SET_ELEMENT (.bmap, NULL), // TODO: implement
199 static bool create_share_name (const char *server, const char *sharename)
201 size_t len = strlen ("smb:///") +
202 strlen (server) +
203 strlen (sharename) + 1;
205 if (NULL == (share = malloc (len)))
207 perror ("Cannot allocate share name");
208 return false;
211 snprintf (share, len, "smb://%s/%s", server, sharename);
212 DEBUG (fprintf (stderr, "Share URL: %s\n", share));
213 return true;
217 static void free_strings (char *sharename)
219 xfree (sharename);
220 clear_and_free (password);
221 xfree (username);
222 xfree (domain);
223 xfree (options);
224 xfree (mountpoint);
225 xfree (share);
226 xfree (server);
230 static bool check_credentials (void)
232 char *url = make_url ("");
233 if (NULL == url)
235 errno = ENOMEM;
236 return false;
239 DEBUG (fprintf (stderr, "URL: %s\n", url));
241 struct stat stat;
242 bool ret = (0 == (smbc_getFunctionStat (ctx) (ctx, url, &stat)));
244 free_errno (url);
245 return ret;
249 static bool get_context (void)
251 ctx = NULL;
253 unsigned attempts = 3;
255 while (0 != attempts--)
257 if ((NULL == password) && !password_read (&password))
258 break;
260 if (!create_smb_context (&ctx))
262 clear_and_free (password);
263 password = NULL;
264 ctx = NULL;
265 break;
268 if (check_credentials())
269 break;
271 perror ("Connection failed");
272 clear_and_free (password);
273 password = NULL;
275 destroy_smb_context (ctx, 1);
276 ctx = NULL;
279 return (NULL != ctx);
283 int main (int argc, char **argv)
285 const char *conffile, *mountid;
286 char *sharename = NULL;
288 if (sizeof (uint64_t) < sizeof (uintptr_t))
290 fputs ("usmb is not supported on this platform.\n", stderr);
291 return EXIT_FAILURE;
295 static char conf[256];
296 snprintf (conf, sizeof (conf), "%s/.usmb.conf", getenv ("HOME"));
297 conffile = conf;
300 if (!parse_args (&argc, &argv, &mountid, &conffile))
301 return EXIT_FAILURE;
303 show_about (stdout);
305 if (!conffile_get_mount (conffile, mountid,
306 &server, &sharename, &mountpoint, &options,
307 &domain, &username, &password))
308 return EXIT_FAILURE;
310 if (!create_share_name (server, sharename) || !get_context())
312 free_strings (sharename);
313 return EXIT_FAILURE;
316 DEBUG (fprintf (stderr, "Username: %s\\%s\n", domain, username));
318 int fuse_argc;
319 char **fuse_argv;
320 build_fuse_args (options, mountpoint, &fuse_argc, &fuse_argv);
321 int ret = fuse_main (fuse_argc, fuse_argv, &fuse_ops, NULL);
323 destroy_smb_context (ctx, 1);
324 free_strings (sharename);
326 return ret;