From patchwork Fri Aug 20 07:45:13 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Druzhinin X-Patchwork-Id: 120485 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.4/8.14.3) with ESMTP id o7K7jPso029965 for ; Fri, 20 Aug 2010 07:45:25 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751018Ab0HTHpY (ORCPT ); Fri, 20 Aug 2010 03:45:24 -0400 Received: from mail-ey0-f174.google.com ([209.85.215.174]:46875 "EHLO mail-ey0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750905Ab0HTHpX (ORCPT ); Fri, 20 Aug 2010 03:45:23 -0400 Received: by eyg5 with SMTP id 5so1899942eyg.19 for ; Fri, 20 Aug 2010 00:45:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:from:to:cc:subject:date :message-id:x-mailer; bh=C3PIyclsVDBnp56cJKx09Kpgklp7Dff0vN1qxPQQi+s=; b=Mp9ZIaESLXfAbqr7ohgvInoG/4p/Dt5CVRFZdaDhoRyy8TdtqtRT6ZJ0gcq6nO2PRx xzwklkb4YE3hX6bP/W8QWLd5ElU2jiwjmzEYywwROtM50/ATsKFiLu+DLIcv06kxC65M hf8aaSwymMZ3HoYJXV7adMespYkJ2+n+rPFxo= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer; b=II8Epbuko3JLecfoT7akuq8jV0mV6Isgoxf1pfIgIRRi6dRVrGxcEAwevcbmrp/q77 QkJBonB2n5aRTZ11ldWVGS0t7TZN4qW0iu7O7iAdpRxXUE62+plT35JSSk/IUXOUnMwY Hvl+CK00r7XElLcmQhxLMXP9kFnefr2McXcZ8= Received: by 10.213.17.195 with SMTP id t3mr1695233eba.63.1282290322423; Fri, 20 Aug 2010 00:45:22 -0700 (PDT) Received: from localhost.localdomain ([79.126.74.3]) by mx.google.com with ESMTPS id u9sm4244832eeh.5.2010.08.20.00.45.21 (version=TLSv1/SSLv3 cipher=RC4-MD5); Fri, 20 Aug 2010 00:45:21 -0700 (PDT) From: Igor Druzhinin To: jlayton@samba.org Cc: linux-cifs@vger.kernel.org, Igor Druzhinin Subject: [PATCH 1/2] cifs-utils: moving resolve_host into separate file Date: Fri, 20 Aug 2010 11:45:13 +0400 Message-Id: <1282290314-9477-1-git-send-email-jaxbrigs@gmail.com> X-Mailer: git-send-email 1.7.2.1 Sender: linux-cifs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-cifs@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Fri, 20 Aug 2010 07:45:25 +0000 (UTC) diff --git a/Makefile.am b/Makefile.am index c53c9ec..1ac9249 100644 --- a/Makefile.am +++ b/Makefile.am @@ -3,7 +3,7 @@ ACLOCAL_AMFLAGS = -I aclocal root_sbindir = "/sbin" root_sbin_PROGRAMS = mount.cifs -mount_cifs_SOURCES = mount.cifs.c mtab.c util.c +mount_cifs_SOURCES = mount.cifs.c mtab.c resolve_host.c util.c mount_cifs_LDADD = $(LIBCAP) $(CAPNG_LDADD) man_MANS = mount.cifs.8 diff --git a/mount.cifs.c b/mount.cifs.c index 3623e76..ed27bba 100644 --- a/mount.cifs.c +++ b/mount.cifs.c @@ -56,6 +56,7 @@ #endif /* HAVE_LIBCAP_NG */ #include "mount.h" #include "util.h" +#include "resolve_host.h" #ifndef MS_MOVE #define MS_MOVE 8192 @@ -87,12 +88,6 @@ /* max length of username (somewhat made up here) */ #define MAX_USERNAME_SIZE 32 -/* currently maximum length of IPv6 address string */ -#define MAX_ADDRESS_LEN INET6_ADDRSTRLEN - -/* limit list of addresses to 16 max-size addrs */ -#define MAX_ADDR_LIST_LEN ((MAX_ADDRESS_LEN + 1) * 16) - #ifndef SAFE_FREE #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x = NULL; } } while (0) #endif @@ -1207,90 +1202,6 @@ nocopy: return 0; } -/* - * resolve "host" portion of parsed info to comma-separated list of - * address(es) - */ -static int resolve_host(struct parsed_mount_info *parsed_info) -{ - int rc; - /* 10 for max width of decimal scopeid */ - char tmpbuf[NI_MAXHOST + 1 + 10 + 1]; - const char *ipaddr; - size_t len; - struct addrinfo *addrlist, *addr; - struct sockaddr_in *sin; - struct sockaddr_in6 *sin6; - - rc = getaddrinfo(parsed_info->host, NULL, NULL, &addrlist); - if (rc != 0) { - fprintf(stderr, "mount error: could not resolve address for " - "%s: %s\n", parsed_info->host, - rc == EAI_SYSTEM ? strerror(errno) : gai_strerror(rc)); - /* FIXME: return better error based on rc? */ - return EX_USAGE; - } - - addr = addrlist; - while (addr) { - /* skip non-TCP entries */ - if (addr->ai_socktype != SOCK_STREAM || - addr->ai_protocol != IPPROTO_TCP) { - addr = addr->ai_next; - continue; - } - - switch (addr->ai_addr->sa_family) { - case AF_INET6: - sin6 = (struct sockaddr_in6 *)addr->ai_addr; - ipaddr = inet_ntop(AF_INET6, &sin6->sin6_addr, tmpbuf, - sizeof(tmpbuf)); - if (!ipaddr) { - rc = EX_SYSERR; - fprintf(stderr, - "mount error: problem parsing address " - "list: %s\n", strerror(errno)); - goto resolve_host_out; - } - - if (sin6->sin6_scope_id) { - len = strnlen(tmpbuf, sizeof(tmpbuf)); - ipaddr = tmpbuf + len; - snprintf(tmpbuf, sizeof(tmpbuf) - len, "%%%u", - sin6->sin6_scope_id); - } - break; - case AF_INET: - sin = (struct sockaddr_in *)addr->ai_addr; - ipaddr = inet_ntop(AF_INET, &sin->sin_addr, tmpbuf, - sizeof(tmpbuf)); - if (!ipaddr) { - rc = EX_SYSERR; - fprintf(stderr, - "mount error: problem parsing address " - "list: %s\n", strerror(errno)); - goto resolve_host_out; - } - - break; - default: - addr = addr->ai_next; - continue; - } - - if (parsed_info->addrlist[0] != '\0') - strlcat(parsed_info->addrlist, ",", - sizeof(parsed_info->addrlist)); - strlcat(parsed_info->addrlist, tmpbuf, - sizeof(parsed_info->addrlist)); - addr = addr->ai_next; - } - -resolve_host_out: - freeaddrinfo(addrlist); - return rc; -} - static int parse_unc(const char *unc_name, struct parsed_mount_info *parsed_info) { int length = strnlen(unc_name, MAX_UNC_LEN); @@ -1645,10 +1556,20 @@ assemble_mountinfo(struct parsed_mount_info *parsed_info, if (rc) goto assemble_exit; - rc = resolve_host(parsed_info); - if (rc) + rc = resolve_host(parsed_info->host, parsed_info->addrlist); + switch (rc) { + case EX_USAGE: + fprintf(stderr, "mount error: could not resolve address for " + "%s: %s\n", parsed_info->host, + rc == EAI_SYSTEM ? strerror(errno) : gai_strerror(rc)); goto assemble_exit; + case EX_SYSERR: + fprintf(stderr, "mount error: problem parsing address " + "list: %s\n", strerror(errno)); + goto assemble_exit; + } + if (!parsed_info->got_user) { /* * Note that the password will not be retrieved from the diff --git a/resolve_host.c b/resolve_host.c new file mode 100644 index 0000000..7687503 --- /dev/null +++ b/resolve_host.c @@ -0,0 +1,105 @@ +/* + * resolving DNS hostname routine + * + * Copyright (C) 2010 Jeff Layton (jlayton@samba.org) + * Copyright (C) 2010 Igor Druzhinin (jaxbrigs@gmail.com) + * + * 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 . + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif /* HAVE_CONFIG_H */ + +#include +#include +#include +#include +#include +#include "mount.h" +#include "util.h" +#include "resolve_host.h" + +/* + * resolve hostname to comma-separated list of address(es) + */ +int resolve_host(const char *host, char *addrstr) +{ + int rc; + /* 10 for max width of decimal scopeid */ + char tmpbuf[NI_MAXHOST + 1 + 10 + 1]; + const char *ipaddr; + size_t len; + struct addrinfo *addrlist, *addr; + struct sockaddr_in *sin; + struct sockaddr_in6 *sin6; + + rc = getaddrinfo(host, NULL, NULL, &addrlist); + if (rc != 0) + return EX_USAGE; + + addr = addrlist; + while (addr) { + /* skip non-TCP entries */ + if (addr->ai_socktype != SOCK_STREAM || + addr->ai_protocol != IPPROTO_TCP) { + addr = addr->ai_next; + continue; + } + + switch (addr->ai_addr->sa_family) { + case AF_INET6: + sin6 = (struct sockaddr_in6 *)addr->ai_addr; + ipaddr = inet_ntop(AF_INET6, &sin6->sin6_addr, tmpbuf, + sizeof(tmpbuf)); + if (!ipaddr) { + rc = EX_SYSERR; + goto resolve_host_out; + } + + if (sin6->sin6_scope_id) { + len = strnlen(tmpbuf, sizeof(tmpbuf)); + ipaddr = tmpbuf + len; + snprintf(tmpbuf, sizeof(tmpbuf) - len, "%%%u", + sin6->sin6_scope_id); + } + break; + case AF_INET: + sin = (struct sockaddr_in *)addr->ai_addr; + ipaddr = inet_ntop(AF_INET, &sin->sin_addr, tmpbuf, + sizeof(tmpbuf)); + if (!ipaddr) { + rc = EX_SYSERR; + goto resolve_host_out; + } + + break; + default: + addr = addr->ai_next; + continue; + } + + if (addr == addrlist) + *addrstr = '\0'; + else + strlcat(addrstr, ",", MAX_ADDR_LIST_LEN); + + strlcat(addrstr, tmpbuf, MAX_ADDR_LIST_LEN); + addr = addr->ai_next; + } + +resolve_host_out: + freeaddrinfo(addrlist); + return rc; +} diff --git a/resolve_host.h b/resolve_host.h new file mode 100644 index 0000000..b949245 --- /dev/null +++ b/resolve_host.h @@ -0,0 +1,34 @@ +/* + * resolving DNS hostname routine + * + * Copyright (C) 2010 Jeff Layton (jlayton@samba.org) + * Copyright (C) 2010 Igor Druzhinin (jaxbrigs@gmail.com) + * + * 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 . + */ + +#ifndef _RESOLVE_HOST_H_ +#define _RESOLVE_HOST_H_ + +#include + +/* currently maximum length of IPv6 address string */ +#define MAX_ADDRESS_LEN INET6_ADDRSTRLEN + +/* limit list of addresses to 16 max-size addrs */ +#define MAX_ADDR_LIST_LEN ((MAX_ADDRESS_LEN + 1) * 16) + +extern int resolve_host(const char *host, char *addrstr); + +#endif /* _RESOLVE_HOST_H_ */