| 1 | /*
|
|---|
| 2 | * Samba Unix/Linux SMB client library
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright (C) Gregor Beck 2011
|
|---|
| 5 | *
|
|---|
| 6 | * This program is free software; you can redistribute it and/or modify
|
|---|
| 7 | * it under the terms of the GNU General Public License as published by
|
|---|
| 8 | * the Free Software Foundation; either version 3 of the License, or
|
|---|
| 9 | * (at your option) any later version.
|
|---|
| 10 | *
|
|---|
| 11 | * This program is distributed in the hope that it will be useful,
|
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 14 | * GNU General Public License for more details.
|
|---|
| 15 | *
|
|---|
| 16 | * You should have received a copy of the GNU General Public License
|
|---|
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 18 | */
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | * @brief Notify smbd about idmap changes
|
|---|
| 22 | * @file msg_idmap.c
|
|---|
| 23 | * @author Gregor Beck <gb@sernet.de>
|
|---|
| 24 | * @date Feb 2011
|
|---|
| 25 | *
|
|---|
| 26 | */
|
|---|
| 27 |
|
|---|
| 28 | #include "includes.h"
|
|---|
| 29 | #include "smbd/smbd.h"
|
|---|
| 30 | #include "globals.h"
|
|---|
| 31 | #include "../libcli/security/dom_sid.h"
|
|---|
| 32 | #include "idmap_cache.h"
|
|---|
| 33 | #include "passdb/lookup_sid.h"
|
|---|
| 34 | #include "auth.h"
|
|---|
| 35 | #include "messages.h"
|
|---|
| 36 |
|
|---|
| 37 | struct id {
|
|---|
| 38 | union {
|
|---|
| 39 | uid_t uid;
|
|---|
| 40 | gid_t gid;
|
|---|
| 41 | struct dom_sid sid;
|
|---|
| 42 | } id;
|
|---|
| 43 | enum {UID, GID, SID} type;
|
|---|
| 44 | };
|
|---|
| 45 |
|
|---|
| 46 | static bool parse_id(const char* str, struct id* id)
|
|---|
| 47 | {
|
|---|
| 48 | struct dom_sid sid;
|
|---|
| 49 | unsigned long ul;
|
|---|
| 50 | char c, trash;
|
|---|
| 51 |
|
|---|
| 52 | if (sscanf(str, "%cID %lu%c", &c, &ul, &trash) == 2) {
|
|---|
| 53 | switch(c) {
|
|---|
| 54 | case 'G':
|
|---|
| 55 | id->id.gid = ul;
|
|---|
| 56 | id->type = GID;
|
|---|
| 57 | return true;
|
|---|
| 58 | case 'U':
|
|---|
| 59 | id->id.uid = ul;
|
|---|
| 60 | id->type = UID;
|
|---|
| 61 | return true;
|
|---|
| 62 | default:
|
|---|
| 63 | break;
|
|---|
| 64 | }
|
|---|
| 65 | } else if (string_to_sid(&sid, str)) {
|
|---|
| 66 | id->id.sid = sid;
|
|---|
| 67 | id->type = SID;
|
|---|
| 68 | return true;
|
|---|
| 69 | }
|
|---|
| 70 | return false;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | static bool uid_in_use(const struct user_struct* user, uid_t uid)
|
|---|
| 74 | {
|
|---|
| 75 | while (user) {
|
|---|
| 76 | if (user->session_info && (user->session_info->utok.uid == uid)) {
|
|---|
| 77 | return true;
|
|---|
| 78 | }
|
|---|
| 79 | user = user->next;
|
|---|
| 80 | }
|
|---|
| 81 | return false;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | static bool gid_in_use(const struct user_struct* user, gid_t gid)
|
|---|
| 85 | {
|
|---|
| 86 | while (user) {
|
|---|
| 87 | if (user->session_info != NULL) {
|
|---|
| 88 | int i;
|
|---|
| 89 | struct security_unix_token utok = user->session_info->utok;
|
|---|
| 90 | if (utok.gid == gid) {
|
|---|
| 91 | return true;
|
|---|
| 92 | }
|
|---|
| 93 | for(i=0; i<utok.ngroups; i++) {
|
|---|
| 94 | if (utok.groups[i] == gid) {
|
|---|
| 95 | return true;
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 | user = user->next;
|
|---|
| 100 | }
|
|---|
| 101 | return false;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | static bool sid_in_use(const struct user_struct* user, const struct dom_sid* psid)
|
|---|
| 105 | {
|
|---|
| 106 | uid_t uid;
|
|---|
| 107 | gid_t gid;
|
|---|
| 108 | if (sid_to_gid(psid, &gid)) {
|
|---|
| 109 | return gid_in_use(user, gid);
|
|---|
| 110 | } else if (sid_to_uid(psid, &uid)) {
|
|---|
| 111 | return uid_in_use(user, uid);
|
|---|
| 112 | }
|
|---|
| 113 | return false;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 | static bool id_in_use(const struct user_struct* user, const struct id* id)
|
|---|
| 118 | {
|
|---|
| 119 | switch(id->type) {
|
|---|
| 120 | case UID:
|
|---|
| 121 | return uid_in_use(user, id->id.uid);
|
|---|
| 122 | case GID:
|
|---|
| 123 | return gid_in_use(user, id->id.gid);
|
|---|
| 124 | case SID:
|
|---|
| 125 | return sid_in_use(user, &id->id.sid);
|
|---|
| 126 | default:
|
|---|
| 127 | break;
|
|---|
| 128 | }
|
|---|
| 129 | return false;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | static void delete_from_cache(const struct id* id)
|
|---|
| 133 | {
|
|---|
| 134 | switch(id->type) {
|
|---|
| 135 | case UID:
|
|---|
| 136 | delete_uid_cache(id->id.uid);
|
|---|
| 137 | idmap_cache_del_uid(id->id.uid);
|
|---|
| 138 | break;
|
|---|
| 139 | case GID:
|
|---|
| 140 | delete_gid_cache(id->id.gid);
|
|---|
| 141 | idmap_cache_del_gid(id->id.gid);
|
|---|
| 142 | break;
|
|---|
| 143 | case SID:
|
|---|
| 144 | delete_sid_cache(&id->id.sid);
|
|---|
| 145 | idmap_cache_del_sid(&id->id.sid);
|
|---|
| 146 | break;
|
|---|
| 147 | default:
|
|---|
| 148 | break;
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 | static void message_idmap_flush(struct messaging_context *msg_ctx,
|
|---|
| 154 | void* private_data,
|
|---|
| 155 | uint32_t msg_type,
|
|---|
| 156 | struct server_id server_id,
|
|---|
| 157 | DATA_BLOB* data)
|
|---|
| 158 | {
|
|---|
| 159 | const char* msg = data ? (const char*)data->data : NULL;
|
|---|
| 160 |
|
|---|
| 161 | if ((msg == NULL) || (msg[0] == '\0')) {
|
|---|
| 162 | flush_gid_cache();
|
|---|
| 163 | flush_uid_cache();
|
|---|
| 164 | } else if (strncmp(msg, "GID", 3)) {
|
|---|
| 165 | flush_gid_cache();
|
|---|
| 166 | } else if (strncmp(msg, "UID", 3)) {
|
|---|
| 167 | flush_uid_cache();
|
|---|
| 168 | } else {
|
|---|
| 169 | DEBUG(0, ("Invalid argument: %s\n", msg));
|
|---|
| 170 | }
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 | static void message_idmap_delete(struct messaging_context *msg_ctx,
|
|---|
| 175 | void* private_data,
|
|---|
| 176 | uint32_t msg_type,
|
|---|
| 177 | struct server_id server_id,
|
|---|
| 178 | DATA_BLOB* data)
|
|---|
| 179 | {
|
|---|
| 180 | const char* msg = (data && data->data) ? (const char*)data->data : "<NULL>";
|
|---|
| 181 | bool do_kill = (msg_type == MSG_IDMAP_KILL);
|
|---|
| 182 | struct user_struct* validated_users = smbd_server_conn->smb1.sessions.validated_users;
|
|---|
| 183 | struct id id;
|
|---|
| 184 |
|
|---|
| 185 | if (!parse_id(msg, &id)) {
|
|---|
| 186 | DEBUG(0, ("Invalid ?ID: %s\n", msg));
|
|---|
| 187 | return;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | if( do_kill && id_in_use(validated_users, &id) ) {
|
|---|
| 191 | exit_server_cleanly(msg);
|
|---|
| 192 | } else {
|
|---|
| 193 | delete_from_cache(&id);
|
|---|
| 194 | }
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | void msg_idmap_register_msgs(struct messaging_context *ctx)
|
|---|
| 198 | {
|
|---|
| 199 | messaging_register(ctx, NULL, MSG_IDMAP_FLUSH, message_idmap_flush);
|
|---|
| 200 | messaging_register(ctx, NULL, MSG_IDMAP_DELETE, message_idmap_delete);
|
|---|
| 201 | messaging_register(ctx, NULL, MSG_IDMAP_KILL, message_idmap_delete);
|
|---|
| 202 | }
|
|---|