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 "samba3x-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);
115 static void * usmb_init (struct fuse_conn_info
*conn UNUSED
)
117 DEBUG (fputs ("usmb_init()\n", stderr
));
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:
135 #define SET_ELEMENT(name,value) value
137 #define SET_ELEMENT(name,value) name = value
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:///") +
185 strlen (sharename
) + 1;
187 if (NULL
== (share
= malloc (len
)))
189 perror ("Cannot allocate share name");
193 snprintf (share
, len
, "smb://%s/%s", server
, sharename
);
194 DEBUG (fprintf (stderr
, "Share URL: %s\n", share
));
199 static void free_strings (char *sharename
)
202 clear_and_free (password
);
212 static bool check_credentials (void)
214 char *url
= make_url ("");
221 DEBUG (fprintf (stderr
, "URL: %s\n", url
));
224 bool ret
= (0 == (smbc_getFunctionStat (ctx
) (ctx
, url
, &stat
)));
231 static bool get_context (void)
235 unsigned attempts
= 3;
237 while (0 != attempts
--)
239 if ((NULL
== password
) && !password_read (&password
))
242 if (!create_smb_context (&ctx
))
244 clear_and_free (password
);
250 if (check_credentials())
253 perror ("Connection failed");
254 clear_and_free (password
);
257 destroy_smb_context (ctx
, 1);
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
);
277 static char conf
[256];
278 snprintf (conf
, sizeof (conf
), "%s/.usmb.conf", getenv ("HOME"));
282 if (!parse_args (&argc
, &argv
, &mountid
, &conffile
))
287 if (!conffile_get_mount (conffile
, mountid
,
288 &server
, &sharename
, &mountpoint
, &options
,
289 &domain
, &username
, &password
))
292 if (!create_share_name (server
, sharename
) || !get_context())
294 free_strings (sharename
);
298 DEBUG (fprintf (stderr
, "Username: %s\\%s\n", domain
, username
));
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
);