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 "3"
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("mount.cifs 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("mount.cifs 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("mount.cifs 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("mount.cifs 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 if(data
[4] == '\0') {
290 printf("\nskipping empty user mount parameter\n");
291 /* remove the parm since it would otherwise be confusing
292 to the kernel code which would think it was a real username */
297 /* BB remove it from mount line so as not to confuse kernel code */
299 printf("username specified with no parameter\n");
300 return 1; /* needs_arg; */
303 if (strnlen(value
, 260) < 260) {
305 percent_char
= strchr(value
,'%');
308 if(mountpassword
== NULL
)
309 mountpassword
= calloc(65,1);
312 printf("\nmount.cifs warning - password specified twice\n");
315 strncpy(mountpassword
, percent_char
,64);
316 /* remove password from username */
317 while(*percent_char
!= 0) {
324 printf("username too long\n");
328 } else if (strncmp(data
, "pass", 4) == 0) {
329 if (!value
|| !*value
) {
331 printf("\npassword specified twice, ignoring second\n");
334 } else if (strnlen(value
, 17) < 17) {
336 printf("\nmount.cifs warning - password specified twice\n");
339 printf("password too long\n");
342 } else if (strncmp(data
, "ip", 2) == 0) {
343 if (!value
|| !*value
) {
344 printf("target ip address argument missing");
345 } else if (strnlen(value
, 35) < 35) {
348 printf("ip address too long\n");
351 } else if ((strncmp(data
, "unc", 3) == 0)
352 || (strncmp(data
, "target", 6) == 0)
353 || (strncmp(data
, "path", 4) == 0)) {
354 if (!value
|| !*value
) {
355 printf("invalid path to network resource\n");
356 return 1; /* needs_arg; */
357 } else if(strnlen(value
,5) < 5) {
358 printf("UNC name too short");
361 if (strnlen(value
, 300) < 300) {
363 if (strncmp(value
, "//", 2) == 0) {
365 printf("unc name specified twice, ignoring second\n");
368 } else if (strncmp(value
, "\\\\", 2) != 0) {
369 printf("UNC Path does not begin with // or \\\\ \n");
373 printf("unc name specified twice, ignoring second\n");
378 printf("CIFS: UNC name too long\n");
381 } else if ((strncmp(data
, "domain", 3) == 0)
382 || (strncmp(data
, "workgroup", 5) == 0)) {
383 if (!value
|| !*value
) {
384 printf("CIFS: invalid domain name\n");
385 return 1; /* needs_arg; */
387 if (strnlen(value
, 65) < 65) {
390 printf("domain name too long\n");
393 } else if (strncmp(data
, "cred", 4) == 0) {
394 if (value
&& *value
) {
395 rc
= open_cred_file(value
);
397 printf("error %d opening credential file %s\n",rc
, value
);
401 printf("invalid credential file name specified\n");
404 } else if (strncmp(data
, "uid", 3) == 0) {
405 if (value
&& *value
) {
408 } else if (strncmp(data
, "gid", 3) == 0) {
409 if (value
&& *value
) {
412 /* fmask and dmask synonyms for people used to smbfs syntax */
413 } else if (strcmp(data
, "file_mode") == 0 || strcmp(data
, "fmask")==0) {
414 if (!value
|| !*value
) {
415 printf ("Option '%s' requires a numerical argument\n", data
);
419 if (value
[0] != '0') {
420 printf ("WARNING: '%s' not expressed in octal.\n", data
);
423 if (strcmp (data
, "fmask") == 0) {
424 printf ("WARNING: CIFS mount option 'fmask' is deprecated. Use 'file_mode' instead.\n");
425 data
= "file_mode"; /* BB fix this */
427 } else if (strcmp(data
, "dir_mode") == 0 || strcmp(data
, "dmask")==0) {
428 if (!value
|| !*value
) {
429 printf ("Option '%s' requires a numerical argument\n", data
);
433 if (value
[0] != '0') {
434 printf ("WARNING: '%s' not expressed in octal.\n", data
);
437 if (strcmp (data
, "dmask") == 0) {
438 printf ("WARNING: CIFS mount option 'dmask' is deprecated. Use 'dir_mode' instead.\n");
441 /* the following eight mount options should be
442 stripped out from what is passed into the kernel
443 since these eight options are best passed as the
444 mount flags rather than redundantly to the kernel
445 and could generate spurious warnings depending on the
446 level of the corresponding cifs vfs kernel code */
447 } else if (strncmp(data
, "nosuid", 6) == 0) {
448 *filesys_flags
|= MS_NOSUID
;
449 } else if (strncmp(data
, "suid", 4) == 0) {
450 *filesys_flags
&= ~MS_NOSUID
;
451 } else if (strncmp(data
, "nodev", 5) == 0) {
452 *filesys_flags
|= MS_NODEV
;
453 } else if (strncmp(data
, "dev", 3) == 0) {
454 *filesys_flags
&= ~MS_NODEV
;
455 } else if (strncmp(data
, "noexec", 6) == 0) {
456 *filesys_flags
|= MS_NOEXEC
;
457 } else if (strncmp(data
, "exec", 4) == 0) {
458 *filesys_flags
&= ~MS_NOEXEC
;
459 } else if (strncmp(data
, "guest", 5) == 0) {
461 } else if (strncmp(data
, "ro", 2) == 0) {
462 *filesys_flags
|= MS_RDONLY
;
463 } else if (strncmp(data
, "rw", 2) == 0) {
464 *filesys_flags
&= ~MS_RDONLY
;
465 } /* else if (strnicmp(data, "port", 4) == 0) {
466 if (value && *value) {
468 simple_strtoul(value, &value, 0);
470 } else if (strnicmp(data, "rsize", 5) == 0) {
471 if (value && *value) {
473 simple_strtoul(value, &value, 0);
475 } else if (strnicmp(data, "wsize", 5) == 0) {
476 if (value && *value) {
478 simple_strtoul(value, &value, 0);
480 } else if (strnicmp(data, "version", 3) == 0) {
482 printf("CIFS: Unknown mount option %s\n",data);
483 } */ /* nothing to do on those four mount options above.
484 Just pass to kernel and ignore them here */
486 /* move to next option */
487 data
= next_keyword
+1;
489 /* put overwritten equals sign back */
495 /* put previous overwritten comma back */
504 /* Note that caller frees the returned buffer if necessary */
505 char * parse_server(char * unc_name
)
507 int length
= strnlen(unc_name
,1024);
509 char * ipaddress_string
= NULL
;
510 struct hostent
* host_entry
;
511 struct in_addr server_ipaddr
;
515 printf("mount error: UNC name too long");
518 if (strncasecmp("cifs://",unc_name
,7) == 0)
519 return parse_cifs_url(unc_name
+7);
520 if (strncasecmp("smb://",unc_name
,6) == 0) {
521 return parse_cifs_url(unc_name
+6);
525 /* BB add code to find DFS root here */
526 printf("\nMounting the DFS root for domain not implemented yet");
529 if(strncmp(unc_name
,"//",2) && strncmp(unc_name
,"\\\\",2)) {
530 printf("mount error: improperly formatted UNC name.");
531 printf(" %s does not begin with \\\\ or //\n",unc_name
);
538 if ((share
= strchr(unc_name
, '/')) ||
539 (share
= strchr(unc_name
,'\\'))) {
540 *share
= 0; /* temporarily terminate the string */
542 host_entry
= gethostbyname(unc_name
);
543 *(share
- 1) = '/'; /* put the slash back */
544 /* rc = getipnodebyname(unc_name, AF_INET, AT_ADDRCONFIG ,&rc);*/
545 if(host_entry
== NULL
) {
546 printf("mount error: could not find target server. TCP name %s not found ", unc_name
);
547 printf(" rc = %d\n",rc
);
551 /* BB should we pass an alternate version of the share name as Unicode */
552 /* BB what about ipv6? BB */
553 /* BB add retries with alternate servers in list */
555 memcpy(&server_ipaddr
.s_addr
, host_entry
->h_addr
, 4);
557 ipaddress_string
= inet_ntoa(server_ipaddr
);
558 if(ipaddress_string
== NULL
) {
559 printf("mount error: could not get valid ip address for target server\n");
562 return ipaddress_string
;
565 /* BB add code to find DFS root (send null path on get DFS Referral to specified server here */
566 printf("Mounting the DFS root for a particular server not implemented yet\n");
573 static struct option longopts
[] = {
574 { "all", 0, 0, 'a' },
575 { "help",0, 0, 'h' },
576 { "move",0, 0, 'm' },
577 { "bind",0, 0, 'b' },
578 { "read-only", 0, 0, 'r' },
580 { "verbose", 0, 0, 'v' },
581 { "version", 0, 0, 'V' },
582 { "read-write", 0, 0, 'w' },
584 { "options", 1, 0, 'o' },
585 { "type", 1, 0, 't' },
586 { "rsize",1, 0, 'R' },
587 { "wsize",1, 0, 'W' },
591 { "username",1,0,'u'},
594 { "password",1,0,'p'},
596 { "credentials",1,0,'c'},
598 /* { "uuid",1,0,'U'}, */ /* BB unimplemented */
602 int main(int argc
, char ** argv
)
605 int flags
= MS_MANDLOCK
; /* no need to set legacy MS_MGC_VAL */
606 char * orgoptions
= NULL
;
607 char * share_name
= NULL
;
608 char * domain_name
= NULL
;
609 char * ipaddr
= NULL
;
613 char * resolved_path
;
624 struct utsname sysinfo
;
625 struct mntent mountent
;
628 /* setlocale(LC_ALL, "");
629 bindtextdomain(PACKAGE, LOCALEDIR);
630 textdomain(PACKAGE); */
633 thisprogram
= argv
[0];
635 if(thisprogram
== NULL
)
636 thisprogram
= "mount.cifs";
639 /* BB add workstation name and domain and pass down */
641 /* #ifdef _GNU_SOURCE
642 printf(" node: %s machine: %s sysname %s domain %s\n", sysinfo.nodename,sysinfo.machine,sysinfo.sysname,sysinfo.domainname);
645 share_name
= argv
[1];
646 mountpoint
= argv
[2];
648 /* add sharename in opts string as unc= parm */
650 while ((c
= getopt_long (argc
, argv
, "afFhilL:no:O:rsSU:vVwt:",
651 longopts
, NULL
)) != -1) {
653 /* No code to do the following options yet */
655 list_with_volumelabel = 1;
658 volumelabel = optarg;
678 orgoptions
= strdup(optarg
);
680 case 'r': /* mount readonly */
690 printf ("mount.cifs version: %s.%s%s\n",
691 MOUNT_CIFS_VERSION_MAJOR
,
692 MOUNT_CIFS_VERSION_MINOR
,
693 MOUNT_CIFS_VENDOR_SUFFIX
);
695 memset(mountpassword
,0,64);
702 rsize
= atoi(optarg
) ;
705 wsize
= atoi(optarg
);
718 domain_name
= optarg
;
721 if(mountpassword
== NULL
)
722 mountpassword
= calloc(65,1);
725 strncpy(mountpassword
,optarg
,64);
729 get_password_from_file(0 /* stdin */,NULL
);
734 printf("unknown mount option %c\n",c
);
743 if (getenv("PASSWD")) {
744 if(mountpassword
== NULL
)
745 mountpassword
= calloc(65,1);
747 strncpy(mountpassword
,getenv("PASSWD"),64);
750 } else if (getenv("PASSWD_FD")) {
751 get_password_from_file(atoi(getenv("PASSWD_FD")),NULL
);
752 } else if (getenv("PASSWD_FILE")) {
753 get_password_from_file(0, getenv("PASSWD_FILE"));
756 ipaddr
= parse_server(share_name
);
761 if (orgoptions
&& parse_options(orgoptions
, &flags
))
764 /* BB save off path and pop after mount returns? */
765 resolved_path
= malloc(PATH_MAX
+1);
767 /* Note that if we can not canonicalize the name, we get
768 another chance to see if it is valid when we chdir to it */
769 if (realpath(mountpoint
, resolved_path
)) {
770 mountpoint
= resolved_path
;
773 if(chdir(mountpoint
)) {
774 printf("mount error: can not change directory into mount target %s\n",mountpoint
);
778 if(stat (".", &statbuf
)) {
779 printf("mount error: mount point %s does not exist\n",mountpoint
);
783 if (S_ISDIR(statbuf
.st_mode
) == 0) {
784 printf("mount error: mount point %s is not a directory\n",mountpoint
);
788 if((getuid() != 0) && (geteuid() == 0)) {
789 if((statbuf
.st_uid
== getuid()) && (S_IRWXU
== (statbuf
.st_mode
& S_IRWXU
))) {
790 #ifndef CIFS_ALLOW_USR_SUID
791 /* Do not allow user mounts to control suid flag
792 for mount unless explicitly built that way */
793 flags
|= MS_NOSUID
| MS_NODEV
;
796 printf("mount error: permission denied or not superuser and mount.cifs not installed SUID\n");
802 user_name
= getusername();
804 if(got_password
== 0) {
805 mountpassword
= getpass("Password: "); /* BB obsolete */
808 /* FIXME launch daemon (handles dfs name resolution and credential change)
809 remember to clear parms and overwrite password field before launching */
811 optlen
= strlen(orgoptions
);
816 optlen
+= strlen(share_name
) + 4;
818 optlen
+= strlen(user_name
) + 6;
820 optlen
+= strlen(ipaddr
) + 4;
822 optlen
+= strlen(mountpassword
) + 6;
823 options
= malloc(optlen
+ 10);
825 if(options
== NULL
) {
826 printf("Could not allocate memory for mount options\n");
832 strncat(options
,"unc=",4);
833 strcat(options
,share_name
);
834 /* scan backwards and reverse direction of slash */
835 temp
= strrchr(options
, '/');
836 if(temp
> options
+ 6)
839 strncat(options
,",ip=",4);
840 strcat(options
,ipaddr
);
843 strncat(options
,",user=",6);
844 strcat(options
,user_name
);
847 strncat(options
,",pass=",6);
848 strcat(options
,mountpassword
);
850 strncat(options
,",ver=",5);
851 strcat(options
,MOUNT_CIFS_VERSION_MAJOR
);
855 strcat(options
,orgoptions
);
858 printf("\nmount.cifs kernel mount options %s \n",options
);
859 if(mount(share_name
, mountpoint
, "cifs", flags
, options
)) {
860 /* remember to kill daemon on error */
863 printf("mount failed but no error number set\n");
866 printf("mount error: cifs filesystem not supported by the system\n");
869 printf("mount error %d = %s\n",errno
,strerror(errno
));
871 printf("Refer to the mount.cifs(8) manual page (e.g.man mount.cifs)\n");
873 memset(mountpassword
,0,64);
877 pmntfile
= setmntent(MOUNTED
, "a+");
879 mountent
.mnt_fsname
= share_name
;
880 mountent
.mnt_dir
= mountpoint
;
881 mountent
.mnt_type
= "cifs";
882 mountent
.mnt_opts
= malloc(200);
883 if(mountent
.mnt_opts
) {
884 memset(mountent
.mnt_opts
,0,200);
885 if(flags
& MS_RDONLY
)
886 strcat(mountent
.mnt_opts
,"ro");
888 strcat(mountent
.mnt_opts
,"rw");
889 if(flags
& MS_MANDLOCK
)
890 strcat(mountent
.mnt_opts
,",mand");
892 strcat(mountent
.mnt_opts
,",nomand");
893 if(flags
& MS_NOEXEC
)
894 strcat(mountent
.mnt_opts
,",noexec");
895 if(flags
& MS_NOSUID
)
896 strcat(mountent
.mnt_opts
,",nosuid");
898 strcat(mountent
.mnt_opts
,",nodev");
899 if(flags
& MS_SYNCHRONOUS
)
900 strcat(mountent
.mnt_opts
,",synch");
902 mountent
.mnt_freq
= 0;
903 mountent
.mnt_passno
= 0;
904 rc
= addmntent(pmntfile
,&mountent
);
906 if(mountent
.mnt_opts
)
907 free(mountent
.mnt_opts
);
909 printf("could not update mount table\n");
913 memset(mountpassword
,0,64);
918 memset(options
,0,optlen
);
923 memset(orgoptions
,0,orgoptlen
);