1 /* Shared library add-on to iptables to add OWNER matching support. */
10 #include <ip6tables.h>
11 #include <linux/netfilter_ipv6/ip6t_owner.h>
13 /* Function which prints out usage message. */
17 #ifdef IP6T_OWNER_COMM
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 /* IP6T_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' },
44 #ifdef IP6T_OWNER_COMM
45 { "cmd-owner", 1, 0, '5' },
50 /* Function which parses command options; returns true if it
53 parse(int c
, char **argv
, int invert
, unsigned int *flags
,
54 const struct ip6t_entry
*entry
,
55 unsigned int *nfcache
,
56 struct ip6t_entry_match
**match
)
58 struct ip6t_owner_info
*ownerinfo
= (struct ip6t_owner_info
*)(*match
)->data
;
65 check_inverse(optarg
, &invert
, &optind
, 0);
67 if ((pwd
= getpwnam(optarg
)))
68 ownerinfo
->uid
= pwd
->pw_uid
;
70 ownerinfo
->uid
= strtoul(optarg
, &end
, 0);
71 if (*end
!= '\0' || end
== optarg
)
72 exit_error(PARAMETER_PROBLEM
, "Bad OWNER UID value `%s'", optarg
);
75 ownerinfo
->invert
|= IP6T_OWNER_UID
;
76 ownerinfo
->match
|= IP6T_OWNER_UID
;
81 check_inverse(optarg
, &invert
, &optind
, 0);
82 if ((grp
= getgrnam(optarg
)))
83 ownerinfo
->gid
= grp
->gr_gid
;
85 ownerinfo
->gid
= strtoul(optarg
, &end
, 0);
86 if (*end
!= '\0' || end
== optarg
)
87 exit_error(PARAMETER_PROBLEM
, "Bad OWNER GID value `%s'", optarg
);
90 ownerinfo
->invert
|= IP6T_OWNER_GID
;
91 ownerinfo
->match
|= IP6T_OWNER_GID
;
96 check_inverse(optarg
, &invert
, &optind
, 0);
97 ownerinfo
->pid
= strtoul(optarg
, &end
, 0);
98 if (*end
!= '\0' || end
== optarg
)
99 exit_error(PARAMETER_PROBLEM
, "Bad OWNER PID value `%s'", optarg
);
101 ownerinfo
->invert
|= IP6T_OWNER_PID
;
102 ownerinfo
->match
|= IP6T_OWNER_PID
;
107 check_inverse(optarg
, &invert
, &optind
, 0);
108 ownerinfo
->sid
= strtoul(optarg
, &end
, 0);
109 if (*end
!= '\0' || end
== optarg
)
110 exit_error(PARAMETER_PROBLEM
, "Bad OWNER SID value `%s'", optarg
);
112 ownerinfo
->invert
|= IP6T_OWNER_SID
;
113 ownerinfo
->match
|= IP6T_OWNER_SID
;
117 #ifdef IP6T_OWNER_COMM
119 check_inverse(optarg
, &invert
, &optind
, 0);
120 if(strlen(optarg
) > sizeof(ownerinfo
->comm
))
121 exit_error(PARAMETER_PROBLEM
, "OWNER CMD `%s' too long, max %d characters", optarg
, sizeof(ownerinfo
->comm
));
123 strncpy(ownerinfo
->comm
, optarg
, sizeof(ownerinfo
->comm
));
124 ownerinfo
->comm
[sizeof(ownerinfo
->comm
)-1] = '\0';
127 ownerinfo
->invert
|= IP6T_OWNER_COMM
;
128 ownerinfo
->match
|= IP6T_OWNER_COMM
;
140 print_item(struct ip6t_owner_info
*info
, u_int8_t flag
, int numeric
, char *label
)
142 if(info
->match
& flag
) {
144 if (info
->invert
& flag
)
149 switch(info
->match
& flag
) {
152 struct passwd
*pwd
= getpwuid(info
->uid
);
154 if(pwd
&& pwd
->pw_name
) {
155 printf("%s ", pwd
->pw_name
);
160 printf("%u ", info
->uid
);
164 struct group
*grp
= getgrgid(info
->gid
);
166 if(grp
&& grp
->gr_name
) {
167 printf("%s ", grp
->gr_name
);
172 printf("%u ", info
->gid
);
175 printf("%u ", info
->pid
);
178 printf("%u ", info
->sid
);
180 #ifdef IP6T_OWNER_COMM
181 case IP6T_OWNER_COMM
:
182 printf("%.*s ", (int)sizeof(info
->comm
), info
->comm
);
191 /* Final check; must have specified --own. */
193 final_check(unsigned int flags
)
196 exit_error(PARAMETER_PROBLEM
,
197 "OWNER match: You must specify one or more options");
200 /* Prints out the matchinfo. */
202 print(const struct ip6t_ip6
*ip
,
203 const struct ip6t_entry_match
*match
,
206 struct ip6t_owner_info
*info
= (struct ip6t_owner_info
*)match
->data
;
208 print_item(info
, IP6T_OWNER_UID
, numeric
, "OWNER UID match ");
209 print_item(info
, IP6T_OWNER_GID
, numeric
, "OWNER GID match ");
210 print_item(info
, IP6T_OWNER_PID
, numeric
, "OWNER PID match ");
211 print_item(info
, IP6T_OWNER_SID
, numeric
, "OWNER SID match ");
212 #ifdef IP6T_OWNER_COMM
213 print_item(info
, IP6T_OWNER_COMM
, numeric
, "OWNER CMD match ");
217 /* Saves the union ip6t_matchinfo in parsable form to stdout. */
219 save(const struct ip6t_ip6
*ip
, const struct ip6t_entry_match
*match
)
221 struct ip6t_owner_info
*info
= (struct ip6t_owner_info
*)match
->data
;
223 print_item(info
, IP6T_OWNER_UID
, 0, "--uid-owner ");
224 print_item(info
, IP6T_OWNER_GID
, 0, "--gid-owner ");
225 print_item(info
, IP6T_OWNER_PID
, 0, "--pid-owner ");
226 print_item(info
, IP6T_OWNER_SID
, 0, "--sid-owner ");
227 #ifdef IP6T_OWNER_COMM
228 print_item(info
, IP6T_OWNER_COMM
, 0, "--cmd-owner ");
232 static struct ip6tables_match owner
= {
234 .version
= IPTABLES_VERSION
,
235 .size
= IP6T_ALIGN(sizeof(struct ip6t_owner_info
)),
236 .userspacesize
= IP6T_ALIGN(sizeof(struct ip6t_owner_info
)),
239 .final_check
= &final_check
,
247 register_match6(&owner
);