Debian Codegraph @

auth/auth.c

raw
1 /* Authentication server.
2 Copyright (C) 1996,97,98,99,2002 Free Software Foundation, Inc.
3 Written by Roland McGrath.
4
5 This file is part of the GNU Hurd.
6
7 The GNU Hurd is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2, or (at
10 your option) any later version.
11
12 The GNU Hurd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include <stddef.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <mach.h>
25 #include <pthread.h>
26 #include <hurd.h>
27 #include <hurd/startup.h>
28 #include <hurd/paths.h>
29 #include <hurd/ports.h>
30 #include <hurd/ihash.h>
31 #include <idvec.h>
32 #include <assert-backtrace.h>
33 #include <argp.h>
34 #include <error.h>
35 #include <version.h>
36 #include "auth_S.h"
37 #include "auth_reply_U.h"
38
39 #include "auth.h"
40
41 const char *argp_program_version = STANDARD_HURD_VERSION(auth);
42
43
44 /* Auth handles are server ports with sets of ids. */
45 struct authhandle
46 {
47 struct port_info pi;
48 struct idvec euids, egids, auids, agids;
49 };
50
51 struct port_bucket *auth_bucket;
52 struct port_class *authhandle_portclass;
53
54
55 /* Create a new auth port. */
56
57 static error_t
58 create_authhandle (struct authhandle **new)
59 {
60 error_t err = ports_create_port (authhandle_portclass, auth_bucket,
61 sizeof **new, new);
62 if (! err)
63 memset (&(*new)->euids, 0, (void *)&(*new)[1] - (void *)&(*new)->euids);
64 return err;
65 }
66
67 /* Clean up a dead auth port. */
68
69 static void
70 destroy_authhandle (void *p)
71 {
72 struct authhandle *h = p;
73 idvec_free_contents (&h->euids);
74 idvec_free_contents (&h->egids);
75 idvec_free_contents (&h->auids);
76 idvec_free_contents (&h->agids);
77 }
78
79 /* id management. */
80
81 static inline void
82 idvec_copyout (struct idvec *idvec, uid_t **ids,
83 mach_msg_type_number_t *nids)
84 {
85 if (idvec->num > *nids)
86 *ids = idvec->ids;
87 else if (idvec->num)
88 memcpy (*ids, idvec->ids, idvec->num * sizeof *ids);
89 *nids = idvec->num;
90 }
91
92 #define C(auth, ids) idvec_copyout (&auth->ids, ids, n##ids)
93 #define OUTIDS(auth) (C (auth, euids), C (auth, egids), \
94 C (auth, auids), C (auth, agids))
95
96 /* Implement auth_getids as described in <hurd/auth.defs>. */
97 kern_return_t
98 S_auth_getids (struct authhandle *auth,
99 uid_t **euids,
100 mach_msg_type_number_t *neuids,
101 uid_t **auids,
102 mach_msg_type_number_t *nauids,
103 uid_t **egids,
104 mach_msg_type_number_t *negids,
105 uid_t **agids,
106 mach_msg_type_number_t *nagids)
107 {
108 if (! auth)
109 return EOPNOTSUPP;
110
111 OUTIDS (auth);
112
113 return 0;
114 }
115
116 /* Implement auth_makeauth as described in <hurd/auth.defs>. */
117 kern_return_t
118 S_auth_makeauth (struct authhandle *auth,
119 const mach_port_t *authpts, mach_msg_type_number_t nauths,
120 const uid_t *euids, mach_msg_type_number_t neuids,
121 const uid_t *auids, mach_msg_type_number_t nauids,
122 const uid_t *egids, mach_msg_type_number_t negids,
123 const uid_t *agids, mach_msg_type_number_t nagids,
124 mach_port_t *newhandle)
125 {
126 struct authhandle *newauth, *auths[1 + nauths];
127 int hasroot = 0;
128 error_t err;
129 size_t i, j;
130
131 if (!auth)
132 return EOPNOTSUPP;
133
134 auths[0] = auth;
135
136 /* Fetch the auth structures for all the ports passed in. */
137 for (i = 0; i < nauths; i++)
138 auths[i + 1] = auth_port_to_handle (authpts[i]);
139
140 ++nauths;
141
142 /* Verify that the union of the handles passed in either contains euid 0
143 (root), or contains all the requested ids. */
144
145 #define isuid(uid, auth) \
146 (idvec_contains (&(auth)->euids, uid) \
147 || idvec_contains (&(auth)->auids, uid))
148 #define groupmember(gid, auth) \
149 (idvec_contains (&(auth)->egids, gid) \
150 || idvec_contains (&(auth)->agids, gid))
151 #define isroot(auth) isuid (0, auth)
152
153 for (i = 0; i < nauths; i++)
154 if (auths[i] && isroot (auths[i]))
155 {
156 hasroot = 1;
157 break;
158 }
159
160 if (!hasroot)
161 {
162 int has_it;
163
164 for (i = 0; i < neuids; i++)
165 {
166 has_it = 0;
167 for (j = 0; j < nauths; j++)
168 if (auths[j] && isuid (euids[i], auths[j]))
169 {
170 has_it = 1;
171 break;
172 }
173 if (!has_it)
174 goto eperm;
175 }
176
177 for (i = 0; i < nauids; i++)
178 {
179 has_it = 0;
180 for (j = 0; j < nauths; j++)
181 if (auths[j] && isuid (auids[i], auths[j]))
182 {
183 has_it = 1;
184 break;
185 }
186 if (!has_it)
187 goto eperm;
188 }
189
190 for (i = 0; i < negids; i++)
191 {
192 has_it = 0;
193 for (j = 0; j < nauths; j++)
194 if (auths[j] && groupmember (egids[i], auths[j]))
195 {
196 has_it = 1;
197 break;
198 }
199 if (!has_it)
200 goto eperm;
201 }
202
203 for (i = 0; i < nagids; i++)
204 {
205 has_it = 0;
206 for (j = 0; j < nauths; j++)
207 if (auths[j] && groupmember (agids[i], auths[j]))
208 {
209 has_it = 1;
210 break;
211 }
212 if (!has_it)
213 goto eperm;
214 }
215 }
216
217 err = create_authhandle (&newauth);
218
219 /* Create a new handle with the specified ids. */
220
221 #define MERGE S (euids); S (egids); S (auids); S (agids);
222 #define S(uids) if (!err) err = idvec_merge_ids (&newauth->uids, uids, n##uids)
223
224 MERGE;
225
226 #undef S
227
228 if (! err)
229 {
230 for (j = 1; j < nauths; ++j)
231 mach_port_deallocate (mach_task_self (), authpts[j - 1]);
232 *newhandle = ports_get_right (newauth);
233 ports_port_deref (newauth);
234 }
235
236 for (j = 1; j < nauths; j++)
237 if (auths[j])
238 ports_port_deref (auths[j]);
239 return err;
240
241 eperm:
242 for (j = 1; j < nauths; j++)
243 if (auths[j])
244 ports_port_deref (auths[j]);
245 return EPERM;
246 }
247
248 /* Transaction handling. */
249
250 /* Since the user is responsible for freeing the rendezvous port, it has to
251 * wait for the server to have finished transmitting uids.
252 *
253 * The server thus waits for the user to give it uids (unless it was already
254 * there), transmits them and provides the passthrough port.
255 *
256 * The user gives the uids and waits for the passthrough port from the server.
257 *
258 * If the user is early, it has to tell the server it arrived.
259 */
260
261 /* A pending user. */
262 struct pending_user
263 {
264 hurd_ihash_locp_t locp; /* Position in the pending_users ihash table. */
265 pthread_cond_t wakeup; /* The reader is blocked on this condition. */
266
267 /* The user's auth handle. */
268 struct authhandle *user;
269
270 /* The port to pass back to the user. */
271 mach_port_t passthrough;
272 };
273
274 /* A pending server. */
275 struct pending_server
276 {
277 hurd_ihash_locp_t locp; /* Position in the pending_servers ihash table. */
278 pthread_cond_t wakeup; /* The server is blocked on this condition. */
279 };
280
281 /* Table of pending transactions keyed on RENDEZVOUS. */
282 struct hurd_ihash pending_users
283 = HURD_IHASH_INITIALIZER (offsetof (struct pending_user, locp));
284 struct hurd_ihash pending_servers
285 = HURD_IHASH_INITIALIZER (offsetof (struct pending_server, locp));
286 pthread_mutex_t pending_lock = PTHREAD_MUTEX_INITIALIZER;
287
288 /* Implement auth_user_authenticate as described in <hurd/auth.defs>. */
289 kern_return_t
290 S_auth_user_authenticate (struct authhandle *userauth,
291 mach_port_t reply,
292 mach_msg_type_name_t reply_type,
293 mach_port_t rendezvous,
294 mach_port_t *newport,
295 mach_msg_type_name_t *newporttype)
296 {
297 struct pending_server *s;
298 struct pending_user u;
299 error_t err;
300
301 if (! userauth)
302 return EOPNOTSUPP;
303
304 if (! MACH_PORT_VALID (rendezvous))
305 return EINVAL;
306
307 u.user = userauth;
308 pthread_cond_init (&u.wakeup, NULL);
309
310 pthread_mutex_lock (&pending_lock);
311
312 err = hurd_ihash_add (&pending_users, rendezvous, &u);
313 if (err) {
314 pthread_mutex_unlock (&pending_lock);
315 return err;
316 }
317
318 /* Give the server the auth port.
319 We need to add a ref in case the port dies. */
320 ports_port_ref (userauth);
321
322 /* Look for this rendezvous in the server list. */
323 s = hurd_ihash_find (&pending_servers, rendezvous);
324 if (s) {
325 /* Found it! */
326
327 /* Remove it from the pending list. */
328 hurd_ihash_locp_remove (&pending_servers, s->locp);
329
330 /* Tell it we eventually arrived. */
331 pthread_cond_signal (&s->wakeup);
332 }
333
334 ports_interrupt_self_on_port_death (userauth, rendezvous);
335 /* Wait for server answer. */
336 if (pthread_hurd_cond_wait_np (&u.wakeup, &pending_lock) &&
337 hurd_ihash_find (&pending_users, rendezvous))
338 /* We were interrupted; remove our record. */
339 {
340 hurd_ihash_locp_remove (&pending_users, u.locp);
341
342 /* Was it a normal interruption or did RENDEZVOUS die? */
343 mach_port_type_t type;
344 mach_port_type (mach_task_self (), rendezvous, &type);
345 err = type & MACH_PORT_TYPE_DEAD_NAME ? EINVAL : EINTR;
346 }
347
348 pthread_mutex_unlock (&pending_lock);
349
350 if (! err)
351 {
352 /* Extract the port. */
353 *newport = u.passthrough;
354 *newporttype = MACH_MSG_TYPE_MOVE_SEND;
355 mach_port_deallocate (mach_task_self (), rendezvous);
356 }
357
358 return err;
359 }
360
361 /* Implement auth_server_authenticate as described in <hurd/auth.defs>. */
362 kern_return_t
363 S_auth_server_authenticate (struct authhandle *serverauth,
364 mach_port_t reply,
365 mach_msg_type_name_t reply_type,
366 mach_port_t rendezvous,
367 mach_port_t newport,
368 mach_msg_type_name_t newport_type,
369 uid_t **euids,
370 mach_msg_type_number_t *neuids,
371 uid_t **auids,
372 mach_msg_type_number_t *nauids,
373 uid_t **egids,
374 mach_msg_type_number_t *negids,
375 uid_t **agids,
376 mach_msg_type_number_t *nagids)
377 {
378 struct pending_user *u;
379 struct authhandle *user;
380 error_t err = 0;
381
382 if (! serverauth)
383 return EOPNOTSUPP;
384
385 if (! MACH_PORT_VALID (rendezvous))
386 return EINVAL;
387
388 pthread_mutex_lock (&pending_lock);
389
390 /* Look for this rendezvous in the user list. */
391 u = hurd_ihash_find (&pending_users, rendezvous);
392 if (! u)
393 {
394 /* User not here yet, have to wait for it. */
395 struct pending_server s;
396 pthread_cond_init (&s.wakeup, NULL);
397 err = hurd_ihash_add (&pending_servers, rendezvous, &s);
398 if (! err)
399 {
400 ports_interrupt_self_on_port_death (serverauth, rendezvous);
401 if (pthread_hurd_cond_wait_np (&s.wakeup, &pending_lock) &&
402 hurd_ihash_find (&pending_servers, rendezvous))
403 /* We were interrupted; remove our record. */
404 {
405 hurd_ihash_locp_remove (&pending_servers, s.locp);
406
407 /* Was it a normal interruption or did RENDEZVOUS die? */
408 mach_port_type_t type;
409 mach_port_type (mach_task_self (), rendezvous, &type);
410 err = type & MACH_PORT_TYPE_DEAD_NAME ? EINVAL : EINTR;
411 }
412 else
413 {
414 u = hurd_ihash_find (&pending_users, rendezvous);
415 if (! u)
416 /* User still not here, odd! */
417 err = EINTR;
418 }
419 }
420 }
421
422 if (u)
423 {
424 error_t err2;
425
426 /* Remove it from the pending list. */
427 hurd_ihash_locp_remove (&pending_users, u->locp);
428
429 /* Found it! */
430 user = u->user;
431
432 pthread_mutex_unlock (&pending_lock);
433
434 /* Tell third party. */
435 err2 = auth_server_authenticate_reply (reply, reply_type, 0,
436 user->euids.ids, user->euids.num,
437 user->auids.ids, user->auids.num,
438 user->egids.ids, user->egids.num,
439 user->agids.ids, user->agids.num);
440
441 if (err2)
442 mach_port_deallocate (mach_task_self (), reply);
443
444 pthread_mutex_lock (&pending_lock);
445
446 /* Give the user the new port and wake the RPC up. */
447 u->passthrough = newport;
448
449 pthread_cond_signal (&u->wakeup);
450 }
451
452 pthread_mutex_unlock (&pending_lock);
453
454 if (err)
455 return err;
456
457 ports_port_deref (user);
458 mach_port_deallocate (mach_task_self (), rendezvous);
459 return MIG_NO_REPLY;
460 }
461
462
463 #include "../libports/notify_S.h"
464 #include "../libports/interrupt_S.h"
465
466 static int
467 auth_demuxer (mach_msg_header_t *inp, mach_msg_header_t *outp)
468 {
469 mig_routine_t routine;
470 if ((routine = auth_server_routine (inp)) ||
471 (routine = ports_interrupt_server_routine (inp)) ||
472 (routine = ports_notify_server_routine (inp)))
473 {
474 (*routine) (inp, outp);
475 return TRUE;
476 }
477 else
478 return FALSE;
479 }
480
481
482 int
483 main (int argc, char **argv)
484 {
485 error_t err;
486 mach_port_t boot;
487 mach_port_t startup;
488 process_t proc;
489 mach_port_t hostpriv, masterdev;
490 struct authhandle *firstauth;
491 struct argp argp = { 0, 0, 0, "Hurd standard authentication server." };
492
493 argp_parse (&argp, argc, argv, 0, 0, 0);
494
495 auth_bucket = ports_create_bucket ();
496 authhandle_portclass = ports_create_class (&destroy_authhandle, 0);
497
498 /* Create the initial root auth handle. */
499 err = create_authhandle (&firstauth);
500 assert_perror_backtrace (err);
501 idvec_add (&firstauth->euids, 0);
502 idvec_add (&firstauth->auids, 0);
503 idvec_add (&firstauth->auids, 0);
504 idvec_merge (&firstauth->egids, &firstauth->euids);
505 idvec_merge (&firstauth->agids, &firstauth->auids);
506
507 /* Fetch our bootstrap port and contact startup. */
508 err = task_get_bootstrap_port (mach_task_self (), &boot);
509 assert_perror_backtrace (err);
510 if (boot == MACH_PORT_NULL)
511 error (2, 0, "auth server can only be run by init during boot");
512 err = startup_authinit (boot, ports_get_right (firstauth),
513 MACH_MSG_TYPE_MAKE_SEND, &proc);
514 if (err)
515 error (2, err, "cannot contact init for bootstrap");
516
517 /* Register ourselves with the proc server and then start signals. */
518 proc_getprivports (proc, &hostpriv, &masterdev);
519 proc_register_version (proc, hostpriv, "auth", "", HURD_VERSION);
520 mach_port_deallocate (mach_task_self (), masterdev);
521 _hurd_port_set (&_hurd_ports[INIT_PORT_PROC], proc);
522 _hurd_proc_init (argv, NULL, 0);
523
524 startup = file_name_lookup (_SERVERS_STARTUP, 0, 0);
525 if (! MACH_PORT_VALID (startup))
526 {
527 error (0, errno, "%s", _SERVERS_STARTUP);
528 /* Fall back to using the bootstrap port as before. */
529 startup = boot;
530 }
531
532 /* Init knows intimately that we will be ready for messages
533 as soon as this returns. */
534 startup_essential_task (startup, mach_task_self (), MACH_PORT_NULL, "auth",
535 hostpriv);
536
537 if (startup != boot)
538 mach_port_deallocate (mach_task_self (), startup);
539 mach_port_deallocate (mach_task_self (), boot);
540 mach_port_deallocate (mach_task_self (), hostpriv);
541
542 /* Be a server. */
543 while (1)
544 ports_manage_port_operations_multithread (auth_bucket,
545 auth_demuxer,
546 30 * 1000, 0, 0);
547 }