2 Mount helper utility for Linux CIFS VFS (virtual filesystem) client
3 Copyright (C) 2003 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 2 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, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
27 #include <sys/types.h>
28 #include <sys/mount.h>
30 #include <sys/utsname.h>
31 #include <sys/socket.h>
32 #include <arpa/inet.h>
40 #define MOUNT_CIFS_VERSION_MAJOR "1"
41 #define MOUNT_CIFS_VERSION_MINOR "2"
43 #ifndef MOUNT_CIFS_VENDOR_SUFFIX
44 #define MOUNT_CIFS_VENDOR_SUFFIX ""
53 static int got_password = 0;
54 static int got_user = 0;
55 static int got_domain = 0;
56 static int got_ip = 0;
57 static int got_unc = 0;
58 static int got_uid = 0;
59 static int got_gid = 0;
60 static char * user_name = NULL;
61 char * mountpassword = NULL;
67 open nofollow - avoid symlink exposure?
68 get owner of dir see if matches self or if root
69 call system(umount argv) etc.
73 static void mount_cifs_usage(void)
75 printf("\nUsage: %s <remotetarget> <dir> -o <options>\n", thisprogram);
76 printf("\nMount the remote target, specified as a UNC name,");
77 printf(" to a local directory.\n\nOptions:\n");
78 printf("\tuser=<arg>\n\tpass=<arg>\n\tdom=<arg>\n");
79 printf("\nOther less commonly used options are described in the manual page");
80 printf("\n\tman 8 mount.cifs\n");
81 printf("\nTo display the version number of the mount helper:");
82 printf("\n\t%s -V\n",thisprogram);
85 memset(mountpassword,0,64);
91 /* caller frees username if necessary */
92 static char * getusername(void) {
93 char *username = NULL;
94 struct passwd *password = getpwuid(getuid());
97 username = password->pw_name;
102 char * parse_cifs_url(char * unc_name)
104 printf("\nMounting cifs URL not implemented yet. Attempt to mount %s\n",unc_name);
108 static int open_cred_file(char * file_name)
114 fs = fopen(file_name,"r");
117 line_buf = malloc(4096);
121 while(fgets(line_buf,4096,fs)) {
122 /* parse line from credential file */
124 /* eat leading white space */
125 for(i=0;i<4086;i++) {
126 if((line_buf[i] != ' ') && (line_buf[i] != '\t'))
128 /* if whitespace - skip past it */
130 if (strncasecmp("username",line_buf+i,8) == 0) {
131 temp_val = strchr(line_buf + i,'=');
133 /* go past equals sign */
135 for(length = 0;length<4087;length++) {
136 if(temp_val[length] == '\n')
140 printf("cifs.mount failed due to malformed username in credentials file");
141 memset(line_buf,0,4096);
143 memset(mountpassword,0,64);
148 user_name = calloc(1 + length,1);
149 /* BB adding free of user_name string before exit,
150 not really necessary but would be cleaner */
151 strncpy(user_name,temp_val, length);
154 } else if (strncasecmp("password",line_buf+i,8) == 0) {
155 temp_val = strchr(line_buf+i,'=');
157 /* go past equals sign */
159 for(length = 0;length<65;length++) {
160 if(temp_val[length] == '\n')
164 printf("cifs.mount failed: password in credentials file too long\n");
165 memset(line_buf,0, 4096);
167 memset(mountpassword,0,64);
171 if(mountpassword == NULL) {
172 mountpassword = calloc(65,1);
174 memset(mountpassword,0,64);
176 /* BB add handling for commas in password here */
177 strncpy(mountpassword,temp_val,length);
186 memset(line_buf,0,4096);
192 static int get_password_from_file(int file_descript, char * filename)
198 if(mountpassword == NULL)
199 mountpassword = calloc(65,1);
201 memset(mountpassword, 0, 64);
203 if(filename != NULL) {
204 file_descript = open(filename, O_RDONLY);
205 if(file_descript < 0) {
206 printf("cifs.mount failed. %s attempting to open password file %s\n",
207 strerror(errno),filename);
211 /* else file already open and fd provided */
214 rc = read(file_descript,&c,1);
216 printf("cifs.mount failed. Error %s reading password file\n",strerror(errno));
217 memset(mountpassword,0,64);
219 close(file_descript);
222 if(mountpassword[0] == 0) {
224 printf("\nWarning: null password used since cifs password file empty");
227 } else /* read valid character */ {
228 if((c == 0) || (c == '\n')) {
231 mountpassword[i] = c;
234 if((i == 64) && (verboseflag)) {
235 printf("\nWarning: password longer than 64 characters specified in cifs password file");
238 if(filename != NULL) {
239 close(file_descript);
245 static int parse_options(char * options, int * filesys_flags)
248 char * percent_char = 0;
250 char * next_keyword = 0;
259 printf("\n parsing options: %s", options);
261 /* while ((data = strsep(&options, ",")) != NULL) { */
262 while(data != NULL) {
263 /* check if ends with trailing comma */
267 /* format is keyword=value,keyword2=value2,keyword3=value3 etc.) */
268 /* data = next keyword */
269 /* value = next value ie stuff after equal sign */
271 next_keyword = strchr(data,',');
273 /* temporarily null terminate end of keyword=value pair */
280 /* temporarily null terminate keyword to make keyword and value distinct */
281 if ((value = strchr(data, '=')) != NULL) {
286 if (strncmp(data, "user", 4) == 0) {
287 if (!value || !*value) {
288 printf("invalid or missing username\n");
289 return 1; /* needs_arg; */
291 if (strnlen(value, 260) < 260) {
293 percent_char = strchr(value,'%');
296 if(mountpassword == NULL)
297 mountpassword = calloc(65,1);
300 printf("\ncifs.mount warning - password specified twice\n");
303 strncpy(mountpassword, percent_char,64);
304 /* remove password from username */
305 while(*percent_char != 0) {
312 printf("username too long\n");
315 } else if (strncmp(data, "pass", 4) == 0) {
316 if (!value || !*value) {
318 printf("\npassword specified twice, ignoring second\n");
321 } else if (strnlen(value, 17) < 17) {
323 printf("\ncifs.mount warning - password specified twice\n");
326 printf("password too long\n");
329 } else if (strncmp(data, "ip", 2) == 0) {
330 if (!value || !*value) {
331 printf("target ip address argument missing");
332 } else if (strnlen(value, 35) < 35) {
335 printf("ip address too long\n");
338 } else if ((strncmp(data, "unc", 3) == 0)
339 || (strncmp(data, "target", 6) == 0)
340 || (strncmp(data, "path", 4) == 0)) {
341 if (!value || !*value) {
342 printf("invalid path to network resource\n");
343 return 1; /* needs_arg; */
344 } else if(strnlen(value,5) < 5) {
345 printf("UNC name too short");
348 if (strnlen(value, 300) < 300) {
350 if (strncmp(value, "//", 2) == 0) {
352 printf("unc name specified twice, ignoring second\n");
355 } else if (strncmp(value, "\\\\", 2) != 0) {
356 printf("UNC Path does not begin with // or \\\\ \n");
360 printf("unc name specified twice, ignoring second\n");
365 printf("CIFS: UNC name too long\n");
368 } else if ((strncmp(data, "domain", 3) == 0)
369 || (strncmp(data, "workgroup", 5) == 0)) {
370 if (!value || !*value) {
371 printf("CIFS: invalid domain name\n");
372 return 1; /* needs_arg; */
374 if (strnlen(value, 65) < 65) {
377 printf("domain name too long\n");
380 } else if (strncmp(data, "cred", 4) == 0) {
381 if (value && *value) {
382 rc = open_cred_file(value);
384 printf("error %d opening credential file %s\n",rc, value);
388 printf("invalid credential file name specified\n");
391 } else if (strncmp(data, "uid", 3) == 0) {
392 if (value && *value) {
395 } else if (strncmp(data, "gid", 3) == 0) {
396 if (value && *value) {
399 /* fmask and dmask synonyms for people used to smbfs syntax */
400 } else if (strcmp(data, "file_mode") == 0 || strcmp(data, "fmask")==0) {
401 if (!value || !*value) {
402 printf ("Option '%s' requires a numerical argument\n", data);
406 if (value[0] != '0') {
407 printf ("WARNING: '%s' not expressed in octal.\n", data);
410 if (strcmp (data, "fmask") == 0) {
411 printf ("WARNING: CIFS mount option 'fmask' is deprecated. Use 'file_mode' instead.\n");
412 data = "file_mode"; /* BB fix this */
414 } else if (strcmp(data, "dir_mode") == 0 || strcmp(data, "dmask")==0) {
415 if (!value || !*value) {
416 printf ("Option '%s' requires a numerical argument\n", data);
420 if (value[0] != '0') {
421 printf ("WARNING: '%s' not expressed in octal.\n", data);
424 if (strcmp (data, "dmask") == 0) {
425 printf ("WARNING: CIFS mount option 'dmask' is deprecated. Use 'dir_mode' instead.\n");
428 /* the following eight mount options should be
429 stripped out from what is passed into the kernel
430 since these eight options are best passed as the
431 mount flags rather than redundantly to the kernel
432 and could generate spurious warnings depending on the
433 level of the corresponding cifs vfs kernel code */
434 } else if (strncmp(data, "nosuid", 6) == 0) {
435 *filesys_flags |= MS_NOSUID;
436 } else if (strncmp(data, "suid", 4) == 0) {
437 *filesys_flags &= ~MS_NOSUID;
438 } else if (strncmp(data, "nodev", 5) == 0) {
439 *filesys_flags |= MS_NODEV;
440 } else if (strncmp(data, "dev", 3) == 0) {
441 *filesys_flags &= ~MS_NODEV;
442 } else if (strncmp(data, "noexec", 6) == 0) {
443 *filesys_flags |= MS_NOEXEC;
444 } else if (strncmp(data, "exec", 4) == 0) {
445 *filesys_flags &= ~MS_NOEXEC;
446 } else if (strncmp(data, "ro", 2) == 0) {
447 *filesys_flags |= MS_RDONLY;
448 } else if (strncmp(data, "rw", 2) == 0) {
449 *filesys_flags &= ~MS_RDONLY;
450 } /* else if (strnicmp(data, "port", 4) == 0) {
451 if (value && *value) {
453 simple_strtoul(value, &value, 0);
455 } else if (strnicmp(data, "rsize", 5) == 0) {
456 if (value && *value) {
458 simple_strtoul(value, &value, 0);
460 } else if (strnicmp(data, "wsize", 5) == 0) {
461 if (value && *value) {
463 simple_strtoul(value, &value, 0);
465 } else if (strnicmp(data, "version", 3) == 0) {
467 printf("CIFS: Unknown mount option %s\n",data);
468 } */ /* nothing to do on those four mount options above.
469 Just pass to kernel and ignore them here */
471 /* move to next option */
472 data = next_keyword+1;
474 /* put overwritten equals sign back */
480 /* put previous overwritten comma back */
490 /* Note that caller frees the returned buffer if necessary */
491 char * parse_server(char * unc_name)
493 int length = strnlen(unc_name,1024);
495 char * ipaddress_string = NULL;
496 struct hostent * host_entry;
497 struct in_addr server_ipaddr;
501 printf("mount error: UNC name too long");
504 if (strncasecmp("cifs://",unc_name,7) == 0)
505 return parse_cifs_url(unc_name+7);
506 if (strncasecmp("smb://",unc_name,6) == 0) {
507 return parse_cifs_url(unc_name+6);
511 /* BB add code to find DFS root here */
512 printf("\nMounting the DFS root for domain not implemented yet");
515 if(strncmp(unc_name,"//",2) && strncmp(unc_name,"\\\\",2)) {
516 printf("mount error: improperly formatted UNC name.");
517 printf(" %s does not begin with \\\\ or //\n",unc_name);
524 if ((share = strchr(unc_name, '/')) ||
525 (share = strchr(unc_name,'\\'))) {
526 *share = 0; /* temporarily terminate the string */
528 host_entry = gethostbyname(unc_name);
529 *(share - 1) = '/'; /* put the slash back */
530 /* rc = getipnodebyname(unc_name, AF_INET, AT_ADDRCONFIG ,&rc);*/
531 if(host_entry == NULL) {
532 printf("mount error: could not find target server. TCP name %s not found ", unc_name);
533 printf(" rc = %d\n",rc);
537 /* BB should we pass an alternate version of the share name as Unicode */
538 /* BB what about ipv6? BB */
539 /* BB add retries with alternate servers in list */
541 memcpy(&server_ipaddr.s_addr, host_entry->h_addr, 4);
543 ipaddress_string = inet_ntoa(server_ipaddr);
544 if(ipaddress_string == NULL) {
545 printf("mount error: could not get valid ip address for target server\n");
548 return ipaddress_string;
551 /* BB add code to find DFS root (send null path on get DFS Referral to specified server here */
552 printf("Mounting the DFS root for a particular server not implemented yet\n");
559 static struct option longopts[] = {
560 { "all", 0, 0, 'a' },
561 { "help",0, 0, 'h' },
562 { "move",0, 0, 'm' },
563 { "bind",0, 0, 'b' },
564 { "read-only", 0, 0, 'r' },
566 { "verbose", 0, 0, 'v' },
567 { "version", 0, 0, 'V' },
568 { "read-write", 0, 0, 'w' },
570 { "options", 1, 0, 'o' },
571 { "type", 1, 0, 't' },
572 { "rsize",1, 0, 'R' },
573 { "wsize",1, 0, 'W' },
577 { "username",1,0,'u'},
580 { "password",1,0,'p'},
582 { "credentials",1,0,'c'},
584 /* { "uuid",1,0,'U'}, */ /* BB unimplemented */
588 int main(int argc, char ** argv)
591 int flags = MS_MANDLOCK; /* no need to set legacy MS_MGC_VAL */
592 char * orgoptions = NULL;
593 char * share_name = NULL;
594 char * domain_name = NULL;
595 char * ipaddr = NULL;
599 char * resolved_path;
610 struct utsname sysinfo;
611 struct mntent mountent;
614 /* setlocale(LC_ALL, "");
615 bindtextdomain(PACKAGE, LOCALEDIR);
616 textdomain(PACKAGE); */
619 thisprogram = argv[0];
621 if(thisprogram == NULL)
622 thisprogram = "mount.cifs";
625 /* BB add workstation name and domain and pass down */
627 /* #ifdef _GNU_SOURCE
628 printf(" node: %s machine: %s sysname %s domain %s\n", sysinfo.nodename,sysinfo.machine,sysinfo.sysname,sysinfo.domainname);
631 share_name = argv[1];
632 mountpoint = argv[2];
634 /* add sharename in opts string as unc= parm */
636 while ((c = getopt_long (argc, argv, "afFhilL:no:O:rsU:vVwt:",
637 longopts, NULL)) != -1) {
639 /* No code to do the following options yet */
641 list_with_volumelabel = 1;
644 volumelabel = optarg;
664 orgoptions = strdup(optarg);
666 case 'r': /* mount readonly */
676 printf ("mount.cifs version: %s.%s%s\n",
677 MOUNT_CIFS_VERSION_MAJOR,
678 MOUNT_CIFS_VERSION_MINOR,
679 MOUNT_CIFS_VENDOR_SUFFIX);
681 memset(mountpassword,0,64);
688 rsize = atoi(optarg) ;
691 wsize = atoi(optarg);
704 domain_name = optarg;
707 if(mountpassword == NULL)
708 mountpassword = calloc(65,1);
711 strncpy(mountpassword,optarg,64);
717 printf("unknown mount option %c\n",c);
726 if (getenv("PASSWD")) {
727 if(mountpassword == NULL)
728 mountpassword = calloc(65,1);
730 strncpy(mountpassword,getenv("PASSWD"),64);
733 } else if (getenv("PASSWD_FD")) {
734 get_password_from_file(atoi(getenv("PASSWD_FD")),NULL);
735 } else if (getenv("PASSWD_FILE")) {
736 get_password_from_file(0, getenv("PASSWD_FILE"));
739 ipaddr = parse_server(share_name);
744 if (orgoptions && parse_options(orgoptions, &flags))
747 /* BB save off path and pop after mount returns? */
748 resolved_path = malloc(PATH_MAX+1);
750 /* Note that if we can not canonicalize the name, we get
751 another chance to see if it is valid when we chdir to it */
752 if (realpath(mountpoint, resolved_path)) {
753 mountpoint = resolved_path;
756 if(chdir(mountpoint)) {
757 printf("mount error: can not change directory into mount target %s\n",mountpoint);
761 if(stat (".", &statbuf)) {
762 printf("mount error: mount point %s does not exist\n",mountpoint);
766 if (S_ISDIR(statbuf.st_mode) == 0) {
767 printf("mount error: mount point %s is not a directory\n",mountpoint);
771 if((getuid() != 0) && (geteuid() == 0)) {
772 if((statbuf.st_uid == getuid()) && (S_IRWXU == (statbuf.st_mode & S_IRWXU))) {
773 #ifndef CIFS_ALLOW_USR_SUID
774 /* Do not allow user mounts to control suid flag
775 for mount unless explicitly built that way */
776 flags |= MS_NOSUID | MS_NODEV;
779 printf("mount error: permission denied or not superuser and cifs.mount not installed SUID\n");
785 user_name = getusername();
787 if(got_password == 0) {
788 mountpassword = getpass("Password: "); /* BB obsolete */
791 /* FIXME launch daemon (handles dfs name resolution and credential change)
792 remember to clear parms and overwrite password field before launching */
794 optlen = strlen(orgoptions);
799 optlen += strlen(share_name) + 4;
801 optlen += strlen(user_name) + 6;
803 optlen += strlen(ipaddr) + 4;
805 optlen += strlen(mountpassword) + 6;
806 options = malloc(optlen + 10);
808 if(options == NULL) {
809 printf("Could not allocate memory for mount options\n");
815 strncat(options,"unc=",4);
816 strcat(options,share_name);
817 /* scan backwards and reverse direction of slash */
818 temp = strrchr(options, '/');
819 if(temp > options + 6)
822 strncat(options,",ip=",4);
823 strcat(options,ipaddr);
826 strncat(options,",user=",6);
827 strcat(options,user_name);
830 strncat(options,",pass=",6);
831 strcat(options,mountpassword);
833 strncat(options,",ver=",5);
834 strcat(options,MOUNT_CIFS_VERSION_MAJOR);
838 strcat(options,orgoptions);
841 printf("\ncifs.mount kernel mount options %s \n",options);
842 if(mount(share_name, mountpoint, "cifs", flags, options)) {
843 /* remember to kill daemon on error */
846 printf("mount failed but no error number set\n");
849 printf("mount error: cifs filesystem not supported by the system\n");
852 printf("mount error %d = %s\n",errno,strerror(errno));
854 printf("Refer to the mount.cifs(8) manual page (e.g.man mount.cifs)\n");
856 memset(mountpassword,0,64);
860 pmntfile = setmntent(MOUNTED, "a+");
862 mountent.mnt_fsname = share_name;
863 mountent.mnt_dir = mountpoint;
864 mountent.mnt_type = "cifs";
865 mountent.mnt_opts = malloc(200);
866 if(mountent.mnt_opts) {
867 memset(mountent.mnt_opts,0,200);
868 if(flags & MS_RDONLY)
869 strcat(mountent.mnt_opts,"ro");
871 strcat(mountent.mnt_opts,"rw");
872 if(flags & MS_MANDLOCK)
873 strcat(mountent.mnt_opts,",mand");
875 strcat(mountent.mnt_opts,",nomand");
876 if(flags & MS_NOEXEC)
877 strcat(mountent.mnt_opts,",noexec");
878 if(flags & MS_NOSUID)
879 strcat(mountent.mnt_opts,",nosuid");
881 strcat(mountent.mnt_opts,",nodev");
882 if(flags & MS_SYNCHRONOUS)
883 strcat(mountent.mnt_opts,",synch");
885 mountent.mnt_freq = 0;
886 mountent.mnt_passno = 0;
887 rc = addmntent(pmntfile,&mountent);
889 if(mountent.mnt_opts)
890 free(mountent.mnt_opts);
892 printf("could not update mount table\n");
896 memset(mountpassword,0,64);
901 memset(options,0,optlen);
906 memset(orgoptions,0,orgoptlen);