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>
21 #include <libsmbclient.h>
35 #include "usmb_file.h"
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
);
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
);
63 static void auth_fn (const char *srv
, const char *shr
, char *wg
, int wglen
,
64 char *un
, int unlen
, 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
));
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();
86 perror ("Cannot create SMB context");
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);
107 static int usmb_statfs (const char *path
, struct statvfs
*vfs
)
109 if ((NULL
== path
) || (NULL
== vfs
))
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
;
124 static void * usmb_init (struct fuse_conn_info
*conn
)
126 DEBUG (fputs ("usmb_init()\n", stderr
));
132 static void usmb_destroy (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".
145 // no easy way of implementing these.
149 #define SET_ELEMENT(name,value) value
151 #define SET_ELEMENT(name,value) name = value
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
)
200 if (0 == stat (conffile
, &buf
))
202 if (getuid() != buf
.st_uid
)
204 fprintf (stderr
, "You do not own the configuration file %s\n",
209 if (buf
.st_mode
& (S_IRWXG
| S_IRWXO
))
211 fprintf (stderr
, "Configuration file %s is accessible to non-owner\n",
218 fprintf (stderr
, "Cannot stat configuration file %s: %s\n",
219 conffile
, strerror (errno
));
227 static bool create_share_name (const char *server
, const char *sharename
)
229 size_t len
= strlen ("smb:///") +
231 strlen (sharename
) + 1;
233 if (NULL
== (share
= malloc (len
)))
235 perror ("Cannot allocate share name");
239 snprintf (share
, len
, "smb://%s/%s", server
, sharename
);
240 DEBUG (fprintf (stderr
, "Share URL: %s\n", share
));
245 static void free_strings (char *sharename
)
248 clear_and_free (password
);
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
);
270 static char conf
[256];
271 snprintf (conf
, sizeof (conf
), "%s/.usmb.conf", getenv ("HOME"));
275 if (!parse_args (&argc
, &argv
, &mountid
, &conffile
))
280 if (!check_conf_perms (conffile
) ||
281 !conffile_get_mount (conffile
, mountid
,
282 &server
, &sharename
, &mountpoint
, &options
,
283 &domain
, &username
, &password
))
286 if (!create_share_name (server
, sharename
) ||
287 !create_smb_context (domain
, username
, &ctx
))
289 free_strings (sharename
);
293 DEBUG (fprintf (stderr
, "Username: %s\\%s\n", domain
, username
));
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
);