| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | #include <stdlib.h> |
| 22 | #include <stdio.h> |
| 23 | #include <unistd.h> |
| 24 | #include <string.h> |
| 25 | #include <hurd.h> |
| 26 | #include <error.h> |
| 27 | |
| 28 | #include "frobauth.h" |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | error_t |
| 40 | frobauth_modify (struct frobauth *frobauth, |
| 41 | const auth_t *auths, size_t num_auths, |
| 42 | error_t (*modify) (struct ugids *ugids, |
| 43 | const struct ugids *change, |
| 44 | pid_t pid, void *hook), |
| 45 | void (*print_info) (const struct ugids *new, |
| 46 | const struct ugids *old, |
| 47 | const struct ugids *change, |
| 48 | pid_t pid, void *hook), |
| 49 | void *hook) |
| 50 | { |
| 51 | int i; |
| 52 | int ok = 1; |
| 53 | size_t num_all_auths = num_auths + 1; |
| 54 | auth_t all_auths[num_all_auths]; |
| 55 | pid_t cur_pid = getpid (); |
| 56 | process_t proc_server = getproc (); |
| 57 | |
| 58 | bcopy (auths, all_auths, num_auths * sizeof *auths); |
| 59 | |
| 60 | |
| 61 | for (i = 0; i < frobauth->num_pids; i++) |
| 62 | if (frobauth->pids[i] != cur_pid) |
| 63 | { |
| 64 | mach_port_t msgport; |
| 65 | pid_t pid = frobauth->pids[i]; |
| 66 | error_t err = proc_getmsgport (proc_server, pid, &msgport); |
| 67 | |
| 68 | if (err) |
| 69 | error (0, err, "%d: Cannot get message port", pid); |
| 70 | else |
| 71 | { |
| 72 | task_t task; |
| 73 | |
| 74 | err = proc_pid2task (proc_server, pid, &task); |
| 75 | if (err) |
| 76 | error (0, err, "%d", pid); |
| 77 | else |
| 78 | { |
| 79 | auth_t old_auth; |
| 80 | |
| 81 | err = msg_get_init_port (msgport, task, INIT_PORT_AUTH, |
| 82 | &old_auth); |
| 83 | if (err) |
| 84 | error (0, err, "%d: Cannot get auth port", pid); |
| 85 | else |
| 86 | { |
| 87 | struct ugids old = UGIDS_INIT{ { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 } }; |
| 88 | |
| 89 | err = ugids_merge_auth (&old, old_auth); |
| 90 | |
| 91 | if (err) |
| 92 | error (0, err, "%d: Cannot get auth port ids", pid); |
| 93 | else |
| 94 | { |
| 95 | struct ugids new = UGIDS_INIT{ { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 } }; |
| 96 | |
| 97 | |
| 98 | |
| 99 | ugids_imply_all (&old); |
| 100 | |
| 101 | err = ugids_set (&new, &old); |
| Value stored to 'err' is never read |
| 102 | |
| 103 | err = (*modify) (&new, &frobauth->ugids, pid, hook); |
| 104 | if (err) |
| 105 | error (99, err, "%d: Cannot modify ids", pid); |
| 106 | else if (! ugids_equal (&new, &old)) |
| 107 | { |
| 108 | if (! frobauth->dry_run) |
| 109 | { |
| 110 | auth_t new_auth; |
| 111 | |
| 112 | |
| 113 | |
| 114 | all_auths[num_all_auths - 1] = old_auth; |
| 115 | |
| 116 | err = ugids_make_auth (&new, |
| 117 | all_auths, |
| 118 | num_all_auths, |
| 119 | &new_auth); |
| 120 | if (err) |
| 121 | error (0, err, |
| 122 | "%d: Authentication failure", pid); |
| 123 | else |
| 124 | { |
| 125 | err = |
| 126 | msg_set_init_port (msgport, task, |
| 127 | INIT_PORT_AUTH, |
| 128 | new_auth, |
| 129 | MACH_MSG_TYPE_COPY_SEND19); |
| 130 | mach_port_deallocate (mach_task_self ()((__mach_task_self_ + 0)), |
| 131 | new_auth); |
| 132 | if (err) |
| 133 | error (0, err, "%d", pid); |
| 134 | } |
| 135 | |
| 136 | } |
| 137 | |
| 138 | if (frobauth->verbose && !err) |
| 139 | (*print_info) (&new, &old, &frobauth->ugids, |
| 140 | pid, hook); |
| 141 | |
| 142 | } |
| 143 | else if (frobauth->verbose) |
| 144 | printf ("%d: Nothing changed\n", pid); |
| 145 | |
| 146 | ugids_fini (&new); |
| 147 | } |
| 148 | |
| 149 | ugids_fini (&old); |
| 150 | mach_port_deallocate (mach_task_self ()((__mach_task_self_ + 0)), old_auth); |
| 151 | } |
| 152 | mach_port_deallocate (mach_task_self ()((__mach_task_self_ + 0)), task); |
| 153 | } |
| 154 | mach_port_deallocate (mach_task_self ()((__mach_task_self_ + 0)), msgport); |
| 155 | } |
| 156 | |
| 157 | if (err) |
| 158 | ok = 0; |
| 159 | } |
| 160 | |
| 161 | return ok; |
| 162 | } |