1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
|
/*
* Copyright (c) 2000-2001, Boris Popov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Boris Popov.
* 4. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: mount_smbfs.c,v 1.28.44.2 2005/06/02 00:55:41 lindak Exp $
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#include <ctype.h>
#include <stdlib.h>
#include <errno.h>
#include <err.h>
#include <libintl.h>
#include <locale.h>
#include <libscf.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <sys/mount.h>
#include <sys/mntent.h>
#include <sys/mnttab.h>
#include <sys/fs/smbfs_mount.h>
/* This needs to know ctx->ct_dev_fd, etc. */
#include <netsmb/smb_lib.h>
extern char *optarg;
extern int optind;
static char mount_point[MAXPATHLEN + 1];
static void usage(void);
static int setsubopt(smb_ctx_t *, struct smbfs_args *, char *);
const char * const optlist[] = {
/* Generic VFS options. */
#define OPT_RO 0
MNTOPT_RO,
#define OPT_RW 1
MNTOPT_RW,
#define OPT_SUID 2
MNTOPT_SUID,
#define OPT_NOSUID 3
MNTOPT_NOSUID,
#define OPT_DEVICES 4
MNTOPT_DEVICES,
#define OPT_NODEVICES 5
MNTOPT_NODEVICES,
#define OPT_SETUID 6
MNTOPT_SETUID,
#define OPT_NOSETUID 7
MNTOPT_NOSETUID,
#define OPT_EXEC 8
MNTOPT_EXEC,
#define OPT_NOEXEC 9
MNTOPT_NOEXEC,
#define OPT_XATTR 10
MNTOPT_XATTR,
#define OPT_NOXATTR 11
MNTOPT_NOXATTR,
/* Sort of generic (from NFS) */
#define OPT_NOAC 12
MNTOPT_NOAC,
#define OPT_ACTIMEO 13
MNTOPT_ACTIMEO,
#define OPT_ACREGMIN 14
MNTOPT_ACREGMIN,
#define OPT_ACREGMAX 15
MNTOPT_ACREGMAX,
#define OPT_ACDIRMIN 16
MNTOPT_ACDIRMIN,
#define OPT_ACDIRMAX 17
MNTOPT_ACDIRMAX,
/* smbfs-specifis options */
#define OPT_DOMAIN 18
"domain",
#define OPT_USER 19
"user",
#define OPT_UID 20
"uid",
#define OPT_GID 21
"gid",
#define OPT_DIRPERMS 22
"dirperms",
#define OPT_FILEPERMS 23
"fileperms",
#define OPT_NOPROMPT 24
"noprompt",
NULL
};
static int Oflg = 0; /* Overlay mounts */
static int qflg = 0; /* quiet - don't print warnings on bad options */
static int noprompt = 0; /* don't prompt for password */
/* Note: smbfs uses _both_ kinds of options. */
static int mntflags = MS_DATA | MS_OPTIONSTR;
#define EX_OK 0 /* normal */
#define EX_OPT 1 /* bad options, usage, etc */
#define EX_MNT 2 /* mount point problems, etc */
#define RET_ERR 3 /* later errors */
#define SERVICE "svc:/network/smb/client:default"
struct smbfs_args mdata;
struct mnttab mnt;
/*
* Initialize this with "rw" just to have something there,
* so we don't have to decide whether to add a comma when
* we strcat another option. Note the "rw" may be changed
* to an "ro" by option processing.
*/
char optbuf[MAX_MNTOPT_STR] = "rw";
int
main(int argc, char *argv[])
{
struct smb_ctx *ctx = NULL;
struct stat st;
int opt, error, err2;
static char *fstype = MNTTYPE_SMBFS;
char *env, *state;
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
(void) textdomain(TEXT_DOMAIN);
if (argc == 2) {
if (strcmp(argv[1], "-h") == 0) {
usage();
} else if (strcmp(argv[1], "-v") == 0) {
errx(EX_OK, gettext("version %d.%d.%d"),
SMBFS_VERSION / 100000,
(SMBFS_VERSION % 10000) / 1000,
(SMBFS_VERSION % 1000) / 100);
}
}
if (argc < 3)
usage();
state = smf_get_state(SERVICE);
if (state == NULL || strcmp(state, SCF_STATE_STRING_ONLINE) != 0) {
fprintf(stderr,
gettext("mount_smbfs: service \"%s\" not enabled.\n"),
SERVICE);
exit(RET_ERR);
}
free(state);
/* Debugging support. */
if ((env = getenv("SMBFS_DEBUG")) != NULL) {
smb_debug = atoi(env);
if (smb_debug < 1)
smb_debug = 1;
}
error = smb_lib_init();
if (error)
exit(RET_ERR);
mnt.mnt_mntopts = optbuf;
bzero(&mdata, sizeof (mdata));
mdata.version = SMBFS_VERSION; /* smbfs mount version */
mdata.uid = (uid_t)-1;
mdata.gid = (gid_t)-1;
error = smb_ctx_alloc(&ctx);
if (error)
exit(RET_ERR);
/*
* Parse the UNC path so we have the server (etc.)
* that we need during rcfile+sharectl parsing.
*/
if (argc < 3)
usage();
error = smb_ctx_parseunc(ctx, argv[argc - 2],
SMBL_SHARE, SMBL_SHARE, USE_DISKDEV, NULL);
if (error)
exit(EX_OPT);
error = smb_ctx_readrc(ctx);
if (error)
exit(EX_OPT);
while ((opt = getopt(argc, argv, "ro:Oq")) != -1) {
switch (opt) {
case 'O':
Oflg++;
break;
case 'q':
qflg++;
break;
case 'r':
mntflags |= MS_RDONLY;
break;
case 'o': {
char *nextopt, *comma, *sopt;
int ret;
for (sopt = optarg; sopt != NULL; sopt = nextopt) {
comma = strchr(sopt, ',');
if (comma) {
nextopt = comma + 1;
*comma = '\0';
} else
nextopt = NULL;
ret = setsubopt(ctx, &mdata, sopt);
if (ret != 0)
exit(EX_OPT);
/* undo changes to optarg */
if (comma)
*comma = ',';
}
break;
}
case '?':
default:
usage();
}
}
if (Oflg)
mntflags |= MS_OVERLAY;
if (mntflags & MS_RDONLY) {
char *p;
/* convert "rw"->"ro" */
if (p = strstr(optbuf, "rw")) {
if (*(p+2) == ',' || *(p+2) == '\0')
*(p+1) = 'o';
}
}
if (optind + 2 != argc)
usage();
mnt.mnt_special = argv[optind];
mnt.mnt_mountp = argv[optind+1];
realpath(argv[optind+1], mount_point);
if (stat(mount_point, &st) == -1)
err(EX_MNT, gettext("could not find mount point %s"),
mount_point);
if (!S_ISDIR(st.st_mode)) {
errno = ENOTDIR;
err(EX_MNT, gettext("can't mount on %s"), mount_point);
}
/*
* Fill in mdata defaults.
*/
if (mdata.uid == (uid_t)-1)
mdata.uid = getuid();
if (mdata.gid == (gid_t)-1)
mdata.gid = getgid();
if (mdata.file_mode == 0)
mdata.file_mode = S_IRWXU;
if (mdata.dir_mode == 0) {
mdata.dir_mode = mdata.file_mode;
if (mdata.dir_mode & S_IRUSR)
mdata.dir_mode |= S_IXUSR;
if (mdata.dir_mode & S_IRGRP)
mdata.dir_mode |= S_IXGRP;
if (mdata.dir_mode & S_IROTH)
mdata.dir_mode |= S_IXOTH;
}
ctx->ct_ssn.ssn_owner = SMBM_ANY_OWNER;
if (noprompt)
ctx->ct_flags |= SMBCF_NOPWD;
/*
* Resolve the server address,
* setup derived defaults.
*/
error = smb_ctx_resolve(ctx);
if (error)
exit(RET_ERR);
/*
* Have server, share, etc. from above:
* smb_ctx_scan_argv, option settings.
* Get the session and tree.
*/
again:
error = smb_ctx_get_ssn(ctx);
if (error == EAUTH && noprompt == 0) {
err2 = smb_get_authentication(ctx);
if (err2 == 0)
goto again;
}
if (error) {
smb_error(gettext("//%s: login failed"),
error, ctx->ct_fullserver);
exit(RET_ERR);
}
error = smb_ctx_get_tree(ctx);
if (error) {
smb_error(gettext("//%s/%s: tree connect failed"),
error, ctx->ct_fullserver, ctx->ct_origshare);
exit(RET_ERR);
}
/*
* Have tree connection, now mount it.
*/
mdata.devfd = ctx->ct_dev_fd;
if (mount(mnt.mnt_special, mnt.mnt_mountp,
mntflags, fstype, &mdata, sizeof (mdata),
mnt.mnt_mntopts, MAX_MNTOPT_STR) < 0) {
if (errno != ENOENT) {
err(EX_MNT, gettext("mount_smbfs: %s"),
mnt.mnt_mountp);
} else {
struct stat sb;
if (stat(mnt.mnt_mountp, &sb) < 0 &&
errno == ENOENT)
err(EX_MNT, gettext("mount_smbfs: %s"),
mnt.mnt_mountp);
else
err(EX_MNT, gettext("mount_smbfs: %s"),
mnt.mnt_special);
}
}
smb_ctx_free(ctx);
return (0);
}
#define bad(val) (val == NULL || !isdigit(*val))
int
setsubopt(smb_ctx_t *ctx, struct smbfs_args *mdatap, char *subopt)
{
char *equals, *optarg;
struct passwd *pwd;
struct group *grp;
long val;
int rc = EX_OK;
int index;
char *p;
equals = strchr(subopt, '=');
if (equals) {
*equals = '\0';
optarg = equals + 1;
} else
optarg = NULL;
for (index = 0; optlist[index] != NULL; index++) {
if (strcmp(subopt, optlist[index]) == 0)
break;
}
/*
* Note: if the option was unknown, index will
* point to the NULL at the end of optlist[],
* and we'll take the switch default.
*/
switch (index) {
case OPT_SUID:
case OPT_NOSUID:
case OPT_DEVICES:
case OPT_NODEVICES:
case OPT_SETUID:
case OPT_NOSETUID:
case OPT_EXEC:
case OPT_NOEXEC:
case OPT_XATTR:
case OPT_NOXATTR:
/*
* These options are handled via the
* generic option string mechanism.
* None of these take an optarg.
*/
if (optarg != NULL)
goto badval;
(void) strlcat(optbuf, ",", sizeof (optbuf));
if (strlcat(optbuf, subopt, sizeof (optbuf)) >=
sizeof (optbuf)) {
if (!qflg)
warnx(gettext("option string too long"));
rc = EX_OPT;
}
break;
/*
* OPT_RO, OPT_RW, are actually generic too,
* but we use the mntflags for these, and
* then update the options string later.
*/
case OPT_RO:
mntflags |= MS_RDONLY;
break;
case OPT_RW:
mntflags &= ~MS_RDONLY;
break;
/*
* NFS-derived options for attribute cache
* handling (disable, set min/max timeouts)
*/
case OPT_NOAC:
mdatap->flags |= SMBFS_MF_NOAC;
break;
case OPT_ACTIMEO:
errno = 0;
val = strtol(optarg, &p, 10);
if (errno || *p != 0)
goto badval;
mdatap->acdirmin = mdatap->acregmin = val;
mdatap->acdirmax = mdatap->acregmax = val;
mdatap->flags |= SMBFS_MF_ACDIRMAX;
mdatap->flags |= SMBFS_MF_ACREGMAX;
mdatap->flags |= SMBFS_MF_ACDIRMIN;
mdatap->flags |= SMBFS_MF_ACREGMIN;
break;
case OPT_ACREGMIN:
errno = 0;
val = strtol(optarg, &p, 10);
if (errno || *p != 0)
goto badval;
mdatap->acregmin = val;
mdatap->flags |= SMBFS_MF_ACREGMIN;
break;
case OPT_ACREGMAX:
errno = 0;
val = strtol(optarg, &p, 10);
if (errno || *p != 0)
goto badval;
mdatap->acregmax = val;
mdatap->flags |= SMBFS_MF_ACREGMAX;
break;
case OPT_ACDIRMIN:
errno = 0;
val = strtol(optarg, &p, 10);
if (errno || *p != 0)
goto badval;
mdatap->acdirmin = val;
mdatap->flags |= SMBFS_MF_ACDIRMIN;
break;
case OPT_ACDIRMAX:
errno = 0;
val = strtol(optarg, &p, 10);
if (errno || *p != 0)
goto badval;
mdatap->acdirmax = val;
mdatap->flags |= SMBFS_MF_ACDIRMAX;
break;
/*
* SMBFS-specific options. Some of these
* don't go through the mount system call,
* but just set libsmbfs options.
*/
case OPT_DOMAIN:
if (smb_ctx_setdomain(ctx, optarg, B_TRUE) != 0)
rc = EX_OPT;
break;
case OPT_USER:
if (smb_ctx_setuser(ctx, optarg, B_TRUE) != 0)
rc = EX_OPT;
break;
case OPT_UID:
pwd = isdigit(optarg[0]) ?
getpwuid(atoi(optarg)) : getpwnam(optarg);
if (pwd == NULL) {
if (!qflg)
warnx(gettext("unknown user '%s'"), optarg);
rc = EX_OPT;
} else {
mdatap->uid = pwd->pw_uid;
}
break;
case OPT_GID:
grp = isdigit(optarg[0]) ?
getgrgid(atoi(optarg)) : getgrnam(optarg);
if (grp == NULL) {
if (!qflg)
warnx(gettext("unknown group '%s'"), optarg);
rc = EX_OPT;
} else {
mdatap->gid = grp->gr_gid;
}
break;
case OPT_DIRPERMS:
errno = 0;
val = strtol(optarg, &p, 8);
if (errno || *p != 0)
goto badval;
mdatap->dir_mode = val;
break;
case OPT_FILEPERMS:
errno = 0;
val = strtol(optarg, &p, 8);
if (errno || *p != 0)
goto badval;
mdatap->file_mode = val;
break;
case OPT_NOPROMPT:
noprompt++;
break;
default:
if (!qflg)
warnx(gettext("unknown option %s"), subopt);
rc = EX_OPT;
break;
badval:
if (!qflg)
warnx(gettext("invalid value for %s"), subopt);
rc = EX_OPT;
break;
}
/* Undo changes made to subopt */
if (equals)
*equals = '=';
return (rc);
}
static void
usage(void)
{
fprintf(stderr, "%s\n",
gettext("usage: mount -F smbfs [-Orq] [-o option[,option]]"
" //[workgroup;][user[:password]@]server[/share] path"));
exit(EX_OPT);
}
|