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