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/keyctl.h>
27 #include <linux/key-type.h>
28 #include <keys/user-type.h>
29 #include "dns_resolve.h"
31 #include "cifsproto.h"
32 #include "cifs_debug.h"
34 static const struct cred
*dns_resolver_cache
;
36 /* Checks if supplied name is IP address
44 struct sockaddr_storage ss
;
46 return cifs_convert_address(name
, &ss
);
50 dns_resolver_instantiate(struct key
*key
, const void *data
,
56 ip
= kmalloc(datalen
+ 1, GFP_KERNEL
);
60 memcpy(ip
, data
, datalen
);
63 /* make sure this looks like an address */
69 key
->type_data
.x
[0] = datalen
;
70 key
->payload
.data
= ip
;
76 dns_resolver_destroy(struct key
*key
)
78 kfree(key
->payload
.data
);
81 struct key_type key_type_dns_resolver
= {
82 .name
= "dns_resolver",
83 .def_datalen
= sizeof(struct in_addr
),
84 .describe
= user_describe
,
85 .instantiate
= dns_resolver_instantiate
,
86 .destroy
= dns_resolver_destroy
,
90 /* Resolves server name to ip address.
94 * *ip_addr - pointer to server ip, caller responcible for freeing it.
98 dns_resolve_server_name_to_ip(const char *unc
, char **ip_addr
)
100 const struct cred
*saved_cred
;
102 struct key
*rkey
= ERR_PTR(-EAGAIN
);
107 if (!ip_addr
|| !unc
)
110 /* search for server name delimiter */
113 cFYI(1, ("%s: unc is too short: %s", __func__
, unc
));
117 name
= memchr(unc
+2, '\\', len
);
119 cFYI(1, ("%s: probably server name is whole unc: %s",
122 len
= (name
- unc
) - 2/* leading // */;
125 name
= kmalloc(len
+1, GFP_KERNEL
);
130 memcpy(name
, unc
+2, len
);
134 cFYI(1, ("%s: it is IP, skipping dns upcall: %s",
140 saved_cred
= override_creds(dns_resolver_cache
);
141 rkey
= request_key(&key_type_dns_resolver
, name
, "");
142 revert_creds(saved_cred
);
144 if (!(rkey
->perm
& KEY_USR_VIEW
)) {
145 down_read(&rkey
->sem
);
146 rkey
->perm
|= KEY_USR_VIEW
;
149 len
= rkey
->type_data
.x
[0];
150 data
= rkey
->payload
.data
;
152 cERROR(1, ("%s: unable to resolve: %s", __func__
, name
));
158 *ip_addr
= kmalloc(len
+ 1, GFP_KERNEL
);
160 memcpy(*ip_addr
, data
, len
+ 1);
162 cFYI(1, ("%s: resolved: %s to %s", __func__
,
179 int __init
cifs_init_dns_resolver(void)
185 printk(KERN_NOTICE
"Registering the %s key type\n",
186 key_type_dns_resolver
.name
);
188 /* create an override credential set with a special thread keyring in
189 * which DNS requests are cached
191 * this is used to prevent malicious redirections from being installed
194 cred
= prepare_kernel_cred(NULL
);
198 keyring
= key_alloc(&key_type_keyring
, ".dns_resolver", 0, 0, cred
,
199 (KEY_POS_ALL
& ~KEY_POS_SETATTR
) |
200 KEY_USR_VIEW
| KEY_USR_READ
,
201 KEY_ALLOC_NOT_IN_QUOTA
);
202 if (IS_ERR(keyring
)) {
203 ret
= PTR_ERR(keyring
);
204 goto failed_put_cred
;
207 ret
= key_instantiate_and_link(keyring
, NULL
, 0, NULL
, NULL
);
211 ret
= register_key_type(&key_type_dns_resolver
);
215 /* instruct request_key() to use this special keyring as a cache for
216 * the results it looks up */
217 cred
->thread_keyring
= keyring
;
218 cred
->jit_keyring
= KEY_REQKEY_DEFL_THREAD_KEYRING
;
219 dns_resolver_cache
= cred
;
229 void cifs_exit_dns_resolver(void)
231 key_revoke(dns_resolver_cache
->thread_keyring
);
232 unregister_key_type(&key_type_dns_resolver
);
233 put_cred(dns_resolver_cache
);
234 printk(KERN_NOTICE
"Unregistered %s key type\n",
235 key_type_dns_resolver
.name
);