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