]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/dns_resolver/dns_query.c
Merge remote-tracking branches 'asoc/topic/samsung', 'asoc/topic/sgtl5000', 'asoc...
[mirror_ubuntu-artful-kernel.git] / net / dns_resolver / dns_query.c
CommitLineData
1a4240f4
WL
1/* Upcall routine, designed to work as a key type and working through
2 * /sbin/request-key to contact userspace when handling DNS queries.
3 *
4 * See Documentation/networking/dns_resolver.txt
5 *
6 * Copyright (c) 2007 Igor Mammedov
7 * Author(s): Igor Mammedov (niallain@gmail.com)
8 * Steve French (sfrench@us.ibm.com)
9 * Wang Lei (wang840925@gmail.com)
10 * David Howells (dhowells@redhat.com)
11 *
12 * The upcall wrapper used to make an arbitrary DNS query.
13 *
14 * This function requires the appropriate userspace tool dns.upcall to be
15 * installed and something like the following lines should be added to the
16 * /etc/request-key.conf file:
17 *
18 * create dns_resolver * * /sbin/dns.upcall %k
19 *
20 * For example to use this module to query AFSDB RR:
21 *
22 * create dns_resolver afsdb:* * /sbin/dns.afsdb %k
23 *
24 * This library is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU Lesser General Public License as published
26 * by the Free Software Foundation; either version 2.1 of the License, or
27 * (at your option) any later version.
28 *
29 * This library is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
32 * the GNU Lesser General Public License for more details.
33 *
34 * You should have received a copy of the GNU Lesser General Public License
c057b190 35 * along with this library; if not, see <http://www.gnu.org/licenses/>.
1a4240f4
WL
36 */
37
38#include <linux/module.h>
39#include <linux/slab.h>
5b825c3a 40#include <linux/cred.h>
1a4240f4 41#include <linux/dns_resolver.h>
af352fe9 42#include <linux/err.h>
5b825c3a 43
1a4240f4
WL
44#include <keys/dns_resolver-type.h>
45#include <keys/user-type.h>
46
47#include "internal.h"
48
ff9517a6 49/**
1a4240f4
WL
50 * dns_query - Query the DNS
51 * @type: Query type (or NULL for straight host->IP lookup)
52 * @name: Name to look up
53 * @namelen: Length of name
54 * @options: Request options (or NULL if no options)
55 * @_result: Where to place the returned data.
56 * @_expiry: Where to store the result expiry time (or NULL)
57 *
58 * The data will be returned in the pointer at *result, and the caller is
59 * responsible for freeing it.
60 *
61 * The description should be of the form "[<query_type>:]<domain_name>", and
62 * the options need to be appropriate for the query type requested. If no
63 * query_type is given, then the query is a straight hostname to IP address
64 * lookup.
65 *
66 * The DNS resolution lookup is performed by upcalling to userspace by way of
67 * requesting a key of type dns_resolver.
68 *
69 * Returns the size of the result on success, -ve error code otherwise.
70 */
71int dns_query(const char *type, const char *name, size_t namelen,
451c2b5c 72 const char *options, char **_result, time64_t *_expiry)
1a4240f4
WL
73{
74 struct key *rkey;
0837e49a 75 struct user_key_payload *upayload;
1a4240f4
WL
76 const struct cred *saved_cred;
77 size_t typelen, desclen;
78 char *desc, *cp;
79 int ret, len;
80
81 kenter("%s,%*.*s,%zu,%s",
82 type, (int)namelen, (int)namelen, name, namelen, options);
83
84 if (!name || namelen == 0 || !_result)
85 return -EINVAL;
86
87 /* construct the query key description as "[<type>:]<name>" */
88 typelen = 0;
89 desclen = 0;
90 if (type) {
91 typelen = strlen(type);
92 if (typelen < 1)
93 return -EINVAL;
94 desclen += typelen + 1;
95 }
96
97 if (!namelen)
9638f671
MS
98 namelen = strnlen(name, 256);
99 if (namelen < 3 || namelen > 255)
1a4240f4
WL
100 return -EINVAL;
101 desclen += namelen + 1;
102
103 desc = kmalloc(desclen, GFP_KERNEL);
104 if (!desc)
105 return -ENOMEM;
106
107 cp = desc;
108 if (type) {
109 memcpy(cp, type, typelen);
110 cp += typelen;
111 *cp++ = ':';
112 }
113 memcpy(cp, name, namelen);
114 cp += namelen;
115 *cp = '\0';
116
117 if (!options)
118 options = "";
119 kdebug("call request_key(,%s,%s)", desc, options);
120
121 /* make the upcall, using special credentials to prevent the use of
122 * add_key() to preinstall malicious redirections
123 */
124 saved_cred = override_creds(dns_resolver_cache);
125 rkey = request_key(&key_type_dns_resolver, desc, options);
126 revert_creds(saved_cred);
127 kfree(desc);
128 if (IS_ERR(rkey)) {
129 ret = PTR_ERR(rkey);
130 goto out;
131 }
132
133 down_read(&rkey->sem);
0c7774ab 134 set_bit(KEY_FLAG_ROOT_CAN_INVAL, &rkey->flags);
1a4240f4
WL
135 rkey->perm |= KEY_USR_VIEW;
136
137 ret = key_validate(rkey);
138 if (ret < 0)
139 goto put;
140
4a2d7892 141 /* If the DNS server gave an error, return that to the caller */
146aa8b1 142 ret = PTR_ERR(rkey->payload.data[dns_key_error]);
4a2d7892
WL
143 if (ret)
144 goto put;
145
0837e49a 146 upayload = user_key_payload_locked(rkey);
1a4240f4
WL
147 len = upayload->datalen;
148
149 ret = -ENOMEM;
150 *_result = kmalloc(len + 1, GFP_KERNEL);
151 if (!*_result)
152 goto put;
153
84a7c0b1 154 memcpy(*_result, upayload->data, len);
640d7efe 155 (*_result)[len] = '\0';
84a7c0b1 156
1a4240f4
WL
157 if (_expiry)
158 *_expiry = rkey->expiry;
159
160 ret = len;
161put:
162 up_read(&rkey->sem);
163 key_put(rkey);
164out:
165 kleave(" = %d", ret);
166 return ret;
167}
168EXPORT_SYMBOL(dns_query);