2 * Mount helper utility for Linux CIFS VFS (virtual filesystem) client
3 * Copyright (C) 2003,2010 Steve French (sfrench@us.ibm.com)
4 * Copyright (C) 2008 Jeremy Allison (jra@samba.org)
5 * Copyright (C) 2010 Jeff Layton (jlayton@samba.org)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #endif /* HAVE_CONFIG_H */
31 #include <sys/types.h>
32 #include <sys/mount.h>
34 #include <sys/utsname.h>
35 #include <sys/socket.h>
36 #include <arpa/inet.h>
50 #ifdef HAVE_SYS_FSUID_H
51 #include <sys/fsuid.h>
52 #endif /* HAVE_SYS_FSUID_H */
55 #else /* HAVE_LIBCAP_NG */
57 #include <sys/prctl.h>
58 #endif /* HAVE_PRCTL */
60 #include <sys/capability.h>
61 #endif /* HAVE_LIBCAP */
62 #endif /* HAVE_LIBCAP_NG */
65 #include "resolve_host.h"
66 #include "build_assert.h"
76 /* private flags - clear these before passing to kernel */
77 #define MS_USERS 0x40000000
78 #define MS_USER 0x80000000
80 #define MAX_UNC_LEN 1024
82 /* I believe that the kernel limits options data to a page */
83 #define MAX_OPTIONS_LEN 4096
85 /* max length of mtab options */
86 #define MTAB_OPTIONS_LEN 220
89 * Max share name, username, password and domain sizes match the kernel's
90 * allowances for these string sizes which in turn match Microsoft's
94 /* Max length of the share name portion of a UNC. Share names over 80
95 * characters cannot be accessed via commandline in Windows 2000/XP. */
96 #define MAX_SHARE_LEN 256
98 /* Max user name length. */
99 #define MAX_USERNAME_SIZE 256
101 /* Max domain size. */
102 #define MAX_DOMAIN_SIZE 256
104 /* Max password size. */
105 #define MOUNT_PASSWD_SIZE 512
108 * mount.cifs has been the subject of many "security" bugs that have arisen
109 * because of users and distributions installing it as a setuid root program
110 * before it had been audited for security holes. The default behavior is
111 * now to allow mount.cifs to be run as a setuid root program. Some admins
112 * may want to disable this fully, so this switch remains in place.
114 #define CIFS_DISABLE_SETUID_CAPABILITY 0
117 * When an unprivileged user runs a setuid mount.cifs, we set certain mount
118 * flags by default. These defaults can be changed here.
120 #define CIFS_SETUID_FLAGS (MS_NOSUID|MS_NODEV)
123 * Values for parsing a credentials file.
125 #define CRED_UNPARSEABLE 0
132 * Values for parsing command line options.
138 #define OPT_USER_XATTR 3
147 #define OPT_FILE_MODE 12
149 #define OPT_DIR_MODE 14
151 #define OPT_NO_SUID 16
153 #define OPT_NO_DEV 18
155 #define OPT_NO_LOCK 20
156 #define OPT_NO_EXEC 21
161 #define OPT_REMOUNT 26
163 #define OPT_NOMAND 28
165 #define OPT_BKUPUID 30
166 #define OPT_BKUPGID 31
167 #define OPT_NOFAIL 32
168 #define OPT_SNAPSHOT 33
171 #define MNT_TMP_FILE "/.mtab.cifs.XXXXXX"
173 #define GMT_NAME_LEN 24 /* length of a @GMT- name */
174 #define GMT_FORMAT "@GMT-%Y.%m.%d-%H.%M.%S"
176 #define NTFS_TIME_OFFSET ((unsigned long long)(369*365 + 89) * 24 * 3600 * 10000000)
179 * struct for holding parsed mount info for use by privileged process.
180 * Please do not keep pointers in this struct.
181 * That way, reinit of this struct is a simple memset.
183 struct parsed_mount_info {
185 char host[NI_MAXHOST + 1];
186 char share[MAX_SHARE_LEN + 1];
187 char prefix[PATH_MAX + 1];
188 char options[MAX_OPTIONS_LEN];
189 char domain[MAX_DOMAIN_SIZE + 1];
190 char username[MAX_USERNAME_SIZE + 1];
191 char password[MOUNT_PASSWD_SIZE + 1];
192 char password2[MOUNT_PASSWD_SIZE + 1];
193 char addrlist[MAX_ADDR_LIST_LEN];
194 unsigned int got_user:1;
195 unsigned int got_password:1;
196 unsigned int got_password2:1;
197 unsigned int fakemnt:1;
198 unsigned int nomtab:1;
199 unsigned int verboseflag:1;
200 unsigned int nofail:1;
201 unsigned int got_domain:1;
202 unsigned int is_krb5:1;
203 unsigned int is_noauth:1;
204 unsigned int is_guest:1;
208 static const char *thisprogram;
209 static const char *cifs_fstype;
211 static int parse_unc(const char *unc_name, struct parsed_mount_info *parsed_info, const char *progname);
213 static int check_setuid(void)
216 fprintf(stderr, "This program is not installed setuid root - "
217 " \"user\" CIFS mounts not supported.\n");
221 #if CIFS_DISABLE_SETUID_CAPABILITY
222 if (getuid() && !geteuid()) {
223 printf("This program has been built with the "
224 "ability to run as a setuid root program disabled.\n");
227 #endif /* CIFS_DISABLE_SETUID_CAPABILITY */
233 check_fstab(const char *progname, const char *mountpoint, const char *devname,
240 /* make sure this mount is listed in /etc/fstab */
241 fstab = setmntent(_PATH_MNTTAB, "r");
243 fprintf(stderr, "Couldn't open %s for reading!\n", _PATH_MNTTAB);
247 while ((mnt = getmntent(fstab))) {
248 len = strlen(mnt->mnt_dir);
250 if (mnt->mnt_dir[len - 1] == '/')
251 mnt->mnt_dir[len - 1] = '\0';
256 if (!strcmp(mountpoint, mnt->mnt_dir))
261 if (mnt == NULL || strcmp(mnt->mnt_fsname, devname)) {
262 fprintf(stderr, "%s: permission denied: no match for "
263 "%s found in %s\n", progname, mountpoint, _PATH_MNTTAB);
268 * 'mount' munges the options from fstab before passing them
269 * to us. It is non-trivial to test that we have the correct
270 * set of options. We don't want to trust what the user
271 * gave us, so just take whatever is in /etc/fstab.
273 *options = strdup(mnt->mnt_opts);
280 open nofollow - avoid symlink exposure?
281 get owner of dir see if matches self or if root
282 call system(umount argv) etc.
286 static int mount_usage(FILE * stream)
288 fprintf(stream, "\nUsage: %s <remotetarget> <dir> -o <options>\n",
290 fprintf(stream, "\nMount the remote target, specified as a UNC name,");
291 fprintf(stream, " to a local directory.\n\nOptions:\n");
292 fprintf(stream, "\tuser=<arg>\n\tpass=<arg>\n\tdom=<arg>\n");
293 fprintf(stream, "\nLess commonly used options:");
295 "\n\tcredentials=<filename>,guest,perm,noperm,setuids,nosetuids,rw,ro,");
297 "\n\tsep=<char>,iocharset=<codepage>,suid,nosuid,exec,noexec,serverino,");
299 "\n\tnoserverino,mapchars,nomapchars,nolock,servernetbiosname=<SRV_RFC1001NAME>");
301 "\n\tcache=<strict|none|loose>,nounix,cifsacl,sec=<authentication mechanism>,");
303 "\n\tsign,seal,fsc,snapshot=<token|time>,nosharesock,persistenthandles,");
305 "\n\tresilienthandles,rdma,vers=<smb_dialect>,cruid,password2=<alt password>");
307 "\n\nOptions not needed for servers supporting CIFS Unix extensions");
309 "\n\t(e.g. unneeded for mounts to most Samba versions):");
311 "\n\tuid=<uid>,gid=<gid>,dir_mode=<mode>,file_mode=<mode>,sfu,");
313 "\n\tmfsymlinks,idsfromsid");
314 fprintf(stream, "\n\nRarely used options:");
316 "\n\tport=<tcpport>,rsize=<size>,wsize=<size>,unc=<unc_name>,ip=<ip_address>,");
318 "\n\tdev,nodev,nouser_xattr,netbiosname=<OUR_RFC1001NAME>,hard,soft,intr,");
320 "\n\tnointr,ignorecase,noposixpaths,noacl,prefixpath=<path>,nobrl,");
322 "\n\techo_interval=<seconds>,actimeo=<seconds>,max_credits=<credits>,");
326 "\n\nOptions are described in more detail in the manual page");
327 fprintf(stream, "\n\tman 8 %s\n", thisprogram);
328 fprintf(stream, "\nTo display the version number of the mount helper:");
329 fprintf(stream, "\n\t%s -V\n", thisprogram);
331 if (stream == stderr)
337 * CIFS has to "escape" commas in the password field so that they don't
338 * end up getting confused for option delimiters. Copy password into pw
339 * field, turning any commas into double commas.
342 set_password(struct parsed_mount_info *parsed_info, const char *src,
343 const int which_pass)
345 int is_pass2 = which_pass == OPT_PASS2 || which_pass == CRED_PASS2;
347 char *dst = is_pass2 ? parsed_info->password2 : parsed_info->password;
348 unsigned int i = 0, j = 0;
349 BUILD_ASSERT(sizeof(parsed_info->password) == sizeof(parsed_info->password2));
352 if (j + 2 >= sizeof(parsed_info->password)) {
353 fprintf(stderr, "Converted password too long!\n");
362 parsed_info->got_password2 = 1;
364 parsed_info->got_password = 1;
369 #ifdef HAVE_LIBCAP_NG
371 drop_capabilities(int parent)
373 capng_select_t set = CAPNG_SELECT_CAPS;
375 capng_setpid(getpid());
376 capng_clear(CAPNG_SELECT_BOTH);
378 if (capng_updatev(CAPNG_ADD, CAPNG_PERMITTED, CAP_DAC_READ_SEARCH, CAP_DAC_OVERRIDE, -1)) {
379 fprintf(stderr, "Unable to update capability set.\n");
382 if (capng_update(CAPNG_ADD, CAPNG_PERMITTED|CAPNG_EFFECTIVE, CAP_SYS_ADMIN)) {
383 fprintf(stderr, "Unable to update capability set.\n");
387 if (capng_update(CAPNG_ADD, CAPNG_PERMITTED, CAP_DAC_READ_SEARCH)) {
388 fprintf(stderr, "Unable to update capability set.\n");
392 if (capng_have_capability(CAPNG_EFFECTIVE, CAP_SETPCAP)) {
393 set = CAPNG_SELECT_BOTH;
395 if (capng_apply(set)) {
396 fprintf(stderr, "Unable to apply new capability set.\n");
403 toggle_dac_capability(int writable, int enable)
405 unsigned int capability = writable ? CAP_DAC_OVERRIDE : CAP_DAC_READ_SEARCH;
407 if (capng_update(enable ? CAPNG_ADD : CAPNG_DROP, CAPNG_EFFECTIVE, capability)) {
408 fprintf(stderr, "Unable to update capability set.\n");
411 if (capng_apply(CAPNG_SELECT_CAPS)) {
412 fprintf(stderr, "Unable to apply new capability set.\n");
417 #else /* HAVE_LIBCAP_NG */
421 prune_bounding_set(void)
424 static int bounding_set_cleared;
426 if (bounding_set_cleared)
429 for (i = 0; i <= CAP_LAST_CAP && rc == 0; ++i)
430 rc = prctl(PR_CAPBSET_DROP, i);
433 fprintf(stderr, "Unable to clear capability bounding set: %d\n", rc);
437 ++bounding_set_cleared;
440 #else /* HAVE_PRCTL */
442 prune_bounding_set(void)
446 #endif /* HAVE_PRCTL */
448 drop_capabilities(int parent)
452 cap_value_t cap_list[3];
454 rc = prune_bounding_set();
458 caps = cap_get_proc();
460 fprintf(stderr, "Unable to get current capability set: %s\n",
465 if (cap_clear(caps) == -1) {
466 fprintf(stderr, "Unable to clear capability set: %s\n",
472 if (parent || getuid() == 0) {
474 cap_list[0] = CAP_DAC_READ_SEARCH;
476 cap_list[1] = CAP_DAC_OVERRIDE;
477 cap_list[2] = CAP_SYS_ADMIN;
480 if (cap_set_flag(caps, CAP_PERMITTED, ncaps, cap_list, CAP_SET) == -1) {
481 fprintf(stderr, "Unable to set permitted capabilities: %s\n",
487 cap_list[0] = CAP_SYS_ADMIN;
488 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, cap_list, CAP_SET) == -1) {
489 fprintf(stderr, "Unable to set effective capabilities: %s\n",
497 if (cap_set_proc(caps) != 0) {
498 fprintf(stderr, "Unable to set current process capabilities: %s\n",
508 toggle_dac_capability(int writable, int enable)
512 cap_value_t capability = writable ? CAP_DAC_OVERRIDE : CAP_DAC_READ_SEARCH;
514 caps = cap_get_proc();
516 fprintf(stderr, "Unable to get current capability set: %s\n",
521 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, &capability,
522 enable ? CAP_SET : CAP_CLEAR) == -1) {
523 fprintf(stderr, "Unable to %s effective capabilities: %s\n",
524 enable ? "set" : "clear", strerror(errno));
529 if (cap_set_proc(caps) != 0) {
530 fprintf(stderr, "Unable to set current process capabilities: %s\n",
538 #else /* HAVE_LIBCAP */
540 drop_capabilities(int parent __attribute((unused)))
546 toggle_dac_capability(int writable __attribute((unused)), int enable __attribute((unused)))
550 #endif /* HAVE_LIBCAP */
551 #endif /* HAVE_LIBCAP_NG */
553 static void null_terminate_endl(char *source)
555 char *newline = strchr(source, '\n');
561 * Parse a line from the credentials file. Changes target to first
562 * character after '=' on 'line' and returns the value type of the line
563 * Returns CRED_UNPARSEABLE on failure or if either parameter is NULL.
565 static int parse_cred_line(char *line, char **target)
567 if (line == NULL || target == NULL)
570 /* position target at first char of value */
571 *target = strchr(line, '=');
576 /* tell the caller which value target points to */
577 if (strncasecmp("user", line, 4) == 0)
579 if (strncasecmp("pass2", line, 5) == 0 ||
580 strncasecmp("password2", line, 9) == 0)
582 if (strncasecmp("pass", line, 4) == 0)
584 if (strncasecmp("dom", line, 3) == 0)
588 return CRED_UNPARSEABLE;
591 static int check_creds_field(const char *v)
593 return strpbrk(v, ",=\r\n") != NULL;
596 static int open_cred_file(char *file_name,
597 struct parsed_mount_info *parsed_info)
599 char *line_buf = NULL;
600 char *temp_val = NULL;
603 const int line_buf_size = 4096;
604 const int min_non_white = 10;
606 i = toggle_dac_capability(0, 1);
610 i = access(file_name, R_OK);
612 toggle_dac_capability(0, 0);
617 fs = fopen(file_name, "r");
619 toggle_dac_capability(0, 0);
624 i = toggle_dac_capability(0, 0);
628 line_buf = (char *)malloc(line_buf_size);
629 if (line_buf == NULL) {
634 /* parse line from credentials file */
635 while (fgets(line_buf, line_buf_size, fs)) {
636 /* eat leading white space */
637 for (i = 0; i < line_buf_size - min_non_white + 1; i++) {
638 if ((line_buf[i] != ' ') && (line_buf[i] != '\t'))
641 null_terminate_endl(line_buf);
643 /* parse next token */
644 switch (parse_cred_line(line_buf + i, &temp_val)) {
646 if (check_creds_field(temp_val)) {
647 fprintf(stderr, "Invalid username in credentials file\n");
651 strlcpy(parsed_info->username, temp_val,
652 sizeof(parsed_info->username));
653 parsed_info->got_user = 1;
656 i = set_password(parsed_info, temp_val, CRED_PASS);
661 i = set_password(parsed_info, temp_val, CRED_PASS2);
666 if (check_creds_field(temp_val)) {
667 fprintf(stderr, "Invalid domain in credentials file\n");
671 strlcpy(parsed_info->domain, temp_val,
672 sizeof(parsed_info->domain));
674 case CRED_UNPARSEABLE:
675 if (parsed_info->verboseflag)
676 fprintf(stderr, "Credential formatted "
686 /* make sure passwords are scrubbed from memory */
687 if (line_buf != NULL)
688 memset(line_buf, 0, line_buf_size);
694 get_password_from_file(int file_descript, char *filename,
695 struct parsed_mount_info *parsed_info, const char *program,
696 const int which_pass)
699 char buf[sizeof(parsed_info->password)];
700 BUILD_ASSERT(sizeof(parsed_info->password) == sizeof(parsed_info->password2));
702 if (filename != NULL) {
703 rc = toggle_dac_capability(0, 1);
707 rc = access(filename, R_OK);
710 "%s failed: access check of %s failed: %s\n",
711 program, filename, strerror(errno));
712 toggle_dac_capability(0, 0);
716 file_descript = open(filename, O_RDONLY);
717 if (file_descript < 0) {
719 "%s failed. %s attempting to open password file %s\n",
720 program, strerror(errno), filename);
721 toggle_dac_capability(0, 0);
725 rc = toggle_dac_capability(0, 0);
732 memset(buf, 0, sizeof(buf));
733 rc = read(file_descript, buf, sizeof(buf) - 1);
736 "%s failed. Error %s reading password file\n",
737 program, strerror(errno));
742 rc = set_password(parsed_info, buf, which_pass);
745 if (filename != NULL)
746 close(file_descript);
751 * Returns OPT_ERROR on unparsable token.
753 static int parse_opt_token(const char *token)
759 * token is NULL terminated and contains exactly the
760 * keyword so we can match exactly
762 if (strcmp(token, "users") == 0)
764 if (strcmp(token, "user_xattr") == 0)
765 return OPT_USER_XATTR;
766 if (strcmp(token, "user") == 0 ||
767 strcmp(token, "username") == 0)
769 if (strcmp(token, "pass") == 0 ||
770 strcmp(token, "password") == 0)
772 if (strcmp(token, "pass2") == 0 ||
773 strcmp(token, "password2") == 0)
775 if (strcmp(token, "sec") == 0)
777 if (strcmp(token, "ip") == 0 ||
778 strcmp(token, "addr") == 0)
780 if (strcmp(token, "unc") == 0 ||
781 strcmp(token, "target") == 0 ||
782 strcmp(token, "path") == 0)
784 if (strcmp(token, "dom") == 0 ||
785 strcmp(token, "domain") == 0 ||
786 strcmp(token, "workgroup") == 0)
788 if (strcmp(token, "cred") == 0 || /* undocumented */
789 strcmp(token, "credentials") == 0)
791 if (strcmp(token, "uid") == 0)
793 if (strcmp(token, "cruid") == 0)
795 if (strcmp(token, "gid") == 0)
797 if (strcmp(token, "fmask") == 0)
799 if (strcmp(token, "file_mode") == 0)
800 return OPT_FILE_MODE;
801 if (strcmp(token, "dmask") == 0)
803 if (strcmp(token, "dir_mode") == 0 ||
804 strcmp(token, "dirm") == 0)
806 if (strcmp(token, "nosuid") == 0)
808 if (strcmp(token, "suid") == 0)
810 if (strcmp(token, "nodev") == 0)
812 if (strcmp(token, "nobrl") == 0 ||
813 strcmp(token, "nolock") == 0)
815 if (strcmp(token, "mand") == 0)
817 if (strcmp(token, "nomand") == 0)
819 if (strcmp(token, "dev") == 0)
821 if (strcmp(token, "noexec") == 0)
823 if (strcmp(token, "exec") == 0)
825 if (strcmp(token, "guest") == 0)
827 if (strcmp(token, "ro") == 0)
829 if (strcmp(token, "rw") == 0)
831 if (strcmp(token, "remount") == 0)
833 if (strcmp(token, "_netdev") == 0)
835 if (strcmp(token, "backupuid") == 0)
837 if (strcmp(token, "backupgid") == 0)
839 if (strcmp(token, "nofail") == 0)
841 if (strcmp(token, "comment") == 0)
843 if (strncmp(token, "x-", 2) == 0)
845 if (strncmp(token, "snapshot", 8) == 0)
852 parse_options(char *data, struct parsed_mount_info *parsed_info)
856 char *next_keyword = NULL;
857 char *out = parsed_info->options;
858 unsigned long *filesys_flags = &parsed_info->flags;
867 int got_snapshot = 0;
868 uid_t uid, cruid = 0, bkupuid = 0;
869 gid_t gid, bkupgid = 0;
874 * max 64-bit uint in decimal is 18446744073709551615 which is 20 chars
875 * wide +1 for NULL, and +1 for good measure
878 unsigned long long snapshot = 0;
881 /* make sure we're starting from beginning */
884 /* BB fixme check for separator override BB */
897 * format is keyword,keyword2=value2,keyword3=value3...
898 * data = next keyword
899 * value = next value ie stuff after equal sign
901 while (data && *data) {
902 next_keyword = strchr(data, ','); /* BB handle sep= */
904 /* temporarily null terminate end of keyword=value pair */
908 /* temporarily null terminate keyword if there's a value */
910 if ((equals = strchr(data, '=')) != NULL) {
915 switch(parse_opt_token(data)) {
917 if (!value || !*value) {
918 *filesys_flags |= MS_USERS;
924 if (!value || !*value) {
925 if (data[4] == '\0') {
926 *filesys_flags |= MS_USER;
930 "username specified with no parameter\n");
934 strlcpy(parsed_info->username, value,
935 sizeof(parsed_info->username));
936 parsed_info->got_user = 1;
941 if (parsed_info->got_password) {
943 "password specified twice, ignoring second\n");
946 if (!value || !*value) {
947 parsed_info->got_password = 1;
950 rc = set_password(parsed_info, value, OPT_PASS);
956 if (parsed_info->got_password2) {
958 "password2 specified twice, ignoring second\n");
961 if (!value || !*value) {
962 parsed_info->got_password2 = 1;
965 rc = set_password(parsed_info, value, OPT_PASS2);
972 if (!strncmp(value, "none", 4)) {
973 parsed_info->is_noauth = 1;
974 parsed_info->got_password = 1;
975 parsed_info->got_password2 = 1;
977 if (!strncmp(value, "krb5", 4)) {
978 parsed_info->is_krb5 = 1;
979 parsed_info->got_password = 1;
980 parsed_info->got_password2 = 1;
986 if (!value || !*value) {
988 "target ip address argument missing\n");
989 } else if (strnlen(value, MAX_ADDRESS_LEN) <
991 strlcpy(parsed_info->addrlist, value,
993 if (parsed_info->verboseflag)
995 "ip address %s override specified\n",
999 fprintf(stderr, "ip address too long\n");
1005 /* unc || target || path */
1007 if (!value || !*value) {
1009 "invalid path to network resource\n");
1012 rc = parse_unc(value, parsed_info, thisprogram);
1017 /* dom || workgroup */
1021 * An empty domain has been passed
1023 /* not necessary but better safe than.. */
1024 parsed_info->domain[0] = '\0';
1025 parsed_info->got_domain = 1;
1028 if (strnlen(value, sizeof(parsed_info->domain)) >=
1029 sizeof(parsed_info->domain)) {
1030 fprintf(stderr, "domain name too long\n");
1033 strlcpy(parsed_info->domain, value,
1034 sizeof(parsed_info->domain));
1038 if (!value || !*value) {
1040 "invalid credential file name specified\n");
1043 rc = open_cred_file(value, parsed_info);
1046 "error %d (%s) opening credential file %s\n",
1047 rc, strerror(rc), value);
1053 if (!value || !*value)
1057 pw = getpwnam(value);
1064 uid = strtoul(value, &ep, 10);
1065 if (errno == 0 && *ep == '\0')
1068 fprintf(stderr, "bad option uid=\"%s\"\n", value);
1071 if (!value || !*value)
1075 pw = getpwnam(value);
1082 cruid = strtoul(value, &ep, 10);
1083 if (errno == 0 && *ep == '\0')
1086 fprintf(stderr, "bad option: cruid=\"%s\"\n", value);
1089 if (!value || !*value)
1093 gr = getgrnam(value);
1100 gid = strtoul(value, &ep, 10);
1101 if (errno == 0 && *ep == '\0')
1104 fprintf(stderr, "bad option: gid=\"%s\"\n", value);
1106 /* fmask falls through to file_mode */
1109 "WARNING: CIFS mount option 'fmask' is\
1110 deprecated. Use 'file_mode' instead.\n");
1111 data = "file_mode"; /* BB fix this */
1114 if (!value || !*value) {
1116 "Option '%s' requires a numerical argument\n",
1121 if (value[0] != '0')
1123 "WARNING: '%s' not expressed in octal.\n",
1127 /* dmask falls through to dir_mode */
1130 "WARNING: CIFS mount option 'dmask' is\
1131 deprecated. Use 'dir_mode' instead.\n");
1135 if (!value || !*value) {
1137 "Option '%s' requires a numerical argument\n",
1142 if (value[0] != '0')
1144 "WARNING: '%s' not expressed in octal.\n",
1148 *filesys_flags |= MS_NOSUID;
1151 *filesys_flags &= ~MS_NOSUID;
1154 *filesys_flags |= MS_NODEV;
1157 *filesys_flags &= ~MS_MANDLOCK;
1160 *filesys_flags |= MS_MANDLOCK;
1163 *filesys_flags &= ~MS_MANDLOCK;
1166 *filesys_flags &= ~MS_NODEV;
1169 *filesys_flags |= MS_NOEXEC;
1172 *filesys_flags &= ~MS_NOEXEC;
1175 parsed_info->is_guest = 1;
1176 parsed_info->got_user = 1;
1177 parsed_info->got_password = 1;
1178 parsed_info->got_password2 = 1;
1181 *filesys_flags |= MS_RDONLY;
1184 *filesys_flags &= ~MS_RDONLY;
1187 *filesys_flags |= MS_REMOUNT;
1192 if (!value || !*value)
1197 bkupuid = strtoul(value, &ep, 10);
1198 if (errno == 0 && *ep == '\0')
1201 pw = getpwnam(value);
1204 "bad user name \"%s\"\n", value);
1208 bkupuid = pw->pw_uid;
1211 if (!value || !*value)
1216 bkupgid = strtoul(value, &ep, 10);
1217 if (errno == 0 && *ep == '\0')
1220 gr = getgrnam(value);
1223 "bad group name \"%s\"\n", value);
1227 bkupgid = gr->gr_gid;
1230 parsed_info->nofail = 1;
1233 if (!value || !*value)
1235 if (strncmp(value, "@GMT-", 5))
1237 if ((strlen(value) != GMT_NAME_LEN) ||
1238 (strptime(value, GMT_FORMAT, &tm) == NULL)) {
1239 fprintf(stderr, "bad snapshot token\n");
1242 snapshot = timegm(&tm) * 10000000 + NTFS_TIME_OFFSET;
1247 /* check size before copying option to buffer */
1248 word_len = strlen(data);
1250 word_len += 1 + strlen(value);
1252 /* need 2 extra bytes for comma and null byte */
1253 if (out_len + word_len + 2 > MAX_OPTIONS_LEN) {
1254 fprintf(stderr, "Options string too long\n");
1258 /* put back equals sign, if any */
1262 /* go ahead and copy */
1264 strlcat(out, ",", MAX_OPTIONS_LEN);
1266 strlcat(out, data, MAX_OPTIONS_LEN);
1267 out_len = strlen(out);
1269 data = next_keyword;
1273 /* special-case the uid and gid */
1275 word_len = snprintf(txtbuf, sizeof(txtbuf), "%u", uid);
1277 /* comma + "uid=" + terminating NULL == 6 */
1278 if (out_len + word_len + 6 > MAX_OPTIONS_LEN) {
1279 fprintf(stderr, "Options string too long\n");
1284 strlcat(out, ",", MAX_OPTIONS_LEN);
1287 rc = snprintf(out + out_len, word_len + 5, "uid=%s", txtbuf);
1289 fprintf(stderr, "Error in creating mount string\n");
1292 out_len = strlen(out);
1294 if (parsed_info->is_krb5 && parsed_info->sudo_uid) {
1295 cruid = parsed_info->sudo_uid;
1299 word_len = snprintf(txtbuf, sizeof(txtbuf), "%u", cruid);
1301 /* comma + "cruid=" + terminating NULL == 8 */
1302 if (out_len + word_len + 8 > MAX_OPTIONS_LEN) {
1303 fprintf(stderr, "Options string too long\n");
1308 strlcat(out, ",", MAX_OPTIONS_LEN);
1311 rc = snprintf(out + out_len, word_len + 7, "cruid=%s", txtbuf);
1313 fprintf(stderr, "Error in creating mount string\n");
1316 out_len = strlen(out);
1319 word_len = snprintf(txtbuf, sizeof(txtbuf), "%u", gid);
1321 /* comma + "gid=" + terminating NULL == 6 */
1322 if (out_len + word_len + 6 > MAX_OPTIONS_LEN) {
1323 fprintf(stderr, "Options string too long\n");
1328 strlcat(out, ",", MAX_OPTIONS_LEN);
1331 rc = snprintf(out + out_len, word_len + 5, "gid=%s", txtbuf);
1333 fprintf(stderr, "Error in creating mount string\n");
1336 out_len = strlen(out);
1339 word_len = snprintf(txtbuf, sizeof(txtbuf), "%u", bkupuid);
1341 /* comma + "backupuid=" + terminating NULL == 12 */
1342 if (out_len + word_len + 12 > MAX_OPTIONS_LEN) {
1343 fprintf(stderr, "Options string too long\n");
1348 strlcat(out, ",", MAX_OPTIONS_LEN);
1351 rc = snprintf(out + out_len, word_len + 11, "backupuid=%s", txtbuf);
1353 fprintf(stderr, "Error in creating mount string\n");
1356 out_len = strlen(out);
1359 word_len = snprintf(txtbuf, sizeof(txtbuf), "%u", bkupgid);
1361 /* comma + "backupgid=" + terminating NULL == 12 */
1362 if (out_len + word_len + 12 > MAX_OPTIONS_LEN) {
1363 fprintf(stderr, "Options string too long\n");
1368 strlcat(out, ",", MAX_OPTIONS_LEN);
1371 rc = snprintf(out + out_len, word_len + 11, "backupgid=%s", txtbuf);
1373 fprintf(stderr, "Error in creating mount string\n");
1376 out_len = strlen(out);
1379 word_len = snprintf(txtbuf, sizeof(txtbuf), "%llu", snapshot);
1381 /* comma + "snapshot=" + terminating NULL == 11 */
1382 if (out_len + word_len + 11 > MAX_OPTIONS_LEN) {
1383 fprintf(stderr, "Options string too long\n");
1388 strlcat(out, ",", MAX_OPTIONS_LEN);
1391 rc = snprintf(out + out_len, word_len + 10, "snapshot=%s", txtbuf);
1393 fprintf(stderr, "Error in creating mount string\n");
1396 out_len = strlen(out);
1402 static int parse_unc(const char *unc_name, struct parsed_mount_info *parsed_info, const char *progname)
1404 int length = strnlen(unc_name, MAX_UNC_LEN);
1405 const char *host, *share, *prepath;
1406 size_t hostlen, sharelen, prepathlen;
1408 if (length > (MAX_UNC_LEN - 1)) {
1409 fprintf(stderr, "mount error: UNC name too long\n");
1414 fprintf(stderr, "mount error: UNC name too short\n");
1418 if ((strncasecmp("cifs://", unc_name, 7) == 0) ||
1419 (strncasecmp("smb://", unc_name, 6) == 0)) {
1421 "Mounting cifs URL not implemented yet. Attempt to mount %s\n",
1426 if (strncmp(unc_name, "//", 2) && strncmp(unc_name, "\\\\", 2)) {
1427 fprintf(stderr, "%s: bad UNC (%s)\n", progname, unc_name);
1431 host = unc_name + 2;
1432 hostlen = strcspn(host, "/\\");
1434 fprintf(stderr, "%s: bad UNC (%s)\n", progname, unc_name);
1437 share = host + hostlen + 1;
1439 if (hostlen + 1 > sizeof(parsed_info->host)) {
1440 fprintf(stderr, "%s: host portion of UNC too long\n", progname);
1444 sharelen = strcspn(share, "/\\");
1445 if (sharelen + 1 > sizeof(parsed_info->share)) {
1446 fprintf(stderr, "%s: share portion of UNC too long\n", progname);
1450 prepath = share + sharelen;
1451 if (*prepath != '\0')
1454 prepathlen = strlen(prepath);
1456 if (prepathlen + 1 > sizeof(parsed_info->prefix)) {
1457 fprintf(stderr, "%s: UNC prefixpath too long\n", progname);
1461 /* copy pieces into their resepective buffers */
1462 memcpy(parsed_info->host, host, hostlen);
1463 memcpy(parsed_info->share, share, sharelen);
1464 memcpy(parsed_info->prefix, prepath, prepathlen);
1469 static int get_pw_from_env(struct parsed_mount_info *parsed_info, const char *program)
1473 if (getenv("PASSWD"))
1474 rc = set_password(parsed_info, getenv("PASSWD"), OPT_PASS);
1475 else if (getenv("PASSWD_FD"))
1476 rc = get_password_from_file(atoi(getenv("PASSWD_FD")), NULL,
1477 parsed_info, program, OPT_PASS);
1478 else if (getenv("PASSWD_FILE"))
1479 rc = get_password_from_file(0, getenv("PASSWD_FILE"),
1480 parsed_info, program, OPT_PASS);
1485 if (getenv("PASSWD2"))
1486 rc = set_password(parsed_info, getenv("PASSWD2"), OPT_PASS2);
1487 else if (getenv("PASSWD2_FD"))
1488 rc = get_password_from_file(atoi(getenv("PASSWD2_FD")), NULL,
1489 parsed_info, program, OPT_PASS2);
1490 else if (getenv("PASSWD2_FILE"))
1491 rc = get_password_from_file(0, getenv("PASSWD2_FILE"),
1492 parsed_info, program, OPT_PASS2);
1497 static struct option longopts[] = {
1498 {"all", 0, NULL, 'a'},
1499 {"help", 0, NULL, 'h'},
1500 {"move", 0, NULL, 'm'},
1501 {"bind", 0, NULL, 'b'},
1502 {"read-only", 0, NULL, 'r'},
1503 {"ro", 0, NULL, 'r'},
1504 {"verbose", 0, NULL, 'v'},
1505 {"version", 0, NULL, 'V'},
1506 {"read-write", 0, NULL, 'w'},
1507 {"rw", 0, NULL, 'w'},
1508 {"options", 1, NULL, 'o'},
1509 {"type", 1, NULL, 't'},
1510 {"uid", 1, NULL, '1'},
1511 {"gid", 1, NULL, '2'},
1512 {"user", 1, NULL, 'u'},
1513 {"username", 1, NULL, 'u'},
1514 {"dom", 1, NULL, 'd'},
1515 {"domain", 1, NULL, 'd'},
1516 {"password", 1, NULL, 'p'},
1517 {"pass", 1, NULL, 'p'},
1518 {"password2", 1, NULL, 0},
1519 {"pass2", 1, NULL, 0},
1520 {"credentials", 1, NULL, 'c'},
1521 {"port", 1, NULL, 'P'},
1522 {"sloppy", 0, NULL, 's'},
1526 /* convert a string to uppercase. return false if the string
1527 * wasn't ASCII. Return success on a NULL ptr */
1528 static int uppercase_string(char *string)
1534 /* check for unicode */
1535 if ((unsigned char)string[0] & 0x80)
1537 *string = toupper((unsigned char)*string);
1544 static void print_cifs_mount_version(const char *progname)
1546 printf("%s version: %s\n", progname, VERSION);
1550 * This function borrowed from fuse-utils...
1552 * glibc's addmntent (at least as of 2.10 or so) doesn't properly encode
1553 * newlines embedded within the text fields. To make sure no one corrupts
1554 * the mtab, fail the mount if there are embedded newlines.
1556 static int check_newline(const char *progname, const char *name)
1559 for (s = "\n"; *s; s++) {
1560 if (strchr(name, *s)) {
1562 "%s: illegal character 0x%02x in mount entry\n",
1570 static int check_mtab(const char *progname, const char *devname,
1573 if (check_newline(progname, devname) || check_newline(progname, dir))
1579 add_mtab(char *devname, char *mountpoint, unsigned long flags, const char *fstype)
1581 int rc = 0, tmprc, fd;
1583 char *mount_user = NULL;
1584 struct mntent mountent;
1585 struct stat statbuf;
1587 sigset_t mask, oldmask;
1591 mount_user = getusername(uid);
1594 * Set the real uid to the effective uid. This prevents unprivileged
1595 * users from sending signals to this process, though ^c on controlling
1596 * terminal should still work.
1598 rc = setreuid(geteuid(), -1);
1600 fprintf(stderr, "Unable to set real uid to effective uid: %s\n",
1605 rc = sigfillset(&mask);
1607 fprintf(stderr, "Unable to set filled signal mask\n");
1611 rc = sigprocmask(SIG_SETMASK, &mask, &oldmask);
1613 fprintf(stderr, "Unable to make process ignore signals\n");
1617 rc = toggle_dac_capability(1, 1);
1621 atexit(unlock_mtab);
1624 fprintf(stderr, "cannot lock mtab");
1629 pmntfile = setmntent(MOUNTED, "a+");
1631 fprintf(stderr, "could not update mount table\n");
1637 fd = fileno(pmntfile);
1639 fprintf(stderr, "mntent does not appear to be valid\n");
1645 rc = fstat(fd, &statbuf);
1647 fprintf(stderr, "unable to fstat open mtab\n");
1648 endmntent(pmntfile);
1654 mountent.mnt_fsname = devname;
1655 mountent.mnt_dir = mountpoint;
1656 mountent.mnt_type = (char *)(void *)fstype;
1657 mountent.mnt_opts = (char *)calloc(MTAB_OPTIONS_LEN, 1);
1658 if (mountent.mnt_opts) {
1659 if (flags & MS_RDONLY)
1660 strlcat(mountent.mnt_opts, "ro", MTAB_OPTIONS_LEN);
1662 strlcat(mountent.mnt_opts, "rw", MTAB_OPTIONS_LEN);
1664 if (flags & MS_MANDLOCK)
1665 strlcat(mountent.mnt_opts, ",mand", MTAB_OPTIONS_LEN);
1666 if (flags & MS_NOEXEC)
1667 strlcat(mountent.mnt_opts, ",noexec", MTAB_OPTIONS_LEN);
1668 if (flags & MS_NOSUID)
1669 strlcat(mountent.mnt_opts, ",nosuid", MTAB_OPTIONS_LEN);
1670 if (flags & MS_NODEV)
1671 strlcat(mountent.mnt_opts, ",nodev", MTAB_OPTIONS_LEN);
1672 if (flags & MS_SYNCHRONOUS)
1673 strlcat(mountent.mnt_opts, ",sync", MTAB_OPTIONS_LEN);
1675 strlcat(mountent.mnt_opts, ",user=", MTAB_OPTIONS_LEN);
1676 strlcat(mountent.mnt_opts, mount_user,
1680 mountent.mnt_freq = 0;
1681 mountent.mnt_passno = 0;
1682 rc = addmntent(pmntfile, &mountent);
1684 int ignore __attribute__((unused));
1686 fprintf(stderr, "unable to add mount entry to mtab\n");
1687 ignore = ftruncate(fd, statbuf.st_size);
1690 tmprc = my_endmntent(pmntfile, statbuf.st_size);
1692 fprintf(stderr, "error %d detected on close of mtab\n", tmprc);
1696 free(mountent.mnt_opts);
1698 toggle_dac_capability(1, 0);
1699 sigprocmask(SIG_SETMASK, &oldmask, NULL);
1705 del_mtab(char *mountpoint)
1707 int len, tmprc, rc = 0, tmpfd;
1708 FILE *mnttmp, *mntmtab;
1709 struct mntent *mountent;
1710 char *mtabfile, *mtabdir, *mtabtmpfile = NULL;
1712 mtabfile = strdup(MOUNTED);
1714 fprintf(stderr, "del_mtab: cannot strdup MOUNTED\n");
1719 mtabdir = dirname(mtabfile);
1720 len = strlen(mtabdir) + strlen(MNT_TMP_FILE);
1721 mtabtmpfile = malloc(len + 1);
1723 fprintf(stderr, "del_mtab: cannot allocate memory to tmp file\n");
1728 if (sprintf(mtabtmpfile, "%s%s", mtabdir, MNT_TMP_FILE) != len) {
1729 fprintf(stderr, "del_mtab: error writing new string\n");
1734 atexit(unlock_mtab);
1737 fprintf(stderr, "del_mtab: cannot lock mtab\n");
1742 // Use mkstemp instead of mktemp
1743 tmpfd = mkstemp(mtabtmpfile);
1745 fprintf(stderr, "del_mtab: cannot setup tmp file destination\n");
1750 mntmtab = setmntent(MOUNTED, "r");
1752 fprintf(stderr, "del_mtab: could not update mount table\n");
1758 mnttmp = fdopen(tmpfd, "w");
1760 fprintf(stderr, "del_mtab: could not update mount table\n");
1767 while ((mountent = getmntent(mntmtab)) != NULL) {
1768 if (!strcmp(mountent->mnt_dir, mountpoint))
1770 rc = addmntent(mnttmp, mountent);
1772 fprintf(stderr, "del_mtab: unable to add mount entry to mtab\n");
1774 goto del_mtab_error;
1780 tmprc = my_endmntent(mnttmp, 0);
1782 fprintf(stderr, "del_mtab: error %d detected on close of tmp file\n", tmprc);
1784 goto del_mtab_error;
1787 if (rename(mtabtmpfile, MOUNTED)) {
1788 fprintf(stderr, "del_mtab: error %d when renaming mtab in place\n", errno);
1790 goto del_mtab_error;
1800 if (unlink(mtabtmpfile))
1801 fprintf(stderr, "del_mtab: failed to delete tmp file - %s\n",
1806 /* have the child drop root privileges */
1808 drop_child_privs(void)
1811 uid_t uid = getuid();
1812 gid_t gid = getgid();
1817 fprintf(stderr, "Unable set group identity: %s\n",
1825 fprintf(stderr, "Unable set user identity: %s\n",
1834 #ifdef ENABLE_SYSTEMD
1835 static int get_passwd_by_systemd(const char *prompt, char *input, int capacity)
1842 if (pipe(fd) == -1) {
1843 fprintf(stderr, "Failed to create pipe: %s\n", strerror(errno));
1849 fprintf(stderr, "Unable to fork: %s\n", strerror(errno));
1856 dup2(fd[1], STDOUT_FILENO);
1857 if (execlp("systemd-ask-password", "systemd-ask-password", prompt, NULL) == -1) {
1858 fprintf(stderr, "Failed to execute systemd-ask-password: %s\n",
1866 if (offs+1 >= capacity) {
1867 fprintf(stderr, "Password too long.\n");
1872 rc = read(fd[0], input + offs, capacity - offs);
1874 fprintf(stderr, "Failed to read from pipe: %s\n", strerror(errno));
1883 if (wait(&rc) == -1) {
1884 fprintf(stderr, "Failed to wait child: %s\n", strerror(errno));
1888 if (!WIFEXITED(rc) || WEXITSTATUS(rc)) {
1902 * If systemd is running and systemd-ask-password --
1903 * is available, then use that else fallback on getpass(..)
1905 * Returns: @input or NULL on error
1908 get_password(const char *prompt, char *input, int capacity)
1910 #ifdef ENABLE_SYSTEMD
1911 int is_systemd_running;
1914 memset(input, 0, capacity);
1916 /* We simply test whether the systemd cgroup hierarchy is
1918 is_systemd_running = (lstat("/sys/fs/cgroup", &a) == 0)
1919 && (lstat("/sys/fs/cgroup/systemd", &b) == 0)
1920 && (a.st_dev != b.st_dev);
1922 if (is_systemd_running && !get_passwd_by_systemd(prompt, input, capacity)) {
1923 int len = strlen(input);
1924 if (input[len - 1] == '\n')
1925 input[len - 1] = '\0';
1929 memset(input, 0, capacity);
1932 * Falling back to getpass(..)
1933 * getpass is obsolete, but there's apparently nothing that replaces it
1935 char *tmp_pass = getpass(prompt);
1939 strncpy(input, tmp_pass, capacity - 1);
1940 input[capacity - 1] = '\0';
1942 /* zero-out the static buffer */
1943 memset(tmp_pass, 0, strlen(tmp_pass));
1949 assemble_mountinfo(struct parsed_mount_info *parsed_info,
1950 const char *thisprogram, const char *mountpoint,
1951 const char *orig_dev, char *orgoptions)
1954 char *newopts = NULL;
1956 rc = drop_capabilities(0);
1960 rc = drop_child_privs();
1965 rc = check_fstab(thisprogram, mountpoint, orig_dev,
1970 orgoptions = newopts;
1971 /* enable any default user mount flags */
1972 parsed_info->flags |= CIFS_SETUID_FLAGS;
1975 rc = get_pw_from_env(parsed_info, thisprogram);
1980 rc = parse_options(orgoptions, parsed_info);
1986 if (!(parsed_info->flags & (MS_USERS | MS_USER))) {
1987 fprintf(stderr, "%s: permission denied\n", thisprogram);
1993 parsed_info->flags &= ~(MS_USERS | MS_USER);
1995 rc = parse_unc(orig_dev, parsed_info, thisprogram);
1999 if (parsed_info->addrlist[0] == '\0') {
2000 rc = resolve_host(parsed_info->host, parsed_info->addrlist);
2001 if (rc == 0 && parsed_info->verboseflag)
2002 fprintf(stderr, "Host \"%s\" resolved to the following IP addresses: %s\n", parsed_info->host, parsed_info->addrlist);
2007 fprintf(stderr, "mount error: could not resolve address for "
2008 "%s: %s\n", parsed_info->host,
2009 rc == EAI_SYSTEM ? strerror(errno) : gai_strerror(rc));
2013 fprintf(stderr, "mount error: problem parsing address "
2014 "list: %s\n", strerror(errno));
2018 if (!parsed_info->got_user) {
2020 * Note that the password will not be retrieved from the
2021 * USER env variable (ie user%password form) as there is
2022 * already a PASSWD environment varaible
2024 if (getenv("USER")) {
2025 if (check_creds_field(getenv("USER"))) {
2026 fprintf(stderr, "Invalid username in USER environment variable\n");
2030 strlcpy(parsed_info->username, getenv("USER"),
2031 sizeof(parsed_info->username));
2033 char *username = getusername(getuid());
2036 if (check_creds_field(username)) {
2037 fprintf(stderr, "Invalid username for uid %u\n",
2038 (unsigned int)getuid());
2042 strlcpy(parsed_info->username, username,
2043 sizeof(parsed_info->username));
2046 parsed_info->got_user = 1;
2049 // no need to prompt for password2
2050 if (!parsed_info->got_password && !(parsed_info->flags & MS_REMOUNT)) {
2051 char tmp_pass[MOUNT_PASSWD_SIZE + 1];
2052 char *prompt = NULL;
2054 if(asprintf(&prompt, "Password for %s@%s: ", parsed_info->username, orig_dev) < 0)
2057 if (get_password(prompt ? prompt : "Password: ", tmp_pass, MOUNT_PASSWD_SIZE + 1)) {
2058 rc = set_password(parsed_info, tmp_pass, OPT_PASS);
2060 fprintf(stderr, "Error reading password, exiting\n");
2069 /* copy in user= string */
2070 if (parsed_info->got_user) {
2071 if (*parsed_info->options)
2072 strlcat(parsed_info->options, ",",
2073 sizeof(parsed_info->options));
2074 strlcat(parsed_info->options, "user=",
2075 sizeof(parsed_info->options));
2076 strlcat(parsed_info->options, parsed_info->username,
2077 sizeof(parsed_info->options));
2080 if (*parsed_info->domain) {
2081 if (*parsed_info->options)
2082 strlcat(parsed_info->options, ",",
2083 sizeof(parsed_info->options));
2084 strlcat(parsed_info->options, "domain=",
2085 sizeof(parsed_info->options));
2086 strlcat(parsed_info->options, parsed_info->domain,
2087 sizeof(parsed_info->options));
2088 } else if (parsed_info->got_domain) {
2089 strlcat(parsed_info->options, ",domain=",
2090 sizeof(parsed_info->options));
2099 * chdir() into the mountpoint and determine "realpath". We assume here that
2100 * "mountpoint" is a statically allocated string and does not need to be freed.
2103 acquire_mountpoint(char **mountpointp)
2106 uid_t realuid, oldfsuid;
2108 char *mountpoint = NULL;
2111 * Acquire the necessary privileges to chdir to the mountpoint. If
2112 * the real uid is root, then we reacquire CAP_DAC_READ_SEARCH. If
2113 * it's not, then we change the fsuid to the real uid to ensure that
2114 * the mounting user actually has access to the mountpoint.
2116 * The mount(8) manpage does not state that users must be able to
2117 * chdir into the mountpoint in order to mount onto it, but if we
2118 * allow that, then an unprivileged user could use this program to
2119 * "probe" into directories to which he does not have access.
2123 rc = toggle_dac_capability(0, 1);
2127 oldfsuid = setfsuid(realuid);
2128 oldfsgid = setfsgid(getgid());
2131 rc = chdir(*mountpointp);
2133 fprintf(stderr, "Couldn't chdir to %s: %s\n", *mountpointp,
2139 mountpoint = realpath(".", NULL);
2141 fprintf(stderr, "Unable to resolve %s to canonical path: %s\n",
2142 *mountpointp, strerror(errno));
2148 dacrc = toggle_dac_capability(0, 0);
2150 rc = rc ? rc : dacrc;
2152 uid_t __attribute__((unused)) uignore = setfsuid(oldfsuid);
2153 gid_t __attribute__((unused)) gignore = setfsgid(oldfsgid);
2162 *mountpointp = mountpoint;
2166 int main(int argc, char **argv)
2169 char *orgoptions = NULL;
2170 char *mountpoint = NULL;
2171 char *options = NULL;
2172 char *orig_dev = NULL;
2173 char *currentaddress, *nextaddress;
2177 int already_uppercased = 0;
2179 int fallback_sudo_uid = geteuid() || getuid();
2180 size_t options_size = MAX_OPTIONS_LEN;
2181 struct parsed_mount_info *parsed_info = NULL;
2182 struct parsed_mount_info *reinit_parsed_info = NULL;
2186 rc = check_setuid();
2190 rc = drop_capabilities(1);
2194 /* setlocale(LC_ALL, "");
2195 bindtextdomain(PACKAGE, LOCALEDIR);
2196 textdomain(PACKAGE); */
2198 if (!argc || !argv) {
2199 rc = mount_usage(stderr);
2203 thisprogram = basename(argv[0]);
2204 if (thisprogram == NULL)
2205 thisprogram = "mount.cifs";
2207 if(strcmp(thisprogram, "mount.cifs") == 0)
2208 cifs_fstype = "cifs";
2210 if(strcmp(thisprogram, "mount.smb3") == 0)
2211 cifs_fstype = "smb3";
2213 /* allocate parsed_info as shared anonymous memory range */
2214 parsed_info = mmap((void *)0, sizeof(*parsed_info), PROT_READ | PROT_WRITE,
2215 MAP_ANONYMOUS | MAP_SHARED, -1, 0);
2216 if (parsed_info == (struct parsed_mount_info *) -1) {
2218 fprintf(stderr, "Unable to allocate memory: %s\n",
2224 reinit_parsed_info = malloc(sizeof(*reinit_parsed_info));
2225 if (reinit_parsed_info == NULL) {
2226 fprintf(stderr, "Unable to allocate memory: %s\n",
2232 options = calloc(options_size, 1);
2234 fprintf(stderr, "Unable to allocate memory.\n");
2239 /* add sharename in opts string as unc= parm */
2240 while ((c = getopt_long(argc, argv, "?fhno:rsvVw",
2241 longopts, NULL)) != -1) {
2244 case 'h': /* help */
2245 rc = mount_usage(stdout);
2248 ++parsed_info->nomtab;
2251 orgoptions = strndup(optarg, MAX_OPTIONS_LEN);
2257 case 'r': /* mount readonly */
2258 parsed_info->flags |= MS_RDONLY;
2261 ++parsed_info->verboseflag;
2264 print_cifs_mount_version(thisprogram);
2267 parsed_info->flags &= ~MS_RDONLY;
2270 ++parsed_info->fakemnt;
2276 fprintf(stderr, "unknown command-line option: %c\n", c);
2277 rc = mount_usage(stderr);
2282 if (argc < optind + 2) {
2283 rc = mount_usage(stderr);
2287 orig_dev = argv[optind];
2288 mountpoint = argv[optind + 1];
2290 /* chdir into mountpoint as soon as possible */
2291 rc = acquire_mountpoint(&mountpoint);
2296 /* Before goto assemble_retry, reinitialize parsed_info with reinit_parsed_info */
2297 memcpy(reinit_parsed_info, parsed_info, sizeof(*reinit_parsed_info));
2301 * mount.cifs does privilege separation. Most of the code to handle
2302 * assembling the mount info is done in a child process that drops
2303 * privileges. The info is assembled in parsed_info which is a
2304 * shared, mmaped memory segment. The parent waits for the child to
2305 * exit and checks the return code. If it's anything but "0", then
2306 * the process exits without attempting anything further.
2310 fprintf(stderr, "Unable to fork: %s\n", strerror(errno));
2315 rc = assemble_mountinfo(parsed_info, thisprogram, mountpoint,
2316 orig_dev, orgoptions);
2317 goto mount_child_exit;
2321 if (!WIFEXITED(rc)) {
2322 fprintf(stderr, "Child process terminated abnormally.\n");
2326 rc = WEXITSTATUS(rc);
2331 currentaddress = parsed_info->addrlist;
2332 nextaddress = strchr(currentaddress, ',');
2334 *nextaddress++ = '\0';
2338 if (!currentaddress) {
2339 fprintf(stderr, "Unable to find suitable address.\n");
2340 rc = parsed_info->nofail ? 0 : EX_FAIL;
2343 strlcpy(options, "ip=", options_size);
2344 strlcat(options, currentaddress, options_size);
2346 strlcat(options, ",unc=\\\\", options_size);
2347 strlcat(options, parsed_info->host, options_size);
2348 strlcat(options, "\\", options_size);
2349 strlcat(options, parsed_info->share, options_size);
2351 if (*parsed_info->options) {
2352 strlcat(options, ",", options_size);
2353 strlcat(options, parsed_info->options, options_size);
2356 if (*parsed_info->prefix) {
2357 strlcat(options, ",prefixpath=", options_size);
2358 strlcat(options, parsed_info->prefix, options_size);
2362 strlcat(options, ",sloppy", options_size);
2364 if (parsed_info->verboseflag)
2365 fprintf(stderr, "%s kernel mount options: %s",
2366 thisprogram, options);
2368 if (parsed_info->got_password &&
2369 !(parsed_info->is_krb5 || parsed_info->is_noauth || parsed_info->is_guest)) {
2371 * Commas have to be doubled, or else they will
2372 * look like the parameter separator
2374 strlcat(options, ",pass=", options_size);
2375 strlcat(options, parsed_info->password, options_size);
2376 if (parsed_info->verboseflag)
2377 fprintf(stderr, ",pass=********");
2380 if (parsed_info->got_password2 &&
2381 !(parsed_info->is_krb5 || parsed_info->is_noauth || parsed_info->is_guest)) {
2382 strlcat(options, ",password2=", options_size);
2383 strlcat(options, parsed_info->password2, options_size);
2384 if (parsed_info->verboseflag)
2385 fprintf(stderr, ",password2=********");
2388 if (parsed_info->verboseflag)
2389 fprintf(stderr, "\n");
2391 rc = check_mtab(thisprogram, orig_dev, mountpoint);
2395 if (!parsed_info->fakemnt) {
2396 toggle_dac_capability(0, 1);
2397 rc = mount(orig_dev, ".", cifs_fstype, parsed_info->flags, options);
2398 toggle_dac_capability(0, 0);
2406 if (currentaddress) {
2407 fprintf(stderr, "mount error(%d): could not connect to %s",
2408 errno, currentaddress);
2410 currentaddress = nextaddress;
2411 if (currentaddress) {
2412 nextaddress = strchr(currentaddress, ',');
2414 *nextaddress++ = '\0';
2419 "mount error: %s filesystem not supported by the system\n", cifs_fstype);
2423 "mount error: Server abruptly closed the connection.\n"
2424 "This can happen if the server does not support the SMB version you are trying to use.\n"
2425 "The default SMB version recently changed from SMB1 to SMB2.1 and above. Try mounting with vers=1.0.\n");
2428 if (!already_uppercased &&
2429 uppercase_string(parsed_info->host) &&
2430 uppercase_string(parsed_info->share) &&
2431 uppercase_string(parsed_info->prefix) &&
2432 uppercase_string(orig_dev)) {
2434 "Retrying with upper case share name\n");
2435 already_uppercased = 1;
2440 if (!fallback_sudo_uid && parsed_info->is_krb5) {
2441 /* mount could have failed because cruid option was not passed when triggered with sudo */
2442 value = getenv("SUDO_UID");
2445 sudo_uid = strtoul(value, &ep, 10);
2446 if (errno == 0 && *ep == '\0' && sudo_uid) {
2447 /* Reinitialize parsed_info and assemble options again with sudo_uid */
2448 memcpy(parsed_info, reinit_parsed_info, sizeof(*parsed_info));
2449 parsed_info->sudo_uid = sudo_uid;
2450 fallback_sudo_uid = 1;
2451 goto assemble_retry;
2457 fprintf(stderr, "mount error(%d): %s\n", errno,
2460 "Refer to the %s(8) manual page (e.g. man "
2461 "%s) and kernel log messages (dmesg)\n", thisprogram, thisprogram);
2467 if (!parsed_info->nomtab && !mtab_unusable()) {
2468 if (parsed_info->flags & MS_REMOUNT) {
2469 rc = del_mtab(mountpoint);
2474 rc = add_mtab(orig_dev, mountpoint, parsed_info->flags, cifs_fstype);
2479 memset(parsed_info->password, 0, sizeof(parsed_info->password));
2480 memset(parsed_info->password2, 0, sizeof(parsed_info->password2));
2481 munmap(parsed_info, sizeof(*parsed_info));
2485 /* Objects to be freed both in main process and child */
2486 free(reinit_parsed_info);