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 | #include "includes.h" |
31 | |
32 | static const struct _exptab { |
33 | char *str; |
34 | int val; |
35 | } exptab[] = { |
36 | { "subject", EXP_USER }, |
37 | { "object", EXP_OBJECT }, |
38 | { "esubject", EXP_EUSER }, |
39 | { NULL((void *)0), 0 } |
40 | }; |
41 | |
42 | char * |
43 | bsm_expand_trigger(struct bsm_record_data *bd, struct bsm_state *bm) |
44 | { |
45 | char *p0, *p1, *ret, token[2048], *tptr; |
46 | const struct _exptab *expptr; |
47 | struct passwd *pw; |
48 | size_t allocated; |
49 | |
50 | |
51 | allocated = strlen(bm->bm_trig) + 1024; |
52 | if ((p1 = ret = calloc(1, allocated)) == NULL((void *)0)) |
53 | return (NULL((void *)0)); |
54 | for (p0 = bm->bm_trig; *p0 != '\0';) { |
55 | if (*p0 == '$') { |
56 | |
57 | tptr = token; |
58 | while (isalpha(*(++p0))__sbistype((*(++p0)), 0x00000100L)) |
59 | *(tptr++) = *p0; |
60 | *tptr = '\0'; |
61 | for (expptr = exptab; expptr->str != NULL((void *)0); expptr++) |
62 | if (strcmp(expptr->str, token) == 0) |
63 | break; |
64 | if (expptr->str == NULL((void *)0)) { |
65 | |
66 | |
67 | |
68 | |
69 | |
70 | free(ret); |
71 | return (NULL((void *)0)); |
72 | } |
73 | switch (expptr->val) { |
74 | case EXP_USER: |
75 | if ((pw = getpwuid(bd->br_auid)) == NULL((void *)0)) |
76 | (void) strlcpy(token, "non-attributable", |
77 | sizeof(token)); |
78 | else |
79 | (void) strlcpy(token, pw->pw_name, |
80 | sizeof(token)); |
81 | break; |
82 | case EXP_EUSER: |
83 | if ((pw = getpwuid(bd->br_euid)) == NULL((void *)0)) |
84 | (void) strlcpy(token, "non-attributable", |
85 | sizeof(token)); |
86 | else |
87 | (void) strlcpy(token, pw->pw_name, |
88 | sizeof(token)); |
89 | break; |
90 | case EXP_OBJECT: |
91 | if (bd->br_path != NULL((void *)0)) |
92 | (void) strlcpy(token, bd->br_path, |
93 | sizeof(token)); |
94 | else { |
95 | free(ret); |
96 | return (NULL((void *)0)); |
97 | } |
98 | break; |
99 | default: |
100 | assert(0)((0) ? (void)0 : __assert(__func__, "trigger.c", 100, "0")); |
101 | } |
102 | (void) strlcat(ret, token, allocated); |
103 | p1 = ret + strlen(ret); |
104 | } else |
105 | *(p1++) = *(p0++); |
106 | if (p1 >= (ret + allocated)) { |
107 | free(ret); |
108 | return (NULL((void *)0)); |
109 | } |
110 | } |
111 | return (ret); |
112 | } |
113 | |
114 | void |
115 | bsm_run_trigger(struct bsm_record_data *bd, struct bsm_state *bm) |
116 | { |
117 | char *cmd, *ptr; |
118 | char **args; |
119 | int ret, n; |
120 | |
121 | assert((bd != NULL) && (bm != NULL))(((bd != ((void *)0)) && (bm != ((void *)0))) ? (void )0 : __assert(__func__, "trigger.c", 121, "(bd != NULL) && (bm != NULL)" )); |
122 | if (bm->bm_trig[0] == '\0') |
| |
123 | return; |
124 | cmd = bsm_expand_trigger(bd, bm); |
125 | if (cmd != NULL((void *)0)) { |
| 2 | | Assuming 'cmd' is not equal to null | |
|
| |
126 | |
127 | |
128 | |
129 | ret = fork(); |
130 | if (ret < 0) |
| |
| |
131 | bsmtrace_error(1, "%s: fork failed", __func__); |
132 | if (ret == 0) { |
| 6 | | Assuming 'ret' is equal to 0 | |
|
| |
133 | n = 0; |
134 | args = calloc(1, sizeof(char *) * TRIGGER_ARGS_MAX64); |
| 8 | | Value assigned to 'args' | |
|
135 | if (args == NULL((void *)0)) |
| 9 | | Assuming 'args' is equal to null | |
|
| |
136 | bsmtrace_error(1, "%s: calloc failed", |
137 | __func__); |
138 | dprintf("executing trigger: '%s'\n", cmd); |
139 | while ((ptr = strsep(&cmd, " ")) != NULL((void *)0)) { |
| 11 | | Loop condition is true. Entering loop body | |
|
140 | if (*ptr == '\0') |
| |
141 | continue; |
142 | if ((args[n++] = strdup(ptr)) == NULL((void *)0)) |
| 13 | | Array access (from variable 'args') results in a null pointer dereference |
|
143 | bsmtrace_error(1, "%s: strdup failed", |
144 | __func__); |
145 | } |
146 | (void) execve(args[0], args, NULL((void *)0)); |
147 | bsmtrace_error(1, "execve: %s", strerror(errno(* __error()))); |
148 | } |
149 | free(cmd); |
150 | } else |
151 | bsmtrace_error(0, "%s: expansion failed", bm->bm_trig); |
152 | } |