fetch-root.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Start a passive translator.
  2. Copyright (C) 2008 Free Software Foundation, Inc.
  3. Written by FlÃvio Cruz <flaviocruz@gmail.com>
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License as
  6. published by the Free Software Foundation; either version 2, or (at
  7. your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. #include <unistd.h>
  16. #include <assert.h>
  17. #include <string.h>
  18. #include <hurd.h>
  19. #include <hurd/fsys.h>
  20. #include <hurd/fshelp.h>
  21. #include <hurd/auth.h>
  22. #include <hurd/io.h>
  23. typedef error_t (*mycallback2_t) (int, mach_port_t *, mach_msg_type_name_t *);
  24. error_t
  25. helper_fetch_root (file_t dotdot,
  26. mycallback2_t callback2,
  27. uid_t uid, gid_t gid,
  28. char *argz, size_t argz_len, mach_port_t * control_port)
  29. {
  30. error_t err;
  31. mach_port_t control;
  32. int i;
  33. mach_port_t ports[INIT_PORT_MAX];
  34. int ints[INIT_INT_MAX];
  35. mach_port_t fds[STDERR_FILENO + 1];
  36. auth_t ourauth, newauth;
  37. mach_port_t reauth (mach_port_t port) /* Consumes PORT. */
  38. {
  39. mach_port_t rend, ret;
  40. error_t err;
  41. if (port == MACH_PORT_NULL)
  42. return port;
  43. if (ourauth == MACH_PORT_NULL)
  44. /* We have no auth server, so we aren't doing reauthentications.
  45. Just pass on our own ports directly. */
  46. return port;
  47. rend = mach_reply_port ();
  48. /* MAKE_SEND is safe here because we destroy REND ourselves. */
  49. err = io_reauthenticate (port, rend, MACH_MSG_TYPE_MAKE_SEND);
  50. mach_port_deallocate (mach_task_self (), port);
  51. if (!err)
  52. err = auth_user_authenticate (newauth, rend,
  53. MACH_MSG_TYPE_MAKE_SEND, &ret);
  54. if (err)
  55. ret = MACH_PORT_NULL;
  56. mach_port_destroy (mach_task_self (), rend);
  57. return ret;
  58. }
  59. error_t fetch_underlying (int flags, mach_port_t * underlying,
  60. mach_msg_type_name_t * underlying_type,
  61. task_t task, void *cookie)
  62. {
  63. (void)task;
  64. (void)cookie;
  65. return (*callback2) (flags, underlying, underlying_type);
  66. }
  67. ourauth = getauth ();
  68. if (ourauth == MACH_PORT_NULL)
  69. newauth = ourauth;
  70. else
  71. {
  72. uid_t uidarray[2] = { uid, uid };
  73. gid_t gidarray[2] = { gid, gid };
  74. err = auth_makeauth (ourauth, 0, MACH_MSG_TYPE_COPY_SEND, 0,
  75. uidarray, 1, uidarray, 2,
  76. gidarray, 1, gidarray, 2, &newauth);
  77. if (err)
  78. return err;
  79. }
  80. bzero (ports, INIT_PORT_MAX * sizeof (mach_port_t));
  81. bzero (fds, (STDERR_FILENO + 1) * sizeof (mach_port_t));
  82. bzero (ints, INIT_INT_MAX * sizeof (int));
  83. ports[INIT_PORT_CWDIR] = dotdot;
  84. ports[INIT_PORT_CRDIR] = reauth (getcrdir ());
  85. ports[INIT_PORT_AUTH] = newauth;
  86. fds[STDERR_FILENO] = reauth (getdport (STDERR_FILENO));
  87. err = fshelp_start_translator_long (fetch_underlying, NULL,
  88. argz, argz, argz_len,
  89. fds, MACH_MSG_TYPE_COPY_SEND,
  90. STDERR_FILENO + 1,
  91. ports, MACH_MSG_TYPE_COPY_SEND,
  92. INIT_PORT_MAX,
  93. ints, INIT_INT_MAX, uid, 0, &control);
  94. for (i = 0; i <= STDERR_FILENO; i++)
  95. mach_port_deallocate (mach_task_self (), fds[i]);
  96. for (i = 0; i < INIT_PORT_MAX; i++)
  97. if (i != INIT_PORT_CWDIR)
  98. mach_port_deallocate (mach_task_self (), ports[i]);
  99. *control_port = control;
  100. return err;
  101. }