1 /*	$NetBSD: res_debug.c,v 1.13 2012/06/25 22:32:45 abs Exp $	*/
2 
3 /*
4  * Portions Copyright (C) 2004, 2005, 2008, 2009  Internet Systems Consortium, Inc. ("ISC")
5  * Portions Copyright (C) 1996-2003  Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /*
21  * Copyright (c) 1985
22  *    The Regents of the University of California.  All rights reserved.
23  *
24  * Redistribution and use in source and binary forms, with or without
25  * modification, are permitted provided that the following conditions
26  * are met:
27  * 1. Redistributions of source code must retain the above copyright
28  *    notice, this list of conditions and the following disclaimer.
29  * 2. Redistributions in binary form must reproduce the above copyright
30  *    notice, this list of conditions and the following disclaimer in the
31  *    documentation and/or other materials provided with the distribution.
32  * 3. All advertising materials mentioning features or use of this software
33  *    must display the following acknowledgement:
34  * 	This product includes software developed by the University of
35  * 	California, Berkeley and its contributors.
36  * 4. Neither the name of the University nor the names of its contributors
37  *    may be used to endorse or promote products derived from this software
38  *    without specific prior written permission.
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  */
52 
53 /*
54  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
55  *
56  * Permission to use, copy, modify, and distribute this software for any
57  * purpose with or without fee is hereby granted, provided that the above
58  * copyright notice and this permission notice appear in all copies, and that
59  * the name of Digital Equipment Corporation not be used in advertising or
60  * publicity pertaining to distribution of the document or software without
61  * specific, written prior permission.
62  *
63  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
64  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
65  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
66  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
67  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
68  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
69  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
70  * SOFTWARE.
71  */
72 
73 /*
74  * Portions Copyright (c) 1995 by International Business Machines, Inc.
75  *
76  * International Business Machines, Inc. (hereinafter called IBM) grants
77  * permission under its copyrights to use, copy, modify, and distribute this
78  * Software with or without fee, provided that the above copyright notice and
79  * all paragraphs of this notice appear in all copies, and that the name of IBM
80  * not be used in connection with the marketing of any product incorporating
81  * the Software or modifications thereof, without specific, written prior
82  * permission.
83  *
84  * To the extent it has a right to do so, IBM grants an immunity from suit
85  * under its patents, if any, for the use, sale or manufacture of products to
86  * the extent that such products are used for performing Domain Name System
87  * dynamic updates in TCP/IP networks by means of the Software.  No immunity is
88  * granted for any product per se or for any other function of any product.
89  *
90  * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
91  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
92  * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
93  * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
94  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
95  * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
96  */
97 
98 #define LOG_TAG "resolv"
99 
100 #include "res_debug.h"
101 
102 #include <sys/param.h>
103 #include <sys/socket.h>
104 
105 #include <arpa/inet.h>
106 #include <arpa/nameser.h>
107 #include <netinet/in.h>
108 
109 #include <aidl/android/net/IDnsResolver.h>
110 #include <android-base/logging.h>
111 #include <ctype.h>
112 #include <errno.h>
113 #include <inttypes.h>
114 #include <math.h>
115 #include <netdb.h>
116 #include <stdlib.h>
117 #include <string.h>
118 #include <strings.h>
119 #include <time.h>
120 
121 #include "doh.h"
122 #include "resolv_private.h"
123 
124 // Default to disabling verbose logging unless overridden by Android.bp
125 // for debuggable builds.
126 //
127 // NOTE: Verbose resolver logs could contain PII -- do NOT enable in production builds
128 #ifndef RESOLV_ALLOW_VERBOSE_LOGGING
129 #define RESOLV_ALLOW_VERBOSE_LOGGING 0
130 #endif
131 
132 struct res_sym {
133     int number;            /* Identifying number, like T_MX */
134     const char* name;      /* Its symbolic name, like "MX" */
135     const char* humanname; /* Its fun name, like "mail exchanger" */
136 };
137 
do_section(ns_msg * handle,ns_sect section)138 static void do_section(ns_msg* handle, ns_sect section) {
139     int n, rrnum = 0;
140     int buflen = 2048;
141     ns_rr rr;
142     std::string s;
143     auto out = std::back_inserter(s);
144     /*
145      * Print answer records.
146      */
147     for (;;) {
148         if (ns_parserr(handle, section, rrnum, &rr)) {
149             if (errno != ENODEV) fmt::format_to(out, "ns_parserr: {}", strerror(errno));
150 
151             LOG(VERBOSE) << s;
152             return;
153         }
154         if (rrnum == 0) {
155             int opcode = ns_msg_getflag(*handle, ns_f_opcode);
156             fmt::format_to(out, ";; {} SECTION:\n", p_section(section, opcode));
157         }
158         if (section == ns_s_qd)
159             fmt::format_to(out, ";;\t{}, type = {}, class = {}\n", ns_rr_name(rr),
160                            p_type(ns_rr_type(rr)), p_class(ns_rr_class(rr)));
161         else if (section == ns_s_ar && ns_rr_type(rr) == ns_t_opt) {
162             size_t rdatalen;
163             uint16_t optcode, optlen;
164 
165             rdatalen = ns_rr_rdlen(rr);
166             fmt::format_to(out, "; EDNS: version: {}, udp={}, flags={}\n", (rr.ttl >> 16) & 0xff,
167                            static_cast<int>(ns_rr_class(rr)), rr.ttl & 0xffff);
168             const uint8_t* cp = ns_rr_rdata(rr);
169             while (rdatalen <= ns_rr_rdlen(rr) && rdatalen >= 4) {
170                 int i;
171 
172                 GETSHORT(optcode, cp);
173                 GETSHORT(optlen, cp);
174 
175                 if (optcode == NS_OPT_NSID) {
176                     fmt::format_to(out, "; NSID: ");
177                     if (optlen == 0) {
178                         fmt::format_to(out, "; NSID\n");
179                     } else {
180                         fmt::format_to(out, "; NSID: ");
181                         for (i = 0; i < optlen; i++) {
182                             fmt::format_to(out, "{:02x} ", cp[i]);
183                         }
184                         fmt::format_to(out, " (");
185                         for (i = 0; i < optlen; i++) {
186                             fmt::format_to(out, "{} ", isprint(cp[i]) ? cp[i] : '.');
187                         }
188                         fmt::format_to(out, ")\n");
189                     }
190                 } else {
191                     if (optlen == 0) {
192                         fmt::format_to(out, "; OPT={}\n", optcode);
193                     } else {
194                         fmt::format_to(out, "; OPT={}: ", optcode);
195                         for (i = 0; i < optlen; i++) {
196                             fmt::format_to(out, "{:02x} ", cp[i]);
197                         }
198                         fmt::format_to(out, " (");
199                         for (i = 0; i < optlen; i++) {
200                             fmt::format_to(out, "{}", isprint(cp[i]) ? cp[i] : '.');
201                         }
202                         fmt::format_to(out, ")\n");
203                     }
204                 }
205                 rdatalen -= 4 + optlen;
206                 cp += optlen;
207             }
208         } else {
209             auto buf = std::make_unique<char[]>(buflen);
210             n = ns_sprintrr(handle, &rr, NULL, NULL, buf.get(), (uint32_t)buflen);
211             if (n < 0) {
212                 if (errno == ENOSPC) {
213                     if (buflen < 131072) {
214                         buflen += 1024;
215                         continue;
216                     } else {
217                         fmt::format_to(out, "buflen over 131072");
218                         PLOG(VERBOSE) << s;
219                         return;
220                     }
221                 }
222                 fmt::format_to(out, "ns_sprintrr failed");
223                 PLOG(VERBOSE) << s;
224                 return;
225             }
226             fmt::format_to(out, ";; {}\n", buf.get());
227         }
228         rrnum++;
229     }
230 }
231 
232 // Convert bytes to its hexadecimal representation.
233 // The returned string is double the size of input.
bytesToHexStr(std::span<const uint8_t> bytes)234 std::string bytesToHexStr(std::span<const uint8_t> bytes) {
235     static char const hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
236                                  '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
237     std::string str;
238     str.reserve(bytes.size() * 2);
239     for (uint8_t ch : bytes) {
240         str.append({hex[(ch & 0xf0) >> 4], hex[ch & 0xf]});
241     }
242     return str;
243 }
244 
245 /*
246  * Print the contents of a query.
247  * This is intended to be primarily a debugging routine.
248  */
res_pquery(std::span<const uint8_t> msg)249 void res_pquery(std::span<const uint8_t> msg) {
250     if (!WOULD_LOG(VERBOSE)) return;
251 
252     ns_msg handle;
253     int qdcount, ancount, nscount, arcount;
254     uint32_t opcode, rcode, id;
255 
256     if (ns_initparse(msg.data(), msg.size(), &handle) < 0) {
257         PLOG(VERBOSE) << "ns_initparse failed";
258         return;
259     }
260     opcode = ns_msg_getflag(handle, ns_f_opcode);
261     rcode = ns_msg_getflag(handle, ns_f_rcode);
262     id = ns_msg_id(handle);
263     qdcount = ns_msg_count(handle, ns_s_qd);
264     ancount = ns_msg_count(handle, ns_s_an);
265     nscount = ns_msg_count(handle, ns_s_ns);
266     arcount = ns_msg_count(handle, ns_s_ar);
267 
268     /*
269      * Print header fields.
270      */
271     std::string s = fmt::format(";; ->>HEADER<<- opcode: {}, status: {}, id: {}\n",
272                                 _res_opcodes[opcode], p_rcode((int)rcode), id);
273     auto out = std::back_inserter(s);
274     fmt::format_to(out, ";; flags:");
275     if (ns_msg_getflag(handle, ns_f_qr)) fmt::format_to(out, " qr");
276     if (ns_msg_getflag(handle, ns_f_aa)) fmt::format_to(out, " aa");
277     if (ns_msg_getflag(handle, ns_f_tc)) fmt::format_to(out, " tc");
278     if (ns_msg_getflag(handle, ns_f_rd)) fmt::format_to(out, " rd");
279     if (ns_msg_getflag(handle, ns_f_ra)) fmt::format_to(out, " ra");
280     if (ns_msg_getflag(handle, ns_f_z)) fmt::format_to(out, " ??");
281     if (ns_msg_getflag(handle, ns_f_ad)) fmt::format_to(out, " ad");
282     if (ns_msg_getflag(handle, ns_f_cd)) fmt::format_to(out, " cd");
283     fmt::format_to(out, "; {}: {}", p_section(ns_s_qd, (int)opcode), qdcount);
284     fmt::format_to(out, ", {}: {}", p_section(ns_s_an, (int)opcode), ancount);
285     fmt::format_to(out, ", {}: {}", p_section(ns_s_ns, (int)opcode), nscount);
286     fmt::format_to(out, ", {}: {}", p_section(ns_s_ar, (int)opcode), arcount);
287 
288     LOG(VERBOSE) << s;
289 
290     /*
291      * Print the various sections.
292      */
293     do_section(&handle, ns_s_qd);
294     do_section(&handle, ns_s_an);
295     do_section(&handle, ns_s_ns);
296     do_section(&handle, ns_s_ar);
297 
298     LOG(VERBOSE) << "Hex dump:";
299     LOG(VERBOSE) << bytesToHexStr(msg);
300 }
301 
302 /*
303  * Names of RR classes and qclasses.  Classes and qclasses are the same, except
304  * that C_ANY is a qclass but not a class.  (You can ask for records of class
305  * C_ANY, but you can't have any records of that class in the database.)
306  */
307 static const struct res_sym p_class_syms[] = {
308         {C_IN, "IN", (char*) 0},     {C_CHAOS, "CH", (char*) 0},  {C_CHAOS, "CHAOS", (char*) 0},
309         {C_HS, "HS", (char*) 0},     {C_HS, "HESIOD", (char*) 0}, {C_ANY, "ANY", (char*) 0},
310         {C_NONE, "NONE", (char*) 0}, {C_IN, (char*) 0, (char*) 0}};
311 
312 /*
313  * Names of message sections.
314  */
315 static const struct res_sym p_default_section_syms[] = {{ns_s_qd, "QUERY", (char*) 0},
316                                                         {ns_s_an, "ANSWER", (char*) 0},
317                                                         {ns_s_ns, "AUTHORITY", (char*) 0},
318                                                         {ns_s_ar, "ADDITIONAL", (char*) 0},
319                                                         {0, (char*) 0, (char*) 0}};
320 
321 static const struct res_sym p_update_section_syms[] = {{S_ZONE, "ZONE", (char*) 0},
322                                                        {S_PREREQ, "PREREQUISITE", (char*) 0},
323                                                        {S_UPDATE, "UPDATE", (char*) 0},
324                                                        {S_ADDT, "ADDITIONAL", (char*) 0},
325                                                        {0, (char*) 0, (char*) 0}};
326 
327 /*
328  * Names of RR types and qtypes.  Types and qtypes are the same, except
329  * that T_ANY is a qtype but not a type.  (You can ask for records of type
330  * T_ANY, but you can't have any records of that type in the database.)
331  */
332 const struct res_sym p_type_syms[] = {
333         {ns_t_a, "A", "address"},
334         {ns_t_ns, "NS", "name server"},
335         {ns_t_md, "MD", "mail destination (deprecated)"},
336         {ns_t_mf, "MF", "mail forwarder (deprecated)"},
337         {ns_t_cname, "CNAME", "canonical name"},
338         {ns_t_soa, "SOA", "start of authority"},
339         {ns_t_mb, "MB", "mailbox"},
340         {ns_t_mg, "MG", "mail group member"},
341         {ns_t_mr, "MR", "mail rename"},
342         {ns_t_null, "NULL", "null"},
343         {ns_t_wks, "WKS", "well-known service (deprecated)"},
344         {ns_t_ptr, "PTR", "domain name pointer"},
345         {ns_t_hinfo, "HINFO", "host information"},
346         {ns_t_minfo, "MINFO", "mailbox information"},
347         {ns_t_mx, "MX", "mail exchanger"},
348         {ns_t_txt, "TXT", "text"},
349         {ns_t_rp, "RP", "responsible person"},
350         {ns_t_afsdb, "AFSDB", "DCE or AFS server"},
351         {ns_t_x25, "X25", "X25 address"},
352         {ns_t_isdn, "ISDN", "ISDN address"},
353         {ns_t_rt, "RT", "router"},
354         {ns_t_nsap, "NSAP", "nsap address"},
355         {ns_t_nsap_ptr, "NSAP_PTR", "domain name pointer"},
356         {ns_t_sig, "SIG", "signature"},
357         {ns_t_key, "KEY", "key"},
358         {ns_t_px, "PX", "mapping information"},
359         {ns_t_gpos, "GPOS", "geographical position (withdrawn)"},
360         {ns_t_aaaa, "AAAA", "IPv6 address"},
361         {ns_t_loc, "LOC", "location"},
362         {ns_t_nxt, "NXT", "next valid name (unimplemented)"},
363         {ns_t_eid, "EID", "endpoint identifier (unimplemented)"},
364         {ns_t_nimloc, "NIMLOC", "NIMROD locator (unimplemented)"},
365         {ns_t_srv, "SRV", "server selection"},
366         {ns_t_atma, "ATMA", "ATM address (unimplemented)"},
367         {ns_t_naptr, "NAPTR", "naptr"},
368         {ns_t_kx, "KX", "key exchange"},
369         {ns_t_cert, "CERT", "certificate"},
370         {ns_t_a6, "A", "IPv6 address (experminental)"},
371         {ns_t_dname, "DNAME", "non-terminal redirection"},
372         {ns_t_opt, "OPT", "opt"},
373         {ns_t_apl, "apl", "apl"},
374         {ns_t_ds, "DS", "delegation signer"},
375         {ns_t_sshfp, "SSFP", "SSH fingerprint"},
376         {ns_t_ipseckey, "IPSECKEY", "IPSEC key"},
377         {ns_t_rrsig, "RRSIG", "rrsig"},
378         {ns_t_nsec, "NSEC", "nsec"},
379         {ns_t_dnskey, "DNSKEY", "DNS key"},
380         {ns_t_dhcid, "DHCID", "dynamic host configuration identifier"},
381         {ns_t_nsec3, "NSEC3", "nsec3"},
382         {ns_t_nsec3param, "NSEC3PARAM", "NSEC3 parameters"},
383         {ns_t_hip, "HIP", "host identity protocol"},
384         {ns_t_spf, "SPF", "sender policy framework"},
385         {ns_t_tkey, "TKEY", "tkey"},
386         {ns_t_tsig, "TSIG", "transaction signature"},
387         {ns_t_ixfr, "IXFR", "incremental zone transfer"},
388         {ns_t_axfr, "AXFR", "zone transfer"},
389         {ns_t_zxfr, "ZXFR", "compressed zone transfer"},
390         {ns_t_mailb, "MAILB", "mailbox-related data (deprecated)"},
391         {ns_t_maila, "MAILA", "mail agent (deprecated)"},
392         {ns_t_naptr, "NAPTR", "URN Naming Authority"},
393         {ns_t_kx, "KX", "Key Exchange"},
394         {ns_t_cert, "CERT", "Certificate"},
395         {ns_t_a6, "A6", "IPv6 Address"},
396         {ns_t_dname, "DNAME", "dname"},
397         {ns_t_sink, "SINK", "Kitchen Sink (experimental)"},
398         {ns_t_opt, "OPT", "EDNS Options"},
399         {ns_t_any, "ANY", "\"any\""},
400         {ns_t_dlv, "DLV", "DNSSEC look-aside validation"},
401         {0, NULL, NULL}};
402 
403 /*
404  * Names of DNS rcodes.
405  */
406 static const struct res_sym p_rcode_syms[] = {{ns_r_noerror, "NOERROR", "no error"},
407                                               {ns_r_formerr, "FORMERR", "format error"},
408                                               {ns_r_servfail, "SERVFAIL", "server failed"},
409                                               {ns_r_nxdomain, "NXDOMAIN", "no such domain name"},
410                                               {ns_r_notimpl, "NOTIMP", "not implemented"},
411                                               {ns_r_refused, "REFUSED", "refused"},
412                                               {ns_r_yxdomain, "YXDOMAIN", "domain name exists"},
413                                               {ns_r_yxrrset, "YXRRSET", "rrset exists"},
414                                               {ns_r_nxrrset, "NXRRSET", "rrset doesn't exist"},
415                                               {ns_r_notauth, "NOTAUTH", "not authoritative"},
416                                               {ns_r_notzone, "NOTZONE", "Not in zone"},
417                                               {ns_r_max, "", ""},
418                                               {ns_r_badsig, "BADSIG", "bad signature"},
419                                               {ns_r_badkey, "BADKEY", "bad key"},
420                                               {ns_r_badtime, "BADTIME", "bad time"},
421                                               {0, NULL, NULL}};
422 
sym_ntos(const struct res_sym * syms,int number,int * success)423 static const char* sym_ntos(const struct res_sym* syms, int number, int* success) {
424     static char unname[20];
425 
426     for (; syms->name != 0; syms++) {
427         if (number == syms->number) {
428             if (success) *success = 1;
429             return (syms->name);
430         }
431     }
432 
433     snprintf(unname, sizeof(unname), "%d", number); /* XXX nonreentrant */
434     if (success) *success = 0;
435     return (unname);
436 }
437 
438 /*
439  * Return a string for the type.
440  */
p_type(int type)441 const char* p_type(int type) {
442     int success;
443     const char* result;
444     static char typebuf[20];
445 
446     result = sym_ntos(p_type_syms, type, &success);
447     if (success) return (result);
448     if (type < 0 || type > 0xffff) return ("BADTYPE");
449     snprintf(typebuf, sizeof(typebuf), "TYPE%d", type);
450     return (typebuf);
451 }
452 
453 /*
454  * Return a string for the type.
455  */
p_section(int section,int opcode)456 const char* p_section(int section, int opcode) {
457     const struct res_sym* symbols;
458 
459     switch (opcode) {
460         case ns_o_update:
461             symbols = p_update_section_syms;
462             break;
463         default:
464             symbols = p_default_section_syms;
465             break;
466     }
467     return (sym_ntos(symbols, section, (int*) 0));
468 }
469 
470 /*
471  * Return a mnemonic for class.
472  */
p_class(int cl)473 const char* p_class(int cl) {
474     int success;
475     const char* result;
476     static char classbuf[20];
477 
478     result = sym_ntos(p_class_syms, cl, &success);
479     if (success) return (result);
480     if (cl < 0 || cl > 0xffff) return ("BADCLASS");
481     snprintf(classbuf, sizeof(classbuf), "CLASS%d", cl);
482     return (classbuf);
483 }
484 
485 /*
486  * Return a string for the rcode.
487  */
p_rcode(int rcode)488 const char* p_rcode(int rcode) {
489     return (sym_ntos(p_rcode_syms, rcode, (int*) 0));
490 }
491 
resolv_set_log_severity(uint32_t logSeverity)492 int resolv_set_log_severity(uint32_t logSeverity) {
493     switch (logSeverity) {
494         case aidl::android::net::IDnsResolver::DNS_RESOLVER_LOG_VERBOSE:
495             logSeverity = android::base::VERBOSE;
496             doh_set_log_level(DOH_LOG_LEVEL_TRACE);
497             // *** enable verbose logging only when DBG is set. It prints sensitive data ***
498             if (RESOLV_ALLOW_VERBOSE_LOGGING == false) {
499                 logSeverity = android::base::DEBUG;
500                 doh_set_log_level(DOH_LOG_LEVEL_DEBUG);
501                 LOG(ERROR) << "Refusing to set VERBOSE logging in non-debuggable build";
502                 // TODO: Return EACCES then callers could know if the log
503                 // severity is acceptable
504             }
505             break;
506         case aidl::android::net::IDnsResolver::DNS_RESOLVER_LOG_DEBUG:
507             logSeverity = android::base::DEBUG;
508             doh_set_log_level(DOH_LOG_LEVEL_DEBUG);
509             break;
510         case aidl::android::net::IDnsResolver::DNS_RESOLVER_LOG_INFO:
511             logSeverity = android::base::INFO;
512             doh_set_log_level(DOH_LOG_LEVEL_INFO);
513             break;
514         case aidl::android::net::IDnsResolver::DNS_RESOLVER_LOG_WARNING:
515             logSeverity = android::base::WARNING;
516             doh_set_log_level(DOH_LOG_LEVEL_WARN);
517             break;
518         case aidl::android::net::IDnsResolver::DNS_RESOLVER_LOG_ERROR:
519             logSeverity = android::base::ERROR;
520             doh_set_log_level(DOH_LOG_LEVEL_ERROR);
521             break;
522         default:
523             LOG(ERROR) << __func__ << ": invalid log severity: " << logSeverity;
524             return -EINVAL;
525     }
526     android::base::SetMinimumLogSeverity(static_cast<android::base::LogSeverity>(logSeverity));
527     return 0;
528 }
529