From patchwork Fri Aug 7 19:43:16 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 30970 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.samba.org (fn.samba.org [216.83.154.106]) by bilbo.ozlabs.org (Postfix) with ESMTP id AE62BB7B61 for ; Sat, 8 Aug 2009 05:43:50 +1000 (EST) Received: from fn.samba.org (localhost [127.0.0.1]) by lists.samba.org (Postfix) with ESMTP id D53FEAD0AA; Fri, 7 Aug 2009 13:39:23 -0600 (MDT) X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on fn.samba.org X-Spam-Level: X-Spam-Status: No, score=-3.2 required=3.8 tests=AWL, BAYES_00, NO_MORE_FUNN, SPF_PASS autolearn=no version=3.2.5 X-Original-To: linux-cifs-client@lists.samba.org Delivered-To: linux-cifs-client@lists.samba.org Received: from cdptpa-omtalb.mail.rr.com (cdptpa-omtalb.mail.rr.com [75.180.132.121]) by lists.samba.org (Postfix) with ESMTP id 4DB17AD060; Fri, 7 Aug 2009 13:39:03 -0600 (MDT) Received: from mail.poochiereds.net ([71.70.153.3]) by cdptpa-omta02.mail.rr.com with ESMTP id <20090807194320038.MERM22035@cdptpa-omta02.mail.rr.com>; Fri, 7 Aug 2009 19:43:20 +0000 Received: by mail.poochiereds.net (Postfix, from userid 4447) id E6DAD58144; Fri, 7 Aug 2009 15:43:17 -0400 (EDT) From: Jeff Layton To: linux-cifs-client@lists.samba.org, samba-technical@lists.samba.org Date: Fri, 7 Aug 2009 15:43:16 -0400 Message-Id: <1249674197-1065-7-git-send-email-jlayton@redhat.com> X-Mailer: git-send-email 1.6.0.6 In-Reply-To: <1249674197-1065-1-git-send-email-jlayton@redhat.com> References: <1249674197-1065-1-git-send-email-jlayton@redhat.com> Subject: [linux-cifs-client] [PATCH 6/7] cifs.upcall: use ip address passed by kernel to get server's hostname X-BeenThere: linux-cifs-client@lists.samba.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: The Linux CIFS VFS client List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: linux-cifs-client-bounces@lists.samba.org Errors-To: linux-cifs-client-bounces@lists.samba.org Instead of using the hostname given by the upcall to get the server's principal, take the IP address given in the upcall and reverse resolve it to a hostname. Signed-off-by: Jeff Layton --- client/cifs.upcall.c | 68 +++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 56 insertions(+), 12 deletions(-) diff --git a/client/cifs.upcall.c b/client/cifs.upcall.c index 38be876..1e58503 100644 --- a/client/cifs.upcall.c +++ b/client/cifs.upcall.c @@ -150,15 +150,15 @@ handle_krb5_mech(const char *oid, const char *principal, DATA_BLOB *secblob, #define DKD_HAVE_HOSTNAME 0x1 #define DKD_HAVE_VERSION 0x2 #define DKD_HAVE_SEC 0x4 -#define DKD_HAVE_IPV4 0x8 -#define DKD_HAVE_IPV6 0x10 -#define DKD_HAVE_UID 0x20 -#define DKD_HAVE_PID 0x40 -#define DKD_MUSTHAVE_SET (DKD_HAVE_HOSTNAME|DKD_HAVE_VERSION|DKD_HAVE_SEC) +#define DKD_HAVE_IP 0x8 +#define DKD_HAVE_UID 0x10 +#define DKD_HAVE_PID 0x20 +#define DKD_MUSTHAVE_SET (DKD_HAVE_IP|DKD_HAVE_VERSION|DKD_HAVE_SEC) static struct decoded_args { int ver; char *hostname; + char *ip; uid_t uid; pid_t pid; sectype_t sec; @@ -167,6 +167,7 @@ static struct decoded_args { static unsigned int decode_key_description(const char *desc, struct decoded_args *arg) { + int len; int retval = 0; char *pos; const char *tkn = desc; @@ -174,7 +175,6 @@ decode_key_description(const char *desc, struct decoded_args *arg) do { pos = index(tkn, ';'); if (strncmp(tkn, "host=", 5) == 0) { - int len; if (pos == NULL) len = strlen(tkn); @@ -186,10 +186,18 @@ decode_key_description(const char *desc, struct decoded_args *arg) arg->hostname = SMB_XMALLOC_ARRAY(char, len); strlcpy(arg->hostname, tkn + 5, len); retval |= DKD_HAVE_HOSTNAME; - } else if (strncmp(tkn, "ipv4=", 5) == 0) { - /* BB: do we need it if we have hostname already? */ - } else if (strncmp(tkn, "ipv6=", 5) == 0) { - /* BB: do we need it if we have hostname already? */ + } 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); @@ -288,6 +296,35 @@ cifs_resolver(const key_serial_t key, const char *key_descr) return 0; } +static int +ip_to_fqdn(const char *ipaddr, char *host, size_t hostlen) +{ + int rc; + struct addrinfo hints = { .ai_flags = AI_NUMERICHOST }; + struct addrinfo *res; + + 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) { @@ -306,6 +343,7 @@ int main(const int argc, char *const argv[]) long rc = 1; int c; char *buf, *princ, *ccname = NULL; + char hostbuf[NI_MAXHOST]; struct decoded_args arg = { }; const char *oid; @@ -383,12 +421,18 @@ int main(const int argc, char *const argv[]) } } + if (have & DKD_HAVE_IP) { + rc = ip_to_fqdn(arg.ip, hostbuf, sizeof(hostbuf)); + if (rc) + goto out; + } + // do mech specific authorization switch (arg.sec) { case MS_KRB5: case KRB5: /* for "cifs/" service name + terminating 0 */ - datalen = strlen(arg.hostname) + 5 + 1; + datalen = strnlen(hostbuf, sizeof(hostbuf)) + 5 + 1; princ = SMB_XMALLOC_ARRAY(char, datalen); if (!princ) { rc = 1; @@ -405,7 +449,7 @@ int main(const int argc, char *const argv[]) * getting a host/ principal if that doesn't work. */ strlcpy(princ, "cifs/", datalen); - strlcpy(princ + 5, arg.hostname, datalen - 5); + strlcpy(princ + 5, hostbuf, datalen - 5); rc = handle_krb5_mech(oid, princ, &secblob, &sess_key, ccname); if (rc) { memcpy(princ, "host/", 5);