blob: 9e57679475e288edb52aff1663cbd08c179ba6b6 [file] [log] [blame]
Yasuyuki KOZAKAI52088062007-07-24 05:44:11 +00001/*
2 * (C) 2000-2006 by the netfilter coreteam <coreteam@netfilter.org>:
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000019#include <errno.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000020#include <fcntl.h>
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +000021#include <netdb.h>
Jan Engelhardtaafd2692008-01-20 13:19:40 +000022#include <stdarg.h>
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +000023#include <stdbool.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000024#include <stdio.h>
25#include <stdlib.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000026#include <string.h>
27#include <unistd.h>
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000028#include <sys/socket.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000029#include <sys/stat.h>
30#include <sys/types.h>
31#include <sys/wait.h>
Jan Engelhardt08b16162008-01-20 13:36:08 +000032#include <arpa/inet.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000033
Yasuyuki KOZAKAI52088062007-07-24 05:44:11 +000034#include <xtables.h>
Jan Engelhardtef18e812008-08-04 12:47:48 +020035#include <libiptc/libxtc.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000036
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000037#ifndef NO_SHARED_LIBS
38#include <dlfcn.h>
39#endif
40
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000041#define NPROTO 255
42
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000043#ifndef PROC_SYS_MODPROBE
44#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
45#endif
46
Jan Engelhardt39bf9c82009-01-27 15:59:06 +010047/* Search path for Xtables .so files */
48static const char *xtables_libdir;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000049
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000050/* the path to command to load kernel module */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +010051const char *xtables_modprobe_program;
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000052
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000053/* Keeping track of external matches and targets: linked lists. */
54struct xtables_match *xtables_matches;
55struct xtables_target *xtables_targets;
56
Jan Engelhardt39bf9c82009-01-27 15:59:06 +010057void xtables_init(void)
58{
59 xtables_libdir = getenv("XTABLES_LIBDIR");
60 if (xtables_libdir != NULL)
61 return;
62 xtables_libdir = getenv("IPTABLES_LIB_DIR");
63 if (xtables_libdir != NULL) {
64 fprintf(stderr, "IPTABLES_LIB_DIR is deprecated, "
65 "use XTABLES_LIBDIR.\n");
66 return;
67 }
68 xtables_libdir = XTABLES_LIBDIR;
69}
70
Jan Engelhardt630ef482009-01-27 14:58:41 +010071/**
72 * xtables_*alloc - wrappers that exit on failure
73 */
74void *xtables_calloc(size_t count, size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000075{
76 void *p;
77
78 if ((p = calloc(count, size)) == NULL) {
79 perror("ip[6]tables: calloc failed");
80 exit(1);
81 }
82
83 return p;
84}
85
Jan Engelhardt630ef482009-01-27 14:58:41 +010086void *xtables_malloc(size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000087{
88 void *p;
89
90 if ((p = malloc(size)) == NULL) {
91 perror("ip[6]tables: malloc failed");
92 exit(1);
93 }
94
95 return p;
96}
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000097
98static char *get_modprobe(void)
99{
100 int procfile;
101 char *ret;
102
103#define PROCFILE_BUFSIZ 1024
104 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
105 if (procfile < 0)
106 return NULL;
107
108 ret = (char *) malloc(PROCFILE_BUFSIZ);
109 if (ret) {
110 memset(ret, 0, PROCFILE_BUFSIZ);
111 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
112 case -1: goto fail;
113 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
114 }
115 if (ret[strlen(ret)-1]=='\n')
116 ret[strlen(ret)-1]=0;
117 close(procfile);
118 return ret;
119 }
120 fail:
121 free(ret);
122 close(procfile);
123 return NULL;
124}
125
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100126int xtables_insmod(const char *modname, const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000127{
128 char *buf = NULL;
129 char *argv[4];
130 int status;
131
132 /* If they don't explicitly set it, read out of kernel */
133 if (!modprobe) {
134 buf = get_modprobe();
135 if (!buf)
136 return -1;
137 modprobe = buf;
138 }
139
140 switch (fork()) {
141 case 0:
142 argv[0] = (char *)modprobe;
143 argv[1] = (char *)modname;
144 if (quiet) {
145 argv[2] = "-q";
146 argv[3] = NULL;
147 } else {
148 argv[2] = NULL;
149 argv[3] = NULL;
150 }
151 execv(argv[0], argv);
152
153 /* not usually reached */
154 exit(1);
155 case -1:
156 return -1;
157
158 default: /* parent */
159 wait(&status);
160 }
161
162 free(buf);
163 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
164 return 0;
165 return -1;
166}
167
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100168int xtables_load_ko(const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000169{
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100170 static bool loaded = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000171 static int ret = -1;
172
173 if (!loaded) {
174 ret = xtables_insmod(afinfo.kmod, modprobe, quiet);
175 loaded = (ret == 0);
176 }
177
178 return ret;
179}
180
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100181/**
182 * xtables_strtou{i,l} - string to number conversion
183 * @s: input string
184 * @end: like strtoul's "end" pointer
185 * @value: pointer for result
186 * @min: minimum accepted value
187 * @max: maximum accepted value
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000188 *
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100189 * If @end is NULL, we assume the caller wants a "strict strtoul", and hence
190 * "15a" is rejected.
191 * In either case, the value obtained is compared for min-max compliance.
192 * Base is always 0, i.e. autodetect depending on @s.
193 *
194 * Returns true/false whether number was accepted. On failure, *value has
195 * undefined contents.
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000196 */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100197bool xtables_strtoul(const char *s, char **end, unsigned long *value,
198 unsigned long min, unsigned long max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000199{
200 unsigned long v;
201 char *my_end;
202
203 errno = 0;
204 v = strtoul(s, &my_end, 0);
205
206 if (my_end == s)
207 return false;
208 if (end != NULL)
209 *end = my_end;
210
211 if (errno != ERANGE && min <= v && (max == 0 || v <= max)) {
212 if (value != NULL)
213 *value = v;
214 if (end == NULL)
215 return *my_end == '\0';
216 return true;
217 }
218
219 return false;
220}
221
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100222bool xtables_strtoui(const char *s, char **end, unsigned int *value,
223 unsigned int min, unsigned int max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000224{
225 unsigned long v;
226 bool ret;
227
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100228 ret = xtables_strtoul(s, end, &v, min, max);
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000229 if (value != NULL)
230 *value = v;
231 return ret;
232}
233
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000234int service_to_port(const char *name, const char *proto)
235{
236 struct servent *service;
237
238 if ((service = getservbyname(name, proto)) != NULL)
239 return ntohs((unsigned short) service->s_port);
240
241 return -1;
242}
243
244u_int16_t parse_port(const char *port, const char *proto)
245{
Jan Engelhardt7a236f42008-03-03 12:30:41 +0100246 unsigned int portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000247
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100248 if (xtables_strtoui(port, NULL, &portnum, 0, UINT16_MAX) ||
Max Kellermann9ee386a2008-01-29 13:48:05 +0000249 (portnum = service_to_port(port, proto)) != (unsigned)-1)
Jan Engelhardt213e1852009-01-27 17:24:34 +0100250 return portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000251
252 exit_error(PARAMETER_PROBLEM,
253 "invalid port/service `%s' specified", port);
254}
255
256void parse_interface(const char *arg, char *vianame, unsigned char *mask)
257{
258 int vialen = strlen(arg);
259 unsigned int i;
260
261 memset(mask, 0, IFNAMSIZ);
262 memset(vianame, 0, IFNAMSIZ);
263
264 if (vialen + 1 > IFNAMSIZ)
265 exit_error(PARAMETER_PROBLEM,
266 "interface name `%s' must be shorter than IFNAMSIZ"
267 " (%i)", arg, IFNAMSIZ-1);
268
269 strcpy(vianame, arg);
270 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
271 memset(mask, 0, IFNAMSIZ);
272 else if (vianame[vialen - 1] == '+') {
273 memset(mask, 0xFF, vialen - 1);
274 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
275 /* Don't remove `+' here! -HW */
276 } else {
277 /* Include nul-terminator in match */
278 memset(mask, 0xFF, vialen + 1);
279 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
280 for (i = 0; vianame[i]; i++) {
281 if (vianame[i] == ':' ||
282 vianame[i] == '!' ||
283 vianame[i] == '*') {
Max Kellermannaae4f822007-10-17 16:36:49 +0000284 fprintf(stderr,
285 "Warning: weird character in interface"
286 " `%s' (No aliases, :, ! or *).\n",
287 vianame);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000288 break;
289 }
290 }
291 }
292}
293
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200294#ifndef NO_SHARED_LIBS
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100295static void *load_extension(const char *search_path, const char *prefix,
296 const char *name, bool is_target)
297{
298 const char *dir = search_path, *next;
299 void *ptr = NULL;
300 struct stat sb;
301 char path[256];
302
303 do {
304 next = strchr(dir, ':');
305 if (next == NULL)
306 next = dir + strlen(dir);
307 snprintf(path, sizeof(path), "%.*s/libxt_%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200308 (unsigned int)(next - dir), dir, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100309
310 if (dlopen(path, RTLD_NOW) != NULL) {
311 /* Found library. If it didn't register itself,
312 maybe they specified target as match. */
313 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100314 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100315 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100316 ptr = xtables_find_match(name,
317 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100318 } else if (stat(path, &sb) == 0) {
319 fprintf(stderr, "%s: %s\n", path, dlerror());
320 }
321
322 if (ptr != NULL)
323 return ptr;
324
325 snprintf(path, sizeof(path), "%.*s/%s%s.so",
Jan Engelhardt2c0a0c92008-04-14 15:58:17 +0200326 (unsigned int)(next - dir), dir, prefix, name);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100327 if (dlopen(path, RTLD_NOW) != NULL) {
328 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100329 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100330 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100331 ptr = xtables_find_match(name,
332 XTF_DONT_LOAD, NULL);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100333 } else if (stat(path, &sb) == 0) {
334 fprintf(stderr, "%s: %s\n", path, dlerror());
335 }
336
337 if (ptr != NULL)
338 return ptr;
339
340 dir = next + 1;
341 } while (*next != '\0');
342
343 return NULL;
344}
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200345#endif
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100346
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100347struct xtables_match *
348xtables_find_match(const char *name, enum xtables_tryload tryload,
349 struct xtables_rule_match **matches)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000350{
351 struct xtables_match *ptr;
352 const char *icmp6 = "icmp6";
353
354 /* This is ugly as hell. Nonetheless, there is no way of changing
355 * this without hurting backwards compatibility */
356 if ( (strcmp(name,"icmpv6") == 0) ||
357 (strcmp(name,"ipv6-icmp") == 0) ||
358 (strcmp(name,"icmp6") == 0) )
359 name = icmp6;
360
361 for (ptr = xtables_matches; ptr; ptr = ptr->next) {
362 if (strcmp(name, ptr->name) == 0) {
363 struct xtables_match *clone;
364
365 /* First match of this type: */
366 if (ptr->m == NULL)
367 break;
368
369 /* Second and subsequent clones */
Jan Engelhardt630ef482009-01-27 14:58:41 +0100370 clone = xtables_malloc(sizeof(struct xtables_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000371 memcpy(clone, ptr, sizeof(struct xtables_match));
372 clone->mflags = 0;
373 /* This is a clone: */
374 clone->next = clone;
375
376 ptr = clone;
377 break;
378 }
379 }
380
381#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100382 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100383 ptr = load_extension(xtables_libdir, afinfo.libprefix,
384 name, false);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000385
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100386 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000387 exit_error(PARAMETER_PROBLEM,
388 "Couldn't load match `%s':%s\n",
389 name, dlerror());
390 }
391#else
392 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100393 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000394 ptr->loaded = 1;
395 else
396 ptr = NULL;
397 }
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100398 if(!ptr && (tryload == XTF_LOAD_MUST_SUCCEED)) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000399 exit_error(PARAMETER_PROBLEM,
400 "Couldn't find match `%s'\n", name);
401 }
402#endif
403
404 if (ptr && matches) {
405 struct xtables_rule_match **i;
406 struct xtables_rule_match *newentry;
407
Jan Engelhardt630ef482009-01-27 14:58:41 +0100408 newentry = xtables_malloc(sizeof(struct xtables_rule_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000409
410 for (i = matches; *i; i = &(*i)->next) {
411 if (strcmp(name, (*i)->match->name) == 0)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100412 (*i)->completed = true;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000413 }
414 newentry->match = ptr;
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100415 newentry->completed = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000416 newentry->next = NULL;
417 *i = newentry;
418 }
419
420 return ptr;
421}
422
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100423struct xtables_target *
424xtables_find_target(const char *name, enum xtables_tryload tryload)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000425{
426 struct xtables_target *ptr;
427
428 /* Standard target? */
429 if (strcmp(name, "") == 0
430 || strcmp(name, XTC_LABEL_ACCEPT) == 0
431 || strcmp(name, XTC_LABEL_DROP) == 0
432 || strcmp(name, XTC_LABEL_QUEUE) == 0
433 || strcmp(name, XTC_LABEL_RETURN) == 0)
434 name = "standard";
435
436 for (ptr = xtables_targets; ptr; ptr = ptr->next) {
437 if (strcmp(name, ptr->name) == 0)
438 break;
439 }
440
441#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100442 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100443 ptr = load_extension(xtables_libdir, afinfo.libprefix,
444 name, true);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000445
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100446 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000447 exit_error(PARAMETER_PROBLEM,
448 "Couldn't load target `%s':%s\n",
449 name, dlerror());
450 }
451#else
452 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100453 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000454 ptr->loaded = 1;
455 else
456 ptr = NULL;
457 }
458 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
459 exit_error(PARAMETER_PROBLEM,
460 "Couldn't find target `%s'\n", name);
461 }
462#endif
463
464 if (ptr)
465 ptr->used = 1;
466
467 return ptr;
468}
469
470static int compatible_revision(const char *name, u_int8_t revision, int opt)
471{
472 struct xt_get_revision rev;
473 socklen_t s = sizeof(rev);
474 int max_rev, sockfd;
475
476 sockfd = socket(afinfo.family, SOCK_RAW, IPPROTO_RAW);
477 if (sockfd < 0) {
Patrick McHardydf1ef382007-12-03 15:32:28 +0000478 if (errno == EPERM) {
479 /* revision 0 is always supported. */
480 if (revision != 0)
481 fprintf(stderr, "Could not determine whether "
482 "revision %u is supported, "
483 "assuming it is.\n",
484 revision);
485 return 1;
486 }
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000487 fprintf(stderr, "Could not open socket to kernel: %s\n",
488 strerror(errno));
489 exit(1);
490 }
491
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100492 xtables_load_ko(xtables_modprobe_program, true);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000493
494 strcpy(rev.name, name);
495 rev.revision = revision;
496
497 max_rev = getsockopt(sockfd, afinfo.ipproto, opt, &rev, &s);
498 if (max_rev < 0) {
499 /* Definitely don't support this? */
500 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
501 close(sockfd);
502 return 0;
503 } else if (errno == ENOPROTOOPT) {
504 close(sockfd);
505 /* Assume only revision 0 support (old kernel) */
506 return (revision == 0);
507 } else {
508 fprintf(stderr, "getsockopt failed strangely: %s\n",
509 strerror(errno));
510 exit(1);
511 }
512 }
513 close(sockfd);
514 return 1;
515}
516
517
518static int compatible_match_revision(const char *name, u_int8_t revision)
519{
520 return compatible_revision(name, revision, afinfo.so_rev_match);
521}
522
523static int compatible_target_revision(const char *name, u_int8_t revision)
524{
525 return compatible_revision(name, revision, afinfo.so_rev_target);
526}
527
528void xtables_register_match(struct xtables_match *me)
529{
530 struct xtables_match **i, *old;
531
532 if (strcmp(me->version, program_version) != 0) {
533 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
534 program_name, me->name, me->version, program_version);
535 exit(1);
536 }
537
538 /* Revision field stole a char from name. */
539 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
540 fprintf(stderr, "%s: target `%s' has invalid name\n",
541 program_name, me->name);
542 exit(1);
543 }
544
545 if (me->family >= NPROTO) {
546 fprintf(stderr,
547 "%s: BUG: match %s has invalid protocol family\n",
548 program_name, me->name);
549 exit(1);
550 }
551
552 /* ignore not interested match */
Jan Engelhardt23545c22008-02-14 04:23:04 +0100553 if (me->family != afinfo.family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000554 return;
555
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100556 old = xtables_find_match(me->name, XTF_DURING_LOAD, NULL);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000557 if (old) {
Jan Engelhardt23545c22008-02-14 04:23:04 +0100558 if (old->revision == me->revision &&
559 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000560 fprintf(stderr,
561 "%s: match `%s' already registered.\n",
562 program_name, me->name);
563 exit(1);
564 }
565
566 /* Now we have two (or more) options, check compatibility. */
567 if (compatible_match_revision(old->name, old->revision)
568 && old->revision > me->revision)
569 return;
570
Jan Engelhardt23545c22008-02-14 04:23:04 +0100571 /* See if new match can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000572 if (!compatible_match_revision(me->name, me->revision))
573 return;
574
Jan Engelhardt23545c22008-02-14 04:23:04 +0100575 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
576 if (old->revision == me->revision && me->family == AF_UNSPEC)
577 return;
578
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000579 /* Delete old one. */
580 for (i = &xtables_matches; *i!=old; i = &(*i)->next);
581 *i = old->next;
582 }
583
584 if (me->size != XT_ALIGN(me->size)) {
585 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
586 program_name, me->name, (unsigned int)me->size);
587 exit(1);
588 }
589
590 /* Append to list. */
591 for (i = &xtables_matches; *i; i = &(*i)->next);
592 me->next = NULL;
593 *i = me;
594
595 me->m = NULL;
596 me->mflags = 0;
597}
598
599void xtables_register_target(struct xtables_target *me)
600{
601 struct xtables_target *old;
602
603 if (strcmp(me->version, program_version) != 0) {
604 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
605 program_name, me->name, me->version, program_version);
606 exit(1);
607 }
608
609 /* Revision field stole a char from name. */
610 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) {
611 fprintf(stderr, "%s: target `%s' has invalid name\n",
612 program_name, me->name);
613 exit(1);
614 }
615
616 if (me->family >= NPROTO) {
617 fprintf(stderr,
618 "%s: BUG: target %s has invalid protocol family\n",
619 program_name, me->name);
620 exit(1);
621 }
622
623 /* ignore not interested target */
Jan Engelhardt23545c22008-02-14 04:23:04 +0100624 if (me->family != afinfo.family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000625 return;
626
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100627 old = xtables_find_target(me->name, XTF_DURING_LOAD);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000628 if (old) {
629 struct xtables_target **i;
630
Jan Engelhardt23545c22008-02-14 04:23:04 +0100631 if (old->revision == me->revision &&
632 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000633 fprintf(stderr,
634 "%s: target `%s' already registered.\n",
635 program_name, me->name);
636 exit(1);
637 }
638
639 /* Now we have two (or more) options, check compatibility. */
640 if (compatible_target_revision(old->name, old->revision)
641 && old->revision > me->revision)
642 return;
643
Jan Engelhardt23545c22008-02-14 04:23:04 +0100644 /* See if new target can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000645 if (!compatible_target_revision(me->name, me->revision))
646 return;
647
Jan Engelhardt23545c22008-02-14 04:23:04 +0100648 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
649 if (old->revision == me->revision && me->family == AF_UNSPEC)
650 return;
651
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000652 /* Delete old one. */
653 for (i = &xtables_targets; *i!=old; i = &(*i)->next);
654 *i = old->next;
655 }
656
657 if (me->size != XT_ALIGN(me->size)) {
658 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
659 program_name, me->name, (unsigned int)me->size);
660 exit(1);
661 }
662
663 /* Prepend to list. */
664 me->next = xtables_targets;
665 xtables_targets = me;
666 me->t = NULL;
667 me->tflags = 0;
668}
Jan Engelhardtaafd2692008-01-20 13:19:40 +0000669
670void param_act(unsigned int status, const char *p1, ...)
671{
672 const char *p2, *p3;
673 va_list args;
674 bool b;
675
676 va_start(args, p1);
677
678 switch (status) {
679 case P_ONLY_ONCE:
680 p2 = va_arg(args, const char *);
681 b = va_arg(args, unsigned int);
682 if (!b)
683 return;
684 exit_error(PARAMETER_PROBLEM,
685 "%s: \"%s\" option may only be specified once",
686 p1, p2);
687 break;
688 case P_NO_INVERT:
689 p2 = va_arg(args, const char *);
690 b = va_arg(args, unsigned int);
691 if (!b)
692 return;
693 exit_error(PARAMETER_PROBLEM,
694 "%s: \"%s\" option cannot be inverted", p1, p2);
695 break;
696 case P_BAD_VALUE:
697 p2 = va_arg(args, const char *);
698 p3 = va_arg(args, const char *);
699 exit_error(PARAMETER_PROBLEM,
700 "%s: Bad value for \"%s\" option: \"%s\"",
701 p1, p2, p3);
702 break;
703 case P_ONE_ACTION:
704 b = va_arg(args, unsigned int);
705 if (!b)
706 return;
707 exit_error(PARAMETER_PROBLEM,
708 "%s: At most one action is possible", p1);
709 break;
710 default:
711 exit_error(status, p1, args);
712 break;
713 }
714
715 va_end(args);
716}
Jan Engelhardt08b16162008-01-20 13:36:08 +0000717
718const char *ipaddr_to_numeric(const struct in_addr *addrp)
719{
720 static char buf[20];
721 const unsigned char *bytep = (const void *)&addrp->s_addr;
722
723 sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]);
724 return buf;
725}
726
727static const char *ipaddr_to_host(const struct in_addr *addr)
728{
729 struct hostent *host;
730
731 host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
732 if (host == NULL)
733 return NULL;
734
735 return host->h_name;
736}
737
738static const char *ipaddr_to_network(const struct in_addr *addr)
739{
740 struct netent *net;
741
742 if ((net = getnetbyaddr(ntohl(addr->s_addr), AF_INET)) != NULL)
743 return net->n_name;
744
745 return NULL;
746}
747
748const char *ipaddr_to_anyname(const struct in_addr *addr)
749{
750 const char *name;
751
752 if ((name = ipaddr_to_host(addr)) != NULL ||
753 (name = ipaddr_to_network(addr)) != NULL)
754 return name;
755
756 return ipaddr_to_numeric(addr);
757}
758
759const char *ipmask_to_numeric(const struct in_addr *mask)
760{
761 static char buf[20];
762 uint32_t maskaddr, bits;
763 int i;
764
765 maskaddr = ntohl(mask->s_addr);
766
767 if (maskaddr == 0xFFFFFFFFL)
768 /* we don't want to see "/32" */
769 return "";
770
771 i = 32;
772 bits = 0xFFFFFFFEL;
773 while (--i >= 0 && maskaddr != bits)
774 bits <<= 1;
775 if (i >= 0)
776 sprintf(buf, "/%d", i);
777 else
778 /* mask was not a decent combination of 1's and 0's */
779 sprintf(buf, "/%s", ipaddr_to_numeric(mask));
780
781 return buf;
782}
783
Jan Engelhardtbd943842008-01-20 13:38:08 +0000784static struct in_addr *__numeric_to_ipaddr(const char *dotted, bool is_mask)
785{
786 static struct in_addr addr;
787 unsigned char *addrp;
788 unsigned int onebyte;
789 char buf[20], *p, *q;
790 int i;
791
792 /* copy dotted string, because we need to modify it */
793 strncpy(buf, dotted, sizeof(buf) - 1);
794 buf[sizeof(buf) - 1] = '\0';
795 addrp = (void *)&addr.s_addr;
796
797 p = buf;
798 for (i = 0; i < 3; ++i) {
799 if ((q = strchr(p, '.')) == NULL) {
800 if (is_mask)
801 return NULL;
802
803 /* autocomplete, this is a network address */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100804 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000805 return NULL;
806
807 addrp[i] = onebyte;
808 while (i < 3)
809 addrp[++i] = 0;
810
811 return &addr;
812 }
813
814 *q = '\0';
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100815 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000816 return NULL;
817
818 addrp[i] = onebyte;
819 p = q + 1;
820 }
821
822 /* we have checked 3 bytes, now we check the last one */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100823 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000824 return NULL;
825
826 addrp[3] = onebyte;
827 return &addr;
828}
829
830struct in_addr *numeric_to_ipaddr(const char *dotted)
831{
832 return __numeric_to_ipaddr(dotted, false);
833}
834
835struct in_addr *numeric_to_ipmask(const char *dotted)
836{
837 return __numeric_to_ipaddr(dotted, true);
838}
839
840static struct in_addr *network_to_ipaddr(const char *name)
841{
842 static struct in_addr addr;
843 struct netent *net;
844
845 if ((net = getnetbyname(name)) != NULL) {
846 if (net->n_addrtype != AF_INET)
847 return NULL;
848 addr.s_addr = htonl(net->n_net);
849 return &addr;
850 }
851
852 return NULL;
853}
854
855static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr)
856{
857 struct hostent *host;
858 struct in_addr *addr;
859 unsigned int i;
860
861 *naddr = 0;
862 if ((host = gethostbyname(name)) != NULL) {
863 if (host->h_addrtype != AF_INET ||
864 host->h_length != sizeof(struct in_addr))
865 return NULL;
866
867 while (host->h_addr_list[*naddr] != NULL)
868 ++*naddr;
Jan Engelhardt630ef482009-01-27 14:58:41 +0100869 addr = xtables_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Jan Engelhardtbd943842008-01-20 13:38:08 +0000870 for (i = 0; i < *naddr; i++)
871 memcpy(&addr[i], host->h_addr_list[i],
872 sizeof(struct in_addr));
873 return addr;
874 }
875
876 return NULL;
877}
878
879static struct in_addr *
880ipparse_hostnetwork(const char *name, unsigned int *naddrs)
881{
882 struct in_addr *addrptmp, *addrp;
883
884 if ((addrptmp = numeric_to_ipaddr(name)) != NULL ||
885 (addrptmp = network_to_ipaddr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +0100886 addrp = xtables_malloc(sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +0000887 memcpy(addrp, addrptmp, sizeof(*addrp));
888 *naddrs = 1;
889 return addrp;
890 }
891 if ((addrptmp = host_to_ipaddr(name, naddrs)) != NULL)
892 return addrptmp;
893
894 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
895}
896
897static struct in_addr *parse_ipmask(const char *mask)
898{
899 static struct in_addr maskaddr;
900 struct in_addr *addrp;
901 unsigned int bits;
902
903 if (mask == NULL) {
904 /* no mask at all defaults to 32 bits */
905 maskaddr.s_addr = 0xFFFFFFFF;
906 return &maskaddr;
907 }
908 if ((addrp = numeric_to_ipmask(mask)) != NULL)
909 /* dotted_to_addr already returns a network byte order addr */
910 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100911 if (!xtables_strtoui(mask, NULL, &bits, 0, 32))
Jan Engelhardtbd943842008-01-20 13:38:08 +0000912 exit_error(PARAMETER_PROBLEM,
913 "invalid mask `%s' specified", mask);
914 if (bits != 0) {
915 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
916 return &maskaddr;
917 }
918
919 maskaddr.s_addr = 0U;
920 return &maskaddr;
921}
922
923void ipparse_hostnetworkmask(const char *name, struct in_addr **addrpp,
924 struct in_addr *maskp, unsigned int *naddrs)
925{
926 unsigned int i, j, k, n;
927 struct in_addr *addrp;
928 char buf[256], *p;
929
930 strncpy(buf, name, sizeof(buf) - 1);
931 buf[sizeof(buf) - 1] = '\0';
932 if ((p = strrchr(buf, '/')) != NULL) {
933 *p = '\0';
934 addrp = parse_ipmask(p + 1);
935 } else {
936 addrp = parse_ipmask(NULL);
937 }
938 memcpy(maskp, addrp, sizeof(*maskp));
939
940 /* if a null mask is given, the name is ignored, like in "any/0" */
941 if (maskp->s_addr == 0U)
942 strcpy(buf, "0.0.0.0");
943
944 addrp = *addrpp = ipparse_hostnetwork(buf, naddrs);
945 n = *naddrs;
946 for (i = 0, j = 0; i < n; ++i) {
947 addrp[j++].s_addr &= maskp->s_addr;
948 for (k = 0; k < j - 1; ++k)
949 if (addrp[k].s_addr == addrp[j-1].s_addr) {
950 --*naddrs;
951 --j;
952 break;
953 }
954 }
955}
956
Jan Engelhardt08b16162008-01-20 13:36:08 +0000957const char *ip6addr_to_numeric(const struct in6_addr *addrp)
958{
959 /* 0000:0000:0000:0000:0000:000.000.000.000
960 * 0000:0000:0000:0000:0000:0000:0000:0000 */
961 static char buf[50+1];
962 return inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
963}
964
965static const char *ip6addr_to_host(const struct in6_addr *addr)
966{
967 static char hostname[NI_MAXHOST];
968 struct sockaddr_in6 saddr;
969 int err;
970
971 memset(&saddr, 0, sizeof(struct sockaddr_in6));
972 memcpy(&saddr.sin6_addr, addr, sizeof(*addr));
973 saddr.sin6_family = AF_INET6;
974
975 err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in6),
976 hostname, sizeof(hostname) - 1, NULL, 0, 0);
977 if (err != 0) {
978#ifdef DEBUG
979 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
980#endif
981 return NULL;
982 }
983
984#ifdef DEBUG
985 fprintf (stderr, "\naddr2host: %s\n", hostname);
986#endif
987 return hostname;
988}
989
990const char *ip6addr_to_anyname(const struct in6_addr *addr)
991{
992 const char *name;
993
994 if ((name = ip6addr_to_host(addr)) != NULL)
995 return name;
996
997 return ip6addr_to_numeric(addr);
998}
999
1000static int ip6addr_prefix_length(const struct in6_addr *k)
1001{
1002 unsigned int bits = 0;
1003 uint32_t a, b, c, d;
1004
Jan Engelhardt48607812008-06-10 15:17:53 +02001005 a = ntohl(k->s6_addr32[0]);
1006 b = ntohl(k->s6_addr32[1]);
1007 c = ntohl(k->s6_addr32[2]);
1008 d = ntohl(k->s6_addr32[3]);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001009 while (a & 0x80000000U) {
1010 ++bits;
1011 a <<= 1;
1012 a |= (b >> 31) & 1;
1013 b <<= 1;
1014 b |= (c >> 31) & 1;
1015 c <<= 1;
1016 c |= (d >> 31) & 1;
1017 d <<= 1;
1018 }
1019 if (a != 0 || b != 0 || c != 0 || d != 0)
1020 return -1;
1021 return bits;
1022}
1023
1024const char *ip6mask_to_numeric(const struct in6_addr *addrp)
1025{
1026 static char buf[50+2];
1027 int l = ip6addr_prefix_length(addrp);
1028
1029 if (l == -1) {
1030 strcpy(buf, "/");
1031 strcat(buf, ip6addr_to_numeric(addrp));
1032 return buf;
1033 }
1034 sprintf(buf, "/%d", l);
1035 return buf;
1036}
Jan Engelhardtbd943842008-01-20 13:38:08 +00001037
1038struct in6_addr *numeric_to_ip6addr(const char *num)
1039{
1040 static struct in6_addr ap;
1041 int err;
1042
1043 if ((err = inet_pton(AF_INET6, num, &ap)) == 1)
1044 return &ap;
1045#ifdef DEBUG
1046 fprintf(stderr, "\nnumeric2addr: %d\n", err);
1047#endif
1048 return NULL;
1049}
1050
1051static struct in6_addr *
1052host_to_ip6addr(const char *name, unsigned int *naddr)
1053{
1054 static struct in6_addr *addr;
1055 struct addrinfo hints;
1056 struct addrinfo *res;
1057 int err;
1058
1059 memset(&hints, 0, sizeof(hints));
1060 hints.ai_flags = AI_CANONNAME;
1061 hints.ai_family = AF_INET6;
1062 hints.ai_socktype = SOCK_RAW;
1063 hints.ai_protocol = IPPROTO_IPV6;
1064 hints.ai_next = NULL;
1065
1066 *naddr = 0;
1067 if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) {
1068#ifdef DEBUG
1069 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
1070#endif
1071 return NULL;
1072 } else {
1073 if (res->ai_family != AF_INET6 ||
1074 res->ai_addrlen != sizeof(struct sockaddr_in6))
1075 return NULL;
1076
1077#ifdef DEBUG
1078 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
1079 ip6addr_to_numeric(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr));
1080#endif
1081 /* Get the first element of the address-chain */
Jan Engelhardt630ef482009-01-27 14:58:41 +01001082 addr = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001083 memcpy(addr, &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
1084 sizeof(struct in6_addr));
1085 freeaddrinfo(res);
1086 *naddr = 1;
1087 return addr;
1088 }
1089
1090 return NULL;
1091}
1092
1093static struct in6_addr *network_to_ip6addr(const char *name)
1094{
1095 /* abort();*/
1096 /* TODO: not implemented yet, but the exception breaks the
1097 * name resolvation */
1098 return NULL;
1099}
1100
1101static struct in6_addr *
1102ip6parse_hostnetwork(const char *name, unsigned int *naddrs)
1103{
1104 struct in6_addr *addrp, *addrptmp;
1105
1106 if ((addrptmp = numeric_to_ip6addr(name)) != NULL ||
1107 (addrptmp = network_to_ip6addr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001108 addrp = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001109 memcpy(addrp, addrptmp, sizeof(*addrp));
1110 *naddrs = 1;
1111 return addrp;
1112 }
1113 if ((addrp = host_to_ip6addr(name, naddrs)) != NULL)
1114 return addrp;
1115
1116 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
1117}
1118
1119static struct in6_addr *parse_ip6mask(char *mask)
1120{
1121 static struct in6_addr maskaddr;
1122 struct in6_addr *addrp;
1123 unsigned int bits;
1124
1125 if (mask == NULL) {
1126 /* no mask at all defaults to 128 bits */
1127 memset(&maskaddr, 0xff, sizeof maskaddr);
1128 return &maskaddr;
1129 }
1130 if ((addrp = numeric_to_ip6addr(mask)) != NULL)
1131 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001132 if (!xtables_strtoui(mask, NULL, &bits, 0, 128))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001133 exit_error(PARAMETER_PROBLEM,
1134 "invalid mask `%s' specified", mask);
1135 if (bits != 0) {
1136 char *p = (void *)&maskaddr;
1137 memset(p, 0xff, bits / 8);
1138 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
1139 p[bits/8] = 0xff << (8 - (bits & 7));
1140 return &maskaddr;
1141 }
1142
1143 memset(&maskaddr, 0, sizeof(maskaddr));
1144 return &maskaddr;
1145}
1146
1147void ip6parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
1148 struct in6_addr *maskp, unsigned int *naddrs)
1149{
1150 struct in6_addr *addrp;
1151 unsigned int i, j, k, n;
1152 char buf[256], *p;
1153
1154 strncpy(buf, name, sizeof(buf) - 1);
1155 buf[sizeof(buf)-1] = '\0';
1156 if ((p = strrchr(buf, '/')) != NULL) {
1157 *p = '\0';
1158 addrp = parse_ip6mask(p + 1);
1159 } else {
1160 addrp = parse_ip6mask(NULL);
1161 }
1162 memcpy(maskp, addrp, sizeof(*maskp));
1163
1164 /* if a null mask is given, the name is ignored, like in "any/0" */
1165 if (memcmp(maskp, &in6addr_any, sizeof(in6addr_any)) == 0)
1166 strcpy(buf, "::");
1167
1168 addrp = *addrpp = ip6parse_hostnetwork(buf, naddrs);
1169 n = *naddrs;
1170 for (i = 0, j = 0; i < n; ++i) {
1171 for (k = 0; k < 4; ++k)
Yasuyuki Kozakai5a2208c2008-06-04 15:16:03 +02001172 addrp[j].s6_addr32[k] &= maskp->s6_addr32[k];
Jan Engelhardtbd943842008-01-20 13:38:08 +00001173 ++j;
1174 for (k = 0; k < j - 1; ++k)
1175 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
1176 --*naddrs;
1177 --j;
1178 break;
1179 }
1180 }
1181}
Max Kellermanna5d09942008-01-29 13:44:34 +00001182
1183void save_string(const char *value)
1184{
1185 static const char no_quote_chars[] = "_-0123456789"
1186 "abcdefghijklmnopqrstuvwxyz"
1187 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1188 static const char escape_chars[] = "\"\\'";
1189 size_t length;
1190 const char *p;
1191
1192 length = strcspn(value, no_quote_chars);
1193 if (length > 0 && value[length] == 0) {
1194 /* no quoting required */
1195 fputs(value, stdout);
1196 putchar(' ');
1197 } else {
1198 /* there is at least one dangerous character in the
1199 value, which we have to quote. Write double quotes
1200 around the value and escape special characters with
1201 a backslash */
1202 putchar('"');
1203
1204 for (p = strpbrk(value, escape_chars); p != NULL;
1205 p = strpbrk(value, escape_chars)) {
1206 if (p > value)
1207 fwrite(value, 1, p - value, stdout);
1208 putchar('\\');
1209 putchar(*p);
1210 value = p + 1;
1211 }
1212
1213 /* print the rest and finish the double quoted
1214 string */
1215 fputs(value, stdout);
1216 printf("\" ");
1217 }
1218}