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
;
89 static int got_password
= 0;
90 static int got_user
= 0;
91 static int got_domain
= 0;
92 static int got_ip
= 0;
93 static int got_unc
= 0;
94 static int got_uid
= 0;
95 static int got_gid
= 0;
96 static char * user_name
= NULL
;
97 static char * mountpassword
= NULL
;
98 char * domain_name
= NULL
;
99 char * prefixpath
= NULL
;
101 /* glibc doesn't have strlcpy, strlcat. Ensure we do. JRA. We
102 * don't link to libreplace so need them here. */
104 /* like strncpy but does not 0 fill the buffer and always null
105 * terminates. bufsize is the size of the destination buffer */
108 static size_t strlcpy(char *d
, const char *s
, size_t bufsize
)
110 size_t len
= strlen(s
);
112 if (bufsize
<= 0) return 0;
113 if (len
>= bufsize
) len
= bufsize
-1;
120 /* like strncat but does not 0 fill the buffer and always null
121 * terminates. bufsize is the length of the buffer, which should
122 * be one more than the maximum resulting string length */
125 static size_t strlcat(char *d
, const char *s
, size_t bufsize
)
127 size_t len1
= strlen(d
);
128 size_t len2
= strlen(s
);
129 size_t ret
= len1
+ len2
;
131 if (len1
+len2
>= bufsize
) {
132 if (bufsize
< (len1
+1)) {
135 len2
= bufsize
- (len1
+1);
138 memcpy(d
+len1
, s
, len2
);
148 open nofollow - avoid symlink exposure?
149 get owner of dir see if matches self or if root
150 call system(umount argv) etc.
154 static char * check_for_domain(char **);
157 static void mount_cifs_usage(void)
159 printf("\nUsage: %s <remotetarget> <dir> -o <options>\n", thisprogram
);
160 printf("\nMount the remote target, specified as a UNC name,");
161 printf(" to a local directory.\n\nOptions:\n");
162 printf("\tuser=<arg>\n\tpass=<arg>\n\tdom=<arg>\n");
163 printf("\nLess commonly used options:");
164 printf("\n\tcredentials=<filename>,guest,perm,noperm,setuids,nosetuids,rw,ro,");
165 printf("\n\tsep=<char>,iocharset=<codepage>,suid,nosuid,exec,noexec,serverino,");
166 printf("\n\tmapchars,nomapchars,nolock,servernetbiosname=<SRV_RFC1001NAME>");
167 printf("\n\tdirectio,nounix,cifsacl,sec=<authentication mechanism>,sign");
168 printf("\n\nOptions not needed for servers supporting CIFS Unix extensions");
169 printf("\n\t(e.g. unneeded for mounts to most Samba versions):");
170 printf("\n\tuid=<uid>,gid=<gid>,dir_mode=<mode>,file_mode=<mode>,sfu");
171 printf("\n\nRarely used options:");
172 printf("\n\tport=<tcpport>,rsize=<size>,wsize=<size>,unc=<unc_name>,ip=<ip_address>,");
173 printf("\n\tdev,nodev,nouser_xattr,netbiosname=<OUR_RFC1001NAME>,hard,soft,intr,");
174 printf("\n\tnointr,ignorecase,noposixpaths,noacl,prefixpath=<path>,nobrl");
175 printf("\n\tin6_addr");
176 printf("\n\nOptions are described in more detail in the manual page");
177 printf("\n\tman 8 mount.cifs\n");
178 printf("\nTo display the version number of the mount helper:");
179 printf("\n\t%s -V\n",thisprogram
);
181 SAFE_FREE(mountpassword
);
185 /* caller frees username if necessary */
186 static char * getusername(void) {
187 char *username
= NULL
;
188 struct passwd
*password
= getpwuid(getuid());
191 username
= password
->pw_name
;
196 static int open_cred_file(char * file_name
)
203 i
= access(file_name
, R_OK
);
207 fs
= fopen(file_name
,"r");
210 line_buf
= (char *)malloc(4096);
211 if(line_buf
== NULL
) {
216 while(fgets(line_buf
,4096,fs
)) {
217 /* parse line from credential file */
219 /* eat leading white space */
220 for(i
=0;i
<4086;i
++) {
221 if((line_buf
[i
] != ' ') && (line_buf
[i
] != '\t'))
223 /* if whitespace - skip past it */
225 if (strncasecmp("username",line_buf
+i
,8) == 0) {
226 temp_val
= strchr(line_buf
+ i
,'=');
228 /* go past equals sign */
230 for(length
= 0;length
<4087;length
++) {
231 if ((temp_val
[length
] == '\n')
232 || (temp_val
[length
] == '\0')) {
233 temp_val
[length
] = '\0';
238 printf("mount.cifs failed due to malformed username in credentials file");
239 memset(line_buf
,0,4096);
243 user_name
= (char *)calloc(1 + length
,1);
244 /* BB adding free of user_name string before exit,
245 not really necessary but would be cleaner */
246 strlcpy(user_name
,temp_val
, length
+1);
249 } else if (strncasecmp("password",line_buf
+i
,8) == 0) {
250 temp_val
= strchr(line_buf
+i
,'=');
252 /* go past equals sign */
254 for(length
= 0;length
<MOUNT_PASSWD_SIZE
+1;length
++) {
255 if ((temp_val
[length
] == '\n')
256 || (temp_val
[length
] == '\0')) {
257 temp_val
[length
] = '\0';
261 if(length
> MOUNT_PASSWD_SIZE
) {
262 printf("mount.cifs failed: password in credentials file too long\n");
263 memset(line_buf
,0, 4096);
266 if(mountpassword
== NULL
) {
267 mountpassword
= (char *)calloc(MOUNT_PASSWD_SIZE
+1,1);
269 memset(mountpassword
,0,MOUNT_PASSWD_SIZE
);
271 strlcpy(mountpassword
,temp_val
,MOUNT_PASSWD_SIZE
+1);
276 } else if (strncasecmp("domain",line_buf
+i
,6) == 0) {
277 temp_val
= strchr(line_buf
+i
,'=');
279 /* go past equals sign */
282 printf("\nDomain %s\n",temp_val
);
283 for(length
= 0;length
<DOMAIN_SIZE
+1;length
++) {
284 if ((temp_val
[length
] == '\n')
285 || (temp_val
[length
] == '\0')) {
286 temp_val
[length
] = '\0';
290 if(length
> DOMAIN_SIZE
) {
291 printf("mount.cifs failed: domain in credentials file too long\n");
294 if(domain_name
== NULL
) {
295 domain_name
= (char *)calloc(DOMAIN_SIZE
+1,1);
297 memset(domain_name
,0,DOMAIN_SIZE
);
299 strlcpy(domain_name
,temp_val
,DOMAIN_SIZE
+1);
312 static int get_password_from_file(int file_descript
, char * filename
)
318 if(mountpassword
== NULL
)
319 mountpassword
= (char *)calloc(MOUNT_PASSWD_SIZE
+1,1);
321 memset(mountpassword
, 0, MOUNT_PASSWD_SIZE
);
323 if (mountpassword
== NULL
) {
324 printf("malloc failed\n");
328 if(filename
!= NULL
) {
329 rc
= access(filename
, R_OK
);
331 fprintf(stderr
, "mount.cifs failed: access check of %s failed: %s\n",
332 filename
, strerror(errno
));
335 file_descript
= open(filename
, O_RDONLY
);
336 if(file_descript
< 0) {
337 printf("mount.cifs failed. %s attempting to open password file %s\n",
338 strerror(errno
),filename
);
342 /* else file already open and fd provided */
344 for(i
=0;i
<MOUNT_PASSWD_SIZE
;i
++) {
345 rc
= read(file_descript
,&c
,1);
347 printf("mount.cifs failed. Error %s reading password file\n",strerror(errno
));
349 close(file_descript
);
352 if(mountpassword
[0] == 0) {
354 printf("\nWarning: null password used since cifs password file empty");
357 } else /* read valid character */ {
358 if((c
== 0) || (c
== '\n')) {
359 mountpassword
[i
] = '\0';
362 mountpassword
[i
] = c
;
365 if((i
== MOUNT_PASSWD_SIZE
) && (verboseflag
)) {
366 printf("\nWarning: password longer than %d characters specified in cifs password file",
370 if(filename
!= NULL
) {
371 close(file_descript
);
377 static int parse_options(char ** optionsp
, int * filesys_flags
)
380 char * percent_char
= NULL
;
382 char * next_keyword
= NULL
;
390 if (!optionsp
|| !*optionsp
)
395 printf("parsing options: %s\n", data
);
397 /* BB fixme check for separator override BB */
401 snprintf(user
,sizeof(user
),"%u",getuid());
403 snprintf(group
,sizeof(group
),"%u",getgid());
406 /* while ((data = strsep(&options, ",")) != NULL) { */
407 while(data
!= NULL
) {
408 /* check if ends with trailing comma */
412 /* format is keyword=value,keyword2=value2,keyword3=value3 etc.) */
413 /* data = next keyword */
414 /* value = next value ie stuff after equal sign */
416 next_keyword
= strchr(data
,','); /* BB handle sep= */
418 /* temporarily null terminate end of keyword=value pair */
422 /* temporarily null terminate keyword to make keyword and value distinct */
423 if ((value
= strchr(data
, '=')) != NULL
) {
428 if (strncmp(data
, "users",5) == 0) {
429 if(!value
|| !*value
) {
432 } else if (strncmp(data
, "user_xattr",10) == 0) {
433 /* do nothing - need to skip so not parsed as user name */
434 } else if (strncmp(data
, "user", 4) == 0) {
436 if (!value
|| !*value
) {
437 if(data
[4] == '\0') {
439 printf("\nskipping empty user mount parameter\n");
440 /* remove the parm since it would otherwise be confusing
441 to the kernel code which would think it was a real username */
444 printf("username specified with no parameter\n");
446 return 1; /* needs_arg; */
449 if (strnlen(value
, 260) < 260) {
451 percent_char
= strchr(value
,'%');
454 if(mountpassword
== NULL
)
455 mountpassword
= (char *)calloc(MOUNT_PASSWD_SIZE
+1,1);
458 printf("\nmount.cifs warning - password specified twice\n");
461 strlcpy(mountpassword
, percent_char
,MOUNT_PASSWD_SIZE
+1);
462 /* remove password from username */
463 while(*percent_char
!= 0) {
469 /* this is only case in which the user
470 name buf is not malloc - so we have to
471 check for domain name embedded within
472 the user name here since the later
473 call to check_for_domain will not be
475 domain_name
= check_for_domain(&value
);
477 printf("username too long\n");
482 } else if (strncmp(data
, "pass", 4) == 0) {
483 if (!value
|| !*value
) {
485 printf("\npassword specified twice, ignoring second\n");
488 } else if (strnlen(value
, 17) < 17) {
490 printf("\nmount.cifs warning - password specified twice\n");
493 printf("password too long\n");
497 } else if (strncmp(data
, "sec", 3) == 0) {
499 if (!strncmp(value
, "none", 4) ||
500 !strncmp(value
, "krb5", 4))
503 } else if (strncmp(data
, "ip", 2) == 0) {
504 if (!value
|| !*value
) {
505 printf("target ip address argument missing");
506 } else if (strnlen(value
, MAX_ADDRESS_LEN
) <= MAX_ADDRESS_LEN
) {
508 printf("ip address %s override specified\n",value
);
511 printf("ip address too long\n");
515 } else if ((strncmp(data
, "unc", 3) == 0)
516 || (strncmp(data
, "target", 6) == 0)
517 || (strncmp(data
, "path", 4) == 0)) {
518 if (!value
|| !*value
) {
519 printf("invalid path to network resource\n");
521 return 1; /* needs_arg; */
522 } else if(strnlen(value
,5) < 5) {
523 printf("UNC name too short");
526 if (strnlen(value
, 300) < 300) {
528 if (strncmp(value
, "//", 2) == 0) {
530 printf("unc name specified twice, ignoring second\n");
533 } else if (strncmp(value
, "\\\\", 2) != 0) {
534 printf("UNC Path does not begin with // or \\\\ \n");
539 printf("unc name specified twice, ignoring second\n");
544 printf("CIFS: UNC name too long\n");
548 } else if ((strncmp(data
, "dom" /* domain */, 3) == 0)
549 || (strncmp(data
, "workg", 5) == 0)) {
550 /* note this allows for synonyms of "domain"
551 such as "DOM" and "dom" and "workgroup"
552 and "WORKGRP" etc. */
553 if (!value
|| !*value
) {
554 printf("CIFS: invalid domain name\n");
556 return 1; /* needs_arg; */
558 if (strnlen(value
, DOMAIN_SIZE
+1) < DOMAIN_SIZE
+1) {
561 printf("domain name too long\n");
565 } else if (strncmp(data
, "cred", 4) == 0) {
566 if (value
&& *value
) {
567 rc
= open_cred_file(value
);
569 printf("error %d (%s) opening credential file %s\n",
570 rc
, strerror(rc
), value
);
575 printf("invalid credential file name specified\n");
579 } else if (strncmp(data
, "uid", 3) == 0) {
580 if (value
&& *value
) {
582 if (!isdigit(*value
)) {
585 if (!(pw
= getpwnam(value
))) {
586 printf("bad user name \"%s\"\n", value
);
589 snprintf(user
, sizeof(user
), "%u", pw
->pw_uid
);
591 strlcpy(user
,value
,sizeof(user
));
595 } else if (strncmp(data
, "gid", 3) == 0) {
596 if (value
&& *value
) {
598 if (!isdigit(*value
)) {
601 if (!(gr
= getgrnam(value
))) {
602 printf("bad group name \"%s\"\n", value
);
605 snprintf(group
, sizeof(group
), "%u", gr
->gr_gid
);
607 strlcpy(group
,value
,sizeof(group
));
611 /* fmask and dmask synonyms for people used to smbfs syntax */
612 } else if (strcmp(data
, "file_mode") == 0 || strcmp(data
, "fmask")==0) {
613 if (!value
|| !*value
) {
614 printf ("Option '%s' requires a numerical argument\n", data
);
619 if (value
[0] != '0') {
620 printf ("WARNING: '%s' not expressed in octal.\n", data
);
623 if (strcmp (data
, "fmask") == 0) {
624 printf ("WARNING: CIFS mount option 'fmask' is deprecated. Use 'file_mode' instead.\n");
625 data
= "file_mode"; /* BB fix this */
627 } else if (strcmp(data
, "dir_mode") == 0 || strcmp(data
, "dmask")==0) {
628 if (!value
|| !*value
) {
629 printf ("Option '%s' requires a numerical argument\n", data
);
634 if (value
[0] != '0') {
635 printf ("WARNING: '%s' not expressed in octal.\n", data
);
638 if (strcmp (data
, "dmask") == 0) {
639 printf ("WARNING: CIFS mount option 'dmask' is deprecated. Use 'dir_mode' instead.\n");
642 /* the following eight mount options should be
643 stripped out from what is passed into the kernel
644 since these eight options are best passed as the
645 mount flags rather than redundantly to the kernel
646 and could generate spurious warnings depending on the
647 level of the corresponding cifs vfs kernel code */
648 } else if (strncmp(data
, "nosuid", 6) == 0) {
649 *filesys_flags
|= MS_NOSUID
;
650 } else if (strncmp(data
, "suid", 4) == 0) {
651 *filesys_flags
&= ~MS_NOSUID
;
652 } else if (strncmp(data
, "nodev", 5) == 0) {
653 *filesys_flags
|= MS_NODEV
;
654 } else if ((strncmp(data
, "nobrl", 5) == 0) ||
655 (strncmp(data
, "nolock", 6) == 0)) {
656 *filesys_flags
&= ~MS_MANDLOCK
;
657 } else if (strncmp(data
, "dev", 3) == 0) {
658 *filesys_flags
&= ~MS_NODEV
;
659 } else if (strncmp(data
, "noexec", 6) == 0) {
660 *filesys_flags
|= MS_NOEXEC
;
661 } else if (strncmp(data
, "exec", 4) == 0) {
662 *filesys_flags
&= ~MS_NOEXEC
;
663 } else if (strncmp(data
, "guest", 5) == 0) {
664 user_name
= (char *)calloc(1, 1);
667 } else if (strncmp(data
, "ro", 2) == 0) {
668 *filesys_flags
|= MS_RDONLY
;
669 } else if (strncmp(data
, "rw", 2) == 0) {
670 *filesys_flags
&= ~MS_RDONLY
;
671 } else if (strncmp(data
, "remount", 7) == 0) {
672 *filesys_flags
|= MS_REMOUNT
;
673 } /* else if (strnicmp(data, "port", 4) == 0) {
674 if (value && *value) {
676 simple_strtoul(value, &value, 0);
678 } else if (strnicmp(data, "rsize", 5) == 0) {
679 if (value && *value) {
681 simple_strtoul(value, &value, 0);
683 } else if (strnicmp(data, "wsize", 5) == 0) {
684 if (value && *value) {
686 simple_strtoul(value, &value, 0);
688 } else if (strnicmp(data, "version", 3) == 0) {
690 printf("CIFS: Unknown mount option %s\n",data);
691 } */ /* nothing to do on those four mount options above.
692 Just pass to kernel and ignore them here */
694 /* Copy (possibly modified) option to out */
695 word_len
= strlen(data
);
697 word_len
+= 1 + strlen(value
);
699 out
= (char *)realloc(out
, out_len
+ word_len
+ 2);
706 strlcat(out
, ",", out_len
+ word_len
+ 2);
711 snprintf(out
+ out_len
, word_len
+ 1, "%s=%s", data
, value
);
713 snprintf(out
+ out_len
, word_len
+ 1, "%s", data
);
714 out_len
= strlen(out
);
720 /* special-case the uid and gid */
722 word_len
= strlen(user
);
724 out
= (char *)realloc(out
, out_len
+ word_len
+ 6);
731 strlcat(out
, ",", out_len
+ word_len
+ 6);
734 snprintf(out
+ out_len
, word_len
+ 5, "uid=%s", user
);
735 out_len
= strlen(out
);
738 word_len
= strlen(group
);
740 out
= (char *)realloc(out
, out_len
+ 1 + word_len
+ 6);
747 strlcat(out
, ",", out_len
+ word_len
+ 6);
750 snprintf(out
+ out_len
, word_len
+ 5, "gid=%s", group
);
751 out_len
= strlen(out
);
754 SAFE_FREE(*optionsp
);
759 /* replace all (one or more) commas with double commas */
760 static void check_for_comma(char ** ppasswrd
)
765 int number_of_commas
= 0;
780 if(number_of_commas
== 0)
782 if(number_of_commas
> MOUNT_PASSWD_SIZE
) {
783 /* would otherwise overflow the mount options buffer */
784 printf("\nInvalid password. Password contains too many commas.\n");
788 new_pass_buf
= (char *)malloc(len
+number_of_commas
+1);
789 if(new_pass_buf
== NULL
)
792 for(i
=0,j
=0;i
<len
;i
++,j
++) {
793 new_pass_buf
[j
] = pass
[i
];
796 new_pass_buf
[j
] = pass
[i
];
799 new_pass_buf
[len
+number_of_commas
] = 0;
801 SAFE_FREE(*ppasswrd
);
802 *ppasswrd
= new_pass_buf
;
807 /* Usernames can not have backslash in them and we use
808 [BB check if usernames can have forward slash in them BB]
809 backslash as domain\user separator character
811 static char * check_for_domain(char **ppuser
)
813 char * original_string
;
823 original_string
= *ppuser
;
825 if (original_string
== NULL
)
828 original_len
= strlen(original_string
);
830 usernm
= strchr(*ppuser
,'/');
831 if (usernm
== NULL
) {
832 usernm
= strchr(*ppuser
,'\\');
838 printf("Domain name specified twice. Username probably malformed\n");
844 if (domainnm
[0] != 0) {
847 printf("null domain\n");
849 len
= strlen(domainnm
);
850 /* reset domainm to new buffer, and copy
851 domain name into it */
852 domainnm
= (char *)malloc(len
+1);
856 strlcpy(domainnm
,*ppuser
,len
+1);
858 /* move_string(*ppuser, usernm+1) */
859 len
= strlen(usernm
+1);
861 if(len
>= original_len
) {
862 /* should not happen */
866 for(i
=0;i
<original_len
;i
++) {
868 original_string
[i
] = usernm
[i
+1];
869 else /* stuff with commas to remove last parm */
870 original_string
[i
] = ',';
873 /* BB add check for more than one slash?
881 /* replace all occurances of "from" in a string with "to" */
882 static void replace_char(char *string
, char from
, char to
, int maxlen
)
884 char *lastchar
= string
+ maxlen
;
886 string
= strchr(string
, from
);
889 if (string
>= lastchar
)
895 /* Note that caller frees the returned buffer if necessary */
896 static struct addrinfo
*
897 parse_server(char ** punc_name
)
899 char * unc_name
= *punc_name
;
900 int length
= strnlen(unc_name
, MAX_UNC_LEN
);
902 struct addrinfo
*addrlist
;
905 if(length
> (MAX_UNC_LEN
- 1)) {
906 printf("mount error: UNC name too long");
909 if ((strncasecmp("cifs://", unc_name
, 7) == 0) ||
910 (strncasecmp("smb://", unc_name
, 6) == 0)) {
911 printf("\nMounting cifs URL not implemented yet. Attempt to mount %s\n", unc_name
);
916 /* BB add code to find DFS root here */
917 printf("\nMounting the DFS root for domain not implemented yet\n");
920 if(strncmp(unc_name
,"//",2) && strncmp(unc_name
,"\\\\",2)) {
921 /* check for nfs syntax ie server:share */
922 share
= strchr(unc_name
,':');
924 *punc_name
= (char *)malloc(length
+3);
925 if(*punc_name
== NULL
) {
926 /* put the original string back if
928 *punc_name
= unc_name
;
932 strlcpy((*punc_name
)+2,unc_name
,length
+1);
934 unc_name
= *punc_name
;
935 unc_name
[length
+2] = 0;
936 goto continue_unc_parsing
;
938 printf("mount error: improperly formatted UNC name.");
939 printf(" %s does not begin with \\\\ or //\n",unc_name
);
943 continue_unc_parsing
:
948 /* allow for either delimiter between host and sharename */
949 if ((share
= strpbrk(unc_name
, "/\\"))) {
950 *share
= 0; /* temporarily terminate the string */
953 rc
= getaddrinfo(unc_name
, NULL
, NULL
, &addrlist
);
955 printf("mount error: could not resolve address for %s: %s\n",
956 unc_name
, gai_strerror(rc
));
960 *(share
- 1) = '/'; /* put delimiter back */
962 /* we don't convert the prefixpath delimiters since '\\' is a valid char in posix paths */
963 if ((prefixpath
= strpbrk(share
, "/\\"))) {
964 *prefixpath
= 0; /* permanently terminate the string */
965 if (!strlen(++prefixpath
))
966 prefixpath
= NULL
; /* this needs to be done explicitly */
970 printf("ip address specified explicitly\n");
973 /* BB should we pass an alternate version of the share name as Unicode */
977 /* BB add code to find DFS root (send null path on get DFS Referral to specified server here */
978 printf("Mounting the DFS root for a particular server not implemented yet\n");
985 static struct option longopts
[] = {
986 { "all", 0, NULL
, 'a' },
987 { "help",0, NULL
, 'h' },
988 { "move",0, NULL
, 'm' },
989 { "bind",0, NULL
, 'b' },
990 { "read-only", 0, NULL
, 'r' },
991 { "ro", 0, NULL
, 'r' },
992 { "verbose", 0, NULL
, 'v' },
993 { "version", 0, NULL
, 'V' },
994 { "read-write", 0, NULL
, 'w' },
995 { "rw", 0, NULL
, 'w' },
996 { "options", 1, NULL
, 'o' },
997 { "type", 1, NULL
, 't' },
998 { "rsize",1, NULL
, 'R' },
999 { "wsize",1, NULL
, 'W' },
1000 { "uid", 1, NULL
, '1'},
1001 { "gid", 1, NULL
, '2'},
1002 { "user",1,NULL
,'u'},
1003 { "username",1,NULL
,'u'},
1004 { "dom",1,NULL
,'d'},
1005 { "domain",1,NULL
,'d'},
1006 { "password",1,NULL
,'p'},
1007 { "pass",1,NULL
,'p'},
1008 { "credentials",1,NULL
,'c'},
1009 { "port",1,NULL
,'P'},
1010 /* { "uuid",1,NULL,'U'}, */ /* BB unimplemented */
1011 { NULL
, 0, NULL
, 0 }
1014 /* convert a string to uppercase. return false if the string
1015 * wasn't ASCII. Return success on a NULL ptr */
1017 uppercase_string(char *string
)
1023 /* check for unicode */
1024 if ((unsigned char) string
[0] & 0x80)
1026 *string
= toupper((unsigned char) *string
);
1033 int main(int argc
, char ** argv
)
1036 int flags
= MS_MANDLOCK
; /* no need to set legacy MS_MGC_VAL */
1037 char * orgoptions
= NULL
;
1038 char * share_name
= NULL
;
1039 const char * ipaddr
= NULL
;
1041 char * mountpoint
= NULL
;
1042 char * options
= NULL
;
1044 char * resolved_path
= NULL
;
1055 size_t options_size
= 0;
1057 int retry
= 0; /* set when we have to retry mount with uppercase */
1058 struct addrinfo
*addrhead
= NULL
, *addr
;
1059 struct stat statbuf
;
1060 struct utsname sysinfo
;
1061 struct mntent mountent
;
1062 struct sockaddr_in
*addr4
;
1063 struct sockaddr_in6
*addr6
;
1066 /* setlocale(LC_ALL, "");
1067 bindtextdomain(PACKAGE, LOCALEDIR);
1068 textdomain(PACKAGE); */
1071 thisprogram
= argv
[0];
1077 if(thisprogram
== NULL
)
1078 thisprogram
= "mount.cifs";
1081 /* BB add workstation name and domain and pass down */
1083 /* #ifdef _GNU_SOURCE
1084 printf(" node: %s machine: %s sysname %s domain %s\n", sysinfo.nodename,sysinfo.machine,sysinfo.sysname,sysinfo.domainname);
1088 share_name
= strndup(argv
[1], MAX_UNC_LEN
);
1089 if (share_name
== NULL
) {
1090 fprintf(stderr
, "%s: %s", argv
[0], strerror(ENOMEM
));
1093 mountpoint
= argv
[2];
1095 if ((strcmp (argv
[1], "--version") == 0) ||
1096 ((strcmp (argv
[1], "-V") == 0))) {
1097 printf ("mount.cifs version: %s.%s%s\n",
1098 MOUNT_CIFS_VERSION_MAJOR
,
1099 MOUNT_CIFS_VERSION_MINOR
,
1100 MOUNT_CIFS_VENDOR_SUFFIX
);
1107 /* add sharename in opts string as unc= parm */
1109 while ((c
= getopt_long (argc
, argv
, "afFhilL:no:O:rsSU:vVwt:",
1110 longopts
, NULL
)) != -1) {
1112 /* No code to do the following options yet */
1114 list_with_volumelabel = 1;
1117 volumelabel = optarg;
1124 case 'h': /* help */
1125 mount_cifs_usage ();
1135 "option 'b' (MS_BIND) not supported\n");
1143 "option 'm' (MS_MOVE) not supported\n");
1147 orgoptions
= strdup(optarg
);
1149 case 'r': /* mount readonly */
1159 printf ("mount.cifs version: %s.%s%s\n",
1160 MOUNT_CIFS_VERSION_MAJOR
,
1161 MOUNT_CIFS_VERSION_MINOR
,
1162 MOUNT_CIFS_VENDOR_SUFFIX
);
1165 flags
&= ~MS_RDONLY
;
1168 rsize
= atoi(optarg
) ;
1171 wsize
= atoi(optarg
);
1174 if (isdigit(*optarg
)) {
1177 uid
= strtoul(optarg
, &ep
, 10);
1179 printf("bad uid value \"%s\"\n", optarg
);
1185 if (!(pw
= getpwnam(optarg
))) {
1186 printf("bad user name \"%s\"\n", optarg
);
1194 if (isdigit(*optarg
)) {
1197 gid
= strtoul(optarg
, &ep
, 10);
1199 printf("bad gid value \"%s\"\n", optarg
);
1205 if (!(gr
= getgrnam(optarg
))) {
1206 printf("bad user name \"%s\"\n", optarg
);
1218 domain_name
= optarg
; /* BB fix this - currently ignored */
1222 if(mountpassword
== NULL
)
1223 mountpassword
= (char *)calloc(MOUNT_PASSWD_SIZE
+1,1);
1226 strlcpy(mountpassword
,optarg
,MOUNT_PASSWD_SIZE
+1);
1230 get_password_from_file(0 /* stdin */,NULL
);
1238 printf("unknown mount option %c\n",c
);
1244 if((argc
< 3) || (dev_name
== NULL
) || (mountpoint
== NULL
)) {
1249 if (getenv("PASSWD")) {
1250 if(mountpassword
== NULL
)
1251 mountpassword
= (char *)calloc(MOUNT_PASSWD_SIZE
+1,1);
1253 strlcpy(mountpassword
,getenv("PASSWD"),MOUNT_PASSWD_SIZE
+1);
1256 } else if (getenv("PASSWD_FD")) {
1257 get_password_from_file(atoi(getenv("PASSWD_FD")),NULL
);
1258 } else if (getenv("PASSWD_FILE")) {
1259 get_password_from_file(0, getenv("PASSWD_FILE"));
1262 if (orgoptions
&& parse_options(&orgoptions
, &flags
)) {
1266 addrhead
= addr
= parse_server(&share_name
);
1267 if((addrhead
== NULL
) && (got_ip
== 0)) {
1268 printf("No ip address specified and hostname not found\n");
1273 /* BB save off path and pop after mount returns? */
1274 resolved_path
= (char *)malloc(PATH_MAX
+1);
1276 /* Note that if we can not canonicalize the name, we get
1277 another chance to see if it is valid when we chdir to it */
1278 if (realpath(mountpoint
, resolved_path
)) {
1279 mountpoint
= resolved_path
;
1282 if(chdir(mountpoint
)) {
1283 printf("mount error: can not change directory into mount target %s\n",mountpoint
);
1288 if(stat (".", &statbuf
)) {
1289 printf("mount error: mount point %s does not exist\n",mountpoint
);
1294 if (S_ISDIR(statbuf
.st_mode
) == 0) {
1295 printf("mount error: mount point %s is not a directory\n",mountpoint
);
1300 if((getuid() != 0) && (geteuid() == 0)) {
1301 if((statbuf
.st_uid
== getuid()) && (S_IRWXU
== (statbuf
.st_mode
& S_IRWXU
))) {
1302 #ifndef CIFS_ALLOW_USR_SUID
1303 /* Do not allow user mounts to control suid flag
1304 for mount unless explicitly built that way */
1305 flags
|= MS_NOSUID
| MS_NODEV
;
1308 printf("mount error: permission denied or not superuser and mount.cifs not installed SUID\n");
1314 /* Note that the password will not be retrieved from the
1315 USER env variable (ie user%password form) as there is
1316 already a PASSWD environment varaible */
1318 user_name
= strdup(getenv("USER"));
1319 if (user_name
== NULL
)
1320 user_name
= getusername();
1324 if(got_password
== 0) {
1325 char *tmp_pass
= getpass("Password: "); /* BB obsolete sys call but
1326 no good replacement yet. */
1327 mountpassword
= (char *)calloc(MOUNT_PASSWD_SIZE
+1,1);
1328 if (!tmp_pass
|| !mountpassword
) {
1329 printf("Password not entered, exiting\n");
1332 strlcpy(mountpassword
, tmp_pass
, MOUNT_PASSWD_SIZE
+1);
1335 /* FIXME launch daemon (handles dfs name resolution and credential change)
1336 remember to clear parms and overwrite password field before launching */
1338 optlen
= strlen(orgoptions
);
1343 optlen
+= strlen(share_name
) + 4;
1345 printf("No server share name specified\n");
1346 printf("\nMounting the DFS root for server not implemented yet\n");
1350 optlen
+= strlen(user_name
) + 6;
1351 optlen
+= MAX_ADDRESS_LEN
+ 4;
1353 optlen
+= strlen(mountpassword
) + 6;
1356 options_size
= optlen
+ 10 + DOMAIN_SIZE
;
1357 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 */);
1359 if(options
== NULL
) {
1360 printf("Could not allocate memory for mount options\n");
1364 strlcpy(options
, "unc=", options_size
);
1365 strlcat(options
,share_name
,options_size
);
1366 /* scan backwards and reverse direction of slash */
1367 temp
= strrchr(options
, '/');
1368 if(temp
> options
+ 6)
1371 /* check for syntax like user=domain\user */
1373 domain_name
= check_for_domain(&user_name
);
1374 strlcat(options
,",user=",options_size
);
1375 strlcat(options
,user_name
,options_size
);
1379 /* extra length accounted for in option string above */
1380 strlcat(options
,",domain=",options_size
);
1381 strlcat(options
,domain_name
,options_size
);
1385 /* Commas have to be doubled, or else they will
1386 look like the parameter separator */
1387 /* if(sep is not set)*/
1389 check_for_comma(&mountpassword
);
1390 strlcat(options
,",pass=",options_size
);
1391 strlcat(options
,mountpassword
,options_size
);
1394 strlcat(options
,",ver=",options_size
);
1395 strlcat(options
,MOUNT_CIFS_VERSION_MAJOR
,options_size
);
1398 strlcat(options
,",",options_size
);
1399 strlcat(options
,orgoptions
,options_size
);
1402 strlcat(options
,",prefixpath=",options_size
);
1403 strlcat(options
,prefixpath
,options_size
); /* no need to cat the / */
1406 printf("\nmount.cifs kernel mount options %s \n",options
);
1408 /* convert all '\\' to '/' in share portion so that /proc/mounts looks pretty */
1409 replace_char(dev_name
, '\\', '/', strlen(share_name
));
1411 if (!got_ip
&& addr
) {
1412 strlcat(options
, ",ip=", options_size
);
1413 current_len
= strnlen(options
, options_size
);
1414 optionstail
= options
+ current_len
;
1415 switch (addr
->ai_addr
->sa_family
) {
1417 addr6
= (struct sockaddr_in6
*) addr
->ai_addr
;
1418 ipaddr
= inet_ntop(AF_INET6
, &addr6
->sin6_addr
, optionstail
,
1419 options_size
- current_len
);
1422 addr4
= (struct sockaddr_in
*) addr
->ai_addr
;
1423 ipaddr
= inet_ntop(AF_INET
, &addr4
->sin_addr
, optionstail
,
1424 options_size
- current_len
);
1428 /* if the address looks bogus, try the next one */
1430 addr
= addr
->ai_next
;
1438 if (!fakemnt
&& mount(dev_name
, mountpoint
, "cifs", flags
, options
)) {
1443 addr
= addr
->ai_next
;
1449 printf("mount error: cifs filesystem not supported by the system\n");
1454 if (uppercase_string(dev_name
) &&
1455 uppercase_string(share_name
) &&
1456 uppercase_string(prefixpath
)) {
1457 printf("retrying with upper case share name\n");
1462 printf("mount error(%d): %s\n", errno
, strerror(errno
));
1463 printf("Refer to the mount.cifs(8) manual page (e.g. man "
1471 atexit(unlock_mtab
);
1474 printf("cannot lock mtab");
1477 pmntfile
= setmntent(MOUNTED
, "a+");
1479 printf("could not update mount table\n");
1484 mountent
.mnt_fsname
= dev_name
;
1485 mountent
.mnt_dir
= mountpoint
;
1486 mountent
.mnt_type
= CONST_DISCARD(char *,"cifs");
1487 mountent
.mnt_opts
= (char *)malloc(220);
1488 if(mountent
.mnt_opts
) {
1489 char * mount_user
= getusername();
1490 memset(mountent
.mnt_opts
,0,200);
1491 if(flags
& MS_RDONLY
)
1492 strlcat(mountent
.mnt_opts
,"ro",220);
1494 strlcat(mountent
.mnt_opts
,"rw",220);
1495 if(flags
& MS_MANDLOCK
)
1496 strlcat(mountent
.mnt_opts
,",mand",220);
1497 if(flags
& MS_NOEXEC
)
1498 strlcat(mountent
.mnt_opts
,",noexec",220);
1499 if(flags
& MS_NOSUID
)
1500 strlcat(mountent
.mnt_opts
,",nosuid",220);
1501 if(flags
& MS_NODEV
)
1502 strlcat(mountent
.mnt_opts
,",nodev",220);
1503 if(flags
& MS_SYNCHRONOUS
)
1504 strlcat(mountent
.mnt_opts
,",sync",220);
1507 strlcat(mountent
.mnt_opts
,
1509 strlcat(mountent
.mnt_opts
,
1514 mountent
.mnt_freq
= 0;
1515 mountent
.mnt_passno
= 0;
1516 rc
= addmntent(pmntfile
,&mountent
);
1517 endmntent(pmntfile
);
1519 SAFE_FREE(mountent
.mnt_opts
);
1524 int len
= strlen(mountpassword
);
1525 memset(mountpassword
,0,len
);
1526 SAFE_FREE(mountpassword
);
1530 freeaddrinfo(addrhead
);
1532 SAFE_FREE(orgoptions
);
1533 SAFE_FREE(resolved_path
);
1534 SAFE_FREE(share_name
);