-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiwscand.c
348 lines (311 loc) · 8.47 KB
/
iwscand.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* Based on
* Wireless Tools
* by
* Jean II - HPLB '99 - HPL 99->07
*
* This tool can access various piece of information on the card
* not part of iwconfig...
*
* This file is released under the GPL license.
* Copyright (c) 1997-2007 Jean Tourrilhes <jt@hpl.hp.com>
* Disfigured in 2016 by Sergey Ptashnick <0comffdiz@inbox.ru>
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <syslog.h>
#include <time.h>
#include "iwscan.h"
#define DEFAULT_PID_FILE ( IWSCAND_PID_FILE_DIR "/iwscand.pid" )
#define MYNAME "iwscand"
#define DEVNULL "/dev/null"
#define PAR_PIDFILE 0x1
#define PAR_NONBLOCK 0x2
#define PAR_SAFE 0x4
#define STATE_FREE 0x0
#define STATE_WORK 0x1
#define STATE_STOP 0x2
int global_state = 0;
static void iwscand_usage(int status)
{
FILE * fd = status ? stderr : stdout;
fprintf(fd, "\nUsage: " MYNAME " <INTERFACE> to <FILE> [safe|nonblock] "
"[essid <ESSID>] [pid [<PIDFILE>]]\n\n");
fprintf(fd, "Note that \"essid\" parameter may not work (depends on your card' driver).\n");
fprintf(fd, "If you specify \"pid\" as last parameter, <PIDFILE> will be \"%s\".\n\n",
DEFAULT_PID_FILE);
exit(status);
}
static void iwscand_version(void)
{
fprintf(stdout, "\n" MYNAME " version " VERSION "\n\n");
exit(0);
}
void iwscand_works(int signal)
{
switch (signal)
{
case SIGUSR1:
global_state |= STATE_WORK;
break;
case SIGTERM:
global_state |= STATE_STOP;
break;
}
}
int
main(int argc,
char ** argv)
{
int skfd; /* Generic raw socket descripto r*/
char *dev; /* Device name */
char **args; /* Command arguments */
int params = 0; /* Parameter flags */
char *out_fname = NULL; /* Output file name */
int fnlen; /* File name length */
char *pid_fname = NULL; /* Name of PID file */
char *pid_fname_real = NULL; /* Real name of PID file - this allow to use
* . and .. in pid_fname and unlink
* such files after chdir */
char pid_fname_default[] = DEFAULT_PID_FILE; /* Source for strcpy - */
/* this allows us to free(pid_fname) */
char **argt; /* temporary pointer for fname search */
int count, cnt; /* Number of arguments */
FILE * fd, * pd; /* fds for output file and for pid file */
int goterr = 0; /* Error indicator */
pid_t pid; /* For checks after forking */
struct timespec cycle_wait = {.tv_sec = 0, .tv_nsec = 10000000 }; /* 10 ms */
char stamp_buf[IWS_TIMESTAMP_LENGTH];
/* Those don't apply to all interfaces */
if((argc == 2) && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")))
iwscand_usage(0);
if((argc == 2) && (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")))
iwscand_version();
if(argc < 4)
{
iwscand_usage(-1);
}
else
{
dev = argv[1];
args = argv + 2;
count = argc - 2;
argt = args;
cnt = count;
while (cnt > 0)
{
cnt--;
if (!strcmp(argt[0], "to"))
{
if (cnt < 1)
{
fprintf(stderr, MYNAME
": too few arguments for scanning option [%s]\n",
argt[0]);
iwscand_usage(-1);
}
argt++;
cnt--;
fnlen = strlen(argt[0]);
out_fname = malloc(fnlen+1);
strcpy(out_fname, argt[0]);
}
else if (!strcmp(argt[0], "pid"))
{
if (cnt < 1)
{
fnlen = strlen(pid_fname_default);
pid_fname = malloc(fnlen+1);
strcpy(pid_fname, pid_fname_default);
}
else
{
argt++;
cnt--;
fnlen = strlen(argt[0]);
pid_fname = malloc(fnlen+1);
strcpy(pid_fname, argt[0]);
}
params |= PAR_PIDFILE;
}
else if (!strcmp(argt[0], "safe"))
{
if (params & PAR_NONBLOCK)
{
fprintf(stderr, MYNAME
": paramaters \"safe\" and \"nonblock\" are mutually "
"exclusive.\n");
iwscand_usage(-1);
}
params |= PAR_SAFE;
}
else if (!strcmp(argt[0], "nonblock"))
{
if (params & PAR_SAFE)
{
fprintf(stderr, MYNAME
": paramaters \"safe\" and \"nonblock\" are mutually "
"exclusive.\n");
iwscand_usage(-1);
}
params |= PAR_NONBLOCK;
}
argt++;
}
}
/* Protecting from some strange people */
if (!strcmp(dev, IWSCAN_ALL_IFACES))
{
fprintf(stderr, MYNAME ": scanning all interfaces are not allowed by "
MYNAME ". Try to use iwscan.\n");
iwscand_usage(-1);
}
/* Open output file */
if (out_fname)
{
if (params & PAR_SAFE)
unlink(out_fname);
if (params & PAR_NONBLOCK)
{
/* Trying to open file (FIFO?) in non-blocking mode */
skfd = open(out_fname, O_CREAT|O_TRUNC|O_WRONLY|O_NONBLOCK);
goterr = (skfd < 0);
if (!goterr)
{
fd = fdopen(skfd, "w");
goterr = (fd == NULL);
skfd = -1;
}
}
else
{
fd = fopen(out_fname, "w");
goterr = (fd == NULL);
}
if (goterr)
{
perror(MYNAME ": cannot open output file");
return -1;
}
free(out_fname);
}
else
{
fprintf(stderr, MYNAME ": output file (\"to FILE\") is mandatory commandline "
"parameter!\n");
iwscand_usage(-1);
}
if (params & PAR_PIDFILE)
{
unlink(pid_fname); /* Uhmmm, TOCTOU, again? */
pd = fopen(pid_fname, "w");
if (!pd)
{
fclose(fd);
perror(MYNAME ": cannot open PID file");
return -1;
}
/* I hope OS will allow us to resolve name at this stage... */
pid_fname_real = realpath(pid_fname, NULL);
free(pid_fname);
if (!pid_fname_real)
{
fclose(fd);
fclose(pd);
perror(MYNAME ": cannot resolve PID file name");
return -1;
}
}
/* Create a channel to the NET kernel. */
skfd = iw_sockets_open();
if(skfd < 0)
{
perror(MYNAME ": cannot open IW socket");
goterr = 1;
goto cleanup;
}
/* Daemonize */
/* First fork */
pid = fork();
if (pid < 0)
{
goterr = 1;
goto cleanup;
}
if (pid > 0)
return 0;
/* Set SID */
if (setsid() < 0)
{
goterr = 1;
goto cleanup;
}
/* Set signal handlers */
signal(SIGUSR1, iwscand_works);
signal(SIGTERM, iwscand_works);
/* Second fork */
pid = fork();
if (pid < 0)
{
goterr = 1;
goto cleanup;
}
if (pid > 0)
return 0;
/* Setting umask and dir */
umask(0);
if (chdir("/") < 0)
{
goterr = 1;
goto cleanup;
}
/* "Close" 0, 1 and 2 without UB */
/* print_scanning_info_xml may write to stderr */
freopen(DEVNULL, "r", stdin);
freopen(DEVNULL, "w", stdout);
freopen(DEVNULL, "w", stderr);
/* Dump our PID to pidfile and close it */
if (params & PAR_PIDFILE)
{
pid = getpid();
fprintf(pd, "%ld\n", (long) pid);
fclose(pd);
}
/* Set timestamp */
iws_format_timestamp((char*) &stamp_buf);
/* We're ready. Let's tell about that! */
fprintf(fd, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
fprintf(fd, "<Scan stamp='%s'>\n", (char *) &stamp_buf);
fflush(fd);
/* do the actual work */
while (!(global_state & STATE_STOP))
{
if (global_state & STATE_WORK)
{
global_state = STATE_FREE;
goterr = iws_print_scanning_info_xml(fd, skfd, dev, args, count);
fprintf(fd, "\n");
fflush(fd);
}
nanosleep(&cycle_wait, NULL);
}
fprintf(fd, "</Scan>\n");
goterr = 0;
cleanup:
/* Close the socket */
if (!(skfd < 0)) /* If there is no error, of course */
iw_sockets_close(skfd);
/* Close output file */
if (fd)
fclose(fd);
/* Remove PID file */
if (params | PAR_PIDFILE)
{
unlink(pid_fname_real);
free(pid_fname_real);
}
return -goterr;
}