blob: 49315cbf742d7ab22876b69349b6441723d850b7 [file] [log] [blame]
Steve French197c1832008-01-10 17:10:23 +00001/*
2 * fs/cifs/dns_resolve.c
3 *
4 * Copyright (c) 2007 Igor Mammedov
5 * Author(s): Igor Mammedov (niallain@gmail.com)
6 * Steve French (sfrench@us.ibm.com)
7 *
8 * Contains the CIFS DFS upcall routines used for hostname to
9 * IP address translation.
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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
David Howells4c0c03c2010-07-22 12:53:18 +010027#include <linux/keyctl.h>
28#include <linux/key-type.h>
Steve French197c1832008-01-10 17:10:23 +000029#include <keys/user-type.h>
30#include "dns_resolve.h"
31#include "cifsglob.h"
32#include "cifsproto.h"
33#include "cifs_debug.h"
34
David Howells4c0c03c2010-07-22 12:53:18 +010035static const struct cred *dns_resolver_cache;
36
Steve Frenchd09e8602008-04-26 00:22:23 +000037/* Checks if supplied name is IP address
38 * returns:
39 * 1 - name is IP
40 * 0 - name is not IP
41 */
Steve French9d815232008-09-23 18:46:07 +000042static int
Jeff Layton681bf722009-06-11 10:27:31 -040043is_ip(char *name)
Steve Frenchd09e8602008-04-26 00:22:23 +000044{
Jeff Layton1e68b2b2009-06-11 10:27:30 -040045 struct sockaddr_storage ss;
Steve Frenchd09e8602008-04-26 00:22:23 +000046
Jeff Layton1e68b2b2009-06-11 10:27:30 -040047 return cifs_convert_address(name, &ss);
Steve Frenchd09e8602008-04-26 00:22:23 +000048}
Steve French197c1832008-01-10 17:10:23 +000049
Steve French9d815232008-09-23 18:46:07 +000050static int
51dns_resolver_instantiate(struct key *key, const void *data,
52 size_t datalen)
53{
54 int rc = 0;
55 char *ip;
56
57 ip = kmalloc(datalen + 1, GFP_KERNEL);
58 if (!ip)
59 return -ENOMEM;
60
61 memcpy(ip, data, datalen);
62 ip[datalen] = '\0';
63
64 /* make sure this looks like an address */
Jeff Layton681bf722009-06-11 10:27:31 -040065 if (!is_ip(ip)) {
Steve French9d815232008-09-23 18:46:07 +000066 kfree(ip);
67 return -EINVAL;
68 }
69
70 key->type_data.x[0] = datalen;
Jeff Laytond9fb5c02009-03-23 01:47:11 -040071 key->payload.data = ip;
Steve French9d815232008-09-23 18:46:07 +000072
73 return rc;
74}
75
76static void
77dns_resolver_destroy(struct key *key)
78{
79 kfree(key->payload.data);
80}
81
82struct key_type key_type_dns_resolver = {
83 .name = "dns_resolver",
84 .def_datalen = sizeof(struct in_addr),
85 .describe = user_describe,
86 .instantiate = dns_resolver_instantiate,
87 .destroy = dns_resolver_destroy,
88 .match = user_match,
89};
90
Steve French197c1832008-01-10 17:10:23 +000091/* Resolves server name to ip address.
92 * input:
93 * unc - server UNC
94 * output:
95 * *ip_addr - pointer to server ip, caller responcible for freeing it.
96 * return 0 on success
97 */
98int
Steve French366781c2008-01-25 10:12:41 +000099dns_resolve_server_name_to_ip(const char *unc, char **ip_addr)
100{
David Howells4c0c03c2010-07-22 12:53:18 +0100101 const struct cred *saved_cred;
Steve French197c1832008-01-10 17:10:23 +0000102 int rc = -EAGAIN;
Steve Frenchd09e8602008-04-26 00:22:23 +0000103 struct key *rkey = ERR_PTR(-EAGAIN);
Steve French197c1832008-01-10 17:10:23 +0000104 char *name;
Steve Frenchd09e8602008-04-26 00:22:23 +0000105 char *data = NULL;
Steve French197c1832008-01-10 17:10:23 +0000106 int len;
107
Steve French366781c2008-01-25 10:12:41 +0000108 if (!ip_addr || !unc)
Steve French197c1832008-01-10 17:10:23 +0000109 return -EINVAL;
110
111 /* search for server name delimiter */
112 len = strlen(unc);
113 if (len < 3) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000114 cFYI(1, "%s: unc is too short: %s", __func__, unc);
Steve French197c1832008-01-10 17:10:23 +0000115 return -EINVAL;
116 }
117 len -= 2;
118 name = memchr(unc+2, '\\', len);
119 if (!name) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000120 cFYI(1, "%s: probably server name is whole unc: %s",
121 __func__, unc);
Steve French197c1832008-01-10 17:10:23 +0000122 } else {
123 len = (name - unc) - 2/* leading // */;
124 }
125
126 name = kmalloc(len+1, GFP_KERNEL);
127 if (!name) {
128 rc = -ENOMEM;
129 return rc;
130 }
131 memcpy(name, unc+2, len);
132 name[len] = 0;
133
Steve Frenchd09e8602008-04-26 00:22:23 +0000134 if (is_ip(name)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000135 cFYI(1, "%s: it is IP, skipping dns upcall: %s",
136 __func__, name);
Steve Frenchd09e8602008-04-26 00:22:23 +0000137 data = name;
138 goto skip_upcall;
139 }
140
David Howells4c0c03c2010-07-22 12:53:18 +0100141 saved_cred = override_creds(dns_resolver_cache);
Steve French197c1832008-01-10 17:10:23 +0000142 rkey = request_key(&key_type_dns_resolver, name, "");
David Howells4c0c03c2010-07-22 12:53:18 +0100143 revert_creds(saved_cred);
Steve French197c1832008-01-10 17:10:23 +0000144 if (!IS_ERR(rkey)) {
David Howells4c0c03c2010-07-22 12:53:18 +0100145 if (!(rkey->perm & KEY_USR_VIEW)) {
146 down_read(&rkey->sem);
147 rkey->perm |= KEY_USR_VIEW;
148 up_read(&rkey->sem);
149 }
Steve French9d815232008-09-23 18:46:07 +0000150 len = rkey->type_data.x[0];
Steve Frenchd09e8602008-04-26 00:22:23 +0000151 data = rkey->payload.data;
Steve Frenchd09e8602008-04-26 00:22:23 +0000152 } else {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000153 cERROR(1, "%s: unable to resolve: %s", __func__, name);
Steve Frenchd09e8602008-04-26 00:22:23 +0000154 goto out;
155 }
156
157skip_upcall:
158 if (data) {
Steve French9d815232008-09-23 18:46:07 +0000159 *ip_addr = kmalloc(len + 1, GFP_KERNEL);
Steve Frenchd09e8602008-04-26 00:22:23 +0000160 if (*ip_addr) {
Steve French9d815232008-09-23 18:46:07 +0000161 memcpy(*ip_addr, data, len + 1);
Igor Mammedov5651ced2008-05-20 13:02:01 +0400162 if (!IS_ERR(rkey))
Joe Perchesb6b38f72010-04-21 03:50:45 +0000163 cFYI(1, "%s: resolved: %s to %s", __func__,
Igor Mammedov5651ced2008-05-20 13:02:01 +0400164 name,
165 *ip_addr
Joe Perchesb6b38f72010-04-21 03:50:45 +0000166 );
Steve French197c1832008-01-10 17:10:23 +0000167 rc = 0;
168 } else {
169 rc = -ENOMEM;
170 }
Steve Frenchd09e8602008-04-26 00:22:23 +0000171 if (!IS_ERR(rkey))
172 key_put(rkey);
Steve French197c1832008-01-10 17:10:23 +0000173 }
174
Steve Frenchd09e8602008-04-26 00:22:23 +0000175out:
Steve French197c1832008-01-10 17:10:23 +0000176 kfree(name);
177 return rc;
178}
179
David Howells4c0c03c2010-07-22 12:53:18 +0100180int __init cifs_init_dns_resolver(void)
181{
182 struct cred *cred;
183 struct key *keyring;
184 int ret;
Steve French197c1832008-01-10 17:10:23 +0000185
David Howells4c0c03c2010-07-22 12:53:18 +0100186 printk(KERN_NOTICE "Registering the %s key type\n",
187 key_type_dns_resolver.name);
188
189 /* create an override credential set with a special thread keyring in
190 * which DNS requests are cached
191 *
192 * this is used to prevent malicious redirections from being installed
193 * with add_key().
194 */
195 cred = prepare_kernel_cred(NULL);
196 if (!cred)
197 return -ENOMEM;
198
199 keyring = key_alloc(&key_type_keyring, ".dns_resolver", 0, 0, cred,
200 (KEY_POS_ALL & ~KEY_POS_SETATTR) |
201 KEY_USR_VIEW | KEY_USR_READ,
202 KEY_ALLOC_NOT_IN_QUOTA);
203 if (IS_ERR(keyring)) {
204 ret = PTR_ERR(keyring);
205 goto failed_put_cred;
206 }
207
208 ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
209 if (ret < 0)
210 goto failed_put_key;
211
212 ret = register_key_type(&key_type_dns_resolver);
213 if (ret < 0)
214 goto failed_put_key;
215
216 /* instruct request_key() to use this special keyring as a cache for
217 * the results it looks up */
218 cred->thread_keyring = keyring;
219 cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
220 dns_resolver_cache = cred;
221 return 0;
222
223failed_put_key:
224 key_put(keyring);
225failed_put_cred:
226 put_cred(cred);
227 return ret;
228}
229
230void __exit cifs_exit_dns_resolver(void)
231{
232 key_revoke(dns_resolver_cache->thread_keyring);
233 unregister_key_type(&key_type_dns_resolver);
234 put_cred(dns_resolver_cache);
235 printk(KERN_NOTICE "Unregistered %s key type\n",
236 key_type_dns_resolver.name);
237}