2 * fs/cifs/dns_resolve.c
4 * Copyright (c) 2007 Igor Mammedov
5 * Author(s): Igor Mammedov (niallain@gmail.com)
6 * Steve French (sfrench@us.ibm.com)
8 * Contains the CIFS DFS upcall routines used for hostname to
9 * IP address translation.
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.
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.
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
26 #include <linux/slab.h>
27 #include <linux/keyctl.h>
28 #include <linux/key-type.h>
29 #include <keys/user-type.h>
30 #include "dns_resolve.h"
32 #include "cifsproto.h"
33 #include "cifs_debug.h"
35 static const struct cred
*dns_resolver_cache
;
37 /* Checks if supplied name is IP address
45 struct sockaddr_storage ss
;
47 return cifs_convert_address(name
, &ss
);
51 dns_resolver_instantiate(struct key
*key
, const void *data
,
57 ip
= kmalloc(datalen
+ 1, GFP_KERNEL
);
61 memcpy(ip
, data
, datalen
);
64 /* make sure this looks like an address */
70 key
->type_data
.x
[0] = datalen
;
71 key
->payload
.data
= ip
;
77 dns_resolver_destroy(struct key
*key
)
79 kfree(key
->payload
.data
);
82 struct 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
,
91 /* Resolves server name to ip address.
95 * *ip_addr - pointer to server ip, caller responcible for freeing it.
99 dns_resolve_server_name_to_ip(const char *unc
, char **ip_addr
)
101 const struct cred
*saved_cred
;
103 struct key
*rkey
= ERR_PTR(-EAGAIN
);
108 if (!ip_addr
|| !unc
)
111 /* search for server name delimiter */
114 cFYI(1, "%s: unc is too short: %s", __func__
, unc
);
118 name
= memchr(unc
+2, '\\', len
);
120 cFYI(1, "%s: probably server name is whole unc: %s",
123 len
= (name
- unc
) - 2/* leading // */;
126 name
= kmalloc(len
+1, GFP_KERNEL
);
131 memcpy(name
, unc
+2, len
);
135 cFYI(1, "%s: it is IP, skipping dns upcall: %s",
141 saved_cred
= override_creds(dns_resolver_cache
);
142 rkey
= request_key(&key_type_dns_resolver
, name
, "");
143 revert_creds(saved_cred
);
145 if (!(rkey
->perm
& KEY_USR_VIEW
)) {
146 down_read(&rkey
->sem
);
147 rkey
->perm
|= KEY_USR_VIEW
;
150 len
= rkey
->type_data
.x
[0];
151 data
= rkey
->payload
.data
;
153 cERROR(1, "%s: unable to resolve: %s", __func__
, name
);
159 *ip_addr
= kmalloc(len
+ 1, GFP_KERNEL
);
161 memcpy(*ip_addr
, data
, len
+ 1);
163 cFYI(1, "%s: resolved: %s to %s", __func__
,
180 int __init
cifs_init_dns_resolver(void)
186 printk(KERN_NOTICE
"Registering the %s key type\n",
187 key_type_dns_resolver
.name
);
189 /* create an override credential set with a special thread keyring in
190 * which DNS requests are cached
192 * this is used to prevent malicious redirections from being installed
195 cred
= prepare_kernel_cred(NULL
);
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
;
208 ret
= key_instantiate_and_link(keyring
, NULL
, 0, NULL
, NULL
);
212 ret
= register_key_type(&key_type_dns_resolver
);
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
;
230 void cifs_exit_dns_resolver(void)
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
);