2 Mount helper utility for Linux CIFS VFS (virtual filesystem) client
3 Copyright (C) 2003,2005 Steve French (sfrench@us.ibm.com)
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include <sys/types.h>
29 #include <sys/mount.h>
31 #include <sys/utsname.h>
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
42 #define MOUNT_CIFS_VERSION_MAJOR "1"
43 #define MOUNT_CIFS_VERSION_MINOR "11"
45 #ifndef MOUNT_CIFS_VENDOR_SUFFIX
47 #include "include/version.h"
48 #ifdef SAMBA_VERSION_VENDOR_SUFFIX
49 #define MOUNT_CIFS_VENDOR_SUFFIX "-"SAMBA_VERSION_OFFICIAL_STRING"-"SAMBA_VERSION_VENDOR_SUFFIX
51 #define MOUNT_CIFS_VENDOR_SUFFIX "-"SAMBA_VERSION_OFFICIAL_STRING
52 #endif /* SAMBA_VERSION_OFFICIAL_STRING and SAMBA_VERSION_VENDOR_SUFFIX */
54 #define MOUNT_CIFS_VENDOR_SUFFIX ""
55 #endif /* _SAMBA_BUILD_ */
56 #endif /* MOUNT_CIFS_VENDOR_SUFFIX */
66 #define MAX_UNC_LEN 1024
68 #define CONST_DISCARD(type, ptr) ((type) ((void *) (ptr)))
70 const char *thisprogram
;
72 static int got_password
= 0;
73 static int got_user
= 0;
74 static int got_domain
= 0;
75 static int got_ip
= 0;
76 static int got_unc
= 0;
77 static int got_uid
= 0;
78 static int got_gid
= 0;
79 static char * user_name
= NULL
;
80 static char * mountpassword
= NULL
;
81 char * domain_name
= NULL
;
82 char * prefixpath
= NULL
;
84 /* glibc doesn't have strlcpy, strlcat. Ensure we do. JRA. We
85 * don't link to libreplace so need them here. */
87 /* like strncpy but does not 0 fill the buffer and always null
88 * terminates. bufsize is the size of the destination buffer */
89 size_t strlcpy(char *d
, const char *s
, size_t bufsize
)
91 size_t len
= strlen(s
);
93 if (bufsize
<= 0) return 0;
94 if (len
>= bufsize
) len
= bufsize
-1;
100 /* like strncat but does not 0 fill the buffer and always null
101 * terminates. bufsize is the length of the buffer, which should
102 * be one more than the maximum resulting string length */
103 size_t strlcat(char *d
, const char *s
, size_t bufsize
)
105 size_t len1
= strlen(d
);
106 size_t len2
= strlen(s
);
107 size_t ret
= len1
+ len2
;
109 if (len1
+len2
>= bufsize
) {
110 len2
= bufsize
- (len1
+1);
113 memcpy(d
+len1
, s
, len2
);
122 open nofollow - avoid symlink exposure?
123 get owner of dir see if matches self or if root
124 call system(umount argv) etc.
128 static char * check_for_domain(char **);
131 static void mount_cifs_usage(void)
133 printf("\nUsage: %s <remotetarget> <dir> -o <options>\n", thisprogram
);
134 printf("\nMount the remote target, specified as a UNC name,");
135 printf(" to a local directory.\n\nOptions:\n");
136 printf("\tuser=<arg>\n\tpass=<arg>\n\tdom=<arg>\n");
137 printf("\nLess commonly used options:");
138 printf("\n\tcredentials=<filename>,guest,perm,noperm,setuids,nosetuids,rw,ro,");
139 printf("\n\tsep=<char>,iocharset=<codepage>,suid,nosuid,exec,noexec,serverino,");
140 printf("\n\tmapchars,nomapchars,nolock,servernetbiosname=<SRV_RFC1001NAME>");
141 printf("\n\tdirectio,nounix,cifsacl,sec=<authentication mechanism>,sign");
142 printf("\n\nOptions not needed for servers supporting CIFS Unix extensions");
143 printf("\n\t(e.g. unneeded for mounts to most Samba versions):");
144 printf("\n\tuid=<uid>,gid=<gid>,dir_mode=<mode>,file_mode=<mode>,sfu");
145 printf("\n\nRarely used options:");
146 printf("\n\tport=<tcpport>,rsize=<size>,wsize=<size>,unc=<unc_name>,ip=<ip_address>,");
147 printf("\n\tdev,nodev,nouser_xattr,netbiosname=<OUR_RFC1001NAME>,hard,soft,intr,");
148 printf("\n\tnointr,ignorecase,noposixpaths,noacl,prefixpath=<path>,nobrl");
149 printf("\n\tin6_addr");
150 printf("\n\nOptions are described in more detail in the manual page");
151 printf("\n\tman 8 mount.cifs\n");
152 printf("\nTo display the version number of the mount helper:");
153 printf("\n\t%s -V\n",thisprogram
);
156 memset(mountpassword
,0,64);
162 /* caller frees username if necessary */
163 static char * getusername(void) {
164 char *username
= NULL
;
165 struct passwd
*password
= getpwuid(getuid());
168 username
= password
->pw_name
;
173 static char * parse_cifs_url(char * unc_name
)
175 printf("\nMounting cifs URL not implemented yet. Attempt to mount %s\n",unc_name
);
179 static int open_cred_file(char * file_name
)
185 fs
= fopen(file_name
,"r");
188 line_buf
= (char *)malloc(4096);
189 if(line_buf
== NULL
) {
194 while(fgets(line_buf
,4096,fs
)) {
195 /* parse line from credential file */
197 /* eat leading white space */
198 for(i
=0;i
<4086;i
++) {
199 if((line_buf
[i
] != ' ') && (line_buf
[i
] != '\t'))
201 /* if whitespace - skip past it */
203 if (strncasecmp("username",line_buf
+i
,8) == 0) {
204 temp_val
= strchr(line_buf
+ i
,'=');
206 /* go past equals sign */
208 for(length
= 0;length
<4087;length
++) {
209 if ((temp_val
[length
] == '\n')
210 || (temp_val
[length
] == '\0')) {
215 printf("mount.cifs failed due to malformed username in credentials file");
216 memset(line_buf
,0,4096);
218 memset(mountpassword
,0,64);
223 user_name
= (char *)calloc(1 + length
,1);
224 /* BB adding free of user_name string before exit,
225 not really necessary but would be cleaner */
226 strncpy(user_name
,temp_val
, length
);
229 } else if (strncasecmp("password",line_buf
+i
,8) == 0) {
230 temp_val
= strchr(line_buf
+i
,'=');
232 /* go past equals sign */
234 for(length
= 0;length
<65;length
++) {
235 if ((temp_val
[length
] == '\n')
236 || (temp_val
[length
] == '\0')) {
241 printf("mount.cifs failed: password in credentials file too long\n");
242 memset(line_buf
,0, 4096);
244 memset(mountpassword
,0,64);
248 if(mountpassword
== NULL
) {
249 mountpassword
= (char *)calloc(65,1);
251 memset(mountpassword
,0,64);
253 strncpy(mountpassword
,temp_val
,length
);
258 } else if (strncasecmp("domain",line_buf
+i
,6) == 0) {
259 temp_val
= strchr(line_buf
+i
,'=');
261 /* go past equals sign */
264 printf("\nDomain %s\n",temp_val
);
265 for(length
= 0;length
<65;length
++) {
266 if ((temp_val
[length
] == '\n')
267 || (temp_val
[length
] == '\0')) {
272 printf("mount.cifs failed: domain in credentials file too long\n");
274 memset(mountpassword
,0,64);
278 if(domain_name
== NULL
) {
279 domain_name
= (char *)calloc(65,1);
281 memset(domain_name
,0,64);
283 strncpy(domain_name
,temp_val
,length
);
293 memset(line_buf
,0,4096);
299 static int get_password_from_file(int file_descript
, char * filename
)
305 if(mountpassword
== NULL
)
306 mountpassword
= (char *)calloc(65,1);
308 memset(mountpassword
, 0, 64);
310 if (mountpassword
== NULL
) {
311 printf("malloc failed\n");
315 if(filename
!= NULL
) {
316 file_descript
= open(filename
, O_RDONLY
);
317 if(file_descript
< 0) {
318 printf("mount.cifs failed. %s attempting to open password file %s\n",
319 strerror(errno
),filename
);
323 /* else file already open and fd provided */
326 rc
= read(file_descript
,&c
,1);
328 printf("mount.cifs failed. Error %s reading password file\n",strerror(errno
));
329 memset(mountpassword
,0,64);
331 close(file_descript
);
334 if(mountpassword
[0] == 0) {
336 printf("\nWarning: null password used since cifs password file empty");
339 } else /* read valid character */ {
340 if((c
== 0) || (c
== '\n')) {
343 mountpassword
[i
] = c
;
346 if((i
== 64) && (verboseflag
)) {
347 printf("\nWarning: password longer than 64 characters specified in cifs password file");
350 if(filename
!= NULL
) {
351 close(file_descript
);
357 static int parse_options(char ** optionsp
, int * filesys_flags
)
360 char * percent_char
= NULL
;
362 char * next_keyword
= NULL
;
370 if (!optionsp
|| !*optionsp
)
375 printf("parsing options: %s\n", data
);
377 /* BB fixme check for separator override BB */
381 snprintf(user
,sizeof(user
),"%u",getuid());
383 snprintf(group
,sizeof(group
),"%u",getgid());
386 /* while ((data = strsep(&options, ",")) != NULL) { */
387 while(data
!= NULL
) {
388 /* check if ends with trailing comma */
392 /* format is keyword=value,keyword2=value2,keyword3=value3 etc.) */
393 /* data = next keyword */
394 /* value = next value ie stuff after equal sign */
396 next_keyword
= strchr(data
,','); /* BB handle sep= */
398 /* temporarily null terminate end of keyword=value pair */
402 /* temporarily null terminate keyword to make keyword and value distinct */
403 if ((value
= strchr(data
, '=')) != NULL
) {
408 if (strncmp(data
, "users",5) == 0) {
409 if(!value
|| !*value
) {
412 } else if (strncmp(data
, "user_xattr",10) == 0) {
413 /* do nothing - need to skip so not parsed as user name */
414 } else if (strncmp(data
, "user", 4) == 0) {
416 if (!value
|| !*value
) {
417 if(data
[4] == '\0') {
419 printf("\nskipping empty user mount parameter\n");
420 /* remove the parm since it would otherwise be confusing
421 to the kernel code which would think it was a real username */
424 printf("username specified with no parameter\n");
425 return 1; /* needs_arg; */
428 if (strnlen(value
, 260) < 260) {
430 percent_char
= strchr(value
,'%');
433 if(mountpassword
== NULL
)
434 mountpassword
= (char *)calloc(65,1);
437 printf("\nmount.cifs warning - password specified twice\n");
440 strncpy(mountpassword
, percent_char
,64);
441 /* remove password from username */
442 while(*percent_char
!= 0) {
448 /* this is only case in which the user
449 name buf is not malloc - so we have to
450 check for domain name embedded within
451 the user name here since the later
452 call to check_for_domain will not be
454 domain_name
= check_for_domain(&value
);
456 printf("username too long\n");
460 } else if (strncmp(data
, "pass", 4) == 0) {
461 if (!value
|| !*value
) {
463 printf("\npassword specified twice, ignoring second\n");
466 } else if (strnlen(value
, 17) < 17) {
468 printf("\nmount.cifs warning - password specified twice\n");
471 printf("password too long\n");
474 } else if (strncmp(data
, "sec", 3) == 0) {
476 if (!strcmp(value
, "none"))
479 } else if (strncmp(data
, "ip", 2) == 0) {
480 if (!value
|| !*value
) {
481 printf("target ip address argument missing");
482 } else if (strnlen(value
, 35) < 35) {
484 printf("ip address %s override specified\n",value
);
487 printf("ip address too long\n");
490 } else if ((strncmp(data
, "unc", 3) == 0)
491 || (strncmp(data
, "target", 6) == 0)
492 || (strncmp(data
, "path", 4) == 0)) {
493 if (!value
|| !*value
) {
494 printf("invalid path to network resource\n");
495 return 1; /* needs_arg; */
496 } else if(strnlen(value
,5) < 5) {
497 printf("UNC name too short");
500 if (strnlen(value
, 300) < 300) {
502 if (strncmp(value
, "//", 2) == 0) {
504 printf("unc name specified twice, ignoring second\n");
507 } else if (strncmp(value
, "\\\\", 2) != 0) {
508 printf("UNC Path does not begin with // or \\\\ \n");
512 printf("unc name specified twice, ignoring second\n");
517 printf("CIFS: UNC name too long\n");
520 } else if ((strncmp(data
, "domain", 3) == 0)
521 || (strncmp(data
, "workgroup", 5) == 0)) {
522 if (!value
|| !*value
) {
523 printf("CIFS: invalid domain name\n");
524 return 1; /* needs_arg; */
526 if (strnlen(value
, 65) < 65) {
529 printf("domain name too long\n");
532 } else if (strncmp(data
, "cred", 4) == 0) {
533 if (value
&& *value
) {
534 rc
= open_cred_file(value
);
536 printf("error %d opening credential file %s\n",rc
, value
);
540 printf("invalid credential file name specified\n");
543 } else if (strncmp(data
, "uid", 3) == 0) {
544 if (value
&& *value
) {
546 if (!isdigit(*value
)) {
549 if (!(pw
= getpwnam(value
))) {
550 printf("bad user name \"%s\"\n", value
);
553 snprintf(user
, sizeof(user
), "%u", pw
->pw_uid
);
555 strlcpy(user
,value
,sizeof(user
));
559 } else if (strncmp(data
, "gid", 3) == 0) {
560 if (value
&& *value
) {
562 if (!isdigit(*value
)) {
565 if (!(gr
= getgrnam(value
))) {
566 printf("bad group name \"%s\"\n", value
);
569 snprintf(group
, sizeof(group
), "%u", gr
->gr_gid
);
571 strlcpy(group
,value
,sizeof(group
));
575 /* fmask and dmask synonyms for people used to smbfs syntax */
576 } else if (strcmp(data
, "file_mode") == 0 || strcmp(data
, "fmask")==0) {
577 if (!value
|| !*value
) {
578 printf ("Option '%s' requires a numerical argument\n", data
);
582 if (value
[0] != '0') {
583 printf ("WARNING: '%s' not expressed in octal.\n", data
);
586 if (strcmp (data
, "fmask") == 0) {
587 printf ("WARNING: CIFS mount option 'fmask' is deprecated. Use 'file_mode' instead.\n");
588 data
= "file_mode"; /* BB fix this */
590 } else if (strcmp(data
, "dir_mode") == 0 || strcmp(data
, "dmask")==0) {
591 if (!value
|| !*value
) {
592 printf ("Option '%s' requires a numerical argument\n", data
);
596 if (value
[0] != '0') {
597 printf ("WARNING: '%s' not expressed in octal.\n", data
);
600 if (strcmp (data
, "dmask") == 0) {
601 printf ("WARNING: CIFS mount option 'dmask' is deprecated. Use 'dir_mode' instead.\n");
604 /* the following eight mount options should be
605 stripped out from what is passed into the kernel
606 since these eight options are best passed as the
607 mount flags rather than redundantly to the kernel
608 and could generate spurious warnings depending on the
609 level of the corresponding cifs vfs kernel code */
610 } else if (strncmp(data
, "nosuid", 6) == 0) {
611 *filesys_flags
|= MS_NOSUID
;
612 } else if (strncmp(data
, "suid", 4) == 0) {
613 *filesys_flags
&= ~MS_NOSUID
;
614 } else if (strncmp(data
, "nodev", 5) == 0) {
615 *filesys_flags
|= MS_NODEV
;
616 } else if ((strncmp(data
, "nobrl", 5) == 0) ||
617 (strncmp(data
, "nolock", 6) == 0)) {
618 *filesys_flags
&= ~MS_MANDLOCK
;
619 } else if (strncmp(data
, "dev", 3) == 0) {
620 *filesys_flags
&= ~MS_NODEV
;
621 } else if (strncmp(data
, "noexec", 6) == 0) {
622 *filesys_flags
|= MS_NOEXEC
;
623 } else if (strncmp(data
, "exec", 4) == 0) {
624 *filesys_flags
&= ~MS_NOEXEC
;
625 } else if (strncmp(data
, "guest", 5) == 0) {
627 } else if (strncmp(data
, "ro", 2) == 0) {
628 *filesys_flags
|= MS_RDONLY
;
629 } else if (strncmp(data
, "rw", 2) == 0) {
630 *filesys_flags
&= ~MS_RDONLY
;
631 } else if (strncmp(data
, "remount", 7) == 0) {
632 *filesys_flags
|= MS_REMOUNT
;
633 } /* else if (strnicmp(data, "port", 4) == 0) {
634 if (value && *value) {
636 simple_strtoul(value, &value, 0);
638 } else if (strnicmp(data, "rsize", 5) == 0) {
639 if (value && *value) {
641 simple_strtoul(value, &value, 0);
643 } else if (strnicmp(data, "wsize", 5) == 0) {
644 if (value && *value) {
646 simple_strtoul(value, &value, 0);
648 } else if (strnicmp(data, "version", 3) == 0) {
650 printf("CIFS: Unknown mount option %s\n",data);
651 } */ /* nothing to do on those four mount options above.
652 Just pass to kernel and ignore them here */
654 /* Copy (possibly modified) option to out */
655 word_len
= strlen(data
);
657 word_len
+= 1 + strlen(value
);
659 out
= (char *)realloc(out
, out_len
+ word_len
+ 2);
666 strlcat(out
, ",", out_len
+ word_len
+ 2);
671 snprintf(out
+ out_len
, word_len
+ 1, "%s=%s", data
, value
);
673 snprintf(out
+ out_len
, word_len
+ 1, "%s", data
);
674 out_len
= strlen(out
);
680 /* special-case the uid and gid */
682 word_len
= strlen(user
);
684 out
= (char *)realloc(out
, out_len
+ word_len
+ 6);
691 strlcat(out
, ",", out_len
+ word_len
+ 6);
694 snprintf(out
+ out_len
, word_len
+ 5, "uid=%s", user
);
695 out_len
= strlen(out
);
698 word_len
= strlen(group
);
700 out
= (char *)realloc(out
, out_len
+ 1 + word_len
+ 6);
707 strlcat(out
, ",", out_len
+ word_len
+ 6);
710 snprintf(out
+ out_len
, word_len
+ 5, "gid=%s", group
);
711 out_len
= strlen(out
);
719 /* replace all (one or more) commas with double commas */
720 static void check_for_comma(char ** ppasswrd
)
725 int number_of_commas
= 0;
740 if(number_of_commas
== 0)
742 if(number_of_commas
> 64) {
743 /* would otherwise overflow the mount options buffer */
744 printf("\nInvalid password. Password contains too many commas.\n");
748 new_pass_buf
= (char *)malloc(len
+number_of_commas
+1);
749 if(new_pass_buf
== NULL
)
752 for(i
=0,j
=0;i
<len
;i
++,j
++) {
753 new_pass_buf
[j
] = pass
[i
];
756 new_pass_buf
[j
] = pass
[i
];
759 new_pass_buf
[len
+number_of_commas
] = 0;
762 *ppasswrd
= new_pass_buf
;
767 /* Usernames can not have backslash in them and we use
768 [BB check if usernames can have forward slash in them BB]
769 backslash as domain\user separator character
771 static char * check_for_domain(char **ppuser
)
773 char * original_string
;
783 original_string
= *ppuser
;
785 if (original_string
== NULL
)
788 original_len
= strlen(original_string
);
790 usernm
= strchr(*ppuser
,'/');
791 if (usernm
== NULL
) {
792 usernm
= strchr(*ppuser
,'\\');
798 printf("Domain name specified twice. Username probably malformed\n");
804 if (domainnm
[0] != 0) {
807 printf("null domain\n");
809 len
= strlen(domainnm
);
810 /* reset domainm to new buffer, and copy
811 domain name into it */
812 domainnm
= (char *)malloc(len
+1);
816 strlcpy(domainnm
,*ppuser
,len
+1);
818 /* move_string(*ppuser, usernm+1) */
819 len
= strlen(usernm
+1);
821 if(len
>= original_len
) {
822 /* should not happen */
826 for(i
=0;i
<original_len
;i
++) {
828 original_string
[i
] = usernm
[i
+1];
829 else /* stuff with commas to remove last parm */
830 original_string
[i
] = ',';
833 /* BB add check for more than one slash?
841 /* replace all occurances of "from" in a string with "to" */
842 static void replace_char(char *string
, char from
, char to
, int maxlen
)
844 char *lastchar
= string
+ maxlen
;
846 string
= strchr(string
, from
);
849 if (string
>= lastchar
)
855 /* Note that caller frees the returned buffer if necessary */
856 static char * parse_server(char ** punc_name
)
858 char * unc_name
= *punc_name
;
859 int length
= strnlen(unc_name
, MAX_UNC_LEN
);
861 char * ipaddress_string
= NULL
;
862 struct hostent
* host_entry
= NULL
;
863 struct in_addr server_ipaddr
;
865 if(length
> (MAX_UNC_LEN
- 1)) {
866 printf("mount error: UNC name too long");
869 if (strncasecmp("cifs://",unc_name
,7) == 0)
870 return parse_cifs_url(unc_name
+7);
871 if (strncasecmp("smb://",unc_name
,6) == 0) {
872 return parse_cifs_url(unc_name
+6);
876 /* BB add code to find DFS root here */
877 printf("\nMounting the DFS root for domain not implemented yet\n");
880 if(strncmp(unc_name
,"//",2) && strncmp(unc_name
,"\\\\",2)) {
881 /* check for nfs syntax ie server:share */
882 share
= strchr(unc_name
,':');
884 *punc_name
= (char *)malloc(length
+3);
885 if(*punc_name
== NULL
) {
886 /* put the original string back if
888 *punc_name
= unc_name
;
892 strncpy((*punc_name
)+2,unc_name
,length
);
894 unc_name
= *punc_name
;
895 unc_name
[length
+2] = 0;
896 goto continue_unc_parsing
;
898 printf("mount error: improperly formatted UNC name.");
899 printf(" %s does not begin with \\\\ or //\n",unc_name
);
903 continue_unc_parsing
:
908 /* allow for either delimiter between host and sharename */
909 if ((share
= strpbrk(unc_name
, "/\\"))) {
910 *share
= 0; /* temporarily terminate the string */
913 host_entry
= gethostbyname(unc_name
);
915 *(share
- 1) = '/'; /* put delimiter back */
917 /* we don't convert the prefixpath delimiters since '\\' is a valid char in posix paths */
918 if ((prefixpath
= strpbrk(share
, "/\\"))) {
919 *prefixpath
= 0; /* permanently terminate the string */
920 if (!strlen(++prefixpath
))
921 prefixpath
= NULL
; /* this needs to be done explicitly */
925 printf("ip address specified explicitly\n");
928 if(host_entry
== NULL
) {
929 printf("mount error: could not find target server. TCP name %s not found\n", unc_name
);
932 /* BB should we pass an alternate version of the share name as Unicode */
933 /* BB what about ipv6? BB */
934 /* BB add retries with alternate servers in list */
936 memcpy(&server_ipaddr
.s_addr
, host_entry
->h_addr
, 4);
938 ipaddress_string
= inet_ntoa(server_ipaddr
);
939 if(ipaddress_string
== NULL
) {
940 printf("mount error: could not get valid ip address for target server\n");
943 return ipaddress_string
;
946 /* BB add code to find DFS root (send null path on get DFS Referral to specified server here */
947 printf("Mounting the DFS root for a particular server not implemented yet\n");
954 static struct option longopts
[] = {
955 { "all", 0, NULL
, 'a' },
956 { "help",0, NULL
, 'h' },
957 { "move",0, NULL
, 'm' },
958 { "bind",0, NULL
, 'b' },
959 { "read-only", 0, NULL
, 'r' },
960 { "ro", 0, NULL
, 'r' },
961 { "verbose", 0, NULL
, 'v' },
962 { "version", 0, NULL
, 'V' },
963 { "read-write", 0, NULL
, 'w' },
964 { "rw", 0, NULL
, 'w' },
965 { "options", 1, NULL
, 'o' },
966 { "type", 1, NULL
, 't' },
967 { "rsize",1, NULL
, 'R' },
968 { "wsize",1, NULL
, 'W' },
969 { "uid", 1, NULL
, '1'},
970 { "gid", 1, NULL
, '2'},
971 { "user",1,NULL
,'u'},
972 { "username",1,NULL
,'u'},
974 { "domain",1,NULL
,'d'},
975 { "password",1,NULL
,'p'},
976 { "pass",1,NULL
,'p'},
977 { "credentials",1,NULL
,'c'},
978 { "port",1,NULL
,'P'},
979 /* { "uuid",1,NULL,'U'}, */ /* BB unimplemented */
983 /* convert a string to uppercase. return false if the string
984 * wasn't ASCII or was a NULL ptr */
986 uppercase_string(char *string
)
992 /* check for unicode */
993 if ((unsigned char) string
[0] & 0x80)
995 *string
= toupper((unsigned char) *string
);
1002 int main(int argc
, char ** argv
)
1005 int flags
= MS_MANDLOCK
; /* no need to set legacy MS_MGC_VAL */
1006 char * orgoptions
= NULL
;
1007 char * share_name
= NULL
;
1008 char * ipaddr
= NULL
;
1010 char * mountpoint
= NULL
;
1011 char * options
= NULL
;
1012 char * resolved_path
= NULL
;
1023 size_t options_size
= 0;
1024 int retry
= 0; /* set when we have to retry mount with uppercase */
1025 struct stat statbuf
;
1026 struct utsname sysinfo
;
1027 struct mntent mountent
;
1030 /* setlocale(LC_ALL, "");
1031 bindtextdomain(PACKAGE, LOCALEDIR);
1032 textdomain(PACKAGE); */
1035 thisprogram
= argv
[0];
1041 if(thisprogram
== NULL
)
1042 thisprogram
= "mount.cifs";
1045 /* BB add workstation name and domain and pass down */
1047 /* #ifdef _GNU_SOURCE
1048 printf(" node: %s machine: %s sysname %s domain %s\n", sysinfo.nodename,sysinfo.machine,sysinfo.sysname,sysinfo.domainname);
1052 share_name
= strndup(argv
[1], MAX_UNC_LEN
);
1053 if (share_name
== NULL
) {
1054 fprintf(stderr
, "%s: %s", argv
[0], strerror(ENOMEM
));
1057 mountpoint
= argv
[2];
1063 /* add sharename in opts string as unc= parm */
1065 while ((c
= getopt_long (argc
, argv
, "afFhilL:no:O:rsSU:vVwt:",
1066 longopts
, NULL
)) != -1) {
1068 /* No code to do the following options yet */
1070 list_with_volumelabel = 1;
1073 volumelabel = optarg;
1080 case 'h': /* help */
1081 mount_cifs_usage ();
1091 "option 'b' (MS_BIND) not supported\n");
1099 "option 'm' (MS_MOVE) not supported\n");
1103 orgoptions
= strdup(optarg
);
1105 case 'r': /* mount readonly */
1115 printf ("mount.cifs version: %s.%s%s\n",
1116 MOUNT_CIFS_VERSION_MAJOR
,
1117 MOUNT_CIFS_VERSION_MINOR
,
1118 MOUNT_CIFS_VENDOR_SUFFIX
);
1120 memset(mountpassword
,0,64);
1124 flags
&= ~MS_RDONLY
;
1127 rsize
= atoi(optarg
) ;
1130 wsize
= atoi(optarg
);
1133 if (isdigit(*optarg
)) {
1136 uid
= strtoul(optarg
, &ep
, 10);
1138 printf("bad uid value \"%s\"\n", optarg
);
1144 if (!(pw
= getpwnam(optarg
))) {
1145 printf("bad user name \"%s\"\n", optarg
);
1153 if (isdigit(*optarg
)) {
1156 gid
= strtoul(optarg
, &ep
, 10);
1158 printf("bad gid value \"%s\"\n", optarg
);
1164 if (!(gr
= getgrnam(optarg
))) {
1165 printf("bad user name \"%s\"\n", optarg
);
1177 domain_name
= optarg
; /* BB fix this - currently ignored */
1181 if(mountpassword
== NULL
)
1182 mountpassword
= (char *)calloc(65,1);
1185 strncpy(mountpassword
,optarg
,64);
1189 get_password_from_file(0 /* stdin */,NULL
);
1194 printf("unknown mount option %c\n",c
);
1200 if((argc
< 3) || (dev_name
== NULL
) || (mountpoint
== NULL
)) {
1205 if (getenv("PASSWD")) {
1206 if(mountpassword
== NULL
)
1207 mountpassword
= (char *)calloc(65,1);
1209 strncpy(mountpassword
,getenv("PASSWD"),64);
1212 } else if (getenv("PASSWD_FD")) {
1213 get_password_from_file(atoi(getenv("PASSWD_FD")),NULL
);
1214 } else if (getenv("PASSWD_FILE")) {
1215 get_password_from_file(0, getenv("PASSWD_FILE"));
1218 if (orgoptions
&& parse_options(&orgoptions
, &flags
)) {
1222 ipaddr
= parse_server(&share_name
);
1223 if((ipaddr
== NULL
) && (got_ip
== 0)) {
1224 printf("No ip address specified and hostname not found\n");
1229 /* BB save off path and pop after mount returns? */
1230 resolved_path
= (char *)malloc(PATH_MAX
+1);
1232 /* Note that if we can not canonicalize the name, we get
1233 another chance to see if it is valid when we chdir to it */
1234 if (realpath(mountpoint
, resolved_path
)) {
1235 mountpoint
= resolved_path
;
1238 if(chdir(mountpoint
)) {
1239 printf("mount error: can not change directory into mount target %s\n",mountpoint
);
1244 if(stat (".", &statbuf
)) {
1245 printf("mount error: mount point %s does not exist\n",mountpoint
);
1250 if (S_ISDIR(statbuf
.st_mode
) == 0) {
1251 printf("mount error: mount point %s is not a directory\n",mountpoint
);
1256 if((getuid() != 0) && (geteuid() == 0)) {
1257 if((statbuf
.st_uid
== getuid()) && (S_IRWXU
== (statbuf
.st_mode
& S_IRWXU
))) {
1258 #ifndef CIFS_ALLOW_USR_SUID
1259 /* Do not allow user mounts to control suid flag
1260 for mount unless explicitly built that way */
1261 flags
|= MS_NOSUID
| MS_NODEV
;
1264 printf("mount error: permission denied or not superuser and mount.cifs not installed SUID\n");
1270 user_name
= getusername();
1274 if(got_password
== 0) {
1275 mountpassword
= getpass("Password: "); /* BB obsolete */
1278 /* FIXME launch daemon (handles dfs name resolution and credential change)
1279 remember to clear parms and overwrite password field before launching */
1282 optlen
= strlen(orgoptions
);
1287 optlen
+= strlen(share_name
) + 4;
1289 printf("No server share name specified\n");
1290 printf("\nMounting the DFS root for server not implemented yet\n");
1294 optlen
+= strlen(user_name
) + 6;
1296 optlen
+= strlen(ipaddr
) + 4;
1298 optlen
+= strlen(mountpassword
) + 6;
1301 options_size
= optlen
+ 10 + 64;
1302 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 */);
1304 if(options
== NULL
) {
1305 printf("Could not allocate memory for mount options\n");
1310 strlcpy(options
,"unc=",options_size
);
1311 strlcat(options
,share_name
,options_size
);
1312 /* scan backwards and reverse direction of slash */
1313 temp
= strrchr(options
, '/');
1314 if(temp
> options
+ 6)
1317 strlcat(options
,",ip=",options_size
);
1318 strlcat(options
,ipaddr
,options_size
);
1322 /* check for syntax like user=domain\user */
1324 domain_name
= check_for_domain(&user_name
);
1325 strlcat(options
,",user=",options_size
);
1326 strlcat(options
,user_name
,options_size
);
1330 /* extra length accounted for in option string above */
1331 strlcat(options
,",domain=",options_size
);
1332 strlcat(options
,domain_name
,options_size
);
1336 /* Commas have to be doubled, or else they will
1337 look like the parameter separator */
1338 /* if(sep is not set)*/
1340 check_for_comma(&mountpassword
);
1341 strlcat(options
,",pass=",options_size
);
1342 strlcat(options
,mountpassword
,options_size
);
1345 strlcat(options
,",ver=",options_size
);
1346 strlcat(options
,MOUNT_CIFS_VERSION_MAJOR
,options_size
);
1349 strlcat(options
,",",options_size
);
1350 strlcat(options
,orgoptions
,options_size
);
1353 strlcat(options
,",prefixpath=",options_size
);
1354 strlcat(options
,prefixpath
,options_size
); /* no need to cat the / */
1357 printf("\nmount.cifs kernel mount options %s \n",options
);
1359 /* convert all '\\' to '/' in share portion so that /proc/mounts looks pretty */
1360 replace_char(dev_name
, '\\', '/', strlen(share_name
));
1362 if(mount(dev_name
, mountpoint
, "cifs", flags
, options
)) {
1363 /* remember to kill daemon on error */
1366 printf("mount failed but no error number set\n");
1369 printf("mount error: cifs filesystem not supported by the system\n");
1374 if (uppercase_string(dev_name
) &&
1375 uppercase_string(share_name
) &&
1376 uppercase_string(prefixpath
)) {
1377 printf("retrying with upper case share name\n");
1382 printf("mount error %d = %s\n",errno
,strerror(errno
));
1384 printf("Refer to the mount.cifs(8) manual page (e.g.man mount.cifs)\n");
1388 pmntfile
= setmntent(MOUNTED
, "a+");
1390 mountent
.mnt_fsname
= dev_name
;
1391 mountent
.mnt_dir
= mountpoint
;
1392 mountent
.mnt_type
= CONST_DISCARD(char *,"cifs");
1393 mountent
.mnt_opts
= (char *)malloc(220);
1394 if(mountent
.mnt_opts
) {
1395 char * mount_user
= getusername();
1396 memset(mountent
.mnt_opts
,0,200);
1397 if(flags
& MS_RDONLY
)
1398 strlcat(mountent
.mnt_opts
,"ro",220);
1400 strlcat(mountent
.mnt_opts
,"rw",220);
1401 if(flags
& MS_MANDLOCK
)
1402 strlcat(mountent
.mnt_opts
,",mand",220);
1403 if(flags
& MS_NOEXEC
)
1404 strlcat(mountent
.mnt_opts
,",noexec",220);
1405 if(flags
& MS_NOSUID
)
1406 strlcat(mountent
.mnt_opts
,",nosuid",220);
1407 if(flags
& MS_NODEV
)
1408 strlcat(mountent
.mnt_opts
,",nodev",220);
1409 if(flags
& MS_SYNCHRONOUS
)
1410 strlcat(mountent
.mnt_opts
,",synch",220);
1413 strlcat(mountent
.mnt_opts
,",user=",220);
1414 strlcat(mountent
.mnt_opts
,mount_user
,220);
1416 /* free(mount_user); do not free static mem */
1419 mountent
.mnt_freq
= 0;
1420 mountent
.mnt_passno
= 0;
1421 rc
= addmntent(pmntfile
,&mountent
);
1422 endmntent(pmntfile
);
1423 if(mountent
.mnt_opts
)
1424 free(mountent
.mnt_opts
);
1426 printf("could not update mount table\n");
1432 int len
= strlen(mountpassword
);
1433 memset(mountpassword
,0,len
);
1434 free(mountpassword
);
1438 memset(options
,0,optlen
);
1443 memset(orgoptions
,0,orgoptlen
);
1447 free(resolved_path
);