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>
41 #define MOUNT_CIFS_VERSION_MAJOR "1"
42 #define MOUNT_CIFS_VERSION_MINOR "10"
44 #ifndef MOUNT_CIFS_VENDOR_SUFFIX
46 #include "include/version.h"
47 #ifdef SAMBA_VERSION_VENDOR_SUFFIX
48 #define MOUNT_CIFS_VENDOR_SUFFIX "-"SAMBA_VERSION_OFFICIAL_STRING"-"SAMBA_VERSION_VENDOR_SUFFIX
50 #define MOUNT_CIFS_VENDOR_SUFFIX "-"SAMBA_VERSION_OFFICIAL_STRING
51 #endif /* SAMBA_VERSION_OFFICIAL_STRING and SAMBA_VERSION_VENDOR_SUFFIX */
53 #define MOUNT_CIFS_VENDOR_SUFFIX ""
54 #endif /* _SAMBA_BUILD_ */
55 #endif /* MOUNT_CIFS_VENDOR_SUFFIX */
65 #define CONST_DISCARD(type, ptr) ((type) ((void *) (ptr)))
67 const char *thisprogram;
69 static int got_password = 0;
70 static int got_user = 0;
71 static int got_domain = 0;
72 static int got_ip = 0;
73 static int got_unc = 0;
74 static int got_uid = 0;
75 static int got_gid = 0;
76 static int free_share_name = 0;
77 static char * user_name = NULL;
78 static char * mountpassword = NULL;
79 char * domain_name = NULL;
80 char * prefixpath = NULL;
86 open nofollow - avoid symlink exposure?
87 get owner of dir see if matches self or if root
88 call system(umount argv) etc.
92 static char * check_for_domain(char **);
95 static void mount_cifs_usage(void)
97 printf("\nUsage: %s <remotetarget> <dir> -o <options>\n", thisprogram);
98 printf("\nMount the remote target, specified as a UNC name,");
99 printf(" to a local directory.\n\nOptions:\n");
100 printf("\tuser=<arg>\n\tpass=<arg>\n\tdom=<arg>\n");
101 printf("\nLess commonly used options:");
102 printf("\n\tcredentials=<filename>,guest,perm,noperm,setuids,nosetuids,rw,ro,");
103 printf("\n\tsep=<char>,iocharset=<codepage>,suid,nosuid,exec,noexec,serverino,");
104 printf("\n\tdirectio,mapchars,nomapchars,nolock,servernetbiosname=<SRV_RFC1001NAME>");
105 printf("\n\nOptions not needed for servers supporting CIFS Unix extensions");
106 printf("\n\t(e.g. unneeded for mounts to most Samba versions):");
107 printf("\n\tuid=<uid>,gid=<gid>,dir_mode=<mode>,file_mode=<mode>,sfu");
108 printf("\n\nRarely used options:");
109 printf("\n\tport=<tcpport>,rsize=<size>,wsize=<size>,unc=<unc_name>,ip=<ip_address>,");
110 printf("\n\tdev,nodev,nouser_xattr,netbiosname=<OUR_RFC1001NAME>,hard,soft,intr,");
111 printf("\n\tnointr,ignorecase,noposixpaths,noacl");
112 printf("\n\nOptions are described in more detail in the manual page");
113 printf("\n\tman 8 mount.cifs\n");
114 printf("\nTo display the version number of the mount helper:");
115 printf("\n\t%s -V\n",thisprogram);
118 memset(mountpassword,0,64);
124 /* caller frees username if necessary */
125 static char * getusername(void) {
126 char *username = NULL;
127 struct passwd *password = getpwuid(getuid());
130 username = password->pw_name;
135 static char * parse_cifs_url(char * unc_name)
137 printf("\nMounting cifs URL not implemented yet. Attempt to mount %s\n",unc_name);
141 static int open_cred_file(char * file_name)
147 fs = fopen(file_name,"r");
150 line_buf = (char *)malloc(4096);
151 if(line_buf == NULL) {
156 while(fgets(line_buf,4096,fs)) {
157 /* parse line from credential file */
159 /* eat leading white space */
160 for(i=0;i<4086;i++) {
161 if((line_buf[i] != ' ') && (line_buf[i] != '\t'))
163 /* if whitespace - skip past it */
165 if (strncasecmp("username",line_buf+i,8) == 0) {
166 temp_val = strchr(line_buf + i,'=');
168 /* go past equals sign */
170 for(length = 0;length<4087;length++) {
171 if(temp_val[length] == '\n')
175 printf("mount.cifs failed due to malformed username in credentials file");
176 memset(line_buf,0,4096);
178 memset(mountpassword,0,64);
183 user_name = (char *)calloc(1 + length,1);
184 /* BB adding free of user_name string before exit,
185 not really necessary but would be cleaner */
186 strncpy(user_name,temp_val, length);
189 } else if (strncasecmp("password",line_buf+i,8) == 0) {
190 temp_val = strchr(line_buf+i,'=');
192 /* go past equals sign */
194 for(length = 0;length<65;length++) {
195 if(temp_val[length] == '\n')
199 printf("mount.cifs failed: password in credentials file too long\n");
200 memset(line_buf,0, 4096);
202 memset(mountpassword,0,64);
206 if(mountpassword == NULL) {
207 mountpassword = (char *)calloc(65,1);
209 memset(mountpassword,0,64);
211 strncpy(mountpassword,temp_val,length);
216 } else if (strncasecmp("domain",line_buf+i,6) == 0) {
217 temp_val = strchr(line_buf+i,'=');
219 /* go past equals sign */
222 printf("\nDomain %s\n",temp_val);
223 for(length = 0;length<65;length++) {
224 if(temp_val[length] == '\n')
228 printf("mount.cifs failed: domain in credentials file too long\n");
230 memset(mountpassword,0,64);
234 if(domain_name == NULL) {
235 domain_name = (char *)calloc(65,1);
237 memset(domain_name,0,64);
239 strncpy(domain_name,temp_val,length);
249 memset(line_buf,0,4096);
255 static int get_password_from_file(int file_descript, char * filename)
261 if(mountpassword == NULL)
262 mountpassword = (char *)calloc(65,1);
264 memset(mountpassword, 0, 64);
266 if (mountpassword == NULL) {
267 printf("malloc failed\n");
271 if(filename != NULL) {
272 file_descript = open(filename, O_RDONLY);
273 if(file_descript < 0) {
274 printf("mount.cifs failed. %s attempting to open password file %s\n",
275 strerror(errno),filename);
279 /* else file already open and fd provided */
282 rc = read(file_descript,&c,1);
284 printf("mount.cifs failed. Error %s reading password file\n",strerror(errno));
285 memset(mountpassword,0,64);
287 close(file_descript);
290 if(mountpassword[0] == 0) {
292 printf("\nWarning: null password used since cifs password file empty");
295 } else /* read valid character */ {
296 if((c == 0) || (c == '\n')) {
299 mountpassword[i] = c;
302 if((i == 64) && (verboseflag)) {
303 printf("\nWarning: password longer than 64 characters specified in cifs password file");
306 if(filename != NULL) {
307 close(file_descript);
313 static int parse_options(char ** optionsp, int * filesys_flags)
316 char * percent_char = NULL;
318 char * next_keyword = NULL;
324 if (!optionsp || !*optionsp)
329 printf("parsing options: %s\n", data);
331 /* BB fixme check for separator override BB */
333 /* while ((data = strsep(&options, ",")) != NULL) { */
334 while(data != NULL) {
335 /* check if ends with trailing comma */
339 /* format is keyword=value,keyword2=value2,keyword3=value3 etc.) */
340 /* data = next keyword */
341 /* value = next value ie stuff after equal sign */
343 next_keyword = strchr(data,','); /* BB handle sep= */
345 /* temporarily null terminate end of keyword=value pair */
349 /* temporarily null terminate keyword to make keyword and value distinct */
350 if ((value = strchr(data, '=')) != NULL) {
355 if (strncmp(data, "users",5) == 0) {
356 if(!value || !*value) {
359 } else if (strncmp(data, "user_xattr",10) == 0) {
360 /* do nothing - need to skip so not parsed as user name */
361 } else if (strncmp(data, "user", 4) == 0) {
363 if (!value || !*value) {
364 if(data[4] == '\0') {
366 printf("\nskipping empty user mount parameter\n");
367 /* remove the parm since it would otherwise be confusing
368 to the kernel code which would think it was a real username */
371 printf("username specified with no parameter\n");
372 return 1; /* needs_arg; */
375 if (strnlen(value, 260) < 260) {
377 percent_char = strchr(value,'%');
380 if(mountpassword == NULL)
381 mountpassword = (char *)calloc(65,1);
384 printf("\nmount.cifs warning - password specified twice\n");
387 strncpy(mountpassword, percent_char,64);
388 /* remove password from username */
389 while(*percent_char != 0) {
395 /* this is only case in which the user
396 name buf is not malloc - so we have to
397 check for domain name embedded within
398 the user name here since the later
399 call to check_for_domain will not be
401 domain_name = check_for_domain(&value);
403 printf("username too long\n");
407 } else if (strncmp(data, "pass", 4) == 0) {
408 if (!value || !*value) {
410 printf("\npassword specified twice, ignoring second\n");
413 } else if (strnlen(value, 17) < 17) {
415 printf("\nmount.cifs warning - password specified twice\n");
418 printf("password too long\n");
421 } else if (strncmp(data, "sec", 3) == 0) {
423 if (!strcmp(value, "none"))
426 } else if (strncmp(data, "ip", 2) == 0) {
427 if (!value || !*value) {
428 printf("target ip address argument missing");
429 } else if (strnlen(value, 35) < 35) {
431 printf("ip address %s override specified\n",value);
434 printf("ip address too long\n");
437 } else if ((strncmp(data, "unc", 3) == 0)
438 || (strncmp(data, "target", 6) == 0)
439 || (strncmp(data, "path", 4) == 0)) {
440 if (!value || !*value) {
441 printf("invalid path to network resource\n");
442 return 1; /* needs_arg; */
443 } else if(strnlen(value,5) < 5) {
444 printf("UNC name too short");
447 if (strnlen(value, 300) < 300) {
449 if (strncmp(value, "//", 2) == 0) {
451 printf("unc name specified twice, ignoring second\n");
454 } else if (strncmp(value, "\\\\", 2) != 0) {
455 printf("UNC Path does not begin with // or \\\\ \n");
459 printf("unc name specified twice, ignoring second\n");
464 printf("CIFS: UNC name too long\n");
467 } else if ((strncmp(data, "domain", 3) == 0)
468 || (strncmp(data, "workgroup", 5) == 0)) {
469 if (!value || !*value) {
470 printf("CIFS: invalid domain name\n");
471 return 1; /* needs_arg; */
473 if (strnlen(value, 65) < 65) {
476 printf("domain name too long\n");
479 } else if (strncmp(data, "cred", 4) == 0) {
480 if (value && *value) {
481 rc = open_cred_file(value);
483 printf("error %d opening credential file %s\n",rc, value);
487 printf("invalid credential file name specified\n");
490 } else if (strncmp(data, "uid", 3) == 0) {
491 if (value && *value) {
493 if (!isdigit(*value)) {
495 static char temp[32];
497 if (!(pw = getpwnam(value))) {
498 printf("bad user name \"%s\"\n", value);
501 sprintf(temp, "%u", pw->pw_uid);
506 } else if (strncmp(data, "gid", 3) == 0) {
507 if (value && *value) {
509 if (!isdigit(*value)) {
511 static char temp[32];
513 if (!(gr = getgrnam(value))) {
514 printf("bad group name \"%s\"\n", value);
517 sprintf(temp, "%u", gr->gr_gid);
522 /* fmask and dmask synonyms for people used to smbfs syntax */
523 } else if (strcmp(data, "file_mode") == 0 || strcmp(data, "fmask")==0) {
524 if (!value || !*value) {
525 printf ("Option '%s' requires a numerical argument\n", data);
529 if (value[0] != '0') {
530 printf ("WARNING: '%s' not expressed in octal.\n", data);
533 if (strcmp (data, "fmask") == 0) {
534 printf ("WARNING: CIFS mount option 'fmask' is deprecated. Use 'file_mode' instead.\n");
535 data = "file_mode"; /* BB fix this */
537 } else if (strcmp(data, "dir_mode") == 0 || strcmp(data, "dmask")==0) {
538 if (!value || !*value) {
539 printf ("Option '%s' requires a numerical argument\n", data);
543 if (value[0] != '0') {
544 printf ("WARNING: '%s' not expressed in octal.\n", data);
547 if (strcmp (data, "dmask") == 0) {
548 printf ("WARNING: CIFS mount option 'dmask' is deprecated. Use 'dir_mode' instead.\n");
551 /* the following eight mount options should be
552 stripped out from what is passed into the kernel
553 since these eight options are best passed as the
554 mount flags rather than redundantly to the kernel
555 and could generate spurious warnings depending on the
556 level of the corresponding cifs vfs kernel code */
557 } else if (strncmp(data, "nosuid", 6) == 0) {
558 *filesys_flags |= MS_NOSUID;
559 } else if (strncmp(data, "suid", 4) == 0) {
560 *filesys_flags &= ~MS_NOSUID;
561 } else if (strncmp(data, "nodev", 5) == 0) {
562 *filesys_flags |= MS_NODEV;
563 } else if ((strncmp(data, "nobrl", 5) == 0) ||
564 (strncmp(data, "nolock", 6) == 0)) {
565 *filesys_flags &= ~MS_MANDLOCK;
566 } else if (strncmp(data, "dev", 3) == 0) {
567 *filesys_flags &= ~MS_NODEV;
568 } else if (strncmp(data, "noexec", 6) == 0) {
569 *filesys_flags |= MS_NOEXEC;
570 } else if (strncmp(data, "exec", 4) == 0) {
571 *filesys_flags &= ~MS_NOEXEC;
572 } else if (strncmp(data, "guest", 5) == 0) {
574 } else if (strncmp(data, "ro", 2) == 0) {
575 *filesys_flags |= MS_RDONLY;
576 } else if (strncmp(data, "rw", 2) == 0) {
577 *filesys_flags &= ~MS_RDONLY;
578 } else if (strncmp(data, "remount", 7) == 0) {
579 *filesys_flags |= MS_REMOUNT;
580 } /* else if (strnicmp(data, "port", 4) == 0) {
581 if (value && *value) {
583 simple_strtoul(value, &value, 0);
585 } else if (strnicmp(data, "rsize", 5) == 0) {
586 if (value && *value) {
588 simple_strtoul(value, &value, 0);
590 } else if (strnicmp(data, "wsize", 5) == 0) {
591 if (value && *value) {
593 simple_strtoul(value, &value, 0);
595 } else if (strnicmp(data, "version", 3) == 0) {
597 printf("CIFS: Unknown mount option %s\n",data);
598 } */ /* nothing to do on those four mount options above.
599 Just pass to kernel and ignore them here */
601 /* Copy (possibly modified) option to out */
602 word_len = strlen(data);
604 word_len += 1 + strlen(value);
606 out = (char *)realloc(out, out_len + word_len + 2);
613 out[out_len++] = ',';
615 sprintf(out + out_len, "%s=%s", data, value);
617 sprintf(out + out_len, "%s", data);
618 out_len = strlen(out);
628 /* replace all (one or more) commas with double commas */
629 static void check_for_comma(char ** ppasswrd)
634 int number_of_commas = 0;
649 if(number_of_commas == 0)
651 if(number_of_commas > 64) {
652 /* would otherwise overflow the mount options buffer */
653 printf("\nInvalid password. Password contains too many commas.\n");
657 new_pass_buf = (char *)malloc(len+number_of_commas+1);
658 if(new_pass_buf == NULL)
661 for(i=0,j=0;i<len;i++,j++) {
662 new_pass_buf[j] = pass[i];
665 new_pass_buf[j] = pass[i];
668 new_pass_buf[len+number_of_commas] = 0;
671 *ppasswrd = new_pass_buf;
676 /* Usernames can not have backslash in them and we use
677 [BB check if usernames can have forward slash in them BB]
678 backslash as domain\user separator character
680 static char * check_for_domain(char **ppuser)
682 char * original_string;
692 original_string = *ppuser;
694 if (original_string == NULL)
697 original_len = strlen(original_string);
699 usernm = strchr(*ppuser,'/');
700 if (usernm == NULL) {
701 usernm = strchr(*ppuser,'\\');
707 printf("Domain name specified twice. Username probably malformed\n");
713 if (domainnm[0] != 0) {
716 printf("null domain\n");
718 len = strlen(domainnm);
719 /* reset domainm to new buffer, and copy
720 domain name into it */
721 domainnm = (char *)malloc(len+1);
725 strcpy(domainnm,*ppuser);
727 /* move_string(*ppuser, usernm+1) */
728 len = strlen(usernm+1);
730 if(len >= original_len) {
731 /* should not happen */
735 for(i=0;i<original_len;i++) {
737 original_string[i] = usernm[i+1];
738 else /* stuff with commas to remove last parm */
739 original_string[i] = ',';
742 /* BB add check for more than one slash?
750 /* Note that caller frees the returned buffer if necessary */
751 static char * parse_server(char ** punc_name)
753 char * unc_name = *punc_name;
754 int length = strnlen(unc_name,1024);
756 char * ipaddress_string = NULL;
757 struct hostent * host_entry = NULL;
758 struct in_addr server_ipaddr;
761 printf("mount error: UNC name too long");
764 if (strncasecmp("cifs://",unc_name,7) == 0)
765 return parse_cifs_url(unc_name+7);
766 if (strncasecmp("smb://",unc_name,6) == 0) {
767 return parse_cifs_url(unc_name+6);
771 /* BB add code to find DFS root here */
772 printf("\nMounting the DFS root for domain not implemented yet\n");
775 if(strncmp(unc_name,"//",2) && strncmp(unc_name,"\\\\",2)) {
776 /* check for nfs syntax ie server:share */
777 share = strchr(unc_name,':');
780 *punc_name = (char *)malloc(length+3);
781 if(*punc_name == NULL) {
782 /* put the original string back if
784 *punc_name = unc_name;
789 strncpy((*punc_name)+2,unc_name,length);
790 unc_name = *punc_name;
791 unc_name[length+2] = 0;
792 goto continue_unc_parsing;
794 printf("mount error: improperly formatted UNC name.");
795 printf(" %s does not begin with \\\\ or //\n",unc_name);
799 continue_unc_parsing:
803 if ((share = strchr(unc_name, '/')) ||
804 (share = strchr(unc_name,'\\'))) {
805 *share = 0; /* temporarily terminate the string */
808 host_entry = gethostbyname(unc_name);
810 *(share - 1) = '/'; /* put the slash back */
811 if ((prefixpath = strchr(share, '/'))) {
812 *prefixpath = 0; /* permanently terminate the string */
813 if (!strlen(++prefixpath))
814 prefixpath = NULL; /* this needs to be done explicitly */
818 printf("ip address specified explicitly\n");
821 if(host_entry == NULL) {
822 printf("mount error: could not find target server. TCP name %s not found\n", unc_name);
825 /* BB should we pass an alternate version of the share name as Unicode */
826 /* BB what about ipv6? BB */
827 /* BB add retries with alternate servers in list */
829 memcpy(&server_ipaddr.s_addr, host_entry->h_addr, 4);
831 ipaddress_string = inet_ntoa(server_ipaddr);
832 if(ipaddress_string == NULL) {
833 printf("mount error: could not get valid ip address for target server\n");
836 return ipaddress_string;
839 /* BB add code to find DFS root (send null path on get DFS Referral to specified server here */
840 printf("Mounting the DFS root for a particular server not implemented yet\n");
847 static struct option longopts[] = {
848 { "all", 0, NULL, 'a' },
849 { "help",0, NULL, 'h' },
850 { "move",0, NULL, 'm' },
851 { "bind",0, NULL, 'b' },
852 { "read-only", 0, NULL, 'r' },
853 { "ro", 0, NULL, 'r' },
854 { "verbose", 0, NULL, 'v' },
855 { "version", 0, NULL, 'V' },
856 { "read-write", 0, NULL, 'w' },
857 { "rw", 0, NULL, 'w' },
858 { "options", 1, NULL, 'o' },
859 { "type", 1, NULL, 't' },
860 { "rsize",1, NULL, 'R' },
861 { "wsize",1, NULL, 'W' },
862 { "uid", 1, NULL, '1'},
863 { "gid", 1, NULL, '2'},
864 { "user",1,NULL,'u'},
865 { "username",1,NULL,'u'},
867 { "domain",1,NULL,'d'},
868 { "password",1,NULL,'p'},
869 { "pass",1,NULL,'p'},
870 { "credentials",1,NULL,'c'},
871 { "port",1,NULL,'P'},
872 /* { "uuid",1,NULL,'U'}, */ /* BB unimplemented */
876 int main(int argc, char ** argv)
879 int flags = MS_MANDLOCK; /* no need to set legacy MS_MGC_VAL */
880 char * orgoptions = NULL;
881 char * share_name = NULL;
882 char * ipaddr = NULL;
884 char * mountpoint = NULL;
885 char * options = NULL;
886 char * resolved_path = NULL;
896 int retry = 0; /* set when we have to retry mount with uppercase */
898 struct utsname sysinfo;
899 struct mntent mountent;
902 /* setlocale(LC_ALL, "");
903 bindtextdomain(PACKAGE, LOCALEDIR);
904 textdomain(PACKAGE); */
907 thisprogram = argv[0];
913 if(thisprogram == NULL)
914 thisprogram = "mount.cifs";
917 /* BB add workstation name and domain and pass down */
919 /* #ifdef _GNU_SOURCE
920 printf(" node: %s machine: %s sysname %s domain %s\n", sysinfo.nodename,sysinfo.machine,sysinfo.sysname,sysinfo.domainname);
923 share_name = argv[1];
924 mountpoint = argv[2];
927 /* add sharename in opts string as unc= parm */
929 while ((c = getopt_long (argc, argv, "afFhilL:no:O:rsSU:vVwt:",
930 longopts, NULL)) != -1) {
932 /* No code to do the following options yet */
934 list_with_volumelabel = 1;
937 volumelabel = optarg;
955 "option 'b' (MS_BIND) not supported\n");
963 "option 'm' (MS_MOVE) not supported\n");
967 orgoptions = strdup(optarg);
969 case 'r': /* mount readonly */
979 printf ("mount.cifs version: %s.%s%s\n",
980 MOUNT_CIFS_VERSION_MAJOR,
981 MOUNT_CIFS_VERSION_MINOR,
982 MOUNT_CIFS_VENDOR_SUFFIX);
984 memset(mountpassword,0,64);
991 rsize = atoi(optarg) ;
994 wsize = atoi(optarg);
997 if (isdigit(*optarg)) {
1000 uid = strtoul(optarg, &ep, 10);
1002 printf("bad uid value \"%s\"\n", optarg);
1008 if (!(pw = getpwnam(optarg))) {
1009 printf("bad user name \"%s\"\n", optarg);
1017 if (isdigit(*optarg)) {
1020 gid = strtoul(optarg, &ep, 10);
1022 printf("bad gid value \"%s\"\n", optarg);
1028 if (!(gr = getgrnam(optarg))) {
1029 printf("bad user name \"%s\"\n", optarg);
1041 domain_name = optarg; /* BB fix this - currently ignored */
1045 if(mountpassword == NULL)
1046 mountpassword = (char *)calloc(65,1);
1049 strncpy(mountpassword,optarg,64);
1053 get_password_from_file(0 /* stdin */,NULL);
1058 printf("unknown mount option %c\n",c);
1064 if((argc < 3) || (share_name == NULL) || (mountpoint == NULL)) {
1069 if (getenv("PASSWD")) {
1070 if(mountpassword == NULL)
1071 mountpassword = (char *)calloc(65,1);
1073 strncpy(mountpassword,getenv("PASSWD"),64);
1076 } else if (getenv("PASSWD_FD")) {
1077 get_password_from_file(atoi(getenv("PASSWD_FD")),NULL);
1078 } else if (getenv("PASSWD_FILE")) {
1079 get_password_from_file(0, getenv("PASSWD_FILE"));
1082 if (orgoptions && parse_options(&orgoptions, &flags)) {
1086 ipaddr = parse_server(&share_name);
1087 if((ipaddr == NULL) && (got_ip == 0)) {
1088 printf("No ip address specified and hostname not found\n");
1093 /* BB save off path and pop after mount returns? */
1094 resolved_path = (char *)malloc(PATH_MAX+1);
1096 /* Note that if we can not canonicalize the name, we get
1097 another chance to see if it is valid when we chdir to it */
1098 if (realpath(mountpoint, resolved_path)) {
1099 mountpoint = resolved_path;
1102 if(chdir(mountpoint)) {
1103 printf("mount error: can not change directory into mount target %s\n",mountpoint);
1108 if(stat (".", &statbuf)) {
1109 printf("mount error: mount point %s does not exist\n",mountpoint);
1114 if (S_ISDIR(statbuf.st_mode) == 0) {
1115 printf("mount error: mount point %s is not a directory\n",mountpoint);
1120 if((getuid() != 0) && (geteuid() == 0)) {
1121 if((statbuf.st_uid == getuid()) && (S_IRWXU == (statbuf.st_mode & S_IRWXU))) {
1122 #ifndef CIFS_ALLOW_USR_SUID
1123 /* Do not allow user mounts to control suid flag
1124 for mount unless explicitly built that way */
1125 flags |= MS_NOSUID | MS_NODEV;
1128 printf("mount error: permission denied or not superuser and mount.cifs not installed SUID\n");
1134 user_name = getusername();
1138 if(got_password == 0) {
1139 mountpassword = getpass("Password: "); /* BB obsolete */
1142 /* FIXME launch daemon (handles dfs name resolution and credential change)
1143 remember to clear parms and overwrite password field before launching */
1146 optlen = strlen(orgoptions);
1151 optlen += strlen(share_name) + 4;
1153 printf("No server share name specified\n");
1154 printf("\nMounting the DFS root for server not implemented yet\n");
1158 optlen += strlen(user_name) + 6;
1160 optlen += strlen(ipaddr) + 4;
1162 optlen += strlen(mountpassword) + 6;
1165 options = (char *)malloc(optlen + 10 + 64 /* space for commas in password */ + 8 /* space for domain= , domain name itself was counted as part of the length username string above */);
1167 if(options == NULL) {
1168 printf("Could not allocate memory for mount options\n");
1174 strncat(options,"unc=",4);
1175 strcat(options,share_name);
1176 /* scan backwards and reverse direction of slash */
1177 temp = strrchr(options, '/');
1178 if(temp > options + 6)
1181 strncat(options,",ip=",4);
1182 strcat(options,ipaddr);
1186 /* check for syntax like user=domain\user */
1188 domain_name = check_for_domain(&user_name);
1189 strncat(options,",user=",6);
1190 strcat(options,user_name);
1194 /* extra length accounted for in option string above */
1195 strncat(options,",domain=",8);
1196 strcat(options,domain_name);
1200 /* Commas have to be doubled, or else they will
1201 look like the parameter separator */
1202 /* if(sep is not set)*/
1204 check_for_comma(&mountpassword);
1205 strncat(options,",pass=",6);
1206 strcat(options,mountpassword);
1209 strncat(options,",ver=",5);
1210 strcat(options,MOUNT_CIFS_VERSION_MAJOR);
1213 strcat(options,",");
1214 strcat(options,orgoptions);
1217 strncat(options,",prefixpath=",12);
1218 strcat(options,prefixpath); /* no need to cat the / */
1221 printf("\nmount.cifs kernel mount options %s \n",options);
1222 if(mount(share_name, mountpoint, "cifs", flags, options)) {
1223 /* remember to kill daemon on error */
1228 printf("mount failed but no error number set\n");
1231 printf("mount error: cifs filesystem not supported by the system\n");
1237 while (*tmp && !(((unsigned char)tmp[0]) & 0x80)) {
1238 *tmp = toupper((unsigned char)*tmp);
1242 printf("retrying with upper case share name\n");
1247 printf("mount error %d = %s\n",errno,strerror(errno));
1249 printf("Refer to the mount.cifs(8) manual page (e.g.man mount.cifs)\n");
1253 pmntfile = setmntent(MOUNTED, "a+");
1255 mountent.mnt_fsname = share_name;
1256 mountent.mnt_dir = mountpoint;
1257 mountent.mnt_type = CONST_DISCARD(char *,"cifs");
1258 mountent.mnt_opts = (char *)malloc(220);
1259 if(mountent.mnt_opts) {
1260 char * mount_user = getusername();
1261 memset(mountent.mnt_opts,0,200);
1262 if(flags & MS_RDONLY)
1263 strcat(mountent.mnt_opts,"ro");
1265 strcat(mountent.mnt_opts,"rw");
1266 if(flags & MS_MANDLOCK)
1267 strcat(mountent.mnt_opts,",mand");
1268 if(flags & MS_NOEXEC)
1269 strcat(mountent.mnt_opts,",noexec");
1270 if(flags & MS_NOSUID)
1271 strcat(mountent.mnt_opts,",nosuid");
1272 if(flags & MS_NODEV)
1273 strcat(mountent.mnt_opts,",nodev");
1274 if(flags & MS_SYNCHRONOUS)
1275 strcat(mountent.mnt_opts,",synch");
1278 strcat(mountent.mnt_opts,",user=");
1279 strcat(mountent.mnt_opts,mount_user);
1281 /* free(mount_user); do not free static mem */
1284 mountent.mnt_freq = 0;
1285 mountent.mnt_passno = 0;
1286 rc = addmntent(pmntfile,&mountent);
1287 endmntent(pmntfile);
1288 if(mountent.mnt_opts)
1289 free(mountent.mnt_opts);
1291 printf("could not update mount table\n");
1297 int len = strlen(mountpassword);
1298 memset(mountpassword,0,len);
1299 free(mountpassword);
1303 memset(options,0,optlen);
1308 memset(orgoptions,0,orgoptlen);
1312 free(resolved_path);
1315 if(free_share_name) {