1 /*
2 * wpa_supplicant/hostapd / Debug prints
3 * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "includes.h"
10 #include <time.h>
11
12 #include "common.h"
13
14 #ifdef CONFIG_DEBUG_SYSLOG
15 #include <syslog.h>
16 #endif /* CONFIG_DEBUG_SYSLOG */
17
18 #ifdef CONFIG_DEBUG_LINUX_TRACING
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 static FILE *wpa_debug_tracing_file = NULL;
27
28 #define WPAS_TRACE_PFX "wpas <%d>: "
29 #endif /* CONFIG_DEBUG_LINUX_TRACING */
30
31 #define HIDDEN_CHAR '*'
32 int wpa_debug_level = MSG_DEBUG;
33 int wpa_debug_show_keys = 1;
34 int wpa_debug_timestamp = 0;
35 int wpa_debug_syslog = 0;
36 #ifndef CONFIG_NO_STDOUT_DEBUG
37 static FILE *out_file = NULL;
38 #endif /* CONFIG_NO_STDOUT_DEBUG */
39
40
41 #ifdef CONFIG_ANDROID_LOG
42
43 #include <android/log.h>
44
45 #ifndef ANDROID_LOG_NAME
46 #define ANDROID_LOG_NAME "wpa_supplicant"
47 #endif /* ANDROID_LOG_NAME */
48
wpa_to_android_level(int level)49 static int wpa_to_android_level(int level)
50 {
51 if (level == MSG_ERROR)
52 return ANDROID_LOG_ERROR;
53 if (level == MSG_WARNING)
54 return ANDROID_LOG_WARN;
55 if (level == MSG_INFO)
56 return ANDROID_LOG_INFO;
57 return ANDROID_LOG_DEBUG;
58 }
59
60 #endif /* CONFIG_ANDROID_LOG */
61
62 #ifndef CONFIG_NO_STDOUT_DEBUG
63
64 #ifdef CONFIG_DEBUG_FILE
65 #include <sys/types.h>
66 #include <sys/stat.h>
67 #include <fcntl.h>
68 #endif /* CONFIG_DEBUG_FILE */
69 #define WPA_MAX_ANONYMIZE_LENGTH 256
70
71 #ifndef WPA_MAX_TOKEN_LEN
72 #define WPA_MAX_TOKEN_LEN 5
73 #endif /* WPA_MAX_TOKEN_LEN */
74
75
wpa_debug_print_timestamp(void)76 void wpa_debug_print_timestamp(void)
77 {
78 #ifndef CONFIG_ANDROID_LOG
79 struct os_time tv;
80
81 if (!wpa_debug_timestamp)
82 return;
83
84 os_get_time(&tv);
85 #ifdef CONFIG_DEBUG_FILE
86 if (out_file)
87 fprintf(out_file, "%ld.%06u: ", (long) tv.sec,
88 (unsigned int) tv.usec);
89 #endif /* CONFIG_DEBUG_FILE */
90 if (!out_file && !wpa_debug_syslog)
91 printf("%ld.%06u: ", (long) tv.sec, (unsigned int) tv.usec);
92 #endif /* CONFIG_ANDROID_LOG */
93 }
94
95
96 #ifdef CONFIG_DEBUG_SYSLOG
97 #ifndef LOG_HOSTAPD
98 #define LOG_HOSTAPD LOG_DAEMON
99 #endif /* LOG_HOSTAPD */
100
wpa_debug_open_syslog(void)101 void wpa_debug_open_syslog(void)
102 {
103 openlog("wpa_supplicant", LOG_PID | LOG_NDELAY, LOG_HOSTAPD);
104 wpa_debug_syslog++;
105 }
106
107
wpa_debug_close_syslog(void)108 void wpa_debug_close_syslog(void)
109 {
110 if (wpa_debug_syslog)
111 closelog();
112 }
113
114
syslog_priority(int level)115 static int syslog_priority(int level)
116 {
117 switch (level) {
118 case MSG_MSGDUMP:
119 case MSG_DEBUG:
120 return LOG_DEBUG;
121 case MSG_INFO:
122 return LOG_NOTICE;
123 case MSG_WARNING:
124 return LOG_WARNING;
125 case MSG_ERROR:
126 return LOG_ERR;
127 }
128 return LOG_INFO;
129 }
130 #endif /* CONFIG_DEBUG_SYSLOG */
131
132
133 #ifdef CONFIG_DEBUG_LINUX_TRACING
134
wpa_debug_open_linux_tracing(void)135 int wpa_debug_open_linux_tracing(void)
136 {
137 int mounts, trace_fd;
138 char buf[4096] = {};
139 ssize_t buflen;
140 char *line, *tmp1, *path = NULL;
141
142 mounts = open("/proc/mounts", O_RDONLY);
143 if (mounts < 0) {
144 printf("no /proc/mounts\n");
145 return -1;
146 }
147
148 buflen = read(mounts, buf, sizeof(buf) - 1);
149 close(mounts);
150 if (buflen < 0) {
151 printf("failed to read /proc/mounts\n");
152 return -1;
153 }
154 buf[buflen] = '\0';
155
156 line = strtok_r(buf, "\n", &tmp1);
157 while (line) {
158 char *tmp2, *tmp_path, *fstype;
159 /* "<dev> <mountpoint> <fs type> ..." */
160 strtok_r(line, " ", &tmp2);
161 tmp_path = strtok_r(NULL, " ", &tmp2);
162 fstype = strtok_r(NULL, " ", &tmp2);
163 if (fstype && strcmp(fstype, "debugfs") == 0) {
164 path = tmp_path;
165 break;
166 }
167
168 line = strtok_r(NULL, "\n", &tmp1);
169 }
170
171 if (path == NULL) {
172 printf("debugfs mountpoint not found\n");
173 return -1;
174 }
175
176 snprintf(buf, sizeof(buf) - 1, "%s/tracing/trace_marker", path);
177
178 trace_fd = open(buf, O_WRONLY);
179 if (trace_fd < 0) {
180 printf("failed to open trace_marker file\n");
181 return -1;
182 }
183 wpa_debug_tracing_file = fdopen(trace_fd, "w");
184 if (wpa_debug_tracing_file == NULL) {
185 close(trace_fd);
186 printf("failed to fdopen()\n");
187 return -1;
188 }
189
190 return 0;
191 }
192
193
wpa_debug_close_linux_tracing(void)194 void wpa_debug_close_linux_tracing(void)
195 {
196 if (wpa_debug_tracing_file == NULL)
197 return;
198 fclose(wpa_debug_tracing_file);
199 wpa_debug_tracing_file = NULL;
200 }
201
202 #endif /* CONFIG_DEBUG_LINUX_TRACING */
203
204 #ifdef CONFIG_OPEN_HARMONY_PATCH
205 #include "hilog/log.h"
206 #include "parameter.h"
207
208 #ifdef LOG_DOMAIN
209 #undef LOG_DOMAIN
210 #endif // LOG_DOMAIN
211 #ifdef LOG_TAG
212 #undef LOG_TAG
213 #endif // LOG_TAG
214 #define LOG_DOMAIN 0xD005200
215 #define LOG_TAG "wpa_supplicant"
216 #define WPA_MAX_LOG_CHAR 8196
217 #define WPA_PROP_KEY_DEBUG_ON "persist.sys.wpa_debug_on"
218 #define PARAM_VALUE_MAX_LEN 10
219
220 enum {
221 WPA_HILOG_UNKNOWN, WPA_HILOG_UNSET, WPA_HILOG_SET
222 };
223
224 int32_t wpa_debug_hilog_switch = WPA_HILOG_UNKNOWN;
225
wpa_can_hilog()226 static bool wpa_can_hilog()
227 {
228 switch (wpa_debug_hilog_switch) {
229 case WPA_HILOG_UNSET:
230 return false;
231 case WPA_HILOG_SET:
232 return true;
233 default:
234 break;
235 }
236
237 wpa_debug_hilog_switch = WPA_HILOG_SET;
238 return true;
239 }
240 #endif // CONFIG_OPEN_HARMONY_PATCH
241
242 /**
243 * wpa_printf - conditional printf
244 * @level: priority level (MSG_*) of the message
245 * @fmt: printf format string, followed by optional arguments
246 *
247 * This function is used to print conditional debugging and error messages. The
248 * output may be directed to stdout, stderr, and/or syslog based on
249 * configuration.
250 *
251 * Note: New line '\n' is added to the end of the text when printing to stdout.
252 */
wpa_printf(int level,const char * fmt,...)253 void wpa_printf(int level, const char *fmt, ...)
254 {
255 #ifdef CONFIG_OPEN_HARMONY_PATCH
256 if (wpa_can_hilog()) {
257 int32_t ulPos = 0;
258 char szStr[WPA_MAX_LOG_CHAR] = {0};
259 va_list arg = {0};
260 int32_t ret;
261
262 va_start(arg, fmt);
263 ret = vsprintf(&szStr[ulPos], fmt, arg);
264 va_end(arg);
265 if (ret > 0) {
266 switch (level) {
267 case MSG_ERROR:
268 HILOG_ERROR(LOG_CORE, "%{public}s", szStr);
269 break;
270 case MSG_WARNING:
271 HILOG_WARN(LOG_CORE, "%{public}s", szStr);
272 break;
273 case MSG_INFO:
274 HILOG_INFO(LOG_CORE, "%{public}s", szStr);
275 break;
276 default:
277 HILOG_DEBUG(LOG_CORE, "%{public}s", szStr);
278 break;
279 }
280 }
281 return;
282 }
283 #endif
284
285 #ifdef CONFIG_WPA_NO_LOG
286 return;
287 #else
288 va_list ap;
289
290 if (level >= wpa_debug_level) {
291 #ifdef CONFIG_ANDROID_LOG
292 va_start(ap, fmt);
293 __android_log_vprint(wpa_to_android_level(level),
294 ANDROID_LOG_NAME, fmt, ap);
295 va_end(ap);
296 #else /* CONFIG_ANDROID_LOG */
297 #ifdef CONFIG_DEBUG_SYSLOG
298 if (wpa_debug_syslog) {
299 va_start(ap, fmt);
300 vsyslog(syslog_priority(level), fmt, ap);
301 va_end(ap);
302 }
303 #endif /* CONFIG_DEBUG_SYSLOG */
304 wpa_debug_print_timestamp();
305 #ifdef CONFIG_DEBUG_FILE
306 if (out_file) {
307 va_start(ap, fmt);
308 vfprintf(out_file, fmt, ap);
309 fprintf(out_file, "\n");
310 va_end(ap);
311 }
312 #endif /* CONFIG_DEBUG_FILE */
313 if (!wpa_debug_syslog && !out_file) {
314 va_start(ap, fmt);
315 vprintf(fmt, ap);
316 printf("\n");
317 va_end(ap);
318 }
319 #endif /* CONFIG_ANDROID_LOG */
320 }
321
322 #ifdef CONFIG_DEBUG_LINUX_TRACING
323 if (wpa_debug_tracing_file != NULL) {
324 va_start(ap, fmt);
325 fprintf(wpa_debug_tracing_file, WPAS_TRACE_PFX, level);
326 vfprintf(wpa_debug_tracing_file, fmt, ap);
327 fprintf(wpa_debug_tracing_file, "\n");
328 fflush(wpa_debug_tracing_file);
329 va_end(ap);
330 }
331 #endif /* CONFIG_DEBUG_LINUX_TRACING */
332 #endif /* CONFIG_WPA_NO_LOG */
333 }
334
335
_wpa_hexdump(int level,const char * title,const u8 * buf,size_t len,int show,int only_syslog)336 static void _wpa_hexdump(int level, const char *title, const u8 *buf,
337 size_t len, int show, int only_syslog)
338 {
339 #ifdef CONFIG_WPA_NO_LOG
340 return;
341 #else
342 size_t i;
343 #ifdef CONFIG_OPEN_HARMONY_PATCH
344 if (wpa_can_hilog()) {
345 const char *display;
346 char *strbuf = NULL;
347 size_t slen = len;
348 if (buf == NULL) {
349 display = " [NULL]";
350 } else if (len == 0) {
351 display = "";
352 } else if (show && len) {
353 if (slen > 32)
354 slen = 32;
355 strbuf = os_malloc(1 + 3 * slen);
356 if (strbuf == NULL) {
357 wpa_printf(MSG_ERROR, "wpa_hexdump: Failed to "
358 "allocate message buffer");
359 return;
360 }
361
362 for (i = 0; i < slen; i++)
363 os_snprintf(&strbuf[i * 3], 4, " %02x",
364 buf[i]);
365
366 display = strbuf;
367 } else {
368 display = " [REMOVED]";
369 }
370 switch (level) {
371 case MSG_ERROR:
372 HILOG_ERROR(LOG_CORE, "%{public}s - hexdump(len=%{public}lu):%{public}s%{public}s",
373 title, (long unsigned int) len, display, len > slen ? " ..." : "");
374 break;
375 case MSG_WARNING:
376 HILOG_WARN(LOG_CORE, "%{public}s - hexdump(len=%{public}lu):%{public}s%{public}s",
377 title, (long unsigned int) len, display, len > slen ? " ..." : "");
378 break;
379 case MSG_INFO:
380 HILOG_INFO(LOG_CORE, "%{public}s - hexdump(len=%{public}lu):%{public}s%{public}s",
381 title, (long unsigned int) len, display, len > slen ? " ..." : "");
382 break;
383 default:
384 HILOG_DEBUG(LOG_CORE, "%{public}s - hexdump(len=%{public}lu):%{public}s%{public}s",
385 title, (long unsigned int) len, display, len > slen ? " ..." : "");
386 break;
387 }
388 bin_clear_free(strbuf, 1 + 3 * slen);
389 return;
390 }
391 #endif
392
393 #ifdef CONFIG_DEBUG_LINUX_TRACING
394 if (wpa_debug_tracing_file != NULL) {
395 fprintf(wpa_debug_tracing_file,
396 WPAS_TRACE_PFX "%s - hexdump(len=%lu):",
397 level, title, (unsigned long) len);
398 if (buf == NULL) {
399 fprintf(wpa_debug_tracing_file, " [NULL]\n");
400 } else if (!show) {
401 fprintf(wpa_debug_tracing_file, " [REMOVED]\n");
402 } else {
403 for (i = 0; i < len; i++)
404 fprintf(wpa_debug_tracing_file,
405 " %02x", buf[i]);
406 }
407 fflush(wpa_debug_tracing_file);
408 }
409 #endif /* CONFIG_DEBUG_LINUX_TRACING */
410
411 if (level < wpa_debug_level)
412 return;
413 #ifdef CONFIG_ANDROID_LOG
414 {
415 const char *display;
416 char *strbuf = NULL;
417 size_t slen = len;
418 if (buf == NULL) {
419 display = " [NULL]";
420 } else if (len == 0) {
421 display = "";
422 } else if (show && len) {
423 /* Limit debug message length for Android log */
424 if (slen > 32)
425 slen = 32;
426 strbuf = os_malloc(1 + 3 * slen);
427 if (strbuf == NULL) {
428 wpa_printf(MSG_ERROR, "wpa_hexdump: Failed to "
429 "allocate message buffer");
430 return;
431 }
432
433 for (i = 0; i < slen; i++)
434 os_snprintf(&strbuf[i * 3], 4, " %02x",
435 buf[i]);
436
437 display = strbuf;
438 } else {
439 display = " [REMOVED]";
440 }
441
442 __android_log_print(wpa_to_android_level(level),
443 ANDROID_LOG_NAME,
444 "%s - hexdump(len=%lu):%s%s",
445 title, (long unsigned int) len, display,
446 len > slen ? " ..." : "");
447 bin_clear_free(strbuf, 1 + 3 * slen);
448 return;
449 }
450 #else /* CONFIG_ANDROID_LOG */
451 #ifdef CONFIG_DEBUG_SYSLOG
452 if (wpa_debug_syslog) {
453 const char *display;
454 char *strbuf = NULL;
455
456 if (buf == NULL) {
457 display = " [NULL]";
458 } else if (len == 0) {
459 display = "";
460 } else if (show && len) {
461 strbuf = os_malloc(1 + 3 * len);
462 if (strbuf == NULL) {
463 wpa_printf(MSG_ERROR, "wpa_hexdump: Failed to "
464 "allocate message buffer");
465 return;
466 }
467
468 for (i = 0; i < len; i++)
469 os_snprintf(&strbuf[i * 3], 4, " %02x",
470 buf[i]);
471
472 display = strbuf;
473 } else {
474 display = " [REMOVED]";
475 }
476
477 syslog(syslog_priority(level), "%s - hexdump(len=%lu):%s",
478 title, (unsigned long) len, display);
479 bin_clear_free(strbuf, 1 + 3 * len);
480 if (only_syslog)
481 return;
482 }
483 #endif /* CONFIG_DEBUG_SYSLOG */
484 wpa_debug_print_timestamp();
485 #ifdef CONFIG_DEBUG_FILE
486 if (out_file) {
487 fprintf(out_file, "%s - hexdump(len=%lu):",
488 title, (unsigned long) len);
489 if (buf == NULL) {
490 fprintf(out_file, " [NULL]");
491 } else if (show) {
492 for (i = 0; i < len; i++)
493 fprintf(out_file, " %02x", buf[i]);
494 } else {
495 fprintf(out_file, " [REMOVED]");
496 }
497 fprintf(out_file, "\n");
498 }
499 #endif /* CONFIG_DEBUG_FILE */
500 if (!wpa_debug_syslog && !out_file) {
501 printf("%s - hexdump(len=%lu):", title, (unsigned long) len);
502 if (buf == NULL) {
503 printf(" [NULL]");
504 } else if (show) {
505 for (i = 0; i < len; i++)
506 printf(" %02x", buf[i]);
507 } else {
508 printf(" [REMOVED]");
509 }
510 printf("\n");
511 }
512 #endif /* CONFIG_ANDROID_LOG */
513 #endif /* CONFIG_WPA_NO_LOG */
514 }
515
disable_anonymized_print()516 int disable_anonymized_print()
517 {
518 char prop[PARAM_VALUE_MAX_LEN] = { 0 };
519 if (GetParameter(WPA_PROP_KEY_DEBUG_ON, "0", prop, sizeof(prop)) > 0) {
520 if (atoi(prop) > 0) {
521 return 1;
522 }
523 }
524 return 0;
525 }
526
get_realtime_microsecond()527 long get_realtime_microsecond()
528 {
529 struct timespec ts = {0};
530
531 clock_gettime(CLOCK_REALTIME, &ts);
532 long microseconds = ts.tv_nsec / 1000;
533 return microseconds;
534 }
535
anonymize_ssid(const char * str)536 const char *anonymize_ssid(const char *str)
537 {
538 if (str == NULL || *str == '\0') {
539 return str;
540 }
541
542 static char s[WPA_MAX_ANONYMIZE_LENGTH];
543 int strLen = os_strlen(str);
544 os_strlcpy(s, str, sizeof(s));
545
546 if (disable_anonymized_print()) {
547 return s;
548 }
549 const char hiddenChar = HIDDEN_CHAR;
550 const int minHiddenSize = 3;
551 const int headKeepSize = 3;
552 const int tailKeepSize = 3;
553
554 if (strLen < minHiddenSize) {
555 os_memset(s, hiddenChar, strLen);
556 return s;
557 }
558
559 if (strLen < minHiddenSize + headKeepSize + tailKeepSize) {
560 int beginIndex = 1;
561 int hiddenSize = strLen - minHiddenSize + 1;
562 hiddenSize = hiddenSize > minHiddenSize ? minHiddenSize : hiddenSize;
563 os_memset(s + beginIndex, hiddenChar, hiddenSize);
564 return s;
565 }
566 os_memset(s + headKeepSize, hiddenChar, strLen - headKeepSize - tailKeepSize);
567 return s;
568 }
569
anonymize_token(const u8 num)570 const char *anonymize_token(const u8 num)
571 {
572 int res;
573 static char str[WPA_MAX_TOKEN_LEN] = { 0 };
574 res = os_snprintf(str, WPA_MAX_TOKEN_LEN, "%u", num);
575 if (os_snprintf_error(WPA_MAX_TOKEN_LEN, res)) {
576 wpa_printf(MSG_ERROR, "anonymize_token: Failed %d", res);
577 return str;
578 }
579 return anonymize_common(str);
580 }
581
anonymize_common(const char * str)582 const char *anonymize_common(const char *str)
583 {
584 static char temp[WPA_MAX_ANONYMIZE_LENGTH] = { 0 };
585
586 if (str == NULL || *str == '\0') {
587 return temp;
588 }
589 int strLen = os_strlen(str);
590 os_strlcpy(temp, str, sizeof(temp));
591
592 if (disable_anonymized_print()) {
593 return temp;
594 }
595 const char hiddenChar = HIDDEN_CHAR;
596 const int minHiddenSize = 3;
597 const int headKeepSize = 3;
598 const int tailKeepSize = 3;
599
600 if (strLen < minHiddenSize) {
601 os_memset(temp, hiddenChar, strLen);
602 return temp;
603 }
604
605 if (strLen < minHiddenSize + headKeepSize + tailKeepSize) {
606 int beginIndex = 1;
607 int hiddenSize = strLen - minHiddenSize + 1;
608 hiddenSize = hiddenSize > minHiddenSize ? minHiddenSize : hiddenSize;
609 os_memset(temp + beginIndex, hiddenChar, hiddenSize);
610 return temp;
611 }
612 os_memset(temp + headKeepSize, hiddenChar, strLen - headKeepSize - tailKeepSize);
613 return temp;
614 }
615
anonymize_ip(const char * str)616 const char *anonymize_ip(const char *str)
617 {
618 if (str == NULL || *str == '\0') {
619 return str;
620 }
621 int colonCount = 0;
622 const int maxDisplayNum = 2;
623 static char s[40];
624 int start = 0;
625 os_strlcpy(s, str, sizeof(s));
626 if (disable_anonymized_print()) {
627 return s;
628 }
629 // ipv4 or ipv6 anonymize
630 for (int i = 0; i < os_strlen(s); i++) {
631 if (s[i] == ':' || s[i] == '.') {
632 colonCount++;
633 if (colonCount == maxDisplayNum) {
634 start = i + 1;
635 }
636 }
637 }
638 for (int j = start; j < os_strlen(s); j++) {
639 if (s[j] != ':' && s[j] != '.') {
640 s[j] = HIDDEN_CHAR;
641 }
642 }
643 return s;
644 }
645
646
get_anonymized_result_setnetwork(const char * str)647 const char *get_anonymized_result_setnetwork(const char *str)
648 {
649 if (str == NULL || *str == '\0') {
650 return str;
651 }
652 static char cmd[WPA_MAX_ANONYMIZE_LENGTH];
653 os_strlcpy(cmd, str, sizeof(cmd));
654 if (disable_anonymized_print()) {
655 return cmd;
656 }
657 // cmd include ssid or identity
658 if (os_strchr(cmd, '\"') && (os_strstr(cmd, "ssid") || os_strstr(cmd, "identity"))) {
659 char tempssid[WPA_MAX_ANONYMIZE_LENGTH];
660 os_strlcpy(tempssid, os_strchr(cmd, '\"') + 1, sizeof(tempssid));
661 tempssid[os_strrchr(cmd, '\"') - os_strchr(cmd, '\"') - 1] = '\0';
662 static char tempStr[WPA_MAX_ANONYMIZE_LENGTH];
663 char *strOfStrtok = strtok(cmd, "\"");
664 if (strOfStrtok == NULL) {
665 return cmd;
666 }
667 os_strlcpy(tempStr, strOfStrtok, sizeof(tempStr));
668 os_snprintf(cmd, sizeof(cmd), "%s\"%s\"", tempStr, anonymize_ssid(tempssid));
669 return cmd;
670 }
671 //cmd include password or psk
672 if (os_strchr(cmd, '\"') && (os_strstr(cmd, "password") || os_strstr(cmd, "psk"))) {
673 char tempNumbel[WPA_MAX_ANONYMIZE_LENGTH];
674 os_strlcpy(tempNumbel, os_strchr(cmd, '\"') + 1, sizeof(tempNumbel));
675 tempNumbel[os_strrchr(cmd, '\"') - os_strchr(cmd, '\"') - 1] = '\0';
676 for (int i = 0; i < os_strlen(tempNumbel); i++) {
677 tempNumbel[i] = HIDDEN_CHAR;
678 }
679 static char tempStr[WPA_MAX_ANONYMIZE_LENGTH];
680 char *strOfStrtok = strtok(cmd, "\"");
681 if (strOfStrtok == NULL) {
682 return cmd;
683 }
684 os_strlcpy(tempStr, strOfStrtok, sizeof(tempStr));
685 os_snprintf(cmd, sizeof(cmd), "%s\"%s\"", tempStr, tempNumbel);
686 os_memset(tempNumbel, 0, sizeof(tempNumbel));
687 return cmd;
688 }
689 return cmd;
690 }
691
get_anonymized_result_setnetwork_for_bssid(const char * str)692 const char *get_anonymized_result_setnetwork_for_bssid(const char *str)
693 {
694 if (str == NULL || *str == '\0') {
695 return str;
696 }
697 static const int colonCountNum = 2;
698 static const int maxHiddenNum = 9;
699 static char cmd[WPA_MAX_ANONYMIZE_LENGTH];
700 os_strlcpy(cmd, str, sizeof(cmd));
701 if (disable_anonymized_print()) {
702 return cmd;
703 }
704 //cmd include bssid
705 if (os_strchr(cmd, ':')) {
706 int colonCount = 0;
707 int start = 0;
708 for (int j = 0; j < os_strlen(cmd); j++) {
709 if (cmd[j] == ':') {
710 colonCount++;
711 }
712 if (colonCount == colonCountNum) {
713 start = j + 1;
714 break;
715 }
716 }
717 if (colonCount != colonCountNum) {
718 return cmd;
719 }
720 for (int k = start; k < start + maxHiddenNum; k++) {
721 if (cmd[k] != ':') {
722 cmd[k] = HIDDEN_CHAR;
723 }
724 }
725 return cmd;
726 }
727 return cmd;
728 }
729
get_anonymized_result_for_set(const char * str)730 const char *get_anonymized_result_for_set(const char *str)
731 {
732 if (str == NULL || *str == '\0') {
733 return str;
734 }
735 static char cmd[WPA_MAX_ANONYMIZE_LENGTH];
736 os_strlcpy(cmd, str, sizeof(cmd));
737 if (disable_anonymized_print()) {
738 return cmd;
739 }
740 if (os_strstr(cmd, "wpa_passphrase")) {
741 char *value = os_strchr(cmd, ' ') + 1;
742 if (value == NULL) {
743 return cmd;
744 }
745 os_memset(value, HIDDEN_CHAR, os_strlen(value));
746 return cmd;
747 } else if (os_strstr(cmd, "ssid")) {
748 char *value = os_strchr(cmd, ' ') + 1;
749 os_snprintf(cmd, sizeof(cmd), "ssid=%s", anonymize_ssid(value));
750 return cmd;
751 } else if (os_strstr(cmd, "P2P_CONNECT")) {
752 char *value = os_strchr(cmd, ' ') + 1;
753 if (value == NULL) {
754 return cmd;
755 }
756 os_snprintf(cmd, sizeof(cmd), "P2P_CONNECT=%s", get_anonymized_result_setnetwork_for_bssid(value));
757 return cmd;
758 }
759 return cmd;
760 }
761
wpa_hexdump(int level,const char * title,const void * buf,size_t len)762 void wpa_hexdump(int level, const char *title, const void *buf, size_t len)
763 {
764 _wpa_hexdump(level, title, buf, len, 1, 0);
765 }
766
767
wpa_hexdump_key(int level,const char * title,const void * buf,size_t len)768 void wpa_hexdump_key(int level, const char *title, const void *buf, size_t len)
769 {
770 _wpa_hexdump(level, title, buf, len, wpa_debug_show_keys, 0);
771 }
772
773
_wpa_hexdump_ascii(int level,const char * title,const void * buf,size_t len,int show)774 static void _wpa_hexdump_ascii(int level, const char *title, const void *buf,
775 size_t len, int show)
776 {
777 #ifdef CONFIG_WPA_NO_LOG
778 return;
779 #else
780 size_t i, llen;
781 const u8 *pos = buf;
782 const size_t line_len = 16;
783
784 #ifdef CONFIG_DEBUG_LINUX_TRACING
785 if (wpa_debug_tracing_file != NULL) {
786 fprintf(wpa_debug_tracing_file,
787 WPAS_TRACE_PFX "%s - hexdump_ascii(len=%lu):",
788 level, title, (unsigned long) len);
789 if (buf == NULL) {
790 fprintf(wpa_debug_tracing_file, " [NULL]\n");
791 } else if (!show) {
792 fprintf(wpa_debug_tracing_file, " [REMOVED]\n");
793 } else {
794 /* can do ascii processing in userspace */
795 for (i = 0; i < len; i++)
796 fprintf(wpa_debug_tracing_file,
797 " %02x", pos[i]);
798 }
799 fflush(wpa_debug_tracing_file);
800 }
801 #endif /* CONFIG_DEBUG_LINUX_TRACING */
802
803 if (level < wpa_debug_level)
804 return;
805 #ifdef CONFIG_ANDROID_LOG
806 _wpa_hexdump(level, title, buf, len, show, 0);
807 #else /* CONFIG_ANDROID_LOG */
808 #ifdef CONFIG_DEBUG_SYSLOG
809 if (wpa_debug_syslog)
810 _wpa_hexdump(level, title, buf, len, show, 1);
811 #endif /* CONFIG_DEBUG_SYSLOG */
812 wpa_debug_print_timestamp();
813 #ifdef CONFIG_DEBUG_FILE
814 if (out_file) {
815 if (!show) {
816 fprintf(out_file,
817 "%s - hexdump_ascii(len=%lu): [REMOVED]\n",
818 title, (unsigned long) len);
819 goto file_done;
820 }
821 if (buf == NULL) {
822 fprintf(out_file,
823 "%s - hexdump_ascii(len=%lu): [NULL]\n",
824 title, (unsigned long) len);
825 goto file_done;
826 }
827 fprintf(out_file, "%s - hexdump_ascii(len=%lu):\n",
828 title, (unsigned long) len);
829 while (len) {
830 llen = len > line_len ? line_len : len;
831 fprintf(out_file, " ");
832 for (i = 0; i < llen; i++)
833 fprintf(out_file, " %02x", pos[i]);
834 for (i = llen; i < line_len; i++)
835 fprintf(out_file, " ");
836 fprintf(out_file, " ");
837 for (i = 0; i < llen; i++) {
838 if (isprint(pos[i]))
839 fprintf(out_file, "%c", pos[i]);
840 else
841 fprintf(out_file, "_");
842 }
843 for (i = llen; i < line_len; i++)
844 fprintf(out_file, " ");
845 fprintf(out_file, "\n");
846 pos += llen;
847 len -= llen;
848 }
849 }
850 file_done:
851 #endif /* CONFIG_DEBUG_FILE */
852 if (!wpa_debug_syslog && !out_file) {
853 if (!show) {
854 printf("%s - hexdump_ascii(len=%lu): [REMOVED]\n",
855 title, (unsigned long) len);
856 return;
857 }
858 if (buf == NULL) {
859 printf("%s - hexdump_ascii(len=%lu): [NULL]\n",
860 title, (unsigned long) len);
861 return;
862 }
863 printf("%s - hexdump_ascii(len=%lu):\n", title,
864 (unsigned long) len);
865 while (len) {
866 llen = len > line_len ? line_len : len;
867 printf(" ");
868 for (i = 0; i < llen; i++)
869 printf(" %02x", pos[i]);
870 for (i = llen; i < line_len; i++)
871 printf(" ");
872 printf(" ");
873 for (i = 0; i < llen; i++) {
874 if (isprint(pos[i]))
875 printf("%c", pos[i]);
876 else
877 printf("_");
878 }
879 for (i = llen; i < line_len; i++)
880 printf(" ");
881 printf("\n");
882 pos += llen;
883 len -= llen;
884 }
885 }
886 #endif /* CONFIG_ANDROID_LOG */
887 #endif /* CONFIG_WPA_NO_LOG */
888 }
889
890
wpa_hexdump_ascii(int level,const char * title,const void * buf,size_t len)891 void wpa_hexdump_ascii(int level, const char *title, const void *buf,
892 size_t len)
893 {
894 _wpa_hexdump_ascii(level, title, buf, len, 1);
895 }
896
897
wpa_hexdump_ascii_key(int level,const char * title,const void * buf,size_t len)898 void wpa_hexdump_ascii_key(int level, const char *title, const void *buf,
899 size_t len)
900 {
901 _wpa_hexdump_ascii(level, title, buf, len, wpa_debug_show_keys);
902 }
903
904
905 #ifdef CONFIG_DEBUG_FILE
906 static char *last_path = NULL;
907 #endif /* CONFIG_DEBUG_FILE */
908
wpa_debug_reopen_file(void)909 int wpa_debug_reopen_file(void)
910 {
911 #ifdef CONFIG_DEBUG_FILE
912 int rv;
913 char *tmp;
914
915 if (!last_path)
916 return 0; /* logfile not used */
917
918 tmp = os_strdup(last_path);
919 if (!tmp)
920 return -1;
921
922 wpa_debug_close_file();
923 rv = wpa_debug_open_file(tmp);
924 os_free(tmp);
925 return rv;
926 #else /* CONFIG_DEBUG_FILE */
927 return 0;
928 #endif /* CONFIG_DEBUG_FILE */
929 }
930
931
wpa_debug_open_file(const char * path)932 int wpa_debug_open_file(const char *path)
933 {
934 #ifdef CONFIG_DEBUG_FILE
935 int out_fd;
936
937 if (!path)
938 return 0;
939
940 if (last_path == NULL || os_strcmp(last_path, path) != 0) {
941 /* Save our path to enable re-open */
942 os_free(last_path);
943 last_path = os_strdup(path);
944 }
945
946 out_fd = open(path, O_CREAT | O_APPEND | O_WRONLY,
947 S_IRUSR | S_IWUSR | S_IRGRP);
948 if (out_fd < 0) {
949 wpa_printf(MSG_ERROR,
950 "%s: Failed to open output file descriptor, using standard output",
951 __func__);
952 return -1;
953 }
954
955 #ifdef __linux__
956 if (fcntl(out_fd, F_SETFD, FD_CLOEXEC) < 0) {
957 wpa_printf(MSG_DEBUG,
958 "%s: Failed to set FD_CLOEXEC - continue without: %s",
959 __func__, strerror(errno));
960 }
961 #endif /* __linux__ */
962
963 out_file = fdopen(out_fd, "a");
964 if (out_file == NULL) {
965 wpa_printf(MSG_ERROR, "wpa_debug_open_file: Failed to open "
966 "output file, using standard output");
967 close(out_fd);
968 return -1;
969 }
970 #ifndef _WIN32
971 setvbuf(out_file, NULL, _IOLBF, 0);
972 #endif /* _WIN32 */
973 #else /* CONFIG_DEBUG_FILE */
974 (void)path;
975 #endif /* CONFIG_DEBUG_FILE */
976 return 0;
977 }
978
979
wpa_debug_close_file(void)980 void wpa_debug_close_file(void)
981 {
982 #ifdef CONFIG_DEBUG_FILE
983 if (!out_file)
984 return;
985 fclose(out_file);
986 out_file = NULL;
987 os_free(last_path);
988 last_path = NULL;
989 #endif /* CONFIG_DEBUG_FILE */
990 }
991
992
wpa_debug_setup_stdout(void)993 void wpa_debug_setup_stdout(void) __attribute__((no_sanitize("cfi")))
994 {
995 #ifndef _WIN32
996 setvbuf(stdout, NULL, _IOLBF, 0);
997 #endif /* _WIN32 */
998 }
999
1000 #endif /* CONFIG_NO_STDOUT_DEBUG */
1001
1002
1003 #ifndef CONFIG_NO_WPA_MSG
1004 static wpa_msg_cb_func wpa_msg_cb = NULL;
1005
wpa_msg_register_cb(wpa_msg_cb_func func)1006 void wpa_msg_register_cb(wpa_msg_cb_func func)
1007 {
1008 wpa_msg_cb = func;
1009 }
1010
1011
1012 static wpa_msg_get_ifname_func wpa_msg_ifname_cb = NULL;
1013
wpa_msg_register_ifname_cb(wpa_msg_get_ifname_func func)1014 void wpa_msg_register_ifname_cb(wpa_msg_get_ifname_func func)
1015 {
1016 wpa_msg_ifname_cb = func;
1017 }
1018
1019
wpa_msg(void * ctx,int level,const char * fmt,...)1020 void wpa_msg(void *ctx, int level, const char *fmt, ...) __attribute__((no_sanitize("cfi")))
1021 {
1022 va_list ap;
1023 char *buf;
1024 int buflen;
1025 int len;
1026 char prefix[130];
1027
1028 va_start(ap, fmt);
1029 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
1030 va_end(ap);
1031
1032 buf = os_malloc(buflen);
1033 if (buf == NULL) {
1034 wpa_printf(MSG_ERROR, "wpa_msg: Failed to allocate message "
1035 "buffer");
1036 return;
1037 }
1038 va_start(ap, fmt);
1039 prefix[0] = '\0';
1040 if (wpa_msg_ifname_cb) {
1041 const char *ifname = wpa_msg_ifname_cb(ctx);
1042 if (ifname) {
1043 int res = os_snprintf(prefix, sizeof(prefix), "%s: ",
1044 ifname);
1045 if (os_snprintf_error(sizeof(prefix), res))
1046 prefix[0] = '\0';
1047 }
1048 }
1049 len = vsnprintf(buf, buflen, fmt, ap);
1050 va_end(ap);
1051 wpa_printf(level, "%s%s", prefix, get_anonymized_result_setnetwork_for_bssid(buf));
1052 if (wpa_msg_cb)
1053 wpa_msg_cb(ctx, level, WPA_MSG_PER_INTERFACE, buf, len);
1054 bin_clear_free(buf, buflen);
1055 }
1056
wpa_msg_only_for_cb(void * ctx,int level,const char * fmt,...)1057 void wpa_msg_only_for_cb(void *ctx, int level, const char *fmt, ...) __attribute__((no_sanitize("cfi")))
1058 {
1059 va_list ap;
1060 char *buf;
1061 int buflen;
1062 int len;
1063 char prefix[130];
1064
1065 va_start(ap, fmt);
1066 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
1067 va_end(ap);
1068
1069 buf = os_malloc(buflen);
1070 if (buf == NULL) {
1071 wpa_printf(MSG_ERROR, "wpa_msg: Failed to allocate message "
1072 "buffer");
1073 return;
1074 }
1075 va_start(ap, fmt);
1076 prefix[0] = '\0';
1077 if (wpa_msg_ifname_cb) {
1078 const char *ifname = wpa_msg_ifname_cb(ctx);
1079 if (ifname) {
1080 int res = os_snprintf(prefix, sizeof(prefix), "%s: ",
1081 ifname);
1082 if (os_snprintf_error(sizeof(prefix), res))
1083 prefix[0] = '\0';
1084 }
1085 }
1086 len = vsnprintf(buf, buflen, fmt, ap);
1087 va_end(ap);
1088 if (wpa_msg_cb)
1089 wpa_msg_cb(ctx, level, WPA_MSG_PER_INTERFACE, buf, len);
1090 bin_clear_free(buf, buflen);
1091 }
1092
1093
wpa_msg_ctrl(void * ctx,int level,const char * fmt,...)1094 void wpa_msg_ctrl(void *ctx, int level, const char *fmt, ...) __attribute__((no_sanitize("cfi")))
1095 {
1096 va_list ap;
1097 char *buf;
1098 int buflen;
1099 int len;
1100
1101 if (!wpa_msg_cb)
1102 return;
1103
1104 va_start(ap, fmt);
1105 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
1106 va_end(ap);
1107
1108 buf = os_malloc(buflen);
1109 if (buf == NULL) {
1110 wpa_printf(MSG_ERROR, "wpa_msg_ctrl: Failed to allocate "
1111 "message buffer");
1112 return;
1113 }
1114 va_start(ap, fmt);
1115 len = vsnprintf(buf, buflen, fmt, ap);
1116 va_end(ap);
1117 wpa_msg_cb(ctx, level, WPA_MSG_PER_INTERFACE, buf, len);
1118 bin_clear_free(buf, buflen);
1119 }
1120
1121
wpa_msg_global(void * ctx,int level,const char * fmt,...)1122 void wpa_msg_global(void *ctx, int level, const char *fmt, ...) __attribute__((no_sanitize("cfi")))
1123 {
1124 va_list ap;
1125 char *buf;
1126 int buflen;
1127 int len;
1128
1129 va_start(ap, fmt);
1130 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
1131 va_end(ap);
1132
1133 buf = os_malloc(buflen);
1134 if (buf == NULL) {
1135 wpa_printf(MSG_ERROR, "wpa_msg_global: Failed to allocate "
1136 "message buffer");
1137 return;
1138 }
1139 va_start(ap, fmt);
1140 len = vsnprintf(buf, buflen, fmt, ap);
1141 va_end(ap);
1142 if (wpa_msg_cb)
1143 wpa_msg_cb(ctx, level, WPA_MSG_GLOBAL, buf, len);
1144 bin_clear_free(buf, buflen);
1145 }
1146
1147
wpa_msg_global_ctrl(void * ctx,int level,const char * fmt,...)1148 void wpa_msg_global_ctrl(void *ctx, int level, const char *fmt, ...) __attribute__((no_sanitize("cfi")))
1149 {
1150 va_list ap;
1151 char *buf;
1152 int buflen;
1153 int len;
1154
1155 if (!wpa_msg_cb)
1156 return;
1157
1158 va_start(ap, fmt);
1159 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
1160 va_end(ap);
1161
1162 buf = os_malloc(buflen);
1163 if (buf == NULL) {
1164 wpa_printf(MSG_ERROR,
1165 "wpa_msg_global_ctrl: Failed to allocate message buffer");
1166 return;
1167 }
1168 va_start(ap, fmt);
1169 len = vsnprintf(buf, buflen, fmt, ap);
1170 va_end(ap);
1171 wpa_msg_cb(ctx, level, WPA_MSG_GLOBAL, buf, len);
1172 bin_clear_free(buf, buflen);
1173 }
1174
1175
wpa_msg_no_global(void * ctx,int level,const char * fmt,...)1176 void wpa_msg_no_global(void *ctx, int level, const char *fmt, ...) __attribute__((no_sanitize("cfi")))
1177 {
1178 va_list ap;
1179 char *buf;
1180 int buflen;
1181 int len;
1182
1183 va_start(ap, fmt);
1184 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
1185 va_end(ap);
1186
1187 buf = os_malloc(buflen);
1188 if (buf == NULL) {
1189 wpa_printf(MSG_ERROR, "wpa_msg_no_global: Failed to allocate "
1190 "message buffer");
1191 return;
1192 }
1193 va_start(ap, fmt);
1194 len = vsnprintf(buf, buflen, fmt, ap);
1195 va_end(ap);
1196 if (wpa_msg_cb)
1197 wpa_msg_cb(ctx, level, WPA_MSG_NO_GLOBAL, buf, len);
1198 bin_clear_free(buf, buflen);
1199 }
1200
1201
wpa_msg_global_only(void * ctx,int level,const char * fmt,...)1202 void wpa_msg_global_only(void *ctx, int level, const char *fmt, ...)
1203 {
1204 va_list ap;
1205 char *buf;
1206 int buflen;
1207 int len;
1208
1209 va_start(ap, fmt);
1210 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
1211 va_end(ap);
1212
1213 buf = os_malloc(buflen);
1214 if (buf == NULL) {
1215 wpa_printf(MSG_ERROR, "%s: Failed to allocate message buffer",
1216 __func__);
1217 return;
1218 }
1219 va_start(ap, fmt);
1220 len = vsnprintf(buf, buflen, fmt, ap);
1221 va_end(ap);
1222 wpa_printf(level, "%s", get_anonymized_result_setnetwork_for_bssid(buf));
1223 if (wpa_msg_cb)
1224 wpa_msg_cb(ctx, level, WPA_MSG_ONLY_GLOBAL, buf, len);
1225 os_free(buf);
1226 }
1227
1228 #endif /* CONFIG_NO_WPA_MSG */
1229
1230
1231 #ifndef CONFIG_NO_HOSTAPD_LOGGER
1232 static hostapd_logger_cb_func hostapd_logger_cb = NULL;
1233
hostapd_logger_register_cb(hostapd_logger_cb_func func)1234 void hostapd_logger_register_cb(hostapd_logger_cb_func func)
1235 {
1236 hostapd_logger_cb = func;
1237 }
1238
1239
hostapd_logger(void * ctx,const u8 * addr,unsigned int module,int level,const char * fmt,...)1240 void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
1241 const char *fmt, ...)
1242 {
1243 va_list ap;
1244 char *buf;
1245 int buflen;
1246 int len;
1247
1248 va_start(ap, fmt);
1249 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
1250 va_end(ap);
1251
1252 buf = os_malloc(buflen);
1253 if (buf == NULL) {
1254 wpa_printf(MSG_ERROR, "hostapd_logger: Failed to allocate "
1255 "message buffer");
1256 return;
1257 }
1258 va_start(ap, fmt);
1259 len = vsnprintf(buf, buflen, fmt, ap);
1260 va_end(ap);
1261 if (hostapd_logger_cb)
1262 hostapd_logger_cb(ctx, addr, module, level, buf, len);
1263 else if (addr)
1264 wpa_printf(MSG_DEBUG, "hostapd_logger: STA " MACSTR_SEC " - %s",
1265 MAC2STR_SEC(addr), get_anonymized_result_setnetwork_for_bssid(buf));
1266 else
1267 wpa_printf(MSG_DEBUG, "hostapd_logger: %s", get_anonymized_result_setnetwork_for_bssid(buf));
1268 bin_clear_free(buf, buflen);
1269 }
1270
hostapd_logger_only_for_cb(void * ctx,const u8 * addr,unsigned int module,int level,const char * fmt,...)1271 void hostapd_logger_only_for_cb(void *ctx, const u8 *addr, unsigned int module, int level,
1272 const char *fmt, ...)
1273 {
1274 va_list ap;
1275 char *buf;
1276 int buflen;
1277 int len;
1278
1279 va_start(ap, fmt);
1280 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
1281 va_end(ap);
1282
1283 buf = os_malloc(buflen);
1284 if (buf == NULL) {
1285 wpa_printf(MSG_ERROR, "hostapd_logger: Failed to allocate "
1286 "message buffer");
1287 return;
1288 }
1289 va_start(ap, fmt);
1290 len = vsnprintf(buf, buflen, fmt, ap);
1291 va_end(ap);
1292 if (hostapd_logger_cb) {
1293 hostapd_logger_cb(ctx, addr, module, level, buf, len);
1294 }
1295 bin_clear_free(buf, buflen);
1296 }
1297
1298 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
1299
1300
debug_level_str(int level)1301 const char * debug_level_str(int level)
1302 {
1303 switch (level) {
1304 case MSG_EXCESSIVE:
1305 return "EXCESSIVE";
1306 case MSG_MSGDUMP:
1307 return "MSGDUMP";
1308 case MSG_DEBUG:
1309 return "DEBUG";
1310 case MSG_INFO:
1311 return "INFO";
1312 case MSG_WARNING:
1313 return "WARNING";
1314 case MSG_ERROR:
1315 return "ERROR";
1316 default:
1317 return "?";
1318 }
1319 }
1320
1321
str_to_debug_level(const char * s)1322 int str_to_debug_level(const char *s)
1323 {
1324 if (os_strcasecmp(s, "EXCESSIVE") == 0)
1325 return MSG_EXCESSIVE;
1326 if (os_strcasecmp(s, "MSGDUMP") == 0)
1327 return MSG_MSGDUMP;
1328 if (os_strcasecmp(s, "DEBUG") == 0)
1329 return MSG_DEBUG;
1330 if (os_strcasecmp(s, "INFO") == 0)
1331 return MSG_INFO;
1332 if (os_strcasecmp(s, "WARNING") == 0)
1333 return MSG_WARNING;
1334 if (os_strcasecmp(s, "ERROR") == 0)
1335 return MSG_ERROR;
1336 return -1;
1337 }
1338