wpa_supplicant: Add -G argument to specify global ctrl group
[mech_eap.git] / wpa_supplicant / main.c
1 /*
2  * WPA Supplicant / main() function for UNIX like OSes and MinGW
3  * Copyright (c) 2003-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 #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                "        [-G<group>] \\\n"
29                "        -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] "
30                "[-p<driver_param>] \\\n"
31                "        [-b<br_ifname>] [-f<debug file>] [-e<entropy file>] "
32                "\\\n"
33                "        [-o<override driver>] [-O<override ctrl>] \\\n"
34                "        [-N -i<ifname> -c<conf> [-C<ctrl>] "
35                "[-D<driver>] \\\n"
36                "        [-p<driver_param>] [-b<br_ifname>] [-I<config file>] "
37                "...]\n"
38                "\n"
39                "drivers:\n",
40                wpa_supplicant_version, wpa_supplicant_license);
41
42         for (i = 0; wpa_drivers[i]; i++) {
43                 printf("  %s = %s\n",
44                        wpa_drivers[i]->name,
45                        wpa_drivers[i]->desc);
46         }
47
48 #ifndef CONFIG_NO_STDOUT_DEBUG
49         printf("options:\n"
50                "  -b = optional bridge interface name\n"
51                "  -B = run daemon in the background\n"
52                "  -c = Configuration file\n"
53                "  -C = ctrl_interface parameter (only used if -c is not)\n"
54                "  -i = interface name\n"
55                "  -I = additional configuration file\n"
56                "  -d = increase debugging verbosity (-dd even more)\n"
57                "  -D = driver name (can be multiple drivers: nl80211,wext)\n"
58                "  -e = entropy file\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                "  -G = global ctrl_interface group\n"
64                "  -K = include keys (passwords, etc.) in debug output\n");
65 #ifdef CONFIG_DEBUG_SYSLOG
66         printf("  -s = log output to syslog instead of stdout\n");
67 #endif /* CONFIG_DEBUG_SYSLOG */
68 #ifdef CONFIG_DEBUG_LINUX_TRACING
69         printf("  -T = record to Linux tracing in addition to logging\n");
70         printf("       (records all messages regardless of debug verbosity)\n");
71 #endif /* CONFIG_DEBUG_LINUX_TRACING */
72         printf("  -t = include timestamp in debug messages\n"
73                "  -h = show this help text\n"
74                "  -L = show license (BSD)\n"
75                "  -o = override driver parameter for new interfaces\n"
76                "  -O = override ctrl_interface parameter for new interfaces\n"
77                "  -p = driver parameters\n"
78                "  -P = PID file\n"
79                "  -q = decrease debugging verbosity (-qq even less)\n");
80 #ifdef CONFIG_DBUS
81         printf("  -u = enable DBus control interface\n");
82 #endif /* CONFIG_DBUS */
83         printf("  -v = show version\n"
84                "  -W = wait for a control interface monitor before starting\n"
85                "  -N = start describing new interface\n");
86
87         printf("example:\n"
88                "  wpa_supplicant -D%s -iwlan0 -c/etc/wpa_supplicant.conf\n",
89                wpa_drivers[0] ? wpa_drivers[0]->name : "nl80211");
90 #endif /* CONFIG_NO_STDOUT_DEBUG */
91 }
92
93
94 static 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
108 static void wpa_supplicant_fd_workaround(int start)
109 {
110 #ifdef __linux__
111         static int fd[3] = { -1, -1, -1 };
112         int i;
113         /* When started from pcmcia-cs scripts, wpa_supplicant might start with
114          * fd 0, 1, and 2 closed. This will cause some issues because many
115          * places in wpa_supplicant are still printing out to stdout. As a
116          * workaround, make sure that fd's 0, 1, and 2 are not used for other
117          * sockets. */
118         if (start) {
119                 for (i = 0; i < 3; i++) {
120                         fd[i] = open("/dev/null", O_RDWR);
121                         if (fd[i] > 2) {
122                                 close(fd[i]);
123                                 fd[i] = -1;
124                                 break;
125                         }
126                 }
127         } else {
128                 for (i = 0; i < 3; i++) {
129                         if (fd[i] >= 0) {
130                                 close(fd[i]);
131                                 fd[i] = -1;
132                         }
133                 }
134         }
135 #endif /* __linux__ */
136 }
137
138
139 int main(int argc, char *argv[])
140 {
141         int c, i;
142         struct wpa_interface *ifaces, *iface;
143         int iface_count, exitcode = -1;
144         struct wpa_params params;
145         struct wpa_global *global;
146
147         if (os_program_init())
148                 return -1;
149
150         os_memset(&params, 0, sizeof(params));
151         params.wpa_debug_level = MSG_INFO;
152
153         iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
154         if (ifaces == NULL)
155                 return -1;
156         iface_count = 1;
157
158         wpa_supplicant_fd_workaround(1);
159
160         for (;;) {
161                 c = getopt(argc, argv,
162                            "b:Bc:C:D:de:f:g:G:hi:I:KLNo:O:p:P:qsTtuvW");
163                 if (c < 0)
164                         break;
165                 switch (c) {
166                 case 'b':
167                         iface->bridge_ifname = optarg;
168                         break;
169                 case 'B':
170                         params.daemonize++;
171                         break;
172                 case 'c':
173                         iface->confname = optarg;
174                         break;
175                 case 'C':
176                         iface->ctrl_interface = optarg;
177                         break;
178                 case 'D':
179                         iface->driver = optarg;
180                         break;
181                 case 'd':
182 #ifdef CONFIG_NO_STDOUT_DEBUG
183                         printf("Debugging disabled with "
184                                "CONFIG_NO_STDOUT_DEBUG=y build time "
185                                "option.\n");
186                         goto out;
187 #else /* CONFIG_NO_STDOUT_DEBUG */
188                         params.wpa_debug_level--;
189                         break;
190 #endif /* CONFIG_NO_STDOUT_DEBUG */
191                 case 'e':
192                         params.entropy_file = optarg;
193                         break;
194 #ifdef CONFIG_DEBUG_FILE
195                 case 'f':
196                         params.wpa_debug_file_path = optarg;
197                         break;
198 #endif /* CONFIG_DEBUG_FILE */
199                 case 'g':
200                         params.ctrl_interface = optarg;
201                         break;
202                 case 'G':
203                         params.ctrl_interface_group = optarg;
204                         break;
205                 case 'h':
206                         usage();
207                         exitcode = 0;
208                         goto out;
209                 case 'i':
210                         iface->ifname = optarg;
211                         break;
212                 case 'I':
213                         iface->confanother = optarg;
214                         break;
215                 case 'K':
216                         params.wpa_debug_show_keys++;
217                         break;
218                 case 'L':
219                         license();
220                         exitcode = 0;
221                         goto out;
222                 case 'o':
223                         params.override_driver = optarg;
224                         break;
225                 case 'O':
226                         params.override_ctrl_interface = optarg;
227                         break;
228                 case 'p':
229                         iface->driver_param = optarg;
230                         break;
231                 case 'P':
232                         os_free(params.pid_file);
233                         params.pid_file = os_rel2abs_path(optarg);
234                         break;
235                 case 'q':
236                         params.wpa_debug_level++;
237                         break;
238 #ifdef CONFIG_DEBUG_SYSLOG
239                 case 's':
240                         params.wpa_debug_syslog++;
241                         break;
242 #endif /* CONFIG_DEBUG_SYSLOG */
243 #ifdef CONFIG_DEBUG_LINUX_TRACING
244                 case 'T':
245                         params.wpa_debug_tracing++;
246                         break;
247 #endif /* CONFIG_DEBUG_LINUX_TRACING */
248                 case 't':
249                         params.wpa_debug_timestamp++;
250                         break;
251 #ifdef CONFIG_DBUS
252                 case 'u':
253                         params.dbus_ctrl_interface = 1;
254                         break;
255 #endif /* CONFIG_DBUS */
256                 case 'v':
257                         printf("%s\n", wpa_supplicant_version);
258                         exitcode = 0;
259                         goto out;
260                 case 'W':
261                         params.wait_for_monitor++;
262                         break;
263                 case 'N':
264                         iface_count++;
265                         iface = os_realloc_array(ifaces, iface_count,
266                                                  sizeof(struct wpa_interface));
267                         if (iface == NULL)
268                                 goto out;
269                         ifaces = iface;
270                         iface = &ifaces[iface_count - 1]; 
271                         os_memset(iface, 0, sizeof(*iface));
272                         break;
273                 default:
274                         usage();
275                         exitcode = 0;
276                         goto out;
277                 }
278         }
279
280         exitcode = 0;
281         global = wpa_supplicant_init(&params);
282         if (global == NULL) {
283                 wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
284                 exitcode = -1;
285                 goto out;
286         } else {
287                 wpa_printf(MSG_INFO, "Successfully initialized "
288                            "wpa_supplicant");
289         }
290
291         for (i = 0; exitcode == 0 && i < iface_count; i++) {
292                 if ((ifaces[i].confname == NULL &&
293                      ifaces[i].ctrl_interface == NULL) ||
294                     ifaces[i].ifname == NULL) {
295                         if (iface_count == 1 && (params.ctrl_interface ||
296                                                  params.dbus_ctrl_interface))
297                                 break;
298                         usage();
299                         exitcode = -1;
300                         break;
301                 }
302                 if (wpa_supplicant_add_iface(global, &ifaces[i]) == NULL)
303                         exitcode = -1;
304         }
305
306         if (exitcode == 0)
307                 exitcode = wpa_supplicant_run(global);
308
309         wpa_supplicant_deinit(global);
310
311 out:
312         wpa_supplicant_fd_workaround(0);
313         os_free(ifaces);
314         os_free(params.pid_file);
315
316         os_program_deinit();
317
318         return exitcode;
319 }