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/>.
18 #include <sys/time.h> // struct timeval needed by libsmbclient.h
20 #include <libsmbclient.h>
21 #include "samba30-compat.h"
36 #include "usmb_file.h"
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
);
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
);
64 static void auth_fn (const char *srv UNUSED
, const char *shr UNUSED
,
65 char *wg
, int wglen
, char *un
, int unlen
,
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
));
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();
95 perror ("Cannot create SMB context");
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);
116 static int usmb_statfs (const char *path
, struct statvfs
*vfs
)
118 if ((NULL
== path
) || (NULL
== vfs
))
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
;
133 static void * usmb_init (struct fuse_conn_info
*conn UNUSED
)
135 DEBUG (fputs ("usmb_init()\n", stderr
));
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:
153 #define SET_ELEMENT(name,value) value
155 #define SET_ELEMENT(name,value) name = value
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:///") +
203 strlen (sharename
) + 1;
205 if (NULL
== (share
= malloc (len
)))
207 perror ("Cannot allocate share name");
211 snprintf (share
, len
, "smb://%s/%s", server
, sharename
);
212 DEBUG (fprintf (stderr
, "Share URL: %s\n", share
));
217 static void free_strings (char *sharename
)
220 clear_and_free (password
);
230 static bool check_credentials (void)
232 char *url
= make_url ("");
239 DEBUG (fprintf (stderr
, "URL: %s\n", url
));
242 bool ret
= (0 == (smbc_getFunctionStat (ctx
) (ctx
, url
, &stat
)));
249 static bool get_context (void)
253 unsigned attempts
= 3;
255 while (0 != attempts
--)
257 if ((NULL
== password
) && !password_read (&password
))
260 if (!create_smb_context (&ctx
))
262 clear_and_free (password
);
268 if (check_credentials())
271 perror ("Connection failed");
272 clear_and_free (password
);
275 destroy_smb_context (ctx
, 1);
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
);
295 static char conf
[256];
296 snprintf (conf
, sizeof (conf
), "%s/.usmb.conf", getenv ("HOME"));
300 if (!parse_args (&argc
, &argv
, &mountid
, &conffile
))
305 if (!conffile_get_mount (conffile
, mountid
,
306 &server
, &sharename
, &mountpoint
, &options
,
307 &domain
, &username
, &password
))
310 if (!create_share_name (server
, sharename
) || !get_context())
312 free_strings (sharename
);
316 DEBUG (fprintf (stderr
, "Username: %s\\%s\n", domain
, username
));
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
);