xshared.c revision 033e25a3ad215ee3f5a07f0a3315f74c4abfaced
1#include <getopt.h>
2#include <libgen.h>
3#include <netdb.h>
4#include <stdbool.h>
5#include <stdint.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <xtables.h>
10#include "xshared.h"
11
12/*
13 * Print out any special helps. A user might like to be able to add a --help
14 * to the commandline, and see expected results. So we call help for all
15 * specified matches and targets.
16 */
17void print_extension_helps(const struct xtables_target *t,
18    const struct xtables_rule_match *m)
19{
20	for (; t != NULL; t = t->next) {
21		if (t->used) {
22			printf("\n");
23			if (t->help == NULL)
24				printf("%s does not take any options\n",
25				       t->name);
26			else
27				t->help();
28		}
29	}
30	for (; m != NULL; m = m->next) {
31		printf("\n");
32		if (m->match->help == NULL)
33			printf("%s does not take any options\n",
34			       m->match->name);
35		else
36			m->match->help();
37	}
38}
39
40const char *
41proto_to_name(uint8_t proto, int nolookup)
42{
43	unsigned int i;
44
45	if (proto && !nolookup) {
46		struct protoent *pent = getprotobynumber(proto);
47		if (pent)
48			return pent->p_name;
49	}
50
51	for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
52		if (xtables_chain_protos[i].num == proto)
53			return xtables_chain_protos[i].name;
54
55	return NULL;
56}
57
58static struct xtables_match *
59find_proto(const char *pname, enum xtables_tryload tryload,
60	   int nolookup, struct xtables_rule_match **matches)
61{
62	unsigned int proto;
63
64	if (xtables_strtoui(pname, NULL, &proto, 0, UINT8_MAX)) {
65		const char *protoname = proto_to_name(proto, nolookup);
66
67		if (protoname)
68			return xtables_find_match(protoname, tryload, matches);
69	} else
70		return xtables_find_match(pname, tryload, matches);
71
72	return NULL;
73}
74
75/*
76 * Some explanations (after four different bugs in 3 different releases): If
77 * we encounter a parameter, that has not been parsed yet, it's not an option
78 * of an explicitly loaded match or a target. However, we support implicit
79 * loading of the protocol match extension. '-p tcp' means 'l4 proto 6' and at
80 * the same time 'load tcp protocol match on demand if we specify --dport'.
81 *
82 * To make this work, we need to make sure:
83 * - the parameter has not been parsed by a match (m above)
84 * - a protocol has been specified
85 * - the protocol extension has not been loaded yet, or is loaded and unused
86 *   [think of ip6tables-restore!]
87 * - the protocol extension can be successively loaded
88 */
89static bool should_load_proto(struct iptables_command_state *cs)
90{
91	if (cs->protocol == NULL)
92		return false;
93	if (find_proto(cs->protocol, XTF_DONT_LOAD,
94	    cs->options & OPT_NUMERIC, NULL) == NULL)
95		return true;
96	return !cs->proto_used;
97}
98
99struct xtables_match *load_proto(struct iptables_command_state *cs)
100{
101	if (!should_load_proto(cs))
102		return NULL;
103	return find_proto(cs->protocol, XTF_TRY_LOAD,
104			  cs->options & OPT_NUMERIC, &cs->matches);
105}
106
107void command_default(struct iptables_command_state *cs,
108		     struct xtables_globals *gl)
109{
110	struct xtables_rule_match *matchp;
111	struct xtables_match *m;
112
113	if (cs->target != NULL &&
114	    (cs->target->parse != NULL || cs->target->x6_parse != NULL) &&
115	    cs->c >= cs->target->option_offset &&
116	    cs->c < cs->target->option_offset + XT_OPTION_OFFSET_SCALE) {
117		xtables_option_tpcall(cs->c, cs->argv, cs->invert,
118				      cs->target, &cs->fw);
119		return;
120	}
121
122	for (matchp = cs->matches; matchp; matchp = matchp->next) {
123		m = matchp->match;
124
125		if (matchp->completed ||
126		    (m->x6_parse == NULL && m->parse == NULL))
127			continue;
128		if (cs->c < matchp->match->option_offset ||
129		    cs->c >= matchp->match->option_offset + XT_OPTION_OFFSET_SCALE)
130			continue;
131		xtables_option_mpcall(cs->c, cs->argv, cs->invert, m, &cs->fw);
132		return;
133	}
134
135	/* Try loading protocol */
136	m = load_proto(cs);
137	if (m != NULL) {
138		size_t size;
139
140		cs->proto_used = 1;
141
142		size = XT_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
143
144		m->m = xtables_calloc(1, size);
145		m->m->u.match_size = size;
146		strcpy(m->m->u.user.name, m->name);
147		m->m->u.user.revision = m->revision;
148		if (m->init != NULL)
149			m->init(m->m);
150
151		if (m->x6_options != NULL)
152			gl->opts = xtables_options_xfrm(gl->orig_opts,
153							gl->opts,
154							m->x6_options,
155							&m->option_offset);
156		else
157			gl->opts = xtables_merge_options(gl->orig_opts,
158							 gl->opts,
159							 m->extra_opts,
160							 &m->option_offset);
161		if (gl->opts == NULL)
162			xtables_error(OTHER_PROBLEM, "can't alloc memory!");
163		optind--;
164		return;
165	}
166
167	if (cs->c == ':')
168		xtables_error(PARAMETER_PROBLEM, "option \"%s\" "
169		              "requires an argument", cs->argv[optind-1]);
170	if (cs->c == '?')
171		xtables_error(PARAMETER_PROBLEM, "unknown option "
172			      "\"%s\"", cs->argv[optind-1]);
173	xtables_error(PARAMETER_PROBLEM, "Unknown arg \"%s\"", optarg);
174}
175
176static mainfunc_t subcmd_get(const char *cmd, const struct subcommand *cb)
177{
178	for (; cb->name != NULL; ++cb)
179		if (strcmp(cb->name, cmd) == 0)
180			return cb->main;
181	return NULL;
182}
183
184int subcmd_main(int argc, char **argv, const struct subcommand *cb)
185{
186	const char *cmd = basename(*argv);
187	mainfunc_t f = subcmd_get(cmd, cb);
188
189	if (f == NULL && argc > 1) {
190		/*
191		 * Unable to find a main method for our command name?
192		 * Let's try again with the first argument!
193		 */
194		++argv;
195		--argc;
196		f = subcmd_get(*argv, cb);
197	}
198
199	/* now we should have a valid function pointer */
200	if (f != NULL)
201		return f(argc, argv);
202
203	fprintf(stderr, "ERROR: No valid subcommand given.\nValid subcommands:\n");
204	for (; cb->name != NULL; ++cb)
205		fprintf(stderr, " * %s\n", cb->name);
206	exit(EXIT_FAILURE);
207}
208