1 /* Shared library add-on to iptables to add OWNER matching support. */
11 #include <linux/netfilter_ipv4/ipt_owner.h>
13 /* Function which prints out usage message. */
19 "OWNER match v%s options:\n"
20 "[!] --uid-owner userid Match local uid\n"
21 "[!] --gid-owner groupid Match local gid\n"
22 "[!] --pid-owner processid Match local pid\n"
23 "[!] --sid-owner sessionid Match local sid\n"
24 "[!] --cmd-owner name Match local command name\n"
29 "OWNER match v%s options:\n"
30 "[!] --uid-owner userid Match local uid\n"
31 "[!] --gid-owner groupid Match local gid\n"
32 "[!] --pid-owner processid Match local pid\n"
33 "[!] --sid-owner sessionid Match local sid\n"
36 #endif /* IPT_OWNER_COMM */
39 static struct option opts[] = {
40 { "uid-owner", 1, 0, '1' },
41 { "gid-owner", 1, 0, '2' },
42 { "pid-owner", 1, 0, '3' },
43 { "sid-owner", 1, 0, '4' },
45 { "cmd-owner", 1, 0, '5' },
50 /* Initialize the match. */
52 init(struct ipt_entry_match *m, unsigned int *nfcache)
54 /* Can't cache this. */
55 *nfcache |= NFC_UNKNOWN;
58 /* Function which parses command options; returns true if it
61 parse(int c, char **argv, int invert, unsigned int *flags,
62 const struct ipt_entry *entry,
63 unsigned int *nfcache,
64 struct ipt_entry_match **match)
66 struct ipt_owner_info *ownerinfo = (struct ipt_owner_info *)(*match)->data;
73 check_inverse(optarg, &invert, &optind, 0);
74 if ((pwd = getpwnam(optarg)))
75 ownerinfo->uid = pwd->pw_uid;
77 ownerinfo->uid = strtoul(optarg, &end, 0);
78 if (*end != '\0' || end == optarg)
79 exit_error(PARAMETER_PROBLEM, "Bad OWNER UID value `%s'", optarg);
82 ownerinfo->invert |= IPT_OWNER_UID;
83 ownerinfo->match |= IPT_OWNER_UID;
88 check_inverse(optarg, &invert, &optind, 0);
89 if ((grp = getgrnam(optarg)))
90 ownerinfo->gid = grp->gr_gid;
92 ownerinfo->gid = strtoul(optarg, &end, 0);
93 if (*end != '\0' || end == optarg)
94 exit_error(PARAMETER_PROBLEM, "Bad OWNER GID value `%s'", optarg);
97 ownerinfo->invert |= IPT_OWNER_GID;
98 ownerinfo->match |= IPT_OWNER_GID;
103 check_inverse(optarg, &invert, &optind, 0);
104 ownerinfo->pid = strtoul(optarg, &end, 0);
105 if (*end != '\0' || end == optarg)
106 exit_error(PARAMETER_PROBLEM, "Bad OWNER PID value `%s'", optarg);
108 ownerinfo->invert |= IPT_OWNER_PID;
109 ownerinfo->match |= IPT_OWNER_PID;
114 check_inverse(optarg, &invert, &optind, 0);
115 ownerinfo->sid = strtoul(optarg, &end, 0);
116 if (*end != '\0' || end == optarg)
117 exit_error(PARAMETER_PROBLEM, "Bad OWNER SID value `%s'", optarg);
119 ownerinfo->invert |= IPT_OWNER_SID;
120 ownerinfo->match |= IPT_OWNER_SID;
124 #ifdef IPT_OWNER_COMM
126 check_inverse(optarg, &invert, &optind, 0);
127 if(strlen(optarg) > sizeof(ownerinfo->comm))
128 exit_error(PARAMETER_PROBLEM, "OWNER CMD `%s' too long, max %d characters", optarg, sizeof(ownerinfo->comm));
130 strncpy(ownerinfo->comm, optarg, sizeof(ownerinfo->comm));
133 ownerinfo->invert |= IPT_OWNER_COMM;
134 ownerinfo->match |= IPT_OWNER_COMM;
146 print_item(struct ipt_owner_info *info, u_int8_t flag, int numeric, char *label)
148 if(info->match & flag) {
150 if (info->invert & flag)
155 switch(info->match & flag) {
158 struct passwd *pwd = getpwuid(info->uid);
160 if(pwd && pwd->pw_name) {
161 printf("%s ", pwd->pw_name);
166 printf("%u ", info->uid);
170 struct group *grp = getgrgid(info->gid);
172 if(grp && grp->gr_name) {
173 printf("%s ", grp->gr_name);
178 printf("%u ", info->gid);
181 printf("%u ", info->pid);
184 printf("%u ", info->sid);
186 #ifdef IPT_OWNER_COMM
188 printf("%.*s ", (int)sizeof(info->comm), info->comm);
197 /* Final check; must have specified --own. */
199 final_check(unsigned int flags)
202 exit_error(PARAMETER_PROBLEM,
203 "OWNER match: You must specify one or more options");
206 /* Prints out the matchinfo. */
208 print(const struct ipt_ip *ip,
209 const struct ipt_entry_match *match,
212 struct ipt_owner_info *info = (struct ipt_owner_info *)match->data;
214 print_item(info, IPT_OWNER_UID, numeric, "OWNER UID match ");
215 print_item(info, IPT_OWNER_GID, numeric, "OWNER GID match ");
216 print_item(info, IPT_OWNER_PID, numeric, "OWNER PID match ");
217 print_item(info, IPT_OWNER_SID, numeric, "OWNER SID match ");
218 #ifdef IPT_OWNER_COMM
219 print_item(info, IPT_OWNER_COMM, numeric, "OWNER CMD match ");
223 /* Saves the union ipt_matchinfo in parsable form to stdout. */
225 save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
227 struct ipt_owner_info *info = (struct ipt_owner_info *)match->data;
229 print_item(info, IPT_OWNER_UID, 0, "--uid-owner ");
230 print_item(info, IPT_OWNER_GID, 0, "--gid-owner ");
231 print_item(info, IPT_OWNER_PID, 0, "--pid-owner ");
232 print_item(info, IPT_OWNER_SID, 0, "--sid-owner ");
233 #ifdef IPT_OWNER_COMM
234 print_item(info, IPT_OWNER_COMM, 0, "--cmd-owner ");
239 struct iptables_match owner
243 IPT_ALIGN(sizeof(struct ipt_owner_info)),
244 IPT_ALIGN(sizeof(struct ipt_owner_info)),
256 register_match(&owner);