diff options
-rw-r--r-- | cifs.upcall.c | 653 | ||||
-rw-r--r-- | cifs_spnego.h | 46 | ||||
-rw-r--r-- | mount.cifs.c | 1779 | ||||
-rw-r--r-- | mount.h | 38 | ||||
-rw-r--r-- | mtab.c | 220 |
5 files changed, 2736 insertions, 0 deletions
diff --git a/cifs.upcall.c b/cifs.upcall.c new file mode 100644 index 0000000..bfc70d1 --- /dev/null +++ b/cifs.upcall.c @@ -0,0 +1,653 @@ +/* +* CIFS user-space helper. +* Copyright (C) Igor Mammedov (niallain@gmail.com) 2007 +* Copyright (C) Jeff Layton (jlayton@redhat.com) 2009 +* +* Used by /sbin/request-key for handling +* cifs upcall for kerberos authorization of access to share and +* cifs upcall for DFS srver name resolving (IPv4/IPv6 aware). +* You should have keyutils installed and add something like the +* following lines to /etc/request-key.conf file: + +create cifs.spnego * * /usr/local/sbin/cifs.upcall %k +create dns_resolver * * /usr/local/sbin/cifs.upcall %k + +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include "includes.h" +#include "../libcli/auth/spnego.h" +#include "smb_krb5.h" +#include <keyutils.h> +#include <getopt.h> + +#include "cifs_spnego.h" + +#define CIFS_DEFAULT_KRB5_DIR "/tmp" +#define CIFS_DEFAULT_KRB5_PREFIX "krb5cc_" + +#define MAX_CCNAME_LEN PATH_MAX + 5 + +const char *CIFSSPNEGO_VERSION = "1.3"; +static const char *prog = "cifs.upcall"; +typedef enum _sectype { + NONE = 0, + KRB5, + MS_KRB5 +} sectype_t; + +/* does the ccache have a valid TGT? */ +static time_t +get_tgt_time(const char *ccname) { + krb5_context context; + krb5_ccache ccache; + krb5_cc_cursor cur; + krb5_creds creds; + krb5_principal principal; + time_t credtime = 0; + char *realm = NULL; + + if (krb5_init_context(&context)) { + syslog(LOG_DEBUG, "%s: unable to init krb5 context", __func__); + return 0; + } + + if (krb5_cc_resolve(context, ccname, &ccache)) { + syslog(LOG_DEBUG, "%s: unable to resolve krb5 cache", __func__); + goto err_cache; + } + + if (krb5_cc_set_flags(context, ccache, 0)) { + syslog(LOG_DEBUG, "%s: unable to set flags", __func__); + goto err_cache; + } + + if (krb5_cc_get_principal(context, ccache, &principal)) { + syslog(LOG_DEBUG, "%s: unable to get principal", __func__); + goto err_princ; + } + + if (krb5_cc_start_seq_get(context, ccache, &cur)) { + syslog(LOG_DEBUG, "%s: unable to seq start", __func__); + goto err_ccstart; + } + + if ((realm = smb_krb5_principal_get_realm(context, principal)) == NULL) { + syslog(LOG_DEBUG, "%s: unable to get realm", __func__); + goto err_ccstart; + } + + while (!credtime && !krb5_cc_next_cred(context, ccache, &cur, &creds)) { + char *name; + if (smb_krb5_unparse_name(NULL, context, creds.server, &name)) { + syslog(LOG_DEBUG, "%s: unable to unparse name", __func__); + goto err_endseq; + } + if (krb5_realm_compare(context, creds.server, principal) && + strnequal(name, KRB5_TGS_NAME, KRB5_TGS_NAME_SIZE) && + strnequal(name+KRB5_TGS_NAME_SIZE+1, realm, strlen(realm)) && + creds.times.endtime > time(NULL)) + credtime = creds.times.endtime; + krb5_free_cred_contents(context, &creds); + TALLOC_FREE(name); + } +err_endseq: + krb5_cc_end_seq_get(context, ccache, &cur); +err_ccstart: + krb5_free_principal(context, principal); +err_princ: +#if defined(KRB5_TC_OPENCLOSE) + krb5_cc_set_flags(context, ccache, KRB5_TC_OPENCLOSE); +#endif + krb5_cc_close(context, ccache); +err_cache: + krb5_free_context(context); + return credtime; +} + +static int +krb5cc_filter(const struct dirent *dirent) +{ + if (strstr(dirent->d_name, CIFS_DEFAULT_KRB5_PREFIX)) + return 1; + else + return 0; +} + +/* search for a credcache that looks like a likely candidate */ +static char * +find_krb5_cc(const char *dirname, uid_t uid) +{ + struct dirent **namelist; + struct stat sbuf; + char ccname[MAX_CCNAME_LEN], *credpath, *best_cache = NULL; + int i, n; + time_t cred_time, best_time = 0; + + n = scandir(dirname, &namelist, krb5cc_filter, NULL); + if (n < 0) { + syslog(LOG_DEBUG, "%s: scandir error on directory '%s': %s", + __func__, dirname, strerror(errno)); + return NULL; + } + + for (i = 0; i < n; i++) { + snprintf(ccname, sizeof(ccname), "FILE:%s/%s", dirname, + namelist[i]->d_name); + credpath = ccname + 5; + syslog(LOG_DEBUG, "%s: considering %s", __func__, credpath); + + if (lstat(credpath, &sbuf)) { + syslog(LOG_DEBUG, "%s: stat error on '%s': %s", + __func__, credpath, strerror(errno)); + free(namelist[i]); + continue; + } + if (sbuf.st_uid != uid) { + syslog(LOG_DEBUG, "%s: %s is owned by %u, not %u", + __func__, credpath, sbuf.st_uid, uid); + free(namelist[i]); + continue; + } + if (!S_ISREG(sbuf.st_mode)) { + syslog(LOG_DEBUG, "%s: %s is not a regular file", + __func__, credpath); + free(namelist[i]); + continue; + } + if (!(cred_time = get_tgt_time(ccname))) { + syslog(LOG_DEBUG, "%s: %s is not a valid credcache.", + __func__, ccname); + free(namelist[i]); + continue; + } + + if (cred_time <= best_time) { + syslog(LOG_DEBUG, "%s: %s expires sooner than current " + "best.", __func__, ccname); + free(namelist[i]); + continue; + } + + syslog(LOG_DEBUG, "%s: %s is valid ccache", __func__, ccname); + free(best_cache); + best_cache = SMB_STRNDUP(ccname, MAX_CCNAME_LEN); + best_time = cred_time; + free(namelist[i]); + } + free(namelist); + + return best_cache; +} + +/* + * Prepares AP-REQ data for mechToken and gets session key + * Uses credentials from cache. It will not ask for password + * you should receive credentials for yuor name manually using + * kinit or whatever you wish. + * + * in: + * oid - string with OID/ Could be OID_KERBEROS5 + * or OID_KERBEROS5_OLD + * principal - Service name. + * Could be "cifs/FQDN" for KRB5 OID + * or for MS_KRB5 OID style server principal + * like "pdc$@YOUR.REALM.NAME" + * + * out: + * secblob - pointer for spnego wrapped AP-REQ data to be stored + * sess_key- pointer for SessionKey data to be stored + * + * ret: 0 - success, others - failure + */ +static int +handle_krb5_mech(const char *oid, const char *principal, DATA_BLOB *secblob, + DATA_BLOB *sess_key, const char *ccname) +{ + int retval; + DATA_BLOB tkt, tkt_wrapped; + + syslog(LOG_DEBUG, "%s: getting service ticket for %s", __func__, + principal); + + /* get a kerberos ticket for the service and extract the session key */ + retval = cli_krb5_get_ticket(principal, 0, &tkt, sess_key, 0, ccname, + NULL, NULL); + + if (retval) { + syslog(LOG_DEBUG, "%s: failed to obtain service ticket (%d)", + __func__, retval); + return retval; + } + + syslog(LOG_DEBUG, "%s: obtained service ticket", __func__); + + /* wrap that up in a nice GSS-API wrapping */ + tkt_wrapped = spnego_gen_krb5_wrap(tkt, TOK_ID_KRB_AP_REQ); + + /* and wrap that in a shiny SPNEGO wrapper */ + *secblob = gen_negTokenInit(oid, tkt_wrapped); + + data_blob_free(&tkt_wrapped); + data_blob_free(&tkt); + return retval; +} + +#define DKD_HAVE_HOSTNAME 0x1 +#define DKD_HAVE_VERSION 0x2 +#define DKD_HAVE_SEC 0x4 +#define DKD_HAVE_IP 0x8 +#define DKD_HAVE_UID 0x10 +#define DKD_HAVE_PID 0x20 +#define DKD_MUSTHAVE_SET (DKD_HAVE_HOSTNAME|DKD_HAVE_VERSION|DKD_HAVE_SEC) + +struct decoded_args { + int ver; + char *hostname; + char *ip; + uid_t uid; + pid_t pid; + sectype_t sec; +}; + +static unsigned int +decode_key_description(const char *desc, struct decoded_args *arg) +{ + int len; + int retval = 0; + char *pos; + const char *tkn = desc; + + do { + pos = index(tkn, ';'); + if (strncmp(tkn, "host=", 5) == 0) { + + if (pos == NULL) + len = strlen(tkn); + else + len = pos - tkn; + + len -= 4; + SAFE_FREE(arg->hostname); + arg->hostname = SMB_XMALLOC_ARRAY(char, len); + strlcpy(arg->hostname, tkn + 5, len); + retval |= DKD_HAVE_HOSTNAME; + } else if (!strncmp(tkn, "ip4=", 4) || + !strncmp(tkn, "ip6=", 4)) { + if (pos == NULL) + len = strlen(tkn); + else + len = pos - tkn; + + len -= 3; + SAFE_FREE(arg->ip); + arg->ip = SMB_XMALLOC_ARRAY(char, len); + strlcpy(arg->ip, tkn + 4, len); + retval |= DKD_HAVE_IP; + } else if (strncmp(tkn, "pid=", 4) == 0) { + errno = 0; + arg->pid = strtol(tkn + 4, NULL, 0); + if (errno != 0) { + syslog(LOG_ERR, "Invalid pid format: %s", + strerror(errno)); + return 1; + } else { + retval |= DKD_HAVE_PID; + } + } else if (strncmp(tkn, "sec=", 4) == 0) { + if (strncmp(tkn + 4, "krb5", 4) == 0) { + retval |= DKD_HAVE_SEC; + arg->sec = KRB5; + } else if (strncmp(tkn + 4, "mskrb5", 6) == 0) { + retval |= DKD_HAVE_SEC; + arg->sec = MS_KRB5; + } + } else if (strncmp(tkn, "uid=", 4) == 0) { + errno = 0; + arg->uid = strtol(tkn + 4, NULL, 16); + if (errno != 0) { + syslog(LOG_ERR, "Invalid uid format: %s", + strerror(errno)); + return 1; + } else { + retval |= DKD_HAVE_UID; + } + } else if (strncmp(tkn, "ver=", 4) == 0) { /* if version */ + errno = 0; + arg->ver = strtol(tkn + 4, NULL, 16); + if (errno != 0) { + syslog(LOG_ERR, "Invalid version format: %s", + strerror(errno)); + return 1; + } else { + retval |= DKD_HAVE_VERSION; + } + } + if (pos == NULL) + break; + tkn = pos + 1; + } while (tkn); + return retval; +} + +static int +cifs_resolver(const key_serial_t key, const char *key_descr) +{ + int c; + struct addrinfo *addr; + char ip[INET6_ADDRSTRLEN]; + void *p; + const char *keyend = key_descr; + /* skip next 4 ';' delimiters to get to description */ + for (c = 1; c <= 4; c++) { + keyend = index(keyend+1, ';'); + if (!keyend) { + syslog(LOG_ERR, "invalid key description: %s", + key_descr); + return 1; + } + } + keyend++; + + /* resolve name to ip */ + c = getaddrinfo(keyend, NULL, NULL, &addr); + if (c) { + syslog(LOG_ERR, "unable to resolve hostname: %s [%s]", + keyend, gai_strerror(c)); + return 1; + } + + /* conver ip to string form */ + if (addr->ai_family == AF_INET) + p = &(((struct sockaddr_in *)addr->ai_addr)->sin_addr); + else + p = &(((struct sockaddr_in6 *)addr->ai_addr)->sin6_addr); + + if (!inet_ntop(addr->ai_family, p, ip, sizeof(ip))) { + syslog(LOG_ERR, "%s: inet_ntop: %s", __func__, strerror(errno)); + freeaddrinfo(addr); + return 1; + } + + /* setup key */ + c = keyctl_instantiate(key, ip, strlen(ip)+1, 0); + if (c == -1) { + syslog(LOG_ERR, "%s: keyctl_instantiate: %s", __func__, + strerror(errno)); + freeaddrinfo(addr); + return 1; + } + + freeaddrinfo(addr); + return 0; +} + +/* + * Older kernels sent IPv6 addresses without colons. Well, at least + * they're fixed-length strings. Convert these addresses to have colon + * delimiters to make getaddrinfo happy. + */ +static void +convert_inet6_addr(const char *from, char *to) +{ + int i = 1; + + while (*from) { + *to++ = *from++; + if (!(i++ % 4) && *from) + *to++ = ':'; + } + *to = 0; +} + +static int +ip_to_fqdn(const char *addrstr, char *host, size_t hostlen) +{ + int rc; + struct addrinfo hints = { .ai_flags = AI_NUMERICHOST }; + struct addrinfo *res; + const char *ipaddr = addrstr; + char converted[INET6_ADDRSTRLEN + 1]; + + if ((strlen(ipaddr) > INET_ADDRSTRLEN) && !strchr(ipaddr, ':')) { + convert_inet6_addr(ipaddr, converted); + ipaddr = converted; + } + + rc = getaddrinfo(ipaddr, NULL, &hints, &res); + if (rc) { + syslog(LOG_DEBUG, "%s: failed to resolve %s to " + "ipaddr: %s", __func__, ipaddr, + rc == EAI_SYSTEM ? strerror(errno) : gai_strerror(rc)); + return rc; + } + + rc = getnameinfo(res->ai_addr, res->ai_addrlen, host, hostlen, + NULL, 0, NI_NAMEREQD); + freeaddrinfo(res); + if (rc) { + syslog(LOG_DEBUG, "%s: failed to resolve %s to fqdn: %s", + __func__, ipaddr, + rc == EAI_SYSTEM ? strerror(errno) : gai_strerror(rc)); + return rc; + } + + syslog(LOG_DEBUG, "%s: resolved %s to %s", __func__, ipaddr, host); + return 0; +} + +static void +usage(void) +{ + syslog(LOG_INFO, "Usage: %s [-t] [-v] key_serial", prog); + fprintf(stderr, "Usage: %s [-t] [-v] key_serial\n", prog); +} + +const struct option long_options[] = { + { "trust-dns", 0, NULL, 't' }, + { "version", 0, NULL, 'v' }, + { NULL, 0, NULL, 0 } +}; + +int main(const int argc, char *const argv[]) +{ + struct cifs_spnego_msg *keydata = NULL; + DATA_BLOB secblob = data_blob_null; + DATA_BLOB sess_key = data_blob_null; + key_serial_t key = 0; + size_t datalen; + unsigned int have; + long rc = 1; + int c, try_dns = 0; + char *buf, *princ = NULL, *ccname = NULL; + char hostbuf[NI_MAXHOST], *host; + struct decoded_args arg = { }; + const char *oid; + + hostbuf[0] = '\0'; + + openlog(prog, 0, LOG_DAEMON); + + while ((c = getopt_long(argc, argv, "ctv", long_options, NULL)) != -1) { + switch (c) { + case 'c': + /* legacy option -- skip it */ + break; + case 't': + try_dns++; + break; + case 'v': + printf("version: %s\n", CIFSSPNEGO_VERSION); + goto out; + default: + syslog(LOG_ERR, "unknown option: %c", c); + goto out; + } + } + + /* is there a key? */ + if (argc <= optind) { + usage(); + goto out; + } + + /* get key and keyring values */ + errno = 0; + key = strtol(argv[optind], NULL, 10); + if (errno != 0) { + key = 0; + syslog(LOG_ERR, "Invalid key format: %s", strerror(errno)); + goto out; + } + + rc = keyctl_describe_alloc(key, &buf); + if (rc == -1) { + syslog(LOG_ERR, "keyctl_describe_alloc failed: %s", + strerror(errno)); + rc = 1; + goto out; + } + + syslog(LOG_DEBUG, "key description: %s", buf); + + if ((strncmp(buf, "cifs.resolver", sizeof("cifs.resolver")-1) == 0) || + (strncmp(buf, "dns_resolver", sizeof("dns_resolver")-1) == 0)) { + rc = cifs_resolver(key, buf); + goto out; + } + + have = decode_key_description(buf, &arg); + SAFE_FREE(buf); + if ((have & DKD_MUSTHAVE_SET) != DKD_MUSTHAVE_SET) { + syslog(LOG_ERR, "unable to get necessary params from key " + "description (0x%x)", have); + rc = 1; + goto out; + } + + if (arg.ver > CIFS_SPNEGO_UPCALL_VERSION) { + syslog(LOG_ERR, "incompatible kernel upcall version: 0x%x", + arg.ver); + rc = 1; + goto out; + } + + if (have & DKD_HAVE_UID) { + rc = setuid(arg.uid); + if (rc == -1) { + syslog(LOG_ERR, "setuid: %s", strerror(errno)); + goto out; + } + + ccname = find_krb5_cc(CIFS_DEFAULT_KRB5_DIR, arg.uid); + } + + host = arg.hostname; + + // do mech specific authorization + switch (arg.sec) { + case MS_KRB5: + case KRB5: +retry_new_hostname: + /* for "cifs/" service name + terminating 0 */ + datalen = strlen(host) + 5 + 1; + princ = SMB_XMALLOC_ARRAY(char, datalen); + if (!princ) { + rc = -ENOMEM; + break; + } + + if (arg.sec == MS_KRB5) + oid = OID_KERBEROS5_OLD; + else + oid = OID_KERBEROS5; + + /* + * try getting a cifs/ principal first and then fall back to + * getting a host/ principal if that doesn't work. + */ + strlcpy(princ, "cifs/", datalen); + strlcpy(princ + 5, host, datalen - 5); + rc = handle_krb5_mech(oid, princ, &secblob, &sess_key, ccname); + if (!rc) + break; + + memcpy(princ, "host/", 5); + rc = handle_krb5_mech(oid, princ, &secblob, &sess_key, ccname); + if (!rc) + break; + + if (!try_dns || !(have & DKD_HAVE_IP)) + break; + + rc = ip_to_fqdn(arg.ip, hostbuf, sizeof(hostbuf)); + if (rc) + break; + + SAFE_FREE(princ); + try_dns = 0; + host = hostbuf; + goto retry_new_hostname; + default: + syslog(LOG_ERR, "sectype: %d is not implemented", arg.sec); + rc = 1; + break; + } + + SAFE_FREE(princ); + + if (rc) + goto out; + + /* pack SecurityBLob and SessionKey into downcall packet */ + datalen = + sizeof(struct cifs_spnego_msg) + secblob.length + sess_key.length; + keydata = (struct cifs_spnego_msg*)SMB_XMALLOC_ARRAY(char, datalen); + if (!keydata) { + rc = 1; + goto out; + } + keydata->version = arg.ver; + keydata->flags = 0; + keydata->sesskey_len = sess_key.length; + keydata->secblob_len = secblob.length; + memcpy(&(keydata->data), sess_key.data, sess_key.length); + memcpy(&(keydata->data) + keydata->sesskey_len, + secblob.data, secblob.length); + + /* setup key */ + rc = keyctl_instantiate(key, keydata, datalen, 0); + if (rc == -1) { + syslog(LOG_ERR, "keyctl_instantiate: %s", strerror(errno)); + goto out; + } + + /* BB: maybe we need use timeout for key: for example no more then + * ticket lifietime? */ + /* keyctl_set_timeout( key, 60); */ +out: + /* + * on error, negatively instantiate the key ourselves so that we can + * make sure the kernel doesn't hang it off of a searchable keyring + * and interfere with the next attempt to instantiate the key. + */ + if (rc != 0 && key == 0) + keyctl_negate(key, 1, KEY_REQKEY_DEFL_DEFAULT); + data_blob_free(&secblob); + data_blob_free(&sess_key); + SAFE_FREE(ccname); + SAFE_FREE(arg.hostname); + SAFE_FREE(arg.ip); + SAFE_FREE(keydata); + return rc; +} diff --git a/cifs_spnego.h b/cifs_spnego.h new file mode 100644 index 0000000..f8753a7 --- /dev/null +++ b/cifs_spnego.h @@ -0,0 +1,46 @@ +/* + * fs/cifs/cifs_spnego.h -- SPNEGO upcall management for CIFS + * + * Copyright (c) 2007 Red Hat, Inc. + * Author(s): Jeff Layton (jlayton@redhat.com) + * Steve French (sfrench@us.ibm.com) + * + * This library is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See + * the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _CIFS_SPNEGO_H +#define _CIFS_SPNEGO_H + +#define CIFS_SPNEGO_UPCALL_VERSION 2 + +/* + * The version field should always be set to CIFS_SPNEGO_UPCALL_VERSION. + * The flags field is for future use. The request-key callout should set + * sesskey_len and secblob_len, and then concatenate the SessKey+SecBlob + * and stuff it in the data field. + */ +struct cifs_spnego_msg { + uint32_t version; + uint32_t flags; + uint32_t sesskey_len; + uint32_t secblob_len; + uint8_t data[1]; +}; + +#ifdef __KERNEL__ +extern struct key_type cifs_spnego_key_type; +#endif /* KERNEL */ + +#endif /* _CIFS_SPNEGO_H */ diff --git a/mount.cifs.c b/mount.cifs.c new file mode 100644 index 0000000..d307f4f --- /dev/null +++ b/mount.cifs.c @@ -0,0 +1,1779 @@ +/* + Mount helper utility for Linux CIFS VFS (virtual filesystem) client + Copyright (C) 2003,2008 Steve French (sfrench@us.ibm.com) + Copyright (C) 2008 Jeremy Allison (jra@samba.org) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> +#include <pwd.h> +#include <grp.h> +#include <ctype.h> +#include <sys/types.h> +#include <sys/mount.h> +#include <sys/stat.h> +#include <sys/utsname.h> +#include <sys/socket.h> +#include <arpa/inet.h> +#include <getopt.h> +#include <errno.h> +#include <netdb.h> +#include <string.h> +#include <mntent.h> +#include <fcntl.h> +#include <limits.h> +#include <fstab.h> +#include "mount.h" + +#define MOUNT_CIFS_VERSION_MAJOR "1" +#define MOUNT_CIFS_VERSION_MINOR "14" + +#ifndef MOUNT_CIFS_VENDOR_SUFFIX + #ifdef _SAMBA_BUILD_ + #include "version.h" + #ifdef SAMBA_VERSION_VENDOR_SUFFIX + #define MOUNT_CIFS_VENDOR_SUFFIX "-"SAMBA_VERSION_OFFICIAL_STRING"-"SAMBA_VERSION_VENDOR_SUFFIX + #else + #define MOUNT_CIFS_VENDOR_SUFFIX "-"SAMBA_VERSION_OFFICIAL_STRING + #endif /* SAMBA_VERSION_OFFICIAL_STRING and SAMBA_VERSION_VENDOR_SUFFIX */ + #else + #define MOUNT_CIFS_VENDOR_SUFFIX "" + #endif /* _SAMBA_BUILD_ */ +#endif /* MOUNT_CIFS_VENDOR_SUFFIX */ + +#ifdef _SAMBA_BUILD_ +#include "include/config.h" +#endif + +#ifndef MS_MOVE +#define MS_MOVE 8192 +#endif + +#ifndef MS_BIND +#define MS_BIND 4096 +#endif + +/* private flags - clear these before passing to kernel */ +#define MS_USERS 0x40000000 +#define MS_USER 0x80000000 + +#define MAX_UNC_LEN 1024 + +#ifndef SAFE_FREE +#define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0) +#endif + +#define MOUNT_PASSWD_SIZE 128 +#define DOMAIN_SIZE 64 + +/* currently maximum length of IPv6 address string */ +#define MAX_ADDRESS_LEN INET6_ADDRSTRLEN + +/* + * mount.cifs has been the subject of many "security" bugs that have arisen + * because of users and distributions installing it as a setuid root program. + * mount.cifs has not been audited for security. Thus, we strongly recommend + * that it not be installed setuid root. To make that abundantly clear, + * mount.cifs now check whether it's running setuid root and exit with an + * error if it is. If you wish to disable this check, then set the following + * #define to 1, but please realize that you do so at your own peril. + */ +#define CIFS_DISABLE_SETUID_CHECK 0 + +/* + * By default, mount.cifs follows the conventions set forth by /bin/mount + * for user mounts. That is, it requires that the mount be listed in + * /etc/fstab with the "user" option when run as an unprivileged user and + * mount.cifs is setuid root. + * + * Older versions of mount.cifs however were "looser" in this regard. When + * made setuid root, a user could run mount.cifs directly and mount any share + * on a directory owned by that user. + * + * The legacy behavior is now disabled by default. To reenable it, set the + * following #define to true. + */ +#define CIFS_LEGACY_SETUID_CHECK 0 + +/* + * When an unprivileged user runs a setuid mount.cifs, we set certain mount + * flags by default. These defaults can be changed here. + */ +#define CIFS_SETUID_FLAGS (MS_NOSUID|MS_NODEV) + +const char *thisprogram; +int verboseflag = 0; +int fakemnt = 0; +static int got_password = 0; +static int got_user = 0; +static int got_domain = 0; +static int got_ip = 0; +static int got_unc = 0; +static int got_uid = 0; +static int got_gid = 0; +static char * user_name = NULL; +static char * mountpassword = NULL; +char * domain_name = NULL; +char * prefixpath = NULL; +const char *cifs_fstype = "cifs"; + +/* glibc doesn't have strlcpy, strlcat. Ensure we do. JRA. We + * don't link to libreplace so need them here. */ + +/* like strncpy but does not 0 fill the buffer and always null + * terminates. bufsize is the size of the destination buffer */ + +#ifndef HAVE_STRLCPY +static size_t strlcpy(char *d, const char *s, size_t bufsize) +{ + size_t len = strlen(s); + size_t ret = len; + if (bufsize <= 0) return 0; + if (len >= bufsize) len = bufsize-1; + memcpy(d, s, len); + d[len] = 0; + return ret; +} +#endif + +/* like strncat but does not 0 fill the buffer and always null + * terminates. bufsize is the length of the buffer, which should + * be one more than the maximum resulting string length */ + +#ifndef HAVE_STRLCAT +static size_t strlcat(char *d, const char *s, size_t bufsize) +{ + size_t len1 = strlen(d); + size_t len2 = strlen(s); + size_t ret = len1 + len2; + + if (len1+len2 >= bufsize) { + if (bufsize < (len1+1)) { + return ret; + } + len2 = bufsize - (len1+1); + } + if (len2 > 0) { + memcpy(d+len1, s, len2); + d[len1+len2] = 0; + } + return ret; +} +#endif + +/* + * If an unprivileged user is doing the mounting then we need to ensure + * that the entry is in /etc/fstab. + */ +static int +check_mountpoint(const char *progname, char *mountpoint) +{ + int err; + struct stat statbuf; + + /* does mountpoint exist and is it a directory? */ + err = stat(".", &statbuf); + if (err) { + fprintf(stderr, "%s: failed to stat %s: %s\n", progname, + mountpoint, strerror(errno)); + return EX_USAGE; + } + + if (!S_ISDIR(statbuf.st_mode)) { + fprintf(stderr, "%s: %s is not a directory!", progname, + mountpoint); + return EX_USAGE; + } + +#if CIFS_LEGACY_SETUID_CHECK + /* do extra checks on mountpoint for legacy setuid behavior */ + if (!getuid() || geteuid()) + return 0; + + if (statbuf.st_uid != getuid()) { + fprintf(stderr, "%s: %s is not owned by user\n", progname, + mountpoint); + return EX_USAGE; + } + + if ((statbuf.st_mode & S_IRWXU) != S_IRWXU) { + fprintf(stderr, "%s: invalid permissions on %s\n", progname, + mountpoint); + return EX_USAGE; + } +#endif /* CIFS_LEGACY_SETUID_CHECK */ + + return 0; +} + +#if CIFS_DISABLE_SETUID_CHECK +static int +check_setuid(void) +{ + return 0; +} +#else /* CIFS_DISABLE_SETUID_CHECK */ +static int +check_setuid(void) +{ + if (getuid() && !geteuid()) { + printf("This mount.cifs program has been built with the " + "ability to run as a setuid root program disabled.\n" + "mount.cifs has not been well audited for security " + "holes. Therefore the Samba team does not recommend " + "installing it as a setuid root program.\n"); + return 1; + } + + return 0; +} +#endif /* CIFS_DISABLE_SETUID_CHECK */ + +#if CIFS_LEGACY_SETUID_CHECK +static int +check_fstab(const char *progname, char *mountpoint, char *devname, + char **options) +{ + return 0; +} +#else /* CIFS_LEGACY_SETUID_CHECK */ +static int +check_fstab(const char *progname, char *mountpoint, char *devname, + char **options) +{ + FILE *fstab; + struct mntent *mnt; + + /* make sure this mount is listed in /etc/fstab */ + fstab = setmntent(_PATH_FSTAB, "r"); + if (!fstab) { + fprintf(stderr, "Couldn't open %s for reading!\n", + _PATH_FSTAB); + return EX_FILEIO; + } + + while((mnt = getmntent(fstab))) { + if (!strcmp(mountpoint, mnt->mnt_dir)) + break; + } + endmntent(fstab); + + if (mnt == NULL || strcmp(mnt->mnt_fsname, devname)) { + fprintf(stderr, "%s: permission denied: no match for " + "%s found in %s\n", progname, mountpoint, + _PATH_FSTAB); + return EX_USAGE; + } + + /* + * 'mount' munges the options from fstab before passing them + * to us. It is non-trivial to test that we have the correct + * set of options. We don't want to trust what the user + * gave us, so just take whatever is in /etc/fstab. + */ + free(*options); + *options = strdup(mnt->mnt_opts); + return 0; +} +#endif /* CIFS_LEGACY_SETUID_CHECK */ + +/* BB finish BB + + cifs_umount + open nofollow - avoid symlink exposure? + get owner of dir see if matches self or if root + call system(umount argv) etc. + +BB end finish BB */ + +static char * check_for_domain(char **); + + +static void mount_cifs_usage(FILE *stream) +{ + fprintf(stream, "\nUsage: %s <remotetarget> <dir> -o <options>\n", thisprogram); + fprintf(stream, "\nMount the remote target, specified as a UNC name,"); + fprintf(stream, " to a local directory.\n\nOptions:\n"); + fprintf(stream, "\tuser=<arg>\n\tpass=<arg>\n\tdom=<arg>\n"); + fprintf(stream, "\nLess commonly used options:"); + fprintf(stream, "\n\tcredentials=<filename>,guest,perm,noperm,setuids,nosetuids,rw,ro,"); + fprintf(stream, "\n\tsep=<char>,iocharset=<codepage>,suid,nosuid,exec,noexec,serverino,"); + fprintf(stream, "\n\tmapchars,nomapchars,nolock,servernetbiosname=<SRV_RFC1001NAME>"); + fprintf(stream, "\n\tdirectio,nounix,cifsacl,sec=<authentication mechanism>,sign"); + fprintf(stream, "\n\nOptions not needed for servers supporting CIFS Unix extensions"); + fprintf(stream, "\n\t(e.g. unneeded for mounts to most Samba versions):"); + fprintf(stream, "\n\tuid=<uid>,gid=<gid>,dir_mode=<mode>,file_mode=<mode>,sfu"); + fprintf(stream, "\n\nRarely used options:"); + fprintf(stream, "\n\tport=<tcpport>,rsize=<size>,wsize=<size>,unc=<unc_name>,ip=<ip_address>,"); + fprintf(stream, "\n\tdev,nodev,nouser_xattr,netbiosname=<OUR_RFC1001NAME>,hard,soft,intr,"); + fprintf(stream, "\n\tnointr,ignorecase,noposixpaths,noacl,prefixpath=<path>,nobrl"); + fprintf(stream, "\n\nOptions are described in more detail in the manual page"); + fprintf(stream, "\n\tman 8 mount.cifs\n"); + fprintf(stream, "\nTo display the version number of the mount helper:"); + fprintf(stream, "\n\t%s -V\n",thisprogram); + + SAFE_FREE(mountpassword); + + if (stream == stderr) + exit(EX_USAGE); + exit(0); +} + +/* caller frees username if necessary */ +static char * getusername(void) { + char *username = NULL; + struct passwd *password = getpwuid(getuid()); + + if (password) { + username = password->pw_name; + } + return username; +} + +static int open_cred_file(char * file_name) +{ + char * line_buf; + char * temp_val; + FILE * fs; + int i, length; + + i = access(file_name, R_OK); + if (i) + return i; + + fs = fopen(file_name,"r"); + if(fs == NULL) + return errno; + line_buf = (char *)malloc(4096); + if(line_buf == NULL) { + fclose(fs); + return ENOMEM; + } + + while(fgets(line_buf,4096,fs)) { + /* parse line from credential file */ + + /* eat leading white space */ + for(i=0;i<4086;i++) { + if((line_buf[i] != ' ') && (line_buf[i] != '\t')) + break; + /* if whitespace - skip past it */ + } + if (strncasecmp("username",line_buf+i,8) == 0) { + temp_val = strchr(line_buf + i,'='); + if(temp_val) { + /* go past equals sign */ + temp_val++; + for(length = 0;length<4087;length++) { + if ((temp_val[length] == '\n') + || (temp_val[length] == '\0')) { + temp_val[length] = '\0'; + break; + } + } + if(length > 4086) { + fprintf(stderr, "mount.cifs failed due to malformed username in credentials file\n"); + memset(line_buf,0,4096); + exit(EX_USAGE); + } else { + got_user = 1; + user_name = (char *)calloc(1 + length,1); + /* BB adding free of user_name string before exit, + not really necessary but would be cleaner */ + strlcpy(user_name,temp_val, length+1); + } + } + } else if (strncasecmp("password",line_buf+i,8) == 0) { + temp_val = strchr(line_buf+i,'='); + if(temp_val) { + /* go past equals sign */ + temp_val++; + for(length = 0;length<MOUNT_PASSWD_SIZE+1;length++) { + if ((temp_val[length] == '\n') + || (temp_val[length] == '\0')) { + temp_val[length] = '\0'; + break; + } + } + if(length > MOUNT_PASSWD_SIZE) { + fprintf(stderr, "mount.cifs failed: password in credentials file too long\n"); + memset(line_buf,0, 4096); + exit(EX_USAGE); + } else { + if(mountpassword == NULL) { + mountpassword = (char *)calloc(MOUNT_PASSWD_SIZE+1,1); + } else + memset(mountpassword,0,MOUNT_PASSWD_SIZE); + if(mountpassword) { + strlcpy(mountpassword,temp_val,MOUNT_PASSWD_SIZE+1); + got_password = 1; + } + } + } + } else if (strncasecmp("domain",line_buf+i,6) == 0) { + temp_val = strchr(line_buf+i,'='); + if(temp_val) { + /* go past equals sign */ + temp_val++; + if(verboseflag) + fprintf(stderr, "\nDomain %s\n",temp_val); + for(length = 0;length<DOMAIN_SIZE+1;length++) { + if ((temp_val[length] == '\n') + || (temp_val[length] == '\0')) { + temp_val[length] = '\0'; + break; + } + } + if(length > DOMAIN_SIZE) { + fprintf(stderr, "mount.cifs failed: domain in credentials file too long\n"); + exit(EX_USAGE); + } else { + if(domain_name == NULL) { + domain_name = (char *)calloc(DOMAIN_SIZE+1,1); + } else + memset(domain_name,0,DOMAIN_SIZE); + if(domain_name) { + strlcpy(domain_name,temp_va |