Skip to contentSkip to conversation

Generalise dns_resolver

PATCH In review David Howells 4 participants 10 messages Jul 7, 2010 – Jul 20, 2010 3 messages not in the archive

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

cover Generalise dns_resolver

Hi Steve,

The associated patches extract the dns_resolver key type from the CIFS filesystem and make it useful by AFS too. Can you check at least the second patch which actually modifies CIFS?

Thanks,
David
---

David Howells (1):
KEYS: Authorise keyctl_set_timeout() on a key if we have its authorisation key

Wang Lei (3):
Add DNS support for AFS
Provide generic DNS query function
Implement a DNS Resolver Module

 Documentation/networking/dnsresolver.txt |  150 +++++++++++++++
 fs/afs/Kconfig                           |    1 
 fs/afs/cell.c                            |   40 +++-
 fs/cifs/Kconfig                          |   17 +-
 fs/cifs/Makefile                         |    2 
 fs/cifs/cifs_dfs_ref.c                   |    4 
 fs/cifs/cifsfs.c                         |   13 -
 fs/cifs/dns_resolve.c                    |  168 -----------------
 fs/cifs/dns_resolve.h                    |   32 ---
 include/keys/dnsresolver-type.h          |   23 ++
 include/linux/dns_resolver.h             |   37 ++++
 net/Kconfig                              |    1 
 net/Makefile                             |    1 
 net/dnsresolver/Kconfig                  |   27 +++
 net/dnsresolver/Makefile                 |    7 +
 net/dnsresolver/dns_query.c              |  130 +++++++++++++
 net/dnsresolver/dns_resolver.c           |  293 ++++++++++++++++++++++++++++++
 net/dnsresolver/internal.h               |   39 ++++
 net/dnsresolver/resolv_unc_to_ip.c       |  111 +++++++++++
 security/keys/keyctl.c                   |   17 ++
 20 files changed, 879 insertions(+), 234 deletions(-)
 create mode 100644 Documentation/networking/dnsresolver.txt
 delete mode 100644 fs/cifs/dns_resolve.c
 delete mode 100644 fs/cifs/dns_resolve.h
 create mode 100644 include/keys/dnsresolver-type.h
 create mode 100644 include/linux/dns_resolver.h
 create mode 100644 net/dnsresolver/Kconfig
 create mode 100644 net/dnsresolver/Makefile
 create mode 100644 net/dnsresolver/dns_query.c
 create mode 100644 net/dnsresolver/dns_resolver.c
 create mode 100644 net/dnsresolver/internal.h
 create mode 100644 net/dnsresolver/resolv_unc_to_ip.c
maintainer
#1 lore raw

PATCH 1/4 KEYS: Authorise keyctl_set_timeout() on a key if we have its authorisation key

Authorise a process to perform keyctl_set_timeout() on an uninstantiated key if that process has the authorisation key for it.

This allows the instantiator to set the timeout on a key it is instantiating - provided it does it before instantiating the key.

For instance, the test upcall script provided with the keyutils package could be modified to set the expiry to an hour hence before instantiating the key:

	[/usr/share/keyutils/request-key-debug.sh]
	 if [ "$3" != "neg" ]
	 then
	+    keyctl timeout $1 3600
	     keyctl instantiate $1 "Debug $3" $4 || exit 1
	 else

---

 security/keys/keyctl.c |   17 ++++++++++++++++-
 1 files changed, 16 insertions(+), 1 deletions(-)
security/keys/keyctl.c +16 -1
index 6261745..639226a 100644
@@ -1091,7 +1091,7 @@ error:
 long keyctl_set_timeout(key_serial_t id, unsigned timeout)
 {
 	struct timespec now;
-	struct key *key;
+	struct key *key, *instkey;
 	key_ref_t key_ref;
 	time_t expiry;
 	long ret;
@@ -1099,10 +1099,25 @@ long keyctl_set_timeout(key_serial_t id, unsigned timeout)
 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
 				  KEY_SETATTR);
 	if (IS_ERR(key_ref)) {
+		/* setting the timeout on a key under construction is permitted
+		 * if we have the authorisation token handy */
+		if (PTR_ERR(key_ref) == -EACCES) {
+			instkey = key_get_instantiation_authkey(id);
+			if (!IS_ERR(instkey)) {
+				key_put(instkey);
+				key_ref = lookup_user_key(id,
+							  KEY_LOOKUP_PARTIAL,
+							  0);
+				if (!IS_ERR(key_ref))
+					goto okay;
+			}
+		}
+
 		ret = PTR_ERR(key_ref);
 		goto error;
 	}
 
+okay:
 	key = key_ref_to_ptr(key_ref);
 
 	/* make the changes with the locks held to prevent races */

A message that is not in the archive (sent off-list or not yet received): 20100707091400.16573.2817.stgit-S6HVgzuS8uM4Awkfq6JHfwNdhmdF6hFW@public.gmane.org

#3 lore raw

PATCH 2/4 Implement a DNS Resolver Module

From: Wang Lei <[email protected]>

Separate out the DNS resolver key type from the CIFS filesystem into its own module so that it can be made available for general use, including the AFS filesystem module.

This makes it possible for the kernel to upcall to userspace to have it issue DNS requests and package up the replies and present them to the kernel in a useful form. The kernel is then able to cache the DNS replies as keys can be retained in keyrings.

Resolver keys are of type "dns_resolver" and have a case-insensitive description that is of the form "[<type>:]<domain_name>". The optional <type> indicates the particular DNS lookup and packaging that's required. The <domain_name> is the query to be made.

If <type> isn't given, a basic hostname to IP address lookup is made, and the result is stored in the key in the form of a printable string consisting of a comma-separated list of IPv4 and IPv6 addresses.

This key type is supported by userspace helpers driven from /sbin/request-key and configured through /etc/request-key.conf. The cifs.upcall utility is invoked for UNC path server name to IP address resolution.

The CIFS functionality is encapsulated by the dns_resolve_unc_to_ip() function, which is used to resolve a UNC path to an IP address for CIFS filesystem. This can be reused by filesystems other than CIFS that want to make the same resolution.

---

 Documentation/networking/dnsresolver.txt |  150 +++++++++++++++
 fs/cifs/Kconfig                          |   17 +-
 fs/cifs/Makefile                         |    2 
 fs/cifs/cifs_dfs_ref.c                   |    4 
 fs/cifs/cifsfs.c                         |   13 -
 fs/cifs/dns_resolve.c                    |  168 -----------------
 fs/cifs/dns_resolve.h                    |   32 ---
 include/keys/dnsresolver-type.h          |   23 ++
 include/linux/dns_resolver.h             |   35 ++++
 net/Kconfig                              |    1 
 net/Makefile                             |    1 
 net/dnsresolver/Kconfig                  |   27 +++
 net/dnsresolver/Makefile                 |    7 +
 net/dnsresolver/dns_resolver.c           |  293 ++++++++++++++++++++++++++++++
 net/dnsresolver/internal.h               |   39 ++++
 net/dnsresolver/resolv_unc_to_ip.c       |  111 +++++++++++
 16 files changed, 700 insertions(+), 223 deletions(-)
 create mode 100644 Documentation/networking/dnsresolver.txt
 delete mode 100644 fs/cifs/dns_resolve.c
 delete mode 100644 fs/cifs/dns_resolve.h
 create mode 100644 include/keys/dnsresolver-type.h
 create mode 100644 include/linux/dns_resolver.h
 create mode 100644 net/dnsresolver/Kconfig
 create mode 100644 net/dnsresolver/Makefile
 create mode 100644 net/dnsresolver/dns_resolver.c
 create mode 100644 net/dnsresolver/internal.h
 create mode 100644 net/dnsresolver/resolv_unc_to_ip.c
Documentation/networking/dnsresolver.txt new file +150 -0
new file mode 100644
index 0000000..a7fd352
@@ -0,0 +1,150 @@
+			     ===================
+			     DNS Resolver Module
+			     ===================
+
+Contents:
+
+ - Overview.
+ - Compilation.
+ - Setting up.
+ - Usage.
+ - Mechanism.
+ - Debugging.
+
+
+========
+OVERVIEW
+========
+
+The DNS resolver module provides a way for kernel services to make DNS queries
+by way of requesting a key of key type dns_resolver.  These queries are
+upcalled to userspace through /sbin/request-key.
+
+These routines must be supported by userspace tools dns.upcall, cifs.upcall and
+request-key.  It is under development and does not yet provide the full feature
+set.  The features it does support include:
+
+ (*) Implements the dns_resolver key_type to contact userspace.
+
+ (*) extracted the dns_resolve_unc_to_ip function from CIFS.
+
+It does not yet support the following AFS features:
+
+ (*) Dns query support for AFSDB resource record.
+
+This code is extracted from the CIFS filesystem.
+
+
+===========
+COMPILATION
+===========
+
+The module should be enabled by turning on the kernel configuration options:
+
+	CONFIG_DNS_RESOLVER	- tristate "DNS Resolver support"
+
+
+==========
+SETTING UP
+==========
+
+To set up this facility, the /etc/request-key.conf file must be altered so that
+/sbin/request-key can appropriately direct the upcalls.  For example, to handle
+basic dname to IPv4/IPv6 address resolution, the following line should be
+added:
+
+	#OP	TYPE		DESC	CO-INFO	PROGRAM ARG1 ARG2 ARG3 ...
+	#======	============	=======	=======	==========================
+	create	dns_resolver  	*	*	/usr/sbin/cifs.upcall %k
+
+To direct a query for query type 'foo', a line of the following should be added
+before the more general line given above as the first match is the one taken.
+
+	create	dns_resolver  	foo:*	*	/usr/sbin/dns.foo %k
+
+
+
+=====
+USAGE
+=====
+
+To make use of this facility, one of the following functions that are
+implemented in the module can be called after doing:
+
+	#include <linux/dns_resolver.h>
+
+ (1) int request_dns_resolution(const char *description, const char *options,
+				char **result);
+
+     This is the basic access function.  It looks for a cached DNS query and if
+     it doesn't find it, it upcalls to userspace to make a new DNS query, which
+     may then be cached.  The description is a string of form:
+
+	[<query_type>:]<dname>
+
+     where <query_type> optionally specifies the particular upcall program to
+     invoke, and thus the type of query to do, and <dname> specifies the string
+     to be looked up.  The default query type is a straight hostname to IP
+     address set lookup.
+
+     The options parameter may be NULL or it may be a set of options
+     appropriate to the query type.
+
+     The return value is a string appropriate to the query type.  For instance,
+     for the default query type it is just a list of comma-separated IPv4 and
+     IPv6 addresses.  The caller must free the result.
+
+     0 is returned on success, and a -ve error code is returned otherwise.
+     -EKEYREJECTED will be returned if the DNS lookup failed.
+
+
+ (2) int dns_resolve_unc_to_ip(const char *unc, char **ip_addr);
+
+     This functions takes a UNC-style path, as used by CIFS, and translates the
+     server address embedded therein into an IP address in string form, which
+     it returns by pointing *ip_addr at it.  The caller must free the result.
+
+     0 is returned on success, and a -ve error code is returned otherwise.
+     -EKEYREJECTED will be returned if the DNS lookup failed.
+
+
+=========
+MECHANISM
+=========
+
+The dnsresolver module registers a key type called "dns_resolver".  Keys of
+this type are used to transport and cache DNS lookup results from userspace.
+
+When request_dns_resolution() is invoked, it calls request_key() to search the
+local keyrings for a cached DNS result.  If that fails to find one, it upcalls
+to userspace to get a new result.
+
+Upcalls to userspace are made through the request_key() upcall vector, and are
+directed by means of configuration lines in /etc/request-key.conf that tell
+/sbin/request-key what program to run to instantiate the key.
+
+The upcall handler program is responsible for querying the DNS, processing the
+result into a form suitable for passing to the keyctl_instantiate_key()
+routine.  This then passes the data to dns_resolver_instantiate() which strips
+off and processes any options included in the data, and then attaches the
+remainder of the string to the key as its payload.
+
+The upcall handler program should set the expiry time on the key to that of the
+lowest TTL of all the records it has extracted a result from.  This means that
+the key will be discarded and recreated when the data it holds has expired.
+
+request_dns_resolution() returns the value attached to the key, or an error if
+that is indicated instead.
+
+See <file:Documentation/keys-request-key.txt> for further information about
+request-key function.
+
+
+=========
+DEBUGGING
+=========
+
+Debugging messages can be turned on dynamically by writing a 1 into the
+following file:
+
+        /sys/module/dnsresolver/parameters/debug
fs/cifs/Kconfig +9 -8
index 80f3525..7a7081c 100644
@@ -71,14 +71,14 @@ config CIFS_WEAK_PW_HASH
 	  If unsure, say N.
 
 config CIFS_UPCALL
-	  bool "Kerberos/SPNEGO advanced session setup"
-	  depends on CIFS && KEYS
-	  help
-	    Enables an upcall mechanism for CIFS which accesses
-	    userspace helper utilities to provide SPNEGO packaged (RFC 4178)
-	    Kerberos tickets which are needed to mount to certain secure servers
-	    (for which more secure Kerberos authentication is required). If
-	    unsure, say N.
+	bool "Kerberos/SPNEGO advanced session setup"
+	depends on CIFS && KEYS
+	select DNS_RESOLVER
+	help
+	  Enables an upcall mechanism for CIFS which accesses userspace helper
+	  utilities to provide SPNEGO packaged (RFC 4178) Kerberos tickets
+	  which are needed to mount to certain secure servers (for which more
+	  secure Kerberos authentication is required). If unsure, say N.
 
 config CIFS_XATTR
         bool "CIFS extended attributes"
@@ -122,6 +122,7 @@ config CIFS_DEBUG2
 config CIFS_DFS_UPCALL
 	  bool "DFS feature support"
 	  depends on CIFS && KEYS
+	  select DNS_RESOLVER
 	  help
 	    Distributed File System (DFS) support is used to access shares
 	    transparently in an enterprise name space, even if the share
fs/cifs/Makefile +1 -1
index 9948c00..db98b59 100644
@@ -10,4 +10,4 @@ cifs-y := cifsfs.o cifssmb.o cifs_debug.o connect.o dir.o file.o inode.o \
 
 cifs-$(CONFIG_CIFS_UPCALL) += cifs_spnego.o
 
-cifs-$(CONFIG_CIFS_DFS_UPCALL) += dns_resolve.o cifs_dfs_ref.o
+cifs-$(CONFIG_CIFS_DFS_UPCALL) += cifs_dfs_ref.o
fs/cifs/cifs_dfs_ref.c +2 -2
index ac19a6f..89ff5e7 100644
@@ -18,10 +18,10 @@
 #include <linux/slab.h>
 #include <linux/vfs.h>
 #include <linux/fs.h>
+#include <linux/dns_resolver.h>
 #include "cifsglob.h"
 #include "cifsproto.h"
 #include "cifsfs.h"
-#include "dns_resolve.h"
 #include "cifs_debug.h"
 
 static LIST_HEAD(cifs_dfs_automount_list);
@@ -140,7 +140,7 @@ char *cifs_compose_mount_options(const char *sb_mountdata,
 		goto compose_mount_options_err;
 	}
 
-	rc = dns_resolve_server_name_to_ip(*devname, &srvIP);
+	rc = dns_resolve_unc_to_ip(*devname, &srvIP);
 	if (rc != 0) {
 		cERROR(1, "%s: Failed to resolve server part of %s to IP: %d",
 			  __func__, *devname, rc);
fs/cifs/cifsfs.c +1 -12
index 484e52b..ef9a773 100644
@@ -45,7 +45,6 @@
 #include "cifs_fs_sb.h"
 #include <linux/mm.h>
 #include <linux/key-type.h>
-#include "dns_resolve.h"
 #include "cifs_spnego.h"
 #define CIFS_MAGIC_NUMBER 0xFF534D42	/* the first four bytes of SMB PDUs */
 
@@ -922,22 +921,13 @@ init_cifs(void)
 	if (rc)
 		goto out_unregister_filesystem;
 #endif
-#ifdef CONFIG_CIFS_DFS_UPCALL
-	rc = register_key_type(&key_type_dns_resolver);
-	if (rc)
-		goto out_unregister_key_type;
-#endif
 	rc = slow_work_register_user(THIS_MODULE);
 	if (rc)
-		goto out_unregister_resolver_key;
+		goto out_unregister_key_type;
 
 	return 0;
 
- out_unregister_resolver_key:
-#ifdef CONFIG_CIFS_DFS_UPCALL
-	unregister_key_type(&key_type_dns_resolver);
  out_unregister_key_type:
-#endif
 #ifdef CONFIG_CIFS_UPCALL
 	unregister_key_type(&cifs_spnego_key_type);
  out_unregister_filesystem:
@@ -961,7 +951,6 @@ exit_cifs(void)
 	cifs_proc_clean();
 #ifdef CONFIG_CIFS_DFS_UPCALL
 	cifs_dfs_release_automount_timer();
-	unregister_key_type(&key_type_dns_resolver);
 #endif
 #ifdef CONFIG_CIFS_UPCALL
 	unregister_key_type(&cifs_spnego_key_type);
fs/cifs/dns_resolve.c deleted +0 -168

168 lines not on this page · raw message

fs/cifs/dns_resolve.h deleted +0 -32
deleted file mode 100644
index 966e928..0000000
@@ -1,32 +0,0 @@
-/*
- *   fs/cifs/dns_resolve.h -- DNS Resolver upcall management for CIFS DFS
- *                            Handles host name to IP address resolution
- *
- *   Copyright (c) International Business Machines  Corp., 2008
- *   Author(s): Steve French (sfrench-r/[email protected])
- *
- *   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 _DNS_RESOLVE_H
-#define _DNS_RESOLVE_H
-
-#ifdef __KERNEL__
-#include <linux/key-type.h>
-extern struct key_type key_type_dns_resolver;
-extern int dns_resolve_server_name_to_ip(const char *unc, char **ip_addr);
-#endif /* KERNEL */
-
-#endif /* _DNS_RESOLVE_H */
include/keys/dnsresolver-type.h new file +23 -0
new file mode 100644
index 0000000..9284a19
@@ -0,0 +1,23 @@
+/* DNS resolver key type
+ *
+ * Copyright (C) 2010 Wang Lei. All Rights Reserved.
+ * Written by Wang Lei ([email protected])
+ *
+ * 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.
+ */
+
+#ifndef _KEYS_DNS_RESOLVER_TYPE_H
+#define _KEYS_DNS_RESOLVER_TYPE_H
+
+#include <linux/key-type.h>
+
+extern struct key_type key_type_dns_resolver;
+
+extern int request_dns_resolver_key(const char *description,
+				    const char *callout_info,
+				    char **data);
+
+#endif /* _KEYS_DNS_RESOLVER_TYPE_H */
include/linux/dns_resolver.h new file +35 -0
new file mode 100644
index 0000000..72b9edf
@@ -0,0 +1,35 @@
+/*
+ *   DNS Resolver upcall management for CIFS DFS and AFS
+ *   Handles host name to IP address resolution and DNS query for AFSDB RR.
+ *
+ *   Copyright (c) International Business Machines  Corp., 2008
+ *   Author(s): Steve French (sfrench-r/[email protected])
+ *              Wang Lei ([email protected])
+ *
+ *   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 _DNS_RESOLVER_H
+#define _DNS_RESOLVER_H
+
+#ifdef __KERNEL__
+
+extern int request_dns_resolution(const char *description, const char *options,
+				  char **result);
+extern int dns_resolve_unc_to_ip(const char *unc, char **ip_addr);
+
+#endif /* KERNEL */
+
+#endif /* _DNS_RESOLVER_H */
net/Kconfig +1 -0
index 0d68b40..3c4eced 100644
@@ -203,6 +203,7 @@ source "net/phonet/Kconfig"
 source "net/ieee802154/Kconfig"
 source "net/sched/Kconfig"
 source "net/dcb/Kconfig"
+source "net/dnsresolver/Kconfig"
 
 config RPS
 	boolean
net/Makefile +1 -0
index cb7bdc1..3a7a17b 100644
@@ -67,3 +67,4 @@ ifeq ($(CONFIG_NET),y)
 obj-$(CONFIG_SYSCTL)		+= sysctl_net.o
 endif
 obj-$(CONFIG_WIMAX)		+= wimax/
+obj-$(CONFIG_DNS_RESOLVER)	+= dnsresolver/
net/dnsresolver/Kconfig new file +27 -0
new file mode 100644
index 0000000..09de536
@@ -0,0 +1,27 @@
+#
+# Configuration for DNS Resolver
+#
+config DNS_RESOLVER
+	tristate "DNS Resolver support"
+	depends on NET && KEYS
+	help
+	  Saying Y here will include support for the DNS Resolver key type
+	  which can be used to make upcalls to perform DNS lookups in
+	  userspace.
+
+	  DNS Resolver is used to query DNS server for information.  Examples
+	  being resolving a UNC hostname element to an IP address for CIFS or
+	  performing a DNS query for AFSDB records so that AFS can locate a
+	  cell's volume location database servers.
+
+	  DNS Resolver is used by the CIFS and AFS modules, and would support
+	  samba4 later.  DNS Resolver is supported by the userspace upcall
+	  helper "/sbin/dns.resolver" via /etc/request-key.conf.
+
+	  See <file:Documentation/networking/dnsresolver.txt> for further
+	  information.
+
+	  To compile this as a module, choose M here: the module will be called
+	  dnsresolver.
+
+	  If unsure, say N.
net/dnsresolver/Makefile new file +7 -0
new file mode 100644
index 0000000..260ccc4
@@ -0,0 +1,7 @@
+#
+# Makefile for the Linux DNS Resolver.
+#
+
+obj-$(CONFIG_DNS_RESOLVER) += dnsresolver.o
+
+dnsresolver-objs :=  dns_resolver.o resolv_unc_to_ip.o
net/dnsresolver/dns_resolver.c new file +293 -0

293 lines not on this page · raw message

net/dnsresolver/internal.h new file +39 -0
new file mode 100644
index 0000000..c094fc4
@@ -0,0 +1,39 @@
+/*
+ *   Copyright (c) 2010 Wang Lei
+ *   Author(s): Wang Lei ([email protected]). All Rights Reserved.
+ *
+ *   Internal DNS Rsolver stuff
+ *
+ *   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
+ */
+
+#include <linux/compiler.h>
+#include <linux/kernel.h>
+#include <linux/sched.h>
+
+/*
+ * debug tracing
+ */
+extern unsigned dnsresolver_debug;
+
+#define	kdebug(FMT, ...)				\
+do {							\
+	if (unlikely(dnsresolver_debug))		\
+		printk(KERN_DEBUG "[%-6.6s] "FMT"\n",	\
+		       current->comm, ##__VA_ARGS__);	\
+} while (0)
+
+#define kenter(FMT, ...) kdebug("==> %s("FMT")", __func__, ##__VA_ARGS__)
+#define kleave(FMT, ...) kdebug("<== %s()"FMT"", __func__, ##__VA_ARGS__)
net/dnsresolver/resolv_unc_to_ip.c new file +111 -0

111 lines not on this page · raw message

A message that is not in the archive (sent off-list or not yet received): 20100707091411.16573.81747.stgit-S6HVgzuS8uM4Awkfq6JHfwNdhmdF6hFW@public.gmane.org

#5 lore raw

Re: [PATCH 2/4] Implement a DNS Resolver Module

On Wed, 07 Jul 2010 10:14:11 +0100
David Howells <[email protected]> wrote:

-----------------[snip]----------------

Quoted 108 lines
net/dnsresolver/resolv_unc_to_ip.c new file +101 -0
new file mode 100644
index 0000000..169f9d9
@@ -0,0 +1,111 @@
+/*
+ *   Copyright (c) 2007 Igor Mammedov
+ *   Author(s): Igor Mammedov ([email protected])
+ *              Steve French (sfrench-r/[email protected])
+ *              Wang Lei ([email protected])
+ *
+ *   Routines used for Universal Naming Convention (UNC) path style hostname to
+ *   IP address translation.  For this function to work, the userspace tool
+ *   dns.upcall is needed and something like the following lines should be
+ *   added to the /etc/request-key.conf file:
+ *
+ *	create dns_resolver * * /sbin/dns.upcall %k
+ *
+ *   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
+ */
+
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/socket.h>
+#include <linux/in.h>
+#include <linux/inet.h>
+#include <linux/in6.h>
+#include <linux/string.h>
+#include <linux/dns_resolver.h>
+#include <keys/dnsresolver-type.h>
+#include "internal.h"
+
+/**
+ * dns_resolve_unc_to_ip - Resolve UNC server name to ip address.
+ * @unc: UNC path specifying the server
+ * @ip_addr: Where to return the IP address.
+ *
+ * The IP address will be returned in string form, and the caller is
+ * responsible for freeing it.
+ *
+ * Returns 0 on success, -ve on error.
+ */
+int
+dns_resolve_unc_to_ip(const char *unc, char **ip_addr)
+{
+	struct in_addr s4;
+	struct in6_addr	s6;
+	char *name, *sep;
+	int len, rc;
+
+	kenter("%s,", unc);
+
+	rc = -EINVAL;
+	if (!ip_addr || !unc)
+		goto out;
+
+	len = strlen(unc);
+	if (len < 3)
+		goto out;
+
+	/* discount leading slashes for cifs */
+	len -= 2;
+	unc += 2;
+
+	/* search for server name delimiter */
+	sep = memchr(unc, '\\', len);
+	if (sep)
+		len = sep - unc;
+	kdebug("server name:%*.*s", len, len, unc);
+
+	rc = -ENOMEM;
+	name = kmalloc(len + 1, GFP_KERNEL);
+	if (!name)
+		goto out;
+
+	memcpy(name, unc, len);
+	name[len] = 0;
+	kdebug("name to resolve '%s'", name);
+
+	/* Try to convert a string to an IPv4 address */
+	rc = in4_pton(name, len, (void *)&s4.s_addr, '\\', NULL);
+	if (rc > 0) {
+		*ip_addr = name;
+		kleave(" = 0 [UNC is IPv4]");
+		return 0;
+	}
+
+	/* Try to convert a string to an IPv6 address */
+	rc = in6_pton(name, len, (void *)&s6.s6_addr, '\\', NULL);
+	if (rc > 0) {
+		*ip_addr = name;
+		kleave(" = 0 [UNC is IPv6]");
+		return 0;
+	}

net/dnsresolver/resolv_unc_to_ip.c line 111

Quoted 10 lines
net/dnsresolver/resolv_unc_to_ip.c new file +10 -0
+
+	/* perform the upcall */
+	rc = request_dns_resolution(name, NULL, ip_addr);
+	kfree(name);
+
+out:
+	kleave(" = %d", rc);
+	return rc;
+}
+EXPORT_SYMBOL(dns_resolve_unc_to_ip);

I'm not sold that the above function really belongs in generic code. It seems like it might make more sense to have a cifs function that parses out the host portion of the UNC and then pass that to a generic function that does the in4_pton/in6_pton and then the upcall if that fails?

Nice work on the set. Moving the DNS upcall into generic code is definitely a good move.

Signature · 1 line
Jeff Layton <[email protected]>
#6 lore raw

Re: [PATCH 2/4] Implement a DNS Resolver Module

In reply to a message not in the archive
Quoted 2 lines · On Wed, 07 Jul 2010 10:14:11 +0100 David Howells…

From: Wang Lei <[email protected]>

----------------[snip]------------------

Quoted 53 lines
net/dnsresolver/resolv_unc_to_ip.c new file +53 -0
+/**
+ * dns_resolve_unc_to_ip - Resolve UNC server name to ip address.
+ * @unc: UNC path specifying the server
+ * @ip_addr: Where to return the IP address.
+ *
+ * The IP address will be returned in string form, and the caller is
+ * responsible for freeing it.
+ *
+ * Returns 0 on success, -ve on error.
+ */
+int
+dns_resolve_unc_to_ip(const char *unc, char **ip_addr)
+{
+	struct in_addr s4;
+	struct in6_addr	s6;
+	char *name, *sep;
+	int len, rc;
+
+	kenter("%s,", unc);
+
+	rc = -EINVAL;
+	if (!ip_addr || !unc)
+		goto out;
+
+	len = strlen(unc);
+	if (len < 3)
+		goto out;
+
+	/* discount leading slashes for cifs */
+	len -= 2;
+	unc += 2;
+
+	/* search for server name delimiter */
+	sep = memchr(unc, '\\', len);
+	if (sep)
+		len = sep - unc;
+	kdebug("server name:%*.*s", len, len, unc);
+
+	rc = -ENOMEM;
+	name = kmalloc(len + 1, GFP_KERNEL);
+	if (!name)
+		goto out;
+
+	memcpy(name, unc, len);
+	name[len] = 0;
+	kdebug("name to resolve '%s'", name);
+
+	/* Try to convert a string to an IPv4 address */
+	rc = in4_pton(name, len, (void *)&s4.s_addr, '\\', NULL);
+	if (rc > 0) {
+		*ip_addr = name;
+		kleave(" = 0 [UNC is IPv4]");
+		return 0;

net/dnsresolver/resolv_unc_to_ip.c line 102

Quoted 10 lines
net/dnsresolver/resolv_unc_to_ip.c new file +10 -0
+	}
+
+	/* Try to convert a string to an IPv6 address */
+	rc = in6_pton(name, len, (void *)&s6.s6_addr, '\\', NULL);
+	if (rc > 0) {
+		*ip_addr = name;
+		kleave(" = 0 [UNC is IPv6]");
+		return 0;
+	}
+

Another (somewhat minor) nit that Steve F pointed out. The function that this replaces in cifs can deal with numeric scopeid's as part of the address. For instance:

    fea1::1%2

...where the scopeid here is "2". For linux machines, the scopeid essentially equates to an interface index and really has no meaning outside of the machine.

It's not clear to me that we'd ever see one of those in a hostname that we want to parse here, but it might not hurt to plan for it and deal with it appropriately.

Quoted 15 lines
net/dnsresolver/resolv_unc_to_ip.c new file +9 -1
+	/* perform the upcall */
+	rc = request_dns_resolution(name, NULL, ip_addr);
+	kfree(name);
+
+out:
+	kleave(" = %d", rc);
+	return rc;
+}
+EXPORT_SYMBOL(dns_resolve_unc_to_ip);
 
--

To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 1 line
Jeff Layton <[email protected]>

A message that is not in the archive (sent off-list or not yet received): 20100720112506.7f7cfe1d-9yPaYZwiELC+kQycOl6kW4xkIHaj4LzF@public.gmane.org

#8 lore raw

Re: [PATCH 2/4] Implement a DNS Resolver Module

Quoted 27 lines · Jeff Layton
Quoted 2 lines · On Wed, 07 Jul 2010 10:14:11 +0100 David Howells…

From: Wang Lei <[email protected]>

----------------[snip]------------------

net/dnsresolver/resolv_unc_to_ip.c line 51

Quoted 12 lines
net/dnsresolver/resolv_unc_to_ip.c new file +12 -0
+/**
+ * dns_resolve_unc_to_ip - Resolve UNC server name to ip address.
+ * @unc: UNC path specifying the server
+ * @ip_addr: Where to return the IP address.
+ *
+ * The IP address will be returned in string form, and the caller is
+ * responsible for freeing it.
+ *
+ * Returns 0 on success, -ve on error.
+ */
+int
+dns_resolve_unc_to_ip(const char *unc, char **ip_addr)

Another (somewhat minor) nit that Steve F pointed out. The function that this replaces in cifs can deal with numeric scopeid's as part of the address. For instance:

   fea1::1%2

Quoted 7 lines

...where the scopeid here is "2". For linux machines, the scopeid essentially equates to an interface index and really has no meaning outside of the machine.

It's not clear to me that we'd ever see one of those in a hostname that we want to parse here, but it might not hurt to plan for it and deal with it appropriately.

I think it is plausible that the scopeid/interface matters here (I heard the
request before and it seems reasonable). For IPv6 in UNC names
Windows maps : to -- IIRC so scope ids look a little different, but I think they support them

Signature · 3 lines
Thanks,

Steve
maintainer
#9 lore raw

Re: [PATCH 2/4] Implement a DNS Resolver Module

In reply to Jeff Layton
Quoted 6 lines · Jeff Layton

Another (somewhat minor) nit that Steve F pointed out. The function that this replaces in cifs can deal with numeric scopeid's as part of the address. For instance:

    fea1::1%2
Quoted 7 lines

...where the scopeid here is "2". For linux machines, the scopeid essentially equates to an interface index and really has no meaning outside of the machine.

It's not clear to me that we'd ever see one of those in a hostname that we want to parse here, but it might not hurt to plan for it and deal with it appropriately.

Yeah. I'm just looking at fixing cifs_convert_address() to take a length and pass it down so that that can be used instead (and it can take a const pointer to the address to be parsed). It's a shame that simple_strtoul() and the like assume NUL-terminated strings.

See the attached patch to cifs_convert_address().

David
---

fs/cifs/netmisc.c +22 -19
index d35d528..a95d3fb 100644
@@ -139,17 +139,18 @@ static const struct smb_to_posix_error mapping_table_ERRHRD[] = {
  * Returns 0 on failure.
  */
 static int
-cifs_inet_pton(const int address_family, const char *cp, void *dst)
+cifs_inet_pton(const int address_family, const char *cp, int len, void *dst)
 {
 	int ret = 0;
 
 	/* calculate length by finding first slash or NULL */
 	if (address_family == AF_INET)
-		ret = in4_pton(cp, -1 /* len */, dst, '\\', NULL);
+		ret = in4_pton(cp, len, dst, '\\', NULL);
 	else if (address_family == AF_INET6)
-		ret = in6_pton(cp, -1 /* len */, dst , '\\', NULL);
+		ret = in6_pton(cp, len, dst , '\\', NULL);
 
-	cFYI(DBG2, "address conversion returned %d for %s", ret, cp);
+	cFYI(DBG2, "address conversion returned %d for %*.*s",
+	     ret, len, len, cp);
 	if (ret > 0)
 		ret = 1;
 	return ret;
@@ -164,37 +165,39 @@ cifs_inet_pton(const int address_family, const char *cp, void *dst)
  * Returns 0 on failure.
  */
 int
-cifs_convert_address(char *src, void *dst)
+cifs_convert_address(const char *src, int len, void *dst)
 {
-	int rc;
-	char *pct, *endp;
+	int rc, alen, slen;
+	const char *pct;
+	char *endp, scope_id[13];
 	struct sockaddr_in *s4 = (struct sockaddr_in *) dst;
 	struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) dst;
 
 	/* IPv4 address */
-	if (cifs_inet_pton(AF_INET, src, &s4->sin_addr.s_addr)) {
+	if (cifs_inet_pton(AF_INET, src, len, &s4->sin_addr.s_addr)) {
 		s4->sin_family = AF_INET;
 		return 1;
 	}
 
-	/* temporarily terminate string */
-	pct = strchr(src, '%');
-	if (pct)
-		*pct = '\0';
-
-	rc = cifs_inet_pton(AF_INET6, src, &s6->sin6_addr.s6_addr);
-
-	/* repair temp termination (if any) and make pct point to scopeid */
-	if (pct)
-		*pct++ = '%';
+	/* attempt to exclude the scope ID from the address part */
+	pct = memchr(src, '%', len);
+	alen = pct ? pct - src : len;
 
+	rc = cifs_inet_pton(AF_INET6, src, alen, &s6->sin6_addr.s6_addr);
 	if (!rc)
 		return rc;
 
 	s6->sin6_family = AF_INET6;
 	if (pct) {
+		/* grab the scope ID */
+		slen = len - (alen + 1);
+		if (slen <= 0 || slen > 12)
+			return 0;
+		memcpy(scope_id, pct + 1, slen);
+		scope_id[slen] = '\0';
+
 		s6->sin6_scope_id = (u32) simple_strtoul(pct, &endp, 0);
-		if (!*pct || *endp)
+		if (endp != scope_id + slen)
 			return 0;
 	}
 
#10 lore raw

PATCH 3/4 Provide generic DNS query function

In reply to a message not in the archive

From: Wang Lei <[email protected]>

Provide a way of making generic DNS queries. Note that the upcall service program may process the result before returning it.

For example, if an AFSDB query is requested to find a list of Volume Location servers for a cell, that would only get a list of server names. That, however, is not what the kernel needs, so the upcall program makes secondary lookups to turn those into IP addresses and then instantiates the key with a string consisting of a comma-separated list of IP addresses.

---

 include/linux/dns_resolver.h |    2 +
 net/dnsresolver/Makefile     |    2 -
 net/dnsresolver/dns_query.c  |  130 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 133 insertions(+), 1 deletions(-)
 create mode 100644 net/dnsresolver/dns_query.c
include/linux/dns_resolver.h +2 -0
index 72b9edf..328cc05 100644
@@ -29,6 +29,8 @@
 extern int request_dns_resolution(const char *description, const char *options,
 				  char **result);
 extern int dns_resolve_unc_to_ip(const char *unc, char **ip_addr);
+extern int dns_query(const char *type, const char *name, const char *inetf,
+		     char **_result, time_t *_expiry);
 
 #endif /* KERNEL */
 
net/dnsresolver/Makefile +1 -1
index 260ccc4..aa7e422 100644
@@ -4,4 +4,4 @@
 
 obj-$(CONFIG_DNS_RESOLVER) += dnsresolver.o
 
-dnsresolver-objs :=  dns_resolver.o resolv_unc_to_ip.o
+dnsresolver-objs :=  dns_resolver.o resolv_unc_to_ip.o dns_query.o
net/dnsresolver/dns_query.c new file +130 -0

130 lines not on this page · raw message

#11 lore raw

PATCH 4/4 Add DNS support for AFS

In reply to a message not in the archive

From: Wang Lei <[email protected]>

Add DNS query support for AFS so that it can get the IP addresses of Volume Location servers from the DNS using an AFSDB record.

This requires userspace support. /etc/request-key.conf must be configured to invoke a helper for dns_resolver type keys with a subtype of "afsdb:" in the description.

---

 fs/afs/Kconfig |    1 +
 fs/afs/cell.c  |   40 ++++++++++++++++++++++++++++++----------
 2 files changed, 31 insertions(+), 10 deletions(-)
fs/afs/Kconfig +1 -0
index 5c4e61d..8f975f2 100644
@@ -2,6 +2,7 @@ config AFS_FS
 	tristate "Andrew File System support (AFS) (EXPERIMENTAL)"
 	depends on INET && EXPERIMENTAL
 	select AF_RXRPC
+	select DNS_RESOLVER
 	help
 	  If you say Y here, you will get an experimental Andrew File System
 	  driver. It currently only supports unsecured read-only AFS access.
fs/afs/cell.c +30 -10
index e19c13f..9eeea5f 100644
@@ -13,6 +13,7 @@
 #include <linux/slab.h>
 #include <linux/key.h>
 #include <linux/ctype.h>
+#include <linux/dns_resolver.h>
 #include <linux/sched.h>
 #include <keys/rxrpc-type.h>
 #include "internal.h"
@@ -36,6 +37,8 @@ static struct afs_cell *afs_cell_alloc(const char *name, char *vllist)
 	struct key *key;
 	size_t namelen;
 	char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp, *next;
+	char  *dvllist = NULL, *_vllist = NULL;
+	char  delimiter = ':';
 	int ret;
 
 	_enter("%s,%s", name, vllist);
@@ -43,8 +46,10 @@ static struct afs_cell *afs_cell_alloc(const char *name, char *vllist)
 	BUG_ON(!name); /* TODO: want to look up "this cell" in the cache */
 
 	namelen = strlen(name);
-	if (namelen > AFS_MAXCELLNAME)
+	if (namelen > AFS_MAXCELLNAME) {
+		_leave(" = -ENAMETOOLONG");
 		return ERR_PTR(-ENAMETOOLONG);
+	}
 
 	/* allocate and initialise a cell record */
 	cell = kzalloc(sizeof(struct afs_cell) + namelen + 1, GFP_KERNEL);
@@ -64,15 +69,31 @@ static struct afs_cell *afs_cell_alloc(const char *name, char *vllist)
 	INIT_LIST_HEAD(&cell->vl_list);
 	spin_lock_init(&cell->vl_lock);
 
+	/* if the ip address is invalid, try dns query */
+	if (!vllist || strlen(vllist) < 7) {
+		ret = dns_query("afsdb", name, "ipv4", &dvllist, NULL);
+		if (ret < 0) {
+			_leave(" = %d", ret);
+			return ERR_PTR(ret);
+		}
+		_vllist = dvllist;
+
+		/* change the delimiter for user-space reply */
+		delimiter = ',';
+
+	} else {
+		_vllist = vllist;
+	}
+
 	/* fill in the VL server list from the rest of the string */
 	do {
 		unsigned a, b, c, d;
 
-		next = strchr(vllist, ':');
+		next = strchr(_vllist, delimiter);
 		if (next)
 			*next++ = 0;
 
-		if (sscanf(vllist, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
+		if (sscanf(_vllist, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
 			goto bad_address;
 
 		if (a > 255 || b > 255 || c > 255 || d > 255)
@@ -81,7 +102,7 @@ static struct afs_cell *afs_cell_alloc(const char *name, char *vllist)
 		cell->vl_addrs[cell->vl_naddrs++].s_addr =
 			htonl((a << 24) | (b << 16) | (c << 8) | d);
 
-	} while (cell->vl_naddrs < AFS_CELL_MAX_ADDRS && (vllist = next));
+	} while (cell->vl_naddrs < AFS_CELL_MAX_ADDRS && (_vllist = next));
 
 	/* create a key to represent an anonymous user */
 	memcpy(keyname, "afs@", 4);
@@ -110,6 +131,7 @@ bad_address:
 	ret = -EINVAL;
 error:
 	key_put(cell->anonymous_key);
+	kfree(dvllist);
 	kfree(cell);
 	_leave(" = %d", ret);
 	return ERR_PTR(ret);
@@ -201,14 +223,12 @@ int afs_cell_init(char *rootcell)
 	}
 
 	cp = strchr(rootcell, ':');
-	if (!cp) {
-		printk(KERN_ERR "kAFS: no VL server IP addresses specified\n");
-		_leave(" = -EINVAL");
-		return -EINVAL;
-	}
+	if (!cp)
+		_debug("kAFS: no VL server IP addresses specified");
+	else
+		*cp++ = 0;
 
 	/* allocate a cell record for the root cell */
-	*cp++ = 0;
 	new_root = afs_cell_create(rootcell, cp);
 	if (IS_ERR(new_root)) {
 		_leave(" = %ld", PTR_ERR(new_root));
#12 lore raw
In reply to a message not in the archive

ok - sounds fine at first glance to me, but I will look. thx (this will help smb2 too - to have dns code separated out)

Quoted 40 lines · David Howells

Hi Steve,

The associated patches extract the dns_resolver key type from the CIFS filesystem and make it useful by AFS too.  Can you check at least the second patch which actually modifies CIFS?

Thanks,
David
---

David Howells (1):
     KEYS: Authorise keyctl_set_timeout() on a key if we have its authorisation key

Wang Lei (3):
     Add DNS support for AFS
     Provide generic DNS query function
     Implement a DNS Resolver Module

 Documentation/networking/dnsresolver.txt |  150 +++++++++++++++ fs/afs/Kconfig                           |    1
 fs/afs/cell.c                            |   40 +++- fs/cifs/Kconfig                          |   17 +- fs/cifs/Makefile                         |    2 fs/cifs/cifs_dfs_ref.c                   |    4
 fs/cifs/cifsfs.c                         |   13 - fs/cifs/dns_resolve.c                    |  168 ----------------- fs/cifs/dns_resolve.h                    |   32 --- include/keys/dnsresolver-type.h          |   23 ++ include/linux/dns_resolver.h             |   37 ++++
 net/Kconfig                              |    1
 net/Makefile                             |    1 net/dnsresolver/Kconfig                  |   27 +++ net/dnsresolver/Makefile                 |    7 + net/dnsresolver/dns_query.c              |  130 +++++++++++++ net/dnsresolver/dns_resolver.c           |  293 ++++++++++++++++++++++++++++++ net/dnsresolver/internal.h               |   39 ++++ net/dnsresolver/resolv_unc_to_ip.c       |  111 +++++++++++ security/keys/keyctl.c                   |   17 ++

 20 files changed, 879 insertions(+), 234 deletions(-)
Quoted 13 lines

 create mode 100644 Documentation/networking/dnsresolver.txt delete mode 100644 fs/cifs/dns_resolve.c
 delete mode 100644 fs/cifs/dns_resolve.h
 create mode 100644 include/keys/dnsresolver-type.h create mode 100644 include/linux/dns_resolver.h create mode 100644 net/dnsresolver/Kconfig
 create mode 100644 net/dnsresolver/Makefile create mode 100644 net/dnsresolver/dns_query.c create mode 100644 net/dnsresolver/dns_resolver.c create mode 100644 net/dnsresolver/internal.h create mode 100644 net/dnsresolver/resolv_unc_to_ip.c

--
Thanks,

Steve