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 | #include <stdio.h> |
29 | #include <X11/Xos.h> |
30 | #include <errno(*__error()).h> |
31 | #include <stdlib.h> |
32 | #include <ctype.h> |
33 | #include <sys/types.h> |
34 | #include <sys/wait.h> |
35 | |
36 | #include "server.h" |
37 | |
38 | static char *Strupr ( char *s0 ); |
39 | static struct auth_info *find_or_create_auth ( char *s ); |
40 | static char * expand ( char *s, int ac, char **av ); |
41 | |
42 | struct list_of_argv { |
43 | struct list_of_argv *next; |
44 | int argc; |
45 | char **argv; |
46 | }; |
47 | |
48 | struct auth_info { |
49 | struct auth_info *next; |
50 | char *name; |
51 | struct list_of_argv *data; |
52 | char **program; |
53 | char **input; |
54 | }; |
55 | |
56 | struct auth_info *auth_schemes = NULL((void *)0); |
57 | |
58 | static char * |
59 | Strupr(char *s0) |
60 | { |
61 | char *s; |
62 | |
63 | for(s = s0; *s; s++) { |
64 | if(islower(*s)) *s = toupper(*s); |
65 | } |
66 | return s0; |
67 | } |
68 | |
69 | |
70 | |
71 | struct auth_info * |
72 | find_or_create_auth(char *s) |
73 | { |
74 | struct auth_info *auth; |
75 | |
76 | Strupr(s); |
77 | |
78 | for(auth = auth_schemes; auth; auth=auth->next) { |
79 | if(!strcmp(s, auth->name)) return auth; |
80 | } |
81 | |
82 | auth = (struct auth_info *)malloc(sizeof(*auth)); |
83 | if(!auth) nomem(); |
84 | |
85 | auth->next = auth_schemes; |
86 | auth->name = s; |
87 | auth->data = NULL((void *)0); |
88 | auth->program = NULL((void *)0); |
89 | auth->input = NULL((void *)0); |
90 | auth_schemes = auth; |
91 | |
92 | return auth; |
93 | } |
94 | |
95 | void |
96 | key_auth(int ac, char **av) |
97 | { |
98 | struct list_of_argv *lav; |
99 | struct auth_info *auth; |
100 | |
101 | if(ac < 2) { |
| |
| |
102 | printf( |
103 | "%s: Failure: Malformed AUTH\n",myname); |
104 | exit(255); |
105 | } |
106 | |
107 | auth = find_or_create_auth(av[1]); |
108 | |
109 | lav = (struct list_of_argv *)malloc(sizeof(*lav)); |
| |
110 | if(!lav) nomem(); |
| |
| |
111 | |
112 | lav->next = auth->data; |
| 6 | | Access to field 'next' results in a dereference of a null pointer (loaded from variable 'lav') |
|
113 | lav->argc = ac-2; |
114 | lav->argv = av+2; |
115 | auth->data = lav; |
116 | } |
117 | |
118 | void |
119 | key_internal_auth_program(int ac, char **av) |
120 | { |
121 | struct auth_info *auth; |
122 | |
123 | if(ac < 4) { |
124 | printf( |
125 | "%s: Failure: Malformed INTERNAL-AUTH-PROGRAM\n",myname); |
126 | exit(255); |
127 | } |
128 | |
129 | auth = find_or_create_auth(av[1]); |
130 | auth->program = av + 2; |
131 | } |
132 | |
133 | void |
134 | key_internal_auth_input(int ac, char **av) |
135 | { |
136 | struct auth_info *auth; |
137 | |
138 | if(ac < 2) { |
139 | printf( |
140 | "%s: Failure: Malformed INTERNAL-AUTH-INPUT\n",myname); |
141 | exit(255); |
142 | } |
143 | |
144 | auth = find_or_create_auth(av[1]); |
145 | auth->input = av + 2; |
146 | } |
147 | |
148 | void |
149 | do_auth(void) |
150 | { |
151 | struct auth_info *auth; |
152 | int p[2]; |
153 | char **pp; |
154 | struct list_of_argv *lav; |
155 | char *s; |
156 | int pid; |
157 | int status; |
158 | |
159 | for(auth = auth_schemes; auth; auth = auth->next) { |
160 | if(!auth->data) continue; |
161 | if(!auth->program) { |
162 | printf( |
163 | "%s: Warning: no %s authorization program specified in this context\n",myname, |
164 | auth->name); |
165 | continue; |
166 | } |
167 | |
168 | if(pipe(p)) { |
169 | printf("%s: Error: pipe - %s\n",myname, strerror(errno(*__error()))); |
170 | exit(255); |
171 | } |
172 | |
173 | fflush(stdout__stdoutp); |
174 | |
175 | switch(pid = fork()) { |
176 | case -1: |
177 | printf("%s: Error: fork - %s\n",myname, strerror(errno(*__error()))); |
178 | exit(255); |
179 | case 0: |
180 | close(0); |
181 | dup(p[0]); |
182 | close(p[0]); |
183 | close(p[1]); |
184 | execvp(auth->program[0], auth->program+1); |
185 | printf("%s: Error: %s - %s\n",myname, auth->program[0], |
186 | strerror(errno(*__error()))); |
187 | exit(255); |
188 | break; |
189 | default: |
190 | close(p[0]); |
191 | for(lav = auth->data; lav; lav=lav->next) { |
192 | for(pp = auth->input; *pp; pp++) { |
193 | s = expand(*pp, lav->argc, lav->argv); |
194 | write(p[1], s, strlen(s)); |
195 | write(p[1], "\n", 1); |
196 | } |
197 | } |
198 | close(p[1]); |
199 | while(wait(&status) != pid) ; |
200 | if(status) { |
201 | printf( |
202 | "%s: Warning: %s authorization setup failed\n",myname, auth->name); |
203 | } |
204 | break; |
205 | } |
206 | } |
207 | } |
208 | |
209 | char * |
210 | expand(char *s, int ac, char **av) |
211 | { |
212 | static char buf[BUFSIZ1024]; |
213 | char *p; |
214 | int i; |
215 | |
216 | p = buf; |
217 | while(*s) { |
218 | if(*s == '$') { |
219 | s++; |
220 | if(*s == '$') { |
221 | *p++ = *s++; |
222 | continue; |
223 | } |
224 | if(!isdigit(*s)) { |
225 | printf( |
226 | "%s: Failure: bad $ in configuration: non-digit after $\n",myname); |
227 | exit(255); |
228 | } |
229 | i = (int)strtol(s, &s, 10); |
230 | if(i > ac) { |
231 | printf( |
232 | "%s: Failure: not enough arguments to AUTH\n",myname); |
233 | exit(255); |
234 | } |
235 | strcpy(p, av[i-1])__builtin___strcpy_chk (p, av[i-1], __builtin_object_size (p, 2 > 1 ? 1 : 0)); |
236 | p += strlen(p); |
237 | } else *p++ = *s++; |
238 | } |
239 | *p = '\0'; |
240 | |
241 | return buf; |
242 | } |