diff options
author | Wang Lei <wang840925@gmail.com> | 2010-08-04 10:16:33 -0400 |
---|---|---|
committer | Steve French <sfrench@us.ibm.com> | 2010-08-05 13:17:51 -0400 |
commit | 1a4240f4764ac78adbf4b0ebb49b3bd8c72ffa11 (patch) | |
tree | 7d9de5b071e7ab8a8355bdf7902db4c0a0e812b1 /net/dns_resolver/dns_key.c | |
parent | ba5dadbf4e7b531bd7ccecffb4d3935c80a3372e (diff) |
DNS: Separate out CIFS DNS Resolver code
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 facility makes it possible for the kernel to upcall to userspace to have
it issue DNS requests, 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
part remains in the CIFS module for now.
See the added Documentation/networking/dns_resolver.txt for more information.
Signed-off-by: Wang Lei <wang840925@gmail.com>
Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: Jeff Layton <jlayton@redhat.com>
Signed-off-by: Steve French <sfrench@us.ibm.com>
Diffstat (limited to 'net/dns_resolver/dns_key.c')
-rw-r--r-- | net/dns_resolver/dns_key.c | 210 |
1 files changed, 210 insertions, 0 deletions
diff --git a/net/dns_resolver/dns_key.c b/net/dns_resolver/dns_key.c new file mode 100644 index 000000000000..1b1b411adcf1 --- /dev/null +++ b/net/dns_resolver/dns_key.c | |||
@@ -0,0 +1,210 @@ | |||
1 | /* Key type used to cache DNS lookups made by the kernel | ||
2 | * | ||
3 | * See Documentation/networking/dns_resolver.txt | ||
4 | * | ||
5 | * Copyright (c) 2007 Igor Mammedov | ||
6 | * Author(s): Igor Mammedov (niallain@gmail.com) | ||
7 | * Steve French (sfrench@us.ibm.com) | ||
8 | * Wang Lei (wang840925@gmail.com) | ||
9 | * David Howells (dhowells@redhat.com) | ||
10 | * | ||
11 | * This library is free software; you can redistribute it and/or modify | ||
12 | * it under the terms of the GNU Lesser General Public License as published | ||
13 | * by the Free Software Foundation; either version 2.1 of the License, or | ||
14 | * (at your option) any later version. | ||
15 | * | ||
16 | * This library is distributed in the hope that it will be useful, | ||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See | ||
19 | * the GNU Lesser General Public License for more details. | ||
20 | * | ||
21 | * You should have received a copy of the GNU Lesser General Public License | ||
22 | * along with this library; if not, write to the Free Software | ||
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
24 | */ | ||
25 | #include <linux/module.h> | ||
26 | #include <linux/moduleparam.h> | ||
27 | #include <linux/slab.h> | ||
28 | #include <linux/string.h> | ||
29 | #include <linux/kernel.h> | ||
30 | #include <linux/keyctl.h> | ||
31 | #include <keys/dns_resolver-type.h> | ||
32 | #include <keys/user-type.h> | ||
33 | #include "internal.h" | ||
34 | |||
35 | MODULE_DESCRIPTION("DNS Resolver"); | ||
36 | MODULE_AUTHOR("Wang Lei"); | ||
37 | MODULE_LICENSE("GPL"); | ||
38 | |||
39 | unsigned dns_resolver_debug; | ||
40 | module_param_named(debug, dns_resolver_debug, uint, S_IWUSR | S_IRUGO); | ||
41 | MODULE_PARM_DESC(debug, "DNS Resolver debugging mask"); | ||
42 | |||
43 | const struct cred *dns_resolver_cache; | ||
44 | |||
45 | /* | ||
46 | * Instantiate a user defined key for dns_resolver. | ||
47 | * | ||
48 | * The data must be a NUL-terminated string, with the NUL char accounted in | ||
49 | * datalen. | ||
50 | * | ||
51 | * If the data contains a '#' characters, then we take the clause after each | ||
52 | * one to be an option of the form 'key=value'. The actual data of interest is | ||
53 | * the string leading up to the first '#'. For instance: | ||
54 | * | ||
55 | * "ip1,ip2,...#foo=bar" | ||
56 | */ | ||
57 | static int | ||
58 | dns_resolver_instantiate(struct key *key, const void *_data, size_t datalen) | ||
59 | { | ||
60 | struct user_key_payload *upayload; | ||
61 | int ret; | ||
62 | size_t result_len = 0; | ||
63 | const char *data = _data, *opt; | ||
64 | |||
65 | kenter("%%%d,%s,'%s',%zu", | ||
66 | key->serial, key->description, data, datalen); | ||
67 | |||
68 | if (datalen <= 1 || !data || data[datalen - 1] != '\0') | ||
69 | return -EINVAL; | ||
70 | datalen--; | ||
71 | |||
72 | /* deal with any options embedded in the data */ | ||
73 | opt = memchr(data, '#', datalen); | ||
74 | if (!opt) { | ||
75 | kdebug("no options currently supported"); | ||
76 | return -EINVAL; | ||
77 | } | ||
78 | |||
79 | result_len = datalen; | ||
80 | ret = key_payload_reserve(key, result_len); | ||
81 | if (ret < 0) | ||
82 | return -EINVAL; | ||
83 | |||
84 | upayload = kmalloc(sizeof(*upayload) + result_len + 1, GFP_KERNEL); | ||
85 | if (!upayload) { | ||
86 | kleave(" = -ENOMEM"); | ||
87 | return -ENOMEM; | ||
88 | } | ||
89 | |||
90 | upayload->datalen = result_len; | ||
91 | memcpy(upayload->data, data, result_len); | ||
92 | upayload->data[result_len] = '\0'; | ||
93 | rcu_assign_pointer(key->payload.data, upayload); | ||
94 | |||
95 | kleave(" = 0"); | ||
96 | return 0; | ||
97 | } | ||
98 | |||
99 | /* | ||
100 | * The description is of the form "[<type>:]<domain_name>" | ||
101 | * | ||
102 | * The domain name may be a simple name or an absolute domain name (which | ||
103 | * should end with a period). The domain name is case-independent. | ||
104 | */ | ||
105 | static int | ||
106 | dns_resolver_match(const struct key *key, const void *description) | ||
107 | { | ||
108 | int slen, dlen, ret = 0; | ||
109 | const char *src = key->description, *dsp = description; | ||
110 | |||
111 | kenter("%s,%s", src, dsp); | ||
112 | |||
113 | if (!src || !dsp) | ||
114 | goto no_match; | ||
115 | |||
116 | if (strcasecmp(src, dsp) == 0) | ||
117 | goto matched; | ||
118 | |||
119 | slen = strlen(src); | ||
120 | dlen = strlen(dsp); | ||
121 | if (slen <= 0 || dlen <= 0) | ||
122 | goto no_match; | ||
123 | if (src[slen - 1] == '.') | ||
124 | slen--; | ||
125 | if (dsp[dlen - 1] == '.') | ||
126 | dlen--; | ||
127 | if (slen != dlen || strncasecmp(src, dsp, slen) != 0) | ||
128 | goto no_match; | ||
129 | |||
130 | matched: | ||
131 | ret = 1; | ||
132 | no_match: | ||
133 | kleave(" = %d", ret); | ||
134 | return ret; | ||
135 | } | ||
136 | |||
137 | struct key_type key_type_dns_resolver = { | ||
138 | .name = "dns_resolver", | ||
139 | .instantiate = dns_resolver_instantiate, | ||
140 | .match = dns_resolver_match, | ||
141 | .revoke = user_revoke, | ||
142 | .destroy = user_destroy, | ||
143 | .describe = user_describe, | ||
144 | .read = user_read, | ||
145 | }; | ||
146 | |||
147 | static int __init init_dns_resolver(void) | ||
148 | { | ||
149 | struct cred *cred; | ||
150 | struct key *keyring; | ||
151 | int ret; | ||
152 | |||
153 | printk(KERN_NOTICE "Registering the %s key type\n", | ||
154 | key_type_dns_resolver.name); | ||
155 | |||
156 | /* create an override credential set with a special thread keyring in | ||
157 | * which DNS requests are cached | ||
158 | * | ||
159 | * this is used to prevent malicious redirections from being installed | ||
160 | * with add_key(). | ||
161 | */ | ||
162 | cred = prepare_kernel_cred(NULL); | ||
163 | if (!cred) | ||
164 | return -ENOMEM; | ||
165 | |||
166 | keyring = key_alloc(&key_type_keyring, ".dns_resolver", 0, 0, cred, | ||
167 | (KEY_POS_ALL & ~KEY_POS_SETATTR) | | ||
168 | KEY_USR_VIEW | KEY_USR_READ, | ||
169 | KEY_ALLOC_NOT_IN_QUOTA); | ||
170 | if (IS_ERR(keyring)) { | ||
171 | ret = PTR_ERR(keyring); | ||
172 | goto failed_put_cred; | ||
173 | } | ||
174 | |||
175 | ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL); | ||
176 | if (ret < 0) | ||
177 | goto failed_put_key; | ||
178 | |||
179 | ret = register_key_type(&key_type_dns_resolver); | ||
180 | if (ret < 0) | ||
181 | goto failed_put_key; | ||
182 | |||
183 | /* instruct request_key() to use this special keyring as a cache for | ||
184 | * the results it looks up */ | ||
185 | cred->thread_keyring = keyring; | ||
186 | cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING; | ||
187 | dns_resolver_cache = cred; | ||
188 | |||
189 | kdebug("DNS resolver keyring: %d\n", key_serial(keyring)); | ||
190 | return 0; | ||
191 | |||
192 | failed_put_key: | ||
193 | key_put(keyring); | ||
194 | failed_put_cred: | ||
195 | put_cred(cred); | ||
196 | return ret; | ||
197 | } | ||
198 | |||
199 | static void __exit exit_dns_resolver(void) | ||
200 | { | ||
201 | key_revoke(dns_resolver_cache->thread_keyring); | ||
202 | unregister_key_type(&key_type_dns_resolver); | ||
203 | put_cred(dns_resolver_cache); | ||
204 | printk(KERN_NOTICE "Unregistered %s key type\n", | ||
205 | key_type_dns_resolver.name); | ||
206 | } | ||
207 | |||
208 | module_init(init_dns_resolver) | ||
209 | module_exit(exit_dns_resolver) | ||
210 | MODULE_LICENSE("GPL"); | ||