2 Mount helper utility for Linux CIFS VFS (virtual filesystem) client
3 Copyright (C) 2003,2008 Steve French (sfrench@us.ibm.com)
4 Copyright (C) 2008 Jeremy Allison (jra@samba.org)
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include <sys/types.h>
30 #include <sys/mount.h>
32 #include <sys/utsname.h>
33 #include <sys/socket.h>
34 #include <arpa/inet.h>
44 #define MOUNT_CIFS_VERSION_MAJOR "1"
45 #define MOUNT_CIFS_VERSION_MINOR "12"
47 #ifndef MOUNT_CIFS_VENDOR_SUFFIX
49 #include "include/version.h"
50 #ifdef SAMBA_VERSION_VENDOR_SUFFIX
51 #define MOUNT_CIFS_VENDOR_SUFFIX "-"SAMBA_VERSION_OFFICIAL_STRING"-"SAMBA_VERSION_VENDOR_SUFFIX
53 #define MOUNT_CIFS_VENDOR_SUFFIX "-"SAMBA_VERSION_OFFICIAL_STRING
54 #endif /* SAMBA_VERSION_OFFICIAL_STRING and SAMBA_VERSION_VENDOR_SUFFIX */
56 #define MOUNT_CIFS_VENDOR_SUFFIX ""
57 #endif /* _SAMBA_BUILD_ */
58 #endif /* MOUNT_CIFS_VENDOR_SUFFIX */
61 #include "include/config.h"
72 #define MAX_UNC_LEN 1024
74 #define CONST_DISCARD(type, ptr) ((type) ((void *) (ptr)))
77 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
80 #define MOUNT_PASSWD_SIZE 64
81 #define DOMAIN_SIZE 64
83 /* currently maximum length of IPv6 address string */
84 #define MAX_ADDRESS_LEN INET6_ADDRSTRLEN
86 const char *thisprogram
;
88 static int got_password
= 0;
89 static int got_user
= 0;
90 static int got_domain
= 0;
91 static int got_ip
= 0;
92 static int got_unc
= 0;
93 static int got_uid
= 0;
94 static int got_gid
= 0;
95 static char * user_name
= NULL
;
96 static char * mountpassword
= NULL
;
97 char * domain_name
= NULL
;
98 char * prefixpath
= NULL
;
100 /* glibc doesn't have strlcpy, strlcat. Ensure we do. JRA. We
101 * don't link to libreplace so need them here. */
103 /* like strncpy but does not 0 fill the buffer and always null
104 * terminates. bufsize is the size of the destination buffer */
107 static size_t strlcpy(char *d
, const char *s
, size_t bufsize
)
109 size_t len
= strlen(s
);
111 if (bufsize
<= 0) return 0;
112 if (len
>= bufsize
) len
= bufsize
-1;
119 /* like strncat but does not 0 fill the buffer and always null
120 * terminates. bufsize is the length of the buffer, which should
121 * be one more than the maximum resulting string length */
124 static size_t strlcat(char *d
, const char *s
, size_t bufsize
)
126 size_t len1
= strlen(d
);
127 size_t len2
= strlen(s
);
128 size_t ret
= len1
+ len2
;
130 if (len1
+len2
>= bufsize
) {
131 if (bufsize
< (len1
+1)) {
134 len2
= bufsize
- (len1
+1);
137 memcpy(d
+len1
, s
, len2
);
147 open nofollow - avoid symlink exposure?
148 get owner of dir see if matches self or if root
149 call system(umount argv) etc.
153 static char * check_for_domain(char **);
156 static void mount_cifs_usage(void)
158 printf("\nUsage: %s <remotetarget> <dir> -o <options>\n", thisprogram
);
159 printf("\nMount the remote target, specified as a UNC name,");
160 printf(" to a local directory.\n\nOptions:\n");
161 printf("\tuser=<arg>\n\tpass=<arg>\n\tdom=<arg>\n");
162 printf("\nLess commonly used options:");
163 printf("\n\tcredentials=<filename>,guest,perm,noperm,setuids,nosetuids,rw,ro,");
164 printf("\n\tsep=<char>,iocharset=<codepage>,suid,nosuid,exec,noexec,serverino,");
165 printf("\n\tmapchars,nomapchars,nolock,servernetbiosname=<SRV_RFC1001NAME>");
166 printf("\n\tdirectio,nounix,cifsacl,sec=<authentication mechanism>,sign");
167 printf("\n\nOptions not needed for servers supporting CIFS Unix extensions");
168 printf("\n\t(e.g. unneeded for mounts to most Samba versions):");
169 printf("\n\tuid=<uid>,gid=<gid>,dir_mode=<mode>,file_mode=<mode>,sfu");
170 printf("\n\nRarely used options:");
171 printf("\n\tport=<tcpport>,rsize=<size>,wsize=<size>,unc=<unc_name>,ip=<ip_address>,");
172 printf("\n\tdev,nodev,nouser_xattr,netbiosname=<OUR_RFC1001NAME>,hard,soft,intr,");
173 printf("\n\tnointr,ignorecase,noposixpaths,noacl,prefixpath=<path>,nobrl");
174 printf("\n\tin6_addr");
175 printf("\n\nOptions are described in more detail in the manual page");
176 printf("\n\tman 8 mount.cifs\n");
177 printf("\nTo display the version number of the mount helper:");
178 printf("\n\t%s -V\n",thisprogram
);
180 SAFE_FREE(mountpassword
);
184 /* caller frees username if necessary */
185 static char * getusername(void) {
186 char *username
= NULL
;
187 struct passwd
*password
= getpwuid(getuid());
190 username
= password
->pw_name
;
195 static int open_cred_file(char * file_name
)
201 fs
= fopen(file_name
,"r");
204 line_buf
= (char *)malloc(4096);
205 if(line_buf
== NULL
) {
210 while(fgets(line_buf
,4096,fs
)) {
211 /* parse line from credential file */
213 /* eat leading white space */
214 for(i
=0;i
<4086;i
++) {
215 if((line_buf
[i
] != ' ') && (line_buf
[i
] != '\t'))
217 /* if whitespace - skip past it */
219 if (strncasecmp("username",line_buf
+i
,8) == 0) {
220 temp_val
= strchr(line_buf
+ i
,'=');
222 /* go past equals sign */
224 for(length
= 0;length
<4087;length
++) {
225 if ((temp_val
[length
] == '\n')
226 || (temp_val
[length
] == '\0')) {
227 temp_val
[length
] = '\0';
232 printf("mount.cifs failed due to malformed username in credentials file");
233 memset(line_buf
,0,4096);
237 user_name
= (char *)calloc(1 + length
,1);
238 /* BB adding free of user_name string before exit,
239 not really necessary but would be cleaner */
240 strlcpy(user_name
,temp_val
, length
+1);
243 } else if (strncasecmp("password",line_buf
+i
,8) == 0) {
244 temp_val
= strchr(line_buf
+i
,'=');
246 /* go past equals sign */
248 for(length
= 0;length
<MOUNT_PASSWD_SIZE
+1;length
++) {
249 if ((temp_val
[length
] == '\n')
250 || (temp_val
[length
] == '\0')) {
251 temp_val
[length
] = '\0';
255 if(length
> MOUNT_PASSWD_SIZE
) {
256 printf("mount.cifs failed: password in credentials file too long\n");
257 memset(line_buf
,0, 4096);
260 if(mountpassword
== NULL
) {
261 mountpassword
= (char *)calloc(MOUNT_PASSWD_SIZE
+1,1);
263 memset(mountpassword
,0,MOUNT_PASSWD_SIZE
);
265 strlcpy(mountpassword
,temp_val
,MOUNT_PASSWD_SIZE
+1);
270 } else if (strncasecmp("domain",line_buf
+i
,6) == 0) {
271 temp_val
= strchr(line_buf
+i
,'=');
273 /* go past equals sign */
276 printf("\nDomain %s\n",temp_val
);
277 for(length
= 0;length
<DOMAIN_SIZE
+1;length
++) {
278 if ((temp_val
[length
] == '\n')
279 || (temp_val
[length
] == '\0')) {
280 temp_val
[length
] = '\0';
284 if(length
> DOMAIN_SIZE
) {
285 printf("mount.cifs failed: domain in credentials file too long\n");
288 if(domain_name
== NULL
) {
289 domain_name
= (char *)calloc(DOMAIN_SIZE
+1,1);
291 memset(domain_name
,0,DOMAIN_SIZE
);
293 strlcpy(domain_name
,temp_val
,DOMAIN_SIZE
+1);
306 static int get_password_from_file(int file_descript
, char * filename
)
312 if(mountpassword
== NULL
)
313 mountpassword
= (char *)calloc(MOUNT_PASSWD_SIZE
+1,1);
315 memset(mountpassword
, 0, MOUNT_PASSWD_SIZE
);
317 if (mountpassword
== NULL
) {
318 printf("malloc failed\n");
322 if(filename
!= NULL
) {
323 file_descript
= open(filename
, O_RDONLY
);
324 if(file_descript
< 0) {
325 printf("mount.cifs failed. %s attempting to open password file %s\n",
326 strerror(errno
),filename
);
330 /* else file already open and fd provided */
332 for(i
=0;i
<MOUNT_PASSWD_SIZE
;i
++) {
333 rc
= read(file_descript
,&c
,1);
335 printf("mount.cifs failed. Error %s reading password file\n",strerror(errno
));
337 close(file_descript
);
340 if(mountpassword
[0] == 0) {
342 printf("\nWarning: null password used since cifs password file empty");
345 } else /* read valid character */ {
346 if((c
== 0) || (c
== '\n')) {
347 mountpassword
[i
] = '\0';
350 mountpassword
[i
] = c
;
353 if((i
== MOUNT_PASSWD_SIZE
) && (verboseflag
)) {
354 printf("\nWarning: password longer than %d characters specified in cifs password file",
358 if(filename
!= NULL
) {
359 close(file_descript
);
365 static int parse_options(char ** optionsp
, int * filesys_flags
)
368 char * percent_char
= NULL
;
370 char * next_keyword
= NULL
;
378 if (!optionsp
|| !*optionsp
)
383 printf("parsing options: %s\n", data
);
385 /* BB fixme check for separator override BB */
389 snprintf(user
,sizeof(user
),"%u",getuid());
391 snprintf(group
,sizeof(group
),"%u",getgid());
394 /* while ((data = strsep(&options, ",")) != NULL) { */
395 while(data
!= NULL
) {
396 /* check if ends with trailing comma */
400 /* format is keyword=value,keyword2=value2,keyword3=value3 etc.) */
401 /* data = next keyword */
402 /* value = next value ie stuff after equal sign */
404 next_keyword
= strchr(data
,','); /* BB handle sep= */
406 /* temporarily null terminate end of keyword=value pair */
410 /* temporarily null terminate keyword to make keyword and value distinct */
411 if ((value
= strchr(data
, '=')) != NULL
) {
416 if (strncmp(data
, "users",5) == 0) {
417 if(!value
|| !*value
) {
420 } else if (strncmp(data
, "user_xattr",10) == 0) {
421 /* do nothing - need to skip so not parsed as user name */
422 } else if (strncmp(data
, "user", 4) == 0) {
424 if (!value
|| !*value
) {
425 if(data
[4] == '\0') {
427 printf("\nskipping empty user mount parameter\n");
428 /* remove the parm since it would otherwise be confusing
429 to the kernel code which would think it was a real username */
432 printf("username specified with no parameter\n");
434 return 1; /* needs_arg; */
437 if (strnlen(value
, 260) < 260) {
439 percent_char
= strchr(value
,'%');
442 if(mountpassword
== NULL
)
443 mountpassword
= (char *)calloc(MOUNT_PASSWD_SIZE
+1,1);
446 printf("\nmount.cifs warning - password specified twice\n");
449 strlcpy(mountpassword
, percent_char
,MOUNT_PASSWD_SIZE
+1);
450 /* remove password from username */
451 while(*percent_char
!= 0) {
457 /* this is only case in which the user
458 name buf is not malloc - so we have to
459 check for domain name embedded within
460 the user name here since the later
461 call to check_for_domain will not be
463 domain_name
= check_for_domain(&value
);
465 printf("username too long\n");
470 } else if (strncmp(data
, "pass", 4) == 0) {
471 if (!value
|| !*value
) {
473 printf("\npassword specified twice, ignoring second\n");
476 } else if (strnlen(value
, 17) < 17) {
478 printf("\nmount.cifs warning - password specified twice\n");
481 printf("password too long\n");
485 } else if (strncmp(data
, "sec", 3) == 0) {
487 if (!strncmp(value
, "none", 4) ||
488 !strncmp(value
, "krb5", 4))
491 } else if (strncmp(data
, "ip", 2) == 0) {
492 if (!value
|| !*value
) {
493 printf("target ip address argument missing");
494 } else if (strnlen(value
, MAX_ADDRESS_LEN
) <= MAX_ADDRESS_LEN
) {
496 printf("ip address %s override specified\n",value
);
499 printf("ip address too long\n");
503 } else if ((strncmp(data
, "unc", 3) == 0)
504 || (strncmp(data
, "target", 6) == 0)
505 || (strncmp(data
, "path", 4) == 0)) {
506 if (!value
|| !*value
) {
507 printf("invalid path to network resource\n");
509 return 1; /* needs_arg; */
510 } else if(strnlen(value
,5) < 5) {
511 printf("UNC name too short");
514 if (strnlen(value
, 300) < 300) {
516 if (strncmp(value
, "//", 2) == 0) {
518 printf("unc name specified twice, ignoring second\n");
521 } else if (strncmp(value
, "\\\\", 2) != 0) {
522 printf("UNC Path does not begin with // or \\\\ \n");
527 printf("unc name specified twice, ignoring second\n");
532 printf("CIFS: UNC name too long\n");
536 } else if ((strncmp(data
, "dom" /* domain */, 3) == 0)
537 || (strncmp(data
, "workg", 5) == 0)) {
538 /* note this allows for synonyms of "domain"
539 such as "DOM" and "dom" and "workgroup"
540 and "WORKGRP" etc. */
541 if (!value
|| !*value
) {
542 printf("CIFS: invalid domain name\n");
544 return 1; /* needs_arg; */
546 if (strnlen(value
, DOMAIN_SIZE
+1) < DOMAIN_SIZE
+1) {
549 printf("domain name too long\n");
553 } else if (strncmp(data
, "cred", 4) == 0) {
554 if (value
&& *value
) {
555 rc
= open_cred_file(value
);
557 printf("error %d (%s) opening credential file %s\n",
558 rc
, strerror(rc
), value
);
563 printf("invalid credential file name specified\n");
567 } else if (strncmp(data
, "uid", 3) == 0) {
568 if (value
&& *value
) {
570 if (!isdigit(*value
)) {
573 if (!(pw
= getpwnam(value
))) {
574 printf("bad user name \"%s\"\n", value
);
577 snprintf(user
, sizeof(user
), "%u", pw
->pw_uid
);
579 strlcpy(user
,value
,sizeof(user
));
583 } else if (strncmp(data
, "gid", 3) == 0) {
584 if (value
&& *value
) {
586 if (!isdigit(*value
)) {
589 if (!(gr
= getgrnam(value
))) {
590 printf("bad group name \"%s\"\n", value
);
593 snprintf(group
, sizeof(group
), "%u", gr
->gr_gid
);
595 strlcpy(group
,value
,sizeof(group
));
599 /* fmask and dmask synonyms for people used to smbfs syntax */
600 } else if (strcmp(data
, "file_mode") == 0 || strcmp(data
, "fmask")==0) {
601 if (!value
|| !*value
) {
602 printf ("Option '%s' requires a numerical argument\n", data
);
607 if (value
[0] != '0') {
608 printf ("WARNING: '%s' not expressed in octal.\n", data
);
611 if (strcmp (data
, "fmask") == 0) {
612 printf ("WARNING: CIFS mount option 'fmask' is deprecated. Use 'file_mode' instead.\n");
613 data
= "file_mode"; /* BB fix this */
615 } else if (strcmp(data
, "dir_mode") == 0 || strcmp(data
, "dmask")==0) {
616 if (!value
|| !*value
) {
617 printf ("Option '%s' requires a numerical argument\n", data
);
622 if (value
[0] != '0') {
623 printf ("WARNING: '%s' not expressed in octal.\n", data
);
626 if (strcmp (data
, "dmask") == 0) {
627 printf ("WARNING: CIFS mount option 'dmask' is deprecated. Use 'dir_mode' instead.\n");
630 /* the following eight mount options should be
631 stripped out from what is passed into the kernel
632 since these eight options are best passed as the
633 mount flags rather than redundantly to the kernel
634 and could generate spurious warnings depending on the
635 level of the corresponding cifs vfs kernel code */
636 } else if (strncmp(data
, "nosuid", 6) == 0) {
637 *filesys_flags
|= MS_NOSUID
;
638 } else if (strncmp(data
, "suid", 4) == 0) {
639 *filesys_flags
&= ~MS_NOSUID
;
640 } else if (strncmp(data
, "nodev", 5) == 0) {
641 *filesys_flags
|= MS_NODEV
;
642 } else if ((strncmp(data
, "nobrl", 5) == 0) ||
643 (strncmp(data
, "nolock", 6) == 0)) {
644 *filesys_flags
&= ~MS_MANDLOCK
;
645 } else if (strncmp(data
, "dev", 3) == 0) {
646 *filesys_flags
&= ~MS_NODEV
;
647 } else if (strncmp(data
, "noexec", 6) == 0) {
648 *filesys_flags
|= MS_NOEXEC
;
649 } else if (strncmp(data
, "exec", 4) == 0) {
650 *filesys_flags
&= ~MS_NOEXEC
;
651 } else if (strncmp(data
, "guest", 5) == 0) {
653 } else if (strncmp(data
, "ro", 2) == 0) {
654 *filesys_flags
|= MS_RDONLY
;
655 } else if (strncmp(data
, "rw", 2) == 0) {
656 *filesys_flags
&= ~MS_RDONLY
;
657 } else if (strncmp(data
, "remount", 7) == 0) {
658 *filesys_flags
|= MS_REMOUNT
;
659 } /* else if (strnicmp(data, "port", 4) == 0) {
660 if (value && *value) {
662 simple_strtoul(value, &value, 0);
664 } else if (strnicmp(data, "rsize", 5) == 0) {
665 if (value && *value) {
667 simple_strtoul(value, &value, 0);
669 } else if (strnicmp(data, "wsize", 5) == 0) {
670 if (value && *value) {
672 simple_strtoul(value, &value, 0);
674 } else if (strnicmp(data, "version", 3) == 0) {
676 printf("CIFS: Unknown mount option %s\n",data);
677 } */ /* nothing to do on those four mount options above.
678 Just pass to kernel and ignore them here */
680 /* Copy (possibly modified) option to out */
681 word_len
= strlen(data
);
683 word_len
+= 1 + strlen(value
);
685 out
= (char *)realloc(out
, out_len
+ word_len
+ 2);
692 strlcat(out
, ",", out_len
+ word_len
+ 2);
697 snprintf(out
+ out_len
, word_len
+ 1, "%s=%s", data
, value
);
699 snprintf(out
+ out_len
, word_len
+ 1, "%s", data
);
700 out_len
= strlen(out
);
706 /* special-case the uid and gid */
708 word_len
= strlen(user
);
710 out
= (char *)realloc(out
, out_len
+ word_len
+ 6);
717 strlcat(out
, ",", out_len
+ word_len
+ 6);
720 snprintf(out
+ out_len
, word_len
+ 5, "uid=%s", user
);
721 out_len
= strlen(out
);
724 word_len
= strlen(group
);
726 out
= (char *)realloc(out
, out_len
+ 1 + word_len
+ 6);
733 strlcat(out
, ",", out_len
+ word_len
+ 6);
736 snprintf(out
+ out_len
, word_len
+ 5, "gid=%s", group
);
737 out_len
= strlen(out
);
740 SAFE_FREE(*optionsp
);
745 /* replace all (one or more) commas with double commas */
746 static void check_for_comma(char ** ppasswrd
)
751 int number_of_commas
= 0;
766 if(number_of_commas
== 0)
768 if(number_of_commas
> MOUNT_PASSWD_SIZE
) {
769 /* would otherwise overflow the mount options buffer */
770 printf("\nInvalid password. Password contains too many commas.\n");
774 new_pass_buf
= (char *)malloc(len
+number_of_commas
+1);
775 if(new_pass_buf
== NULL
)
778 for(i
=0,j
=0;i
<len
;i
++,j
++) {
779 new_pass_buf
[j
] = pass
[i
];
782 new_pass_buf
[j
] = pass
[i
];
785 new_pass_buf
[len
+number_of_commas
] = 0;
787 SAFE_FREE(*ppasswrd
);
788 *ppasswrd
= new_pass_buf
;
793 /* Usernames can not have backslash in them and we use
794 [BB check if usernames can have forward slash in them BB]
795 backslash as domain\user separator character
797 static char * check_for_domain(char **ppuser
)
799 char * original_string
;
809 original_string
= *ppuser
;
811 if (original_string
== NULL
)
814 original_len
= strlen(original_string
);
816 usernm
= strchr(*ppuser
,'/');
817 if (usernm
== NULL
) {
818 usernm
= strchr(*ppuser
,'\\');
824 printf("Domain name specified twice. Username probably malformed\n");
830 if (domainnm
[0] != 0) {
833 printf("null domain\n");
835 len
= strlen(domainnm
);
836 /* reset domainm to new buffer, and copy
837 domain name into it */
838 domainnm
= (char *)malloc(len
+1);
842 strlcpy(domainnm
,*ppuser
,len
+1);
844 /* move_string(*ppuser, usernm+1) */
845 len
= strlen(usernm
+1);
847 if(len
>= original_len
) {
848 /* should not happen */
852 for(i
=0;i
<original_len
;i
++) {
854 original_string
[i
] = usernm
[i
+1];
855 else /* stuff with commas to remove last parm */
856 original_string
[i
] = ',';
859 /* BB add check for more than one slash?
867 /* replace all occurances of "from" in a string with "to" */
868 static void replace_char(char *string
, char from
, char to
, int maxlen
)
870 char *lastchar
= string
+ maxlen
;
872 string
= strchr(string
, from
);
875 if (string
>= lastchar
)
881 /* Note that caller frees the returned buffer if necessary */
882 static struct addrinfo
*
883 parse_server(char ** punc_name
)
885 char * unc_name
= *punc_name
;
886 int length
= strnlen(unc_name
, MAX_UNC_LEN
);
888 struct addrinfo
*addrlist
;
891 if(length
> (MAX_UNC_LEN
- 1)) {
892 printf("mount error: UNC name too long");
895 if ((strncasecmp("cifs://", unc_name
, 7) == 0) ||
896 (strncasecmp("smb://", unc_name
, 6) == 0)) {
897 printf("\nMounting cifs URL not implemented yet. Attempt to mount %s\n", unc_name
);
902 /* BB add code to find DFS root here */
903 printf("\nMounting the DFS root for domain not implemented yet\n");
906 if(strncmp(unc_name
,"//",2) && strncmp(unc_name
,"\\\\",2)) {
907 /* check for nfs syntax ie server:share */
908 share
= strchr(unc_name
,':');
910 *punc_name
= (char *)malloc(length
+3);
911 if(*punc_name
== NULL
) {
912 /* put the original string back if
914 *punc_name
= unc_name
;
918 strlcpy((*punc_name
)+2,unc_name
,length
+1);
920 unc_name
= *punc_name
;
921 unc_name
[length
+2] = 0;
922 goto continue_unc_parsing
;
924 printf("mount error: improperly formatted UNC name.");
925 printf(" %s does not begin with \\\\ or //\n",unc_name
);
929 continue_unc_parsing
:
934 /* allow for either delimiter between host and sharename */
935 if ((share
= strpbrk(unc_name
, "/\\"))) {
936 *share
= 0; /* temporarily terminate the string */
939 rc
= getaddrinfo(unc_name
, NULL
, NULL
, &addrlist
);
941 printf("mount error: could not resolve address for %s: %s\n",
942 unc_name
, gai_strerror(rc
));
946 *(share
- 1) = '/'; /* put delimiter back */
948 /* we don't convert the prefixpath delimiters since '\\' is a valid char in posix paths */
949 if ((prefixpath
= strpbrk(share
, "/\\"))) {
950 *prefixpath
= 0; /* permanently terminate the string */
951 if (!strlen(++prefixpath
))
952 prefixpath
= NULL
; /* this needs to be done explicitly */
956 printf("ip address specified explicitly\n");
959 /* BB should we pass an alternate version of the share name as Unicode */
963 /* BB add code to find DFS root (send null path on get DFS Referral to specified server here */
964 printf("Mounting the DFS root for a particular server not implemented yet\n");
971 static struct option longopts
[] = {
972 { "all", 0, NULL
, 'a' },
973 { "help",0, NULL
, 'h' },
974 { "move",0, NULL
, 'm' },
975 { "bind",0, NULL
, 'b' },
976 { "read-only", 0, NULL
, 'r' },
977 { "ro", 0, NULL
, 'r' },
978 { "verbose", 0, NULL
, 'v' },
979 { "version", 0, NULL
, 'V' },
980 { "read-write", 0, NULL
, 'w' },
981 { "rw", 0, NULL
, 'w' },
982 { "options", 1, NULL
, 'o' },
983 { "type", 1, NULL
, 't' },
984 { "rsize",1, NULL
, 'R' },
985 { "wsize",1, NULL
, 'W' },
986 { "uid", 1, NULL
, '1'},
987 { "gid", 1, NULL
, '2'},
988 { "user",1,NULL
,'u'},
989 { "username",1,NULL
,'u'},
991 { "domain",1,NULL
,'d'},
992 { "password",1,NULL
,'p'},
993 { "pass",1,NULL
,'p'},
994 { "credentials",1,NULL
,'c'},
995 { "port",1,NULL
,'P'},
996 /* { "uuid",1,NULL,'U'}, */ /* BB unimplemented */
1000 /* convert a string to uppercase. return false if the string
1001 * wasn't ASCII. Return success on a NULL ptr */
1003 uppercase_string(char *string
)
1009 /* check for unicode */
1010 if ((unsigned char) string
[0] & 0x80)
1012 *string
= toupper((unsigned char) *string
);
1019 int main(int argc
, char ** argv
)
1022 int flags
= MS_MANDLOCK
; /* no need to set legacy MS_MGC_VAL */
1023 char * orgoptions
= NULL
;
1024 char * share_name
= NULL
;
1025 const char * ipaddr
= NULL
;
1027 char * mountpoint
= NULL
;
1028 char * options
= NULL
;
1030 char * resolved_path
= NULL
;
1041 size_t options_size
= 0;
1043 int retry
= 0; /* set when we have to retry mount with uppercase */
1044 struct addrinfo
*addrhead
= NULL
, *addr
;
1045 struct stat statbuf
;
1046 struct utsname sysinfo
;
1047 struct mntent mountent
;
1048 struct sockaddr_in
*addr4
;
1049 struct sockaddr_in6
*addr6
;
1052 /* setlocale(LC_ALL, "");
1053 bindtextdomain(PACKAGE, LOCALEDIR);
1054 textdomain(PACKAGE); */
1057 thisprogram
= argv
[0];
1063 if(thisprogram
== NULL
)
1064 thisprogram
= "mount.cifs";
1067 /* BB add workstation name and domain and pass down */
1069 /* #ifdef _GNU_SOURCE
1070 printf(" node: %s machine: %s sysname %s domain %s\n", sysinfo.nodename,sysinfo.machine,sysinfo.sysname,sysinfo.domainname);
1074 share_name
= strndup(argv
[1], MAX_UNC_LEN
);
1075 if (share_name
== NULL
) {
1076 fprintf(stderr
, "%s: %s", argv
[0], strerror(ENOMEM
));
1079 mountpoint
= argv
[2];
1085 /* add sharename in opts string as unc= parm */
1087 while ((c
= getopt_long (argc
, argv
, "afFhilL:no:O:rsSU:vVwt:",
1088 longopts
, NULL
)) != -1) {
1090 /* No code to do the following options yet */
1092 list_with_volumelabel = 1;
1095 volumelabel = optarg;
1102 case 'h': /* help */
1103 mount_cifs_usage ();
1113 "option 'b' (MS_BIND) not supported\n");
1121 "option 'm' (MS_MOVE) not supported\n");
1125 orgoptions
= strdup(optarg
);
1127 case 'r': /* mount readonly */
1137 printf ("mount.cifs version: %s.%s%s\n",
1138 MOUNT_CIFS_VERSION_MAJOR
,
1139 MOUNT_CIFS_VERSION_MINOR
,
1140 MOUNT_CIFS_VENDOR_SUFFIX
);
1143 flags
&= ~MS_RDONLY
;
1146 rsize
= atoi(optarg
) ;
1149 wsize
= atoi(optarg
);
1152 if (isdigit(*optarg
)) {
1155 uid
= strtoul(optarg
, &ep
, 10);
1157 printf("bad uid value \"%s\"\n", optarg
);
1163 if (!(pw
= getpwnam(optarg
))) {
1164 printf("bad user name \"%s\"\n", optarg
);
1172 if (isdigit(*optarg
)) {
1175 gid
= strtoul(optarg
, &ep
, 10);
1177 printf("bad gid value \"%s\"\n", optarg
);
1183 if (!(gr
= getgrnam(optarg
))) {
1184 printf("bad user name \"%s\"\n", optarg
);
1196 domain_name
= optarg
; /* BB fix this - currently ignored */
1200 if(mountpassword
== NULL
)
1201 mountpassword
= (char *)calloc(MOUNT_PASSWD_SIZE
+1,1);
1204 strlcpy(mountpassword
,optarg
,MOUNT_PASSWD_SIZE
+1);
1208 get_password_from_file(0 /* stdin */,NULL
);
1213 printf("unknown mount option %c\n",c
);
1219 if((argc
< 3) || (dev_name
== NULL
) || (mountpoint
== NULL
)) {
1224 if (getenv("PASSWD")) {
1225 if(mountpassword
== NULL
)
1226 mountpassword
= (char *)calloc(MOUNT_PASSWD_SIZE
+1,1);
1228 strlcpy(mountpassword
,getenv("PASSWD"),MOUNT_PASSWD_SIZE
+1);
1231 } else if (getenv("PASSWD_FD")) {
1232 get_password_from_file(atoi(getenv("PASSWD_FD")),NULL
);
1233 } else if (getenv("PASSWD_FILE")) {
1234 get_password_from_file(0, getenv("PASSWD_FILE"));
1237 if (orgoptions
&& parse_options(&orgoptions
, &flags
)) {
1241 addrhead
= addr
= parse_server(&share_name
);
1242 if((addrhead
== NULL
) && (got_ip
== 0)) {
1243 printf("No ip address specified and hostname not found\n");
1248 /* BB save off path and pop after mount returns? */
1249 resolved_path
= (char *)malloc(PATH_MAX
+1);
1251 /* Note that if we can not canonicalize the name, we get
1252 another chance to see if it is valid when we chdir to it */
1253 if (realpath(mountpoint
, resolved_path
)) {
1254 mountpoint
= resolved_path
;
1257 if(chdir(mountpoint
)) {
1258 printf("mount error: can not change directory into mount target %s\n",mountpoint
);
1263 if(stat (".", &statbuf
)) {
1264 printf("mount error: mount point %s does not exist\n",mountpoint
);
1269 if (S_ISDIR(statbuf
.st_mode
) == 0) {
1270 printf("mount error: mount point %s is not a directory\n",mountpoint
);
1275 if((getuid() != 0) && (geteuid() == 0)) {
1276 if((statbuf
.st_uid
== getuid()) && (S_IRWXU
== (statbuf
.st_mode
& S_IRWXU
))) {
1277 #ifndef CIFS_ALLOW_USR_SUID
1278 /* Do not allow user mounts to control suid flag
1279 for mount unless explicitly built that way */
1280 flags
|= MS_NOSUID
| MS_NODEV
;
1283 printf("mount error: permission denied or not superuser and mount.cifs not installed SUID\n");
1289 /* Note that the password will not be retrieved from the
1290 USER env variable (ie user%password form) as there is
1291 already a PASSWD environment varaible */
1293 user_name
= strdup(getenv("USER"));
1294 if (user_name
== NULL
)
1295 user_name
= getusername();
1299 if(got_password
== 0) {
1300 char *tmp_pass
= getpass("Password: "); /* BB obsolete sys call but
1301 no good replacement yet. */
1302 mountpassword
= (char *)calloc(MOUNT_PASSWD_SIZE
+1,1);
1303 if (!tmp_pass
|| !mountpassword
) {
1304 printf("Password not entered, exiting\n");
1307 strlcpy(mountpassword
, tmp_pass
, MOUNT_PASSWD_SIZE
+1);
1310 /* FIXME launch daemon (handles dfs name resolution and credential change)
1311 remember to clear parms and overwrite password field before launching */
1313 optlen
= strlen(orgoptions
);
1318 optlen
+= strlen(share_name
) + 4;
1320 printf("No server share name specified\n");
1321 printf("\nMounting the DFS root for server not implemented yet\n");
1325 optlen
+= strlen(user_name
) + 6;
1326 optlen
+= MAX_ADDRESS_LEN
+ 4;
1328 optlen
+= strlen(mountpassword
) + 6;
1331 options_size
= optlen
+ 10 + DOMAIN_SIZE
;
1332 options
= (char *)malloc(options_size
/* space for commas in password */ + 8 /* space for domain= , domain name itself was counted as part of the length username string above */);
1334 if(options
== NULL
) {
1335 printf("Could not allocate memory for mount options\n");
1339 strlcpy(options
, "unc=", options_size
);
1340 strlcat(options
,share_name
,options_size
);
1341 /* scan backwards and reverse direction of slash */
1342 temp
= strrchr(options
, '/');
1343 if(temp
> options
+ 6)
1346 /* check for syntax like user=domain\user */
1348 domain_name
= check_for_domain(&user_name
);
1349 strlcat(options
,",user=",options_size
);
1350 strlcat(options
,user_name
,options_size
);
1354 /* extra length accounted for in option string above */
1355 strlcat(options
,",domain=",options_size
);
1356 strlcat(options
,domain_name
,options_size
);
1360 /* Commas have to be doubled, or else they will
1361 look like the parameter separator */
1362 /* if(sep is not set)*/
1364 check_for_comma(&mountpassword
);
1365 strlcat(options
,",pass=",options_size
);
1366 strlcat(options
,mountpassword
,options_size
);
1369 strlcat(options
,",ver=",options_size
);
1370 strlcat(options
,MOUNT_CIFS_VERSION_MAJOR
,options_size
);
1373 strlcat(options
,",",options_size
);
1374 strlcat(options
,orgoptions
,options_size
);
1377 strlcat(options
,",prefixpath=",options_size
);
1378 strlcat(options
,prefixpath
,options_size
); /* no need to cat the / */
1381 printf("\nmount.cifs kernel mount options %s \n",options
);
1383 /* convert all '\\' to '/' in share portion so that /proc/mounts looks pretty */
1384 replace_char(dev_name
, '\\', '/', strlen(share_name
));
1386 if (!got_ip
&& addr
) {
1387 strlcat(options
, ",ip=", options_size
);
1388 current_len
= strnlen(options
, options_size
);
1389 optionstail
= options
+ current_len
;
1390 switch (addr
->ai_addr
->sa_family
) {
1392 addr6
= (struct sockaddr_in6
*) addr
->ai_addr
;
1393 ipaddr
= inet_ntop(AF_INET6
, &addr6
->sin6_addr
, optionstail
,
1394 options_size
- current_len
);
1397 addr4
= (struct sockaddr_in
*) addr
->ai_addr
;
1398 ipaddr
= inet_ntop(AF_INET
, &addr4
->sin_addr
, optionstail
,
1399 options_size
- current_len
);
1403 /* if the address looks bogus, try the next one */
1405 addr
= addr
->ai_next
;
1413 if (mount(dev_name
, mountpoint
, "cifs", flags
, options
)) {
1418 addr
= addr
->ai_next
;
1424 printf("mount error: cifs filesystem not supported by the system\n");
1429 if (uppercase_string(dev_name
) &&
1430 uppercase_string(share_name
) &&
1431 uppercase_string(prefixpath
)) {
1432 printf("retrying with upper case share name\n");
1437 printf("mount error(%d): %s\n", errno
, strerror(errno
));
1438 printf("Refer to the mount.cifs(8) manual page (e.g.man mount.cifs)\n");
1443 atexit(unlock_mtab
);
1446 printf("cannot lock mtab");
1449 pmntfile
= setmntent(MOUNTED
, "a+");
1451 printf("could not update mount table\n");
1456 mountent
.mnt_fsname
= dev_name
;
1457 mountent
.mnt_dir
= mountpoint
;
1458 mountent
.mnt_type
= CONST_DISCARD(char *,"cifs");
1459 mountent
.mnt_opts
= (char *)malloc(220);
1460 if(mountent
.mnt_opts
) {
1461 char * mount_user
= getusername();
1462 memset(mountent
.mnt_opts
,0,200);
1463 if(flags
& MS_RDONLY
)
1464 strlcat(mountent
.mnt_opts
,"ro",220);
1466 strlcat(mountent
.mnt_opts
,"rw",220);
1467 if(flags
& MS_MANDLOCK
)
1468 strlcat(mountent
.mnt_opts
,",mand",220);
1469 if(flags
& MS_NOEXEC
)
1470 strlcat(mountent
.mnt_opts
,",noexec",220);
1471 if(flags
& MS_NOSUID
)
1472 strlcat(mountent
.mnt_opts
,",nosuid",220);
1473 if(flags
& MS_NODEV
)
1474 strlcat(mountent
.mnt_opts
,",nodev",220);
1475 if(flags
& MS_SYNCHRONOUS
)
1476 strlcat(mountent
.mnt_opts
,",sync",220);
1479 strlcat(mountent
.mnt_opts
,
1481 strlcat(mountent
.mnt_opts
,
1486 mountent
.mnt_freq
= 0;
1487 mountent
.mnt_passno
= 0;
1488 rc
= addmntent(pmntfile
,&mountent
);
1489 endmntent(pmntfile
);
1491 SAFE_FREE(mountent
.mnt_opts
);
1496 int len
= strlen(mountpassword
);
1497 memset(mountpassword
,0,len
);
1498 SAFE_FREE(mountpassword
);
1502 freeaddrinfo(addrhead
);
1504 SAFE_FREE(orgoptions
);
1505 SAFE_FREE(resolved_path
);
1506 SAFE_FREE(share_name
);