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>
43 #define MOUNT_CIFS_VERSION_MAJOR "1"
44 #define MOUNT_CIFS_VERSION_MINOR "11"
46 #ifndef MOUNT_CIFS_VENDOR_SUFFIX
48 #include "include/version.h"
49 #ifdef SAMBA_VERSION_VENDOR_SUFFIX
50 #define MOUNT_CIFS_VENDOR_SUFFIX "-"SAMBA_VERSION_OFFICIAL_STRING"-"SAMBA_VERSION_VENDOR_SUFFIX
52 #define MOUNT_CIFS_VENDOR_SUFFIX "-"SAMBA_VERSION_OFFICIAL_STRING
53 #endif /* SAMBA_VERSION_OFFICIAL_STRING and SAMBA_VERSION_VENDOR_SUFFIX */
55 #define MOUNT_CIFS_VENDOR_SUFFIX ""
56 #endif /* _SAMBA_BUILD_ */
57 #endif /* MOUNT_CIFS_VENDOR_SUFFIX */
60 #include "include/config.h"
71 #define MAX_UNC_LEN 1024
73 #define CONST_DISCARD(type, ptr) ((type) ((void *) (ptr)))
76 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
79 #define MOUNT_PASSWD_SIZE 64
80 #define DOMAIN_SIZE 64
82 const char *thisprogram
;
84 static int got_password
= 0;
85 static int got_user
= 0;
86 static int got_domain
= 0;
87 static int got_ip
= 0;
88 static int got_unc
= 0;
89 static int got_uid
= 0;
90 static int got_gid
= 0;
91 static char * user_name
= NULL
;
92 static char * mountpassword
= NULL
;
93 char * domain_name
= NULL
;
94 char * prefixpath
= NULL
;
96 /* glibc doesn't have strlcpy, strlcat. Ensure we do. JRA. We
97 * don't link to libreplace so need them here. */
99 /* like strncpy but does not 0 fill the buffer and always null
100 * terminates. bufsize is the size of the destination buffer */
103 static size_t strlcpy(char *d
, const char *s
, size_t bufsize
)
105 size_t len
= strlen(s
);
107 if (bufsize
<= 0) return 0;
108 if (len
>= bufsize
) len
= bufsize
-1;
115 /* like strncat but does not 0 fill the buffer and always null
116 * terminates. bufsize is the length of the buffer, which should
117 * be one more than the maximum resulting string length */
120 static size_t strlcat(char *d
, const char *s
, size_t bufsize
)
122 size_t len1
= strlen(d
);
123 size_t len2
= strlen(s
);
124 size_t ret
= len1
+ len2
;
126 if (len1
+len2
>= bufsize
) {
127 if (bufsize
< (len1
+1)) {
130 len2
= bufsize
- (len1
+1);
133 memcpy(d
+len1
, s
, len2
);
143 open nofollow - avoid symlink exposure?
144 get owner of dir see if matches self or if root
145 call system(umount argv) etc.
149 static char * check_for_domain(char **);
152 static void mount_cifs_usage(void)
154 printf("\nUsage: %s <remotetarget> <dir> -o <options>\n", thisprogram
);
155 printf("\nMount the remote target, specified as a UNC name,");
156 printf(" to a local directory.\n\nOptions:\n");
157 printf("\tuser=<arg>\n\tpass=<arg>\n\tdom=<arg>\n");
158 printf("\nLess commonly used options:");
159 printf("\n\tcredentials=<filename>,guest,perm,noperm,setuids,nosetuids,rw,ro,");
160 printf("\n\tsep=<char>,iocharset=<codepage>,suid,nosuid,exec,noexec,serverino,");
161 printf("\n\tmapchars,nomapchars,nolock,servernetbiosname=<SRV_RFC1001NAME>");
162 printf("\n\tdirectio,nounix,cifsacl,sec=<authentication mechanism>,sign");
163 printf("\n\nOptions not needed for servers supporting CIFS Unix extensions");
164 printf("\n\t(e.g. unneeded for mounts to most Samba versions):");
165 printf("\n\tuid=<uid>,gid=<gid>,dir_mode=<mode>,file_mode=<mode>,sfu");
166 printf("\n\nRarely used options:");
167 printf("\n\tport=<tcpport>,rsize=<size>,wsize=<size>,unc=<unc_name>,ip=<ip_address>,");
168 printf("\n\tdev,nodev,nouser_xattr,netbiosname=<OUR_RFC1001NAME>,hard,soft,intr,");
169 printf("\n\tnointr,ignorecase,noposixpaths,noacl,prefixpath=<path>,nobrl");
170 printf("\n\tin6_addr");
171 printf("\n\nOptions are described in more detail in the manual page");
172 printf("\n\tman 8 mount.cifs\n");
173 printf("\nTo display the version number of the mount helper:");
174 printf("\n\t%s -V\n",thisprogram
);
176 SAFE_FREE(mountpassword
);
180 /* caller frees username if necessary */
181 static char * getusername(void) {
182 char *username
= NULL
;
183 struct passwd
*password
= getpwuid(getuid());
186 username
= password
->pw_name
;
191 static char * parse_cifs_url(char * unc_name
)
193 printf("\nMounting cifs URL not implemented yet. Attempt to mount %s\n",unc_name
);
197 static int open_cred_file(char * file_name
)
203 fs
= fopen(file_name
,"r");
206 line_buf
= (char *)malloc(4096);
207 if(line_buf
== NULL
) {
212 while(fgets(line_buf
,4096,fs
)) {
213 /* parse line from credential file */
215 /* eat leading white space */
216 for(i
=0;i
<4086;i
++) {
217 if((line_buf
[i
] != ' ') && (line_buf
[i
] != '\t'))
219 /* if whitespace - skip past it */
221 if (strncasecmp("username",line_buf
+i
,8) == 0) {
222 temp_val
= strchr(line_buf
+ i
,'=');
224 /* go past equals sign */
226 for(length
= 0;length
<4087;length
++) {
227 if ((temp_val
[length
] == '\n')
228 || (temp_val
[length
] == '\0')) {
229 temp_val
[length
] = '\0';
234 printf("mount.cifs failed due to malformed username in credentials file");
235 memset(line_buf
,0,4096);
239 user_name
= (char *)calloc(1 + length
,1);
240 /* BB adding free of user_name string before exit,
241 not really necessary but would be cleaner */
242 strlcpy(user_name
,temp_val
, length
+1);
245 } else if (strncasecmp("password",line_buf
+i
,8) == 0) {
246 temp_val
= strchr(line_buf
+i
,'=');
248 /* go past equals sign */
250 for(length
= 0;length
<MOUNT_PASSWD_SIZE
+1;length
++) {
251 if ((temp_val
[length
] == '\n')
252 || (temp_val
[length
] == '\0')) {
253 temp_val
[length
] = '\0';
257 if(length
> MOUNT_PASSWD_SIZE
) {
258 printf("mount.cifs failed: password in credentials file too long\n");
259 memset(line_buf
,0, 4096);
262 if(mountpassword
== NULL
) {
263 mountpassword
= (char *)calloc(MOUNT_PASSWD_SIZE
+1,1);
265 memset(mountpassword
,0,MOUNT_PASSWD_SIZE
);
267 strlcpy(mountpassword
,temp_val
,MOUNT_PASSWD_SIZE
+1);
272 } else if (strncasecmp("domain",line_buf
+i
,6) == 0) {
273 temp_val
= strchr(line_buf
+i
,'=');
275 /* go past equals sign */
278 printf("\nDomain %s\n",temp_val
);
279 for(length
= 0;length
<DOMAIN_SIZE
+1;length
++) {
280 if ((temp_val
[length
] == '\n')
281 || (temp_val
[length
] == '\0')) {
282 temp_val
[length
] = '\0';
286 if(length
> DOMAIN_SIZE
) {
287 printf("mount.cifs failed: domain in credentials file too long\n");
290 if(domain_name
== NULL
) {
291 domain_name
= (char *)calloc(DOMAIN_SIZE
+1,1);
293 memset(domain_name
,0,DOMAIN_SIZE
);
295 strlcpy(domain_name
,temp_val
,DOMAIN_SIZE
+1);
308 static int get_password_from_file(int file_descript
, char * filename
)
314 if(mountpassword
== NULL
)
315 mountpassword
= (char *)calloc(MOUNT_PASSWD_SIZE
+1,1);
317 memset(mountpassword
, 0, MOUNT_PASSWD_SIZE
);
319 if (mountpassword
== NULL
) {
320 printf("malloc failed\n");
324 if(filename
!= NULL
) {
325 file_descript
= open(filename
, O_RDONLY
);
326 if(file_descript
< 0) {
327 printf("mount.cifs failed. %s attempting to open password file %s\n",
328 strerror(errno
),filename
);
332 /* else file already open and fd provided */
334 for(i
=0;i
<MOUNT_PASSWD_SIZE
;i
++) {
335 rc
= read(file_descript
,&c
,1);
337 printf("mount.cifs failed. Error %s reading password file\n",strerror(errno
));
339 close(file_descript
);
342 if(mountpassword
[0] == 0) {
344 printf("\nWarning: null password used since cifs password file empty");
347 } else /* read valid character */ {
348 if((c
== 0) || (c
== '\n')) {
349 mountpassword
[i
] = '\0';
352 mountpassword
[i
] = c
;
355 if((i
== MOUNT_PASSWD_SIZE
) && (verboseflag
)) {
356 printf("\nWarning: password longer than %d characters specified in cifs password file",
360 if(filename
!= NULL
) {
361 close(file_descript
);
367 static int parse_options(char ** optionsp
, int * filesys_flags
)
370 char * percent_char
= NULL
;
372 char * next_keyword
= NULL
;
380 if (!optionsp
|| !*optionsp
)
385 printf("parsing options: %s\n", data
);
387 /* BB fixme check for separator override BB */
391 snprintf(user
,sizeof(user
),"%u",getuid());
393 snprintf(group
,sizeof(group
),"%u",getgid());
396 /* while ((data = strsep(&options, ",")) != NULL) { */
397 while(data
!= NULL
) {
398 /* check if ends with trailing comma */
402 /* format is keyword=value,keyword2=value2,keyword3=value3 etc.) */
403 /* data = next keyword */
404 /* value = next value ie stuff after equal sign */
406 next_keyword
= strchr(data
,','); /* BB handle sep= */
408 /* temporarily null terminate end of keyword=value pair */
412 /* temporarily null terminate keyword to make keyword and value distinct */
413 if ((value
= strchr(data
, '=')) != NULL
) {
418 if (strncmp(data
, "users",5) == 0) {
419 if(!value
|| !*value
) {
422 } else if (strncmp(data
, "user_xattr",10) == 0) {
423 /* do nothing - need to skip so not parsed as user name */
424 } else if (strncmp(data
, "user", 4) == 0) {
426 if (!value
|| !*value
) {
427 if(data
[4] == '\0') {
429 printf("\nskipping empty user mount parameter\n");
430 /* remove the parm since it would otherwise be confusing
431 to the kernel code which would think it was a real username */
434 printf("username specified with no parameter\n");
435 return 1; /* needs_arg; */
438 if (strnlen(value
, 260) < 260) {
440 percent_char
= strchr(value
,'%');
443 if(mountpassword
== NULL
)
444 mountpassword
= (char *)calloc(MOUNT_PASSWD_SIZE
+1,1);
447 printf("\nmount.cifs warning - password specified twice\n");
450 strlcpy(mountpassword
, percent_char
,MOUNT_PASSWD_SIZE
+1);
451 /* remove password from username */
452 while(*percent_char
!= 0) {
458 /* this is only case in which the user
459 name buf is not malloc - so we have to
460 check for domain name embedded within
461 the user name here since the later
462 call to check_for_domain will not be
464 domain_name
= check_for_domain(&value
);
466 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");
484 } else if (strncmp(data
, "sec", 3) == 0) {
486 if (!strncmp(value
, "none", 4) ||
487 !strncmp(value
, "krb5", 4))
490 } else if (strncmp(data
, "ip", 2) == 0) {
491 if (!value
|| !*value
) {
492 printf("target ip address argument missing");
493 } else if (strnlen(value
, 35) < 35) {
495 printf("ip address %s override specified\n",value
);
498 printf("ip address too long\n");
501 } else if ((strncmp(data
, "unc", 3) == 0)
502 || (strncmp(data
, "target", 6) == 0)
503 || (strncmp(data
, "path", 4) == 0)) {
504 if (!value
|| !*value
) {
505 printf("invalid path to network resource\n");
506 return 1; /* needs_arg; */
507 } else if(strnlen(value
,5) < 5) {
508 printf("UNC name too short");
511 if (strnlen(value
, 300) < 300) {
513 if (strncmp(value
, "//", 2) == 0) {
515 printf("unc name specified twice, ignoring second\n");
518 } else if (strncmp(value
, "\\\\", 2) != 0) {
519 printf("UNC Path does not begin with // or \\\\ \n");
523 printf("unc name specified twice, ignoring second\n");
528 printf("CIFS: UNC name too long\n");
531 } else if ((strncmp(data
, "dom" /* domain */, 3) == 0)
532 || (strncmp(data
, "workg", 5) == 0)) {
533 /* note this allows for synonyms of "domain"
534 such as "DOM" and "dom" and "workgroup"
535 and "WORKGRP" etc. */
536 if (!value
|| !*value
) {
537 printf("CIFS: invalid domain name\n");
538 return 1; /* needs_arg; */
540 if (strnlen(value
, DOMAIN_SIZE
+1) < DOMAIN_SIZE
+1) {
543 printf("domain name too long\n");
546 } else if (strncmp(data
, "cred", 4) == 0) {
547 if (value
&& *value
) {
548 rc
= open_cred_file(value
);
550 printf("error %d (%s) opening credential file %s\n",
551 rc
, strerror(rc
), value
);
555 printf("invalid credential file name specified\n");
558 } else if (strncmp(data
, "uid", 3) == 0) {
559 if (value
&& *value
) {
561 if (!isdigit(*value
)) {
564 if (!(pw
= getpwnam(value
))) {
565 printf("bad user name \"%s\"\n", value
);
568 snprintf(user
, sizeof(user
), "%u", pw
->pw_uid
);
570 strlcpy(user
,value
,sizeof(user
));
574 } else if (strncmp(data
, "gid", 3) == 0) {
575 if (value
&& *value
) {
577 if (!isdigit(*value
)) {
580 if (!(gr
= getgrnam(value
))) {
581 printf("bad group name \"%s\"\n", value
);
584 snprintf(group
, sizeof(group
), "%u", gr
->gr_gid
);
586 strlcpy(group
,value
,sizeof(group
));
590 /* fmask and dmask synonyms for people used to smbfs syntax */
591 } else if (strcmp(data
, "file_mode") == 0 || strcmp(data
, "fmask")==0) {
592 if (!value
|| !*value
) {
593 printf ("Option '%s' requires a numerical argument\n", data
);
597 if (value
[0] != '0') {
598 printf ("WARNING: '%s' not expressed in octal.\n", data
);
601 if (strcmp (data
, "fmask") == 0) {
602 printf ("WARNING: CIFS mount option 'fmask' is deprecated. Use 'file_mode' instead.\n");
603 data
= "file_mode"; /* BB fix this */
605 } else if (strcmp(data
, "dir_mode") == 0 || strcmp(data
, "dmask")==0) {
606 if (!value
|| !*value
) {
607 printf ("Option '%s' requires a numerical argument\n", data
);
611 if (value
[0] != '0') {
612 printf ("WARNING: '%s' not expressed in octal.\n", data
);
615 if (strcmp (data
, "dmask") == 0) {
616 printf ("WARNING: CIFS mount option 'dmask' is deprecated. Use 'dir_mode' instead.\n");
619 /* the following eight mount options should be
620 stripped out from what is passed into the kernel
621 since these eight options are best passed as the
622 mount flags rather than redundantly to the kernel
623 and could generate spurious warnings depending on the
624 level of the corresponding cifs vfs kernel code */
625 } else if (strncmp(data
, "nosuid", 6) == 0) {
626 *filesys_flags
|= MS_NOSUID
;
627 } else if (strncmp(data
, "suid", 4) == 0) {
628 *filesys_flags
&= ~MS_NOSUID
;
629 } else if (strncmp(data
, "nodev", 5) == 0) {
630 *filesys_flags
|= MS_NODEV
;
631 } else if ((strncmp(data
, "nobrl", 5) == 0) ||
632 (strncmp(data
, "nolock", 6) == 0)) {
633 *filesys_flags
&= ~MS_MANDLOCK
;
634 } else if (strncmp(data
, "dev", 3) == 0) {
635 *filesys_flags
&= ~MS_NODEV
;
636 } else if (strncmp(data
, "noexec", 6) == 0) {
637 *filesys_flags
|= MS_NOEXEC
;
638 } else if (strncmp(data
, "exec", 4) == 0) {
639 *filesys_flags
&= ~MS_NOEXEC
;
640 } else if (strncmp(data
, "guest", 5) == 0) {
642 } else if (strncmp(data
, "ro", 2) == 0) {
643 *filesys_flags
|= MS_RDONLY
;
644 } else if (strncmp(data
, "rw", 2) == 0) {
645 *filesys_flags
&= ~MS_RDONLY
;
646 } else if (strncmp(data
, "remount", 7) == 0) {
647 *filesys_flags
|= MS_REMOUNT
;
648 } /* else if (strnicmp(data, "port", 4) == 0) {
649 if (value && *value) {
651 simple_strtoul(value, &value, 0);
653 } else if (strnicmp(data, "rsize", 5) == 0) {
654 if (value && *value) {
656 simple_strtoul(value, &value, 0);
658 } else if (strnicmp(data, "wsize", 5) == 0) {
659 if (value && *value) {
661 simple_strtoul(value, &value, 0);
663 } else if (strnicmp(data, "version", 3) == 0) {
665 printf("CIFS: Unknown mount option %s\n",data);
666 } */ /* nothing to do on those four mount options above.
667 Just pass to kernel and ignore them here */
669 /* Copy (possibly modified) option to out */
670 word_len
= strlen(data
);
672 word_len
+= 1 + strlen(value
);
674 out
= (char *)realloc(out
, out_len
+ word_len
+ 2);
681 strlcat(out
, ",", out_len
+ word_len
+ 2);
686 snprintf(out
+ out_len
, word_len
+ 1, "%s=%s", data
, value
);
688 snprintf(out
+ out_len
, word_len
+ 1, "%s", data
);
689 out_len
= strlen(out
);
695 /* special-case the uid and gid */
697 word_len
= strlen(user
);
699 out
= (char *)realloc(out
, out_len
+ word_len
+ 6);
706 strlcat(out
, ",", out_len
+ word_len
+ 6);
709 snprintf(out
+ out_len
, word_len
+ 5, "uid=%s", user
);
710 out_len
= strlen(out
);
713 word_len
= strlen(group
);
715 out
= (char *)realloc(out
, out_len
+ 1 + word_len
+ 6);
722 strlcat(out
, ",", out_len
+ word_len
+ 6);
725 snprintf(out
+ out_len
, word_len
+ 5, "gid=%s", group
);
726 out_len
= strlen(out
);
729 SAFE_FREE(*optionsp
);
734 /* replace all (one or more) commas with double commas */
735 static void check_for_comma(char ** ppasswrd
)
740 int number_of_commas
= 0;
755 if(number_of_commas
== 0)
757 if(number_of_commas
> MOUNT_PASSWD_SIZE
) {
758 /* would otherwise overflow the mount options buffer */
759 printf("\nInvalid password. Password contains too many commas.\n");
763 new_pass_buf
= (char *)malloc(len
+number_of_commas
+1);
764 if(new_pass_buf
== NULL
)
767 for(i
=0,j
=0;i
<len
;i
++,j
++) {
768 new_pass_buf
[j
] = pass
[i
];
771 new_pass_buf
[j
] = pass
[i
];
774 new_pass_buf
[len
+number_of_commas
] = 0;
776 SAFE_FREE(*ppasswrd
);
777 *ppasswrd
= new_pass_buf
;
782 /* Usernames can not have backslash in them and we use
783 [BB check if usernames can have forward slash in them BB]
784 backslash as domain\user separator character
786 static char * check_for_domain(char **ppuser
)
788 char * original_string
;
798 original_string
= *ppuser
;
800 if (original_string
== NULL
)
803 original_len
= strlen(original_string
);
805 usernm
= strchr(*ppuser
,'/');
806 if (usernm
== NULL
) {
807 usernm
= strchr(*ppuser
,'\\');
813 printf("Domain name specified twice. Username probably malformed\n");
819 if (domainnm
[0] != 0) {
822 printf("null domain\n");
824 len
= strlen(domainnm
);
825 /* reset domainm to new buffer, and copy
826 domain name into it */
827 domainnm
= (char *)malloc(len
+1);
831 strlcpy(domainnm
,*ppuser
,len
+1);
833 /* move_string(*ppuser, usernm+1) */
834 len
= strlen(usernm
+1);
836 if(len
>= original_len
) {
837 /* should not happen */
841 for(i
=0;i
<original_len
;i
++) {
843 original_string
[i
] = usernm
[i
+1];
844 else /* stuff with commas to remove last parm */
845 original_string
[i
] = ',';
848 /* BB add check for more than one slash?
856 /* replace all occurances of "from" in a string with "to" */
857 static void replace_char(char *string
, char from
, char to
, int maxlen
)
859 char *lastchar
= string
+ maxlen
;
861 string
= strchr(string
, from
);
864 if (string
>= lastchar
)
870 /* Note that caller frees the returned buffer if necessary */
871 static char * parse_server(char ** punc_name
)
873 char * unc_name
= *punc_name
;
874 int length
= strnlen(unc_name
, MAX_UNC_LEN
);
876 char * ipaddress_string
= NULL
;
877 struct hostent
* host_entry
= NULL
;
878 struct in_addr server_ipaddr
;
880 if(length
> (MAX_UNC_LEN
- 1)) {
881 printf("mount error: UNC name too long");
884 if (strncasecmp("cifs://",unc_name
,7) == 0)
885 return parse_cifs_url(unc_name
+7);
886 if (strncasecmp("smb://",unc_name
,6) == 0) {
887 return parse_cifs_url(unc_name
+6);
891 /* BB add code to find DFS root here */
892 printf("\nMounting the DFS root for domain not implemented yet\n");
895 if(strncmp(unc_name
,"//",2) && strncmp(unc_name
,"\\\\",2)) {
896 /* check for nfs syntax ie server:share */
897 share
= strchr(unc_name
,':');
899 *punc_name
= (char *)malloc(length
+3);
900 if(*punc_name
== NULL
) {
901 /* put the original string back if
903 *punc_name
= unc_name
;
907 strlcpy((*punc_name
)+2,unc_name
,length
+1);
909 unc_name
= *punc_name
;
910 unc_name
[length
+2] = 0;
911 goto continue_unc_parsing
;
913 printf("mount error: improperly formatted UNC name.");
914 printf(" %s does not begin with \\\\ or //\n",unc_name
);
918 continue_unc_parsing
:
923 /* allow for either delimiter between host and sharename */
924 if ((share
= strpbrk(unc_name
, "/\\"))) {
925 *share
= 0; /* temporarily terminate the string */
928 host_entry
= gethostbyname(unc_name
);
930 *(share
- 1) = '/'; /* put delimiter back */
932 /* we don't convert the prefixpath delimiters since '\\' is a valid char in posix paths */
933 if ((prefixpath
= strpbrk(share
, "/\\"))) {
934 *prefixpath
= 0; /* permanently terminate the string */
935 if (!strlen(++prefixpath
))
936 prefixpath
= NULL
; /* this needs to be done explicitly */
940 printf("ip address specified explicitly\n");
943 if(host_entry
== NULL
) {
944 printf("mount error: could not find target server. TCP name %s not found\n", unc_name
);
947 /* BB should we pass an alternate version of the share name as Unicode */
948 /* BB what about ipv6? BB */
949 /* BB add retries with alternate servers in list */
951 memcpy(&server_ipaddr
.s_addr
, host_entry
->h_addr
, 4);
953 ipaddress_string
= inet_ntoa(server_ipaddr
);
954 if(ipaddress_string
== NULL
) {
955 printf("mount error: could not get valid ip address for target server\n");
958 return ipaddress_string
;
961 /* BB add code to find DFS root (send null path on get DFS Referral to specified server here */
962 printf("Mounting the DFS root for a particular server not implemented yet\n");
969 static struct option longopts
[] = {
970 { "all", 0, NULL
, 'a' },
971 { "help",0, NULL
, 'h' },
972 { "move",0, NULL
, 'm' },
973 { "bind",0, NULL
, 'b' },
974 { "read-only", 0, NULL
, 'r' },
975 { "ro", 0, NULL
, 'r' },
976 { "verbose", 0, NULL
, 'v' },
977 { "version", 0, NULL
, 'V' },
978 { "read-write", 0, NULL
, 'w' },
979 { "rw", 0, NULL
, 'w' },
980 { "options", 1, NULL
, 'o' },
981 { "type", 1, NULL
, 't' },
982 { "rsize",1, NULL
, 'R' },
983 { "wsize",1, NULL
, 'W' },
984 { "uid", 1, NULL
, '1'},
985 { "gid", 1, NULL
, '2'},
986 { "user",1,NULL
,'u'},
987 { "username",1,NULL
,'u'},
989 { "domain",1,NULL
,'d'},
990 { "password",1,NULL
,'p'},
991 { "pass",1,NULL
,'p'},
992 { "credentials",1,NULL
,'c'},
993 { "port",1,NULL
,'P'},
994 /* { "uuid",1,NULL,'U'}, */ /* BB unimplemented */
998 /* convert a string to uppercase. return false if the string
999 * wasn't ASCII or was a NULL ptr */
1001 uppercase_string(char *string
)
1007 /* check for unicode */
1008 if ((unsigned char) string
[0] & 0x80)
1010 *string
= toupper((unsigned char) *string
);
1017 int main(int argc
, char ** argv
)
1020 int flags
= MS_MANDLOCK
; /* no need to set legacy MS_MGC_VAL */
1021 char * orgoptions
= NULL
;
1022 char * share_name
= NULL
;
1023 char * ipaddr
= NULL
;
1025 char * mountpoint
= NULL
;
1026 char * options
= NULL
;
1027 char * resolved_path
= NULL
;
1038 size_t options_size
= 0;
1039 int retry
= 0; /* set when we have to retry mount with uppercase */
1040 struct stat statbuf
;
1041 struct utsname sysinfo
;
1042 struct mntent mountent
;
1045 /* setlocale(LC_ALL, "");
1046 bindtextdomain(PACKAGE, LOCALEDIR);
1047 textdomain(PACKAGE); */
1050 thisprogram
= argv
[0];
1056 if(thisprogram
== NULL
)
1057 thisprogram
= "mount.cifs";
1060 /* BB add workstation name and domain and pass down */
1062 /* #ifdef _GNU_SOURCE
1063 printf(" node: %s machine: %s sysname %s domain %s\n", sysinfo.nodename,sysinfo.machine,sysinfo.sysname,sysinfo.domainname);
1067 share_name
= strndup(argv
[1], MAX_UNC_LEN
);
1068 if (share_name
== NULL
) {
1069 fprintf(stderr
, "%s: %s", argv
[0], strerror(ENOMEM
));
1072 mountpoint
= argv
[2];
1078 /* add sharename in opts string as unc= parm */
1080 while ((c
= getopt_long (argc
, argv
, "afFhilL:no:O:rsSU:vVwt:",
1081 longopts
, NULL
)) != -1) {
1083 /* No code to do the following options yet */
1085 list_with_volumelabel = 1;
1088 volumelabel = optarg;
1095 case 'h': /* help */
1096 mount_cifs_usage ();
1106 "option 'b' (MS_BIND) not supported\n");
1114 "option 'm' (MS_MOVE) not supported\n");
1118 orgoptions
= strdup(optarg
);
1120 case 'r': /* mount readonly */
1130 printf ("mount.cifs version: %s.%s%s\n",
1131 MOUNT_CIFS_VERSION_MAJOR
,
1132 MOUNT_CIFS_VERSION_MINOR
,
1133 MOUNT_CIFS_VENDOR_SUFFIX
);
1136 flags
&= ~MS_RDONLY
;
1139 rsize
= atoi(optarg
) ;
1142 wsize
= atoi(optarg
);
1145 if (isdigit(*optarg
)) {
1148 uid
= strtoul(optarg
, &ep
, 10);
1150 printf("bad uid value \"%s\"\n", optarg
);
1156 if (!(pw
= getpwnam(optarg
))) {
1157 printf("bad user name \"%s\"\n", optarg
);
1165 if (isdigit(*optarg
)) {
1168 gid
= strtoul(optarg
, &ep
, 10);
1170 printf("bad gid value \"%s\"\n", optarg
);
1176 if (!(gr
= getgrnam(optarg
))) {
1177 printf("bad user name \"%s\"\n", optarg
);
1189 domain_name
= optarg
; /* BB fix this - currently ignored */
1193 if(mountpassword
== NULL
)
1194 mountpassword
= (char *)calloc(MOUNT_PASSWD_SIZE
+1,1);
1197 strlcpy(mountpassword
,optarg
,MOUNT_PASSWD_SIZE
+1);
1201 get_password_from_file(0 /* stdin */,NULL
);
1206 printf("unknown mount option %c\n",c
);
1212 if((argc
< 3) || (dev_name
== NULL
) || (mountpoint
== NULL
)) {
1217 if (getenv("PASSWD")) {
1218 if(mountpassword
== NULL
)
1219 mountpassword
= (char *)calloc(MOUNT_PASSWD_SIZE
+1,1);
1221 strlcpy(mountpassword
,getenv("PASSWD"),MOUNT_PASSWD_SIZE
+1);
1224 } else if (getenv("PASSWD_FD")) {
1225 get_password_from_file(atoi(getenv("PASSWD_FD")),NULL
);
1226 } else if (getenv("PASSWD_FILE")) {
1227 get_password_from_file(0, getenv("PASSWD_FILE"));
1230 if (orgoptions
&& parse_options(&orgoptions
, &flags
)) {
1234 ipaddr
= parse_server(&share_name
);
1235 if((ipaddr
== NULL
) && (got_ip
== 0)) {
1236 printf("No ip address specified and hostname not found\n");
1241 /* BB save off path and pop after mount returns? */
1242 resolved_path
= (char *)malloc(PATH_MAX
+1);
1244 /* Note that if we can not canonicalize the name, we get
1245 another chance to see if it is valid when we chdir to it */
1246 if (realpath(mountpoint
, resolved_path
)) {
1247 mountpoint
= resolved_path
;
1250 if(chdir(mountpoint
)) {
1251 printf("mount error: can not change directory into mount target %s\n",mountpoint
);
1256 if(stat (".", &statbuf
)) {
1257 printf("mount error: mount point %s does not exist\n",mountpoint
);
1262 if (S_ISDIR(statbuf
.st_mode
) == 0) {
1263 printf("mount error: mount point %s is not a directory\n",mountpoint
);
1268 if((getuid() != 0) && (geteuid() == 0)) {
1269 if((statbuf
.st_uid
== getuid()) && (S_IRWXU
== (statbuf
.st_mode
& S_IRWXU
))) {
1270 #ifndef CIFS_ALLOW_USR_SUID
1271 /* Do not allow user mounts to control suid flag
1272 for mount unless explicitly built that way */
1273 flags
|= MS_NOSUID
| MS_NODEV
;
1276 printf("mount error: permission denied or not superuser and mount.cifs not installed SUID\n");
1282 user_name
= getusername();
1286 if(got_password
== 0) {
1287 char *tmp_pass
= getpass("Password: "); /* BB obsolete sys call but
1288 no good replacement yet. */
1289 mountpassword
= (char *)calloc(MOUNT_PASSWD_SIZE
+1,1);
1290 if (!tmp_pass
|| !mountpassword
) {
1291 printf("Password not entered, exiting\n");
1294 strlcpy(mountpassword
, tmp_pass
, MOUNT_PASSWD_SIZE
+1);
1297 /* FIXME launch daemon (handles dfs name resolution and credential change)
1298 remember to clear parms and overwrite password field before launching */
1301 optlen
= strlen(orgoptions
);
1306 optlen
+= strlen(share_name
) + 4;
1308 printf("No server share name specified\n");
1309 printf("\nMounting the DFS root for server not implemented yet\n");
1313 optlen
+= strlen(user_name
) + 6;
1315 optlen
+= strlen(ipaddr
) + 4;
1317 optlen
+= strlen(mountpassword
) + 6;
1319 options_size
= optlen
+ 10 + DOMAIN_SIZE
;
1320 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 */);
1322 if(options
== NULL
) {
1323 printf("Could not allocate memory for mount options\n");
1328 strlcpy(options
,"unc=",options_size
);
1329 strlcat(options
,share_name
,options_size
);
1330 /* scan backwards and reverse direction of slash */
1331 temp
= strrchr(options
, '/');
1332 if(temp
> options
+ 6)
1335 strlcat(options
,",ip=",options_size
);
1336 strlcat(options
,ipaddr
,options_size
);
1340 /* check for syntax like user=domain\user */
1342 domain_name
= check_for_domain(&user_name
);
1343 strlcat(options
,",user=",options_size
);
1344 strlcat(options
,user_name
,options_size
);
1348 /* extra length accounted for in option string above */
1349 strlcat(options
,",domain=",options_size
);
1350 strlcat(options
,domain_name
,options_size
);
1354 /* Commas have to be doubled, or else they will
1355 look like the parameter separator */
1356 /* if(sep is not set)*/
1358 check_for_comma(&mountpassword
);
1359 strlcat(options
,",pass=",options_size
);
1360 strlcat(options
,mountpassword
,options_size
);
1363 strlcat(options
,",ver=",options_size
);
1364 strlcat(options
,MOUNT_CIFS_VERSION_MAJOR
,options_size
);
1367 strlcat(options
,",",options_size
);
1368 strlcat(options
,orgoptions
,options_size
);
1371 strlcat(options
,",prefixpath=",options_size
);
1372 strlcat(options
,prefixpath
,options_size
); /* no need to cat the / */
1375 printf("\nmount.cifs kernel mount options %s \n",options
);
1377 /* convert all '\\' to '/' in share portion so that /proc/mounts looks pretty */
1378 replace_char(dev_name
, '\\', '/', strlen(share_name
));
1380 if(mount(dev_name
, mountpoint
, "cifs", flags
, options
)) {
1381 /* remember to kill daemon on error */
1384 printf("mount failed but no error number set\n");
1387 printf("mount error: cifs filesystem not supported by the system\n");
1392 if (uppercase_string(dev_name
) &&
1393 uppercase_string(share_name
) &&
1394 uppercase_string(prefixpath
)) {
1395 printf("retrying with upper case share name\n");
1400 printf("mount error %d = %s\n",errno
,strerror(errno
));
1402 printf("Refer to the mount.cifs(8) manual page (e.g.man mount.cifs)\n");
1406 pmntfile
= setmntent(MOUNTED
, "a+");
1408 mountent
.mnt_fsname
= dev_name
;
1409 mountent
.mnt_dir
= mountpoint
;
1410 mountent
.mnt_type
= CONST_DISCARD(char *,"cifs");
1411 mountent
.mnt_opts
= (char *)malloc(220);
1412 if(mountent
.mnt_opts
) {
1413 char * mount_user
= getusername();
1414 memset(mountent
.mnt_opts
,0,200);
1415 if(flags
& MS_RDONLY
)
1416 strlcat(mountent
.mnt_opts
,"ro",220);
1418 strlcat(mountent
.mnt_opts
,"rw",220);
1419 if(flags
& MS_MANDLOCK
)
1420 strlcat(mountent
.mnt_opts
,",mand",220);
1421 if(flags
& MS_NOEXEC
)
1422 strlcat(mountent
.mnt_opts
,",noexec",220);
1423 if(flags
& MS_NOSUID
)
1424 strlcat(mountent
.mnt_opts
,",nosuid",220);
1425 if(flags
& MS_NODEV
)
1426 strlcat(mountent
.mnt_opts
,",nodev",220);
1427 if(flags
& MS_SYNCHRONOUS
)
1428 strlcat(mountent
.mnt_opts
,",synch",220);
1431 strlcat(mountent
.mnt_opts
,",user=",220);
1432 strlcat(mountent
.mnt_opts
,mount_user
,220);
1434 /* free(mount_user); do not free static mem */
1437 mountent
.mnt_freq
= 0;
1438 mountent
.mnt_passno
= 0;
1439 rc
= addmntent(pmntfile
,&mountent
);
1440 endmntent(pmntfile
);
1441 SAFE_FREE(mountent
.mnt_opts
);
1443 printf("could not update mount table\n");
1449 int len
= strlen(mountpassword
);
1450 memset(mountpassword
,0,len
);
1451 SAFE_FREE(mountpassword
);
1455 SAFE_FREE(orgoptions
);
1456 SAFE_FREE(resolved_path
);
1457 SAFE_FREE(share_name
);