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