hostapd: Fix internal crypto build without TLS
[wpasupplicant] / wpa_supplicant / main.c
1 /*
2  * WPA Supplicant / main() function for UNIX like OSes and MinGW
3  * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16 #ifdef __linux__
17 #include <fcntl.h>
18 #endif /* __linux__ */
19
20 #include "common.h"
21 #include "wpa_supplicant_i.h"
22 #include "driver_i.h"
23
24 extern struct wpa_driver_ops *wpa_drivers[];
25
26
27 static void usage(void)
28 {
29         int i;
30         printf("%s\n\n%s\n"
31                "usage:\n"
32                "  wpa_supplicant [-BddhKLqqstuvW] [-P<pid file>] "
33                "[-g<global ctrl>] \\\n"
34                "        -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] "
35                "[-p<driver_param>] \\\n"
36                "        [-b<br_ifname>] [-f<debug file>] \\\n"
37                "        [-N -i<ifname> -c<conf> [-C<ctrl>] "
38                "[-D<driver>] \\\n"
39                "        [-p<driver_param>] [-b<br_ifname>] ...]\n"
40                "\n"
41                "drivers:\n",
42                wpa_supplicant_version, wpa_supplicant_license);
43
44         for (i = 0; wpa_drivers[i]; i++) {
45                 printf("  %s = %s\n",
46                        wpa_drivers[i]->name,
47                        wpa_drivers[i]->desc);
48         }
49
50 #ifndef CONFIG_NO_STDOUT_DEBUG
51         printf("options:\n"
52                "  -b = optional bridge interface name\n"
53                "  -B = run daemon in the background\n"
54                "  -c = Configuration file\n"
55                "  -C = ctrl_interface parameter (only used if -c is not)\n"
56                "  -i = interface name\n"
57                "  -d = increase debugging verbosity (-dd even more)\n"
58                "  -D = driver name (can be multiple drivers: nl80211,wext)\n");
59 #ifdef CONFIG_DEBUG_FILE
60         printf("  -f = log output to debug file instead of stdout\n");
61 #endif /* CONFIG_DEBUG_FILE */
62         printf("  -g = global ctrl_interface\n"
63                "  -K = include keys (passwords, etc.) in debug output\n");
64 #ifdef CONFIG_DEBUG_SYSLOG
65         printf("  -s = log output to syslog instead of stdout\n");
66 #endif /* CONFIG_DEBUG_SYSLOG */
67         printf("  -t = include timestamp in debug messages\n"
68                "  -h = show this help text\n"
69                "  -L = show license (GPL and BSD)\n"
70                "  -p = driver parameters\n"
71                "  -P = PID file\n"
72                "  -q = decrease debugging verbosity (-qq even less)\n");
73 #ifdef CONFIG_CTRL_IFACE_DBUS
74         printf("  -u = enable DBus control interface\n");
75 #endif /* CONFIG_CTRL_IFACE_DBUS */
76         printf("  -v = show version\n"
77                "  -W = wait for a control interface monitor before starting\n"
78                "  -N = start describing new interface\n");
79
80         printf("example:\n"
81                "  wpa_supplicant -D%s -iwlan0 -c/etc/wpa_supplicant.conf\n",
82                wpa_drivers[i] ? wpa_drivers[i]->name : "wext");
83 #endif /* CONFIG_NO_STDOUT_DEBUG */
84 }
85
86
87 static void license(void)
88 {
89 #ifndef CONFIG_NO_STDOUT_DEBUG
90         printf("%s\n\n%s%s%s%s%s\n",
91                wpa_supplicant_version,
92                wpa_supplicant_full_license1,
93                wpa_supplicant_full_license2,
94                wpa_supplicant_full_license3,
95                wpa_supplicant_full_license4,
96                wpa_supplicant_full_license5);
97 #endif /* CONFIG_NO_STDOUT_DEBUG */
98 }
99
100
101 static void wpa_supplicant_fd_workaround(void)
102 {
103 #ifdef __linux__
104         int s, i;
105         /* When started from pcmcia-cs scripts, wpa_supplicant might start with
106          * fd 0, 1, and 2 closed. This will cause some issues because many
107          * places in wpa_supplicant are still printing out to stdout. As a
108          * workaround, make sure that fd's 0, 1, and 2 are not used for other
109          * sockets. */
110         for (i = 0; i < 3; i++) {
111                 s = open("/dev/null", O_RDWR);
112                 if (s > 2) {
113                         close(s);
114                         break;
115                 }
116         }
117 #endif /* __linux__ */
118 }
119
120
121 int main(int argc, char *argv[])
122 {
123         int c, i;
124         struct wpa_interface *ifaces, *iface;
125         int iface_count, exitcode = -1;
126         struct wpa_params params;
127         struct wpa_global *global;
128
129         if (os_program_init())
130                 return -1;
131
132         os_memset(&params, 0, sizeof(params));
133         params.wpa_debug_level = MSG_INFO;
134
135         iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
136         if (ifaces == NULL)
137                 return -1;
138         iface_count = 1;
139
140         wpa_supplicant_fd_workaround();
141
142         for (;;) {
143                 c = getopt(argc, argv, "b:Bc:C:D:df:g:hi:KLNp:P:qstuvW");
144                 if (c < 0)
145                         break;
146                 switch (c) {
147                 case 'b':
148                         iface->bridge_ifname = optarg;
149                         break;
150                 case 'B':
151                         params.daemonize++;
152                         break;
153                 case 'c':
154                         iface->confname = optarg;
155                         break;
156                 case 'C':
157                         iface->ctrl_interface = optarg;
158                         break;
159                 case 'D':
160                         iface->driver = optarg;
161                         break;
162                 case 'd':
163 #ifdef CONFIG_NO_STDOUT_DEBUG
164                         printf("Debugging disabled with "
165                                "CONFIG_NO_STDOUT_DEBUG=y build time "
166                                "option.\n");
167                         goto out;
168 #else /* CONFIG_NO_STDOUT_DEBUG */
169                         params.wpa_debug_level--;
170                         break;
171 #endif /* CONFIG_NO_STDOUT_DEBUG */
172 #ifdef CONFIG_DEBUG_FILE
173                 case 'f':
174                         params.wpa_debug_file_path = optarg;
175                         break;
176 #endif /* CONFIG_DEBUG_FILE */
177                 case 'g':
178                         params.ctrl_interface = optarg;
179                         break;
180                 case 'h':
181                         usage();
182                         exitcode = 0;
183                         goto out;
184                 case 'i':
185                         iface->ifname = optarg;
186                         break;
187                 case 'K':
188                         params.wpa_debug_show_keys++;
189                         break;
190                 case 'L':
191                         license();
192                         exitcode = 0;
193                         goto out;
194                 case 'p':
195                         iface->driver_param = optarg;
196                         break;
197                 case 'P':
198                         os_free(params.pid_file);
199                         params.pid_file = os_rel2abs_path(optarg);
200                         break;
201                 case 'q':
202                         params.wpa_debug_level++;
203                         break;
204 #ifdef CONFIG_DEBUG_SYSLOG
205                 case 's':
206                         params.wpa_debug_syslog++;
207                         break;
208 #endif /* CONFIG_DEBUG_SYSLOG */
209                 case 't':
210                         params.wpa_debug_timestamp++;
211                         break;
212 #ifdef CONFIG_CTRL_IFACE_DBUS
213                 case 'u':
214                         params.dbus_ctrl_interface = 1;
215                         break;
216 #endif /* CONFIG_CTRL_IFACE_DBUS */
217                 case 'v':
218                         printf("%s\n", wpa_supplicant_version);
219                         exitcode = 0;
220                         goto out;
221                 case 'W':
222                         params.wait_for_monitor++;
223                         break;
224                 case 'N':
225                         iface_count++;
226                         iface = os_realloc(ifaces, iface_count *
227                                            sizeof(struct wpa_interface));
228                         if (iface == NULL)
229                                 goto out;
230                         ifaces = iface;
231                         iface = &ifaces[iface_count - 1]; 
232                         os_memset(iface, 0, sizeof(*iface));
233                         break;
234                 default:
235                         usage();
236                         exitcode = 0;
237                         goto out;
238                 }
239         }
240
241         exitcode = 0;
242         global = wpa_supplicant_init(&params);
243         if (global == NULL) {
244                 wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
245                 exitcode = -1;
246                 goto out;
247         }
248
249         for (i = 0; exitcode == 0 && i < iface_count; i++) {
250                 if ((ifaces[i].confname == NULL &&
251                      ifaces[i].ctrl_interface == NULL) ||
252                     ifaces[i].ifname == NULL) {
253                         if (iface_count == 1 && (params.ctrl_interface ||
254                                                  params.dbus_ctrl_interface))
255                                 break;
256                         usage();
257                         exitcode = -1;
258                         break;
259                 }
260                 if (wpa_supplicant_add_iface(global, &ifaces[i]) == NULL)
261                         exitcode = -1;
262         }
263
264         if (exitcode == 0)
265                 exitcode = wpa_supplicant_run(global);
266
267         wpa_supplicant_deinit(global);
268
269 out:
270         os_free(ifaces);
271         os_free(params.pid_file);
272
273         os_program_deinit();
274
275         return exitcode;
276 }