| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 | ID Mapping
|
|---|
| 4 | Copyright (C) Simo Sorce 2003
|
|---|
| 5 | Copyright (C) Jeremy Allison 2006
|
|---|
| 6 |
|
|---|
| 7 | This program is free software; you can redistribute it and/or modify
|
|---|
| 8 | it under the terms of the GNU General Public License as published by
|
|---|
| 9 | the Free Software Foundation; either version 3 of the License, or
|
|---|
| 10 | (at your option) any later version.
|
|---|
| 11 |
|
|---|
| 12 | This program is distributed in the hope that it will be useful,
|
|---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 15 | GNU 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, see <http://www.gnu.org/licenses/>.*/
|
|---|
| 19 |
|
|---|
| 20 | #include "includes.h"
|
|---|
| 21 |
|
|---|
| 22 | #undef DBGC_CLASS
|
|---|
| 23 | #define DBGC_CLASS DBGC_IDMAP
|
|---|
| 24 |
|
|---|
| 25 | /*****************************************************************
|
|---|
| 26 | Returns the SID mapped to the given UID.
|
|---|
| 27 | If mapping is not possible returns an error.
|
|---|
| 28 | *****************************************************************/
|
|---|
| 29 |
|
|---|
| 30 | NTSTATUS idmap_uid_to_sid(const char *domname, DOM_SID *sid, uid_t uid)
|
|---|
| 31 | {
|
|---|
| 32 | NTSTATUS ret;
|
|---|
| 33 | struct id_map map;
|
|---|
| 34 | bool expired;
|
|---|
| 35 |
|
|---|
| 36 | DEBUG(10,("idmap_uid_to_sid: uid = [%lu], domain = '%s'\n",
|
|---|
| 37 | (unsigned long)uid, domname?domname:"NULL"));
|
|---|
| 38 |
|
|---|
| 39 | if (idmap_cache_find_uid2sid(uid, sid, &expired)) {
|
|---|
| 40 | DEBUG(10, ("idmap_cache_find_uid2sid found %d%s\n", uid,
|
|---|
| 41 | expired ? " (expired)": ""));
|
|---|
| 42 | if (expired && idmap_is_online()) {
|
|---|
| 43 | DEBUG(10, ("revalidating expired entry\n"));
|
|---|
| 44 | goto backend;
|
|---|
| 45 | }
|
|---|
| 46 | if (is_null_sid(sid)) {
|
|---|
| 47 | DEBUG(10, ("Returning negative cache entry\n"));
|
|---|
| 48 | return NT_STATUS_NONE_MAPPED;
|
|---|
| 49 | }
|
|---|
| 50 | DEBUG(10, ("Returning positive cache entry\n"));
|
|---|
| 51 | return NT_STATUS_OK;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | backend:
|
|---|
| 55 | map.sid = sid;
|
|---|
| 56 | map.xid.type = ID_TYPE_UID;
|
|---|
| 57 | map.xid.id = uid;
|
|---|
| 58 |
|
|---|
| 59 | ret = idmap_backends_unixid_to_sid(domname, &map);
|
|---|
| 60 | if ( ! NT_STATUS_IS_OK(ret)) {
|
|---|
| 61 | DEBUG(10, ("error mapping uid [%lu]\n", (unsigned long)uid));
|
|---|
| 62 | return ret;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | if (map.status != ID_MAPPED) {
|
|---|
| 66 | struct dom_sid null_sid;
|
|---|
| 67 | ZERO_STRUCT(null_sid);
|
|---|
| 68 | idmap_cache_set_sid2uid(&null_sid, uid);
|
|---|
| 69 | DEBUG(10, ("uid [%lu] not mapped\n", (unsigned long)uid));
|
|---|
| 70 | return NT_STATUS_NONE_MAPPED;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | idmap_cache_set_sid2uid(sid, uid);
|
|---|
| 74 |
|
|---|
| 75 | return NT_STATUS_OK;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | /*****************************************************************
|
|---|
| 79 | Returns SID mapped to the given GID.
|
|---|
| 80 | If mapping is not possible returns an error.
|
|---|
| 81 | *****************************************************************/
|
|---|
| 82 |
|
|---|
| 83 | NTSTATUS idmap_gid_to_sid(const char *domname, DOM_SID *sid, gid_t gid)
|
|---|
| 84 | {
|
|---|
| 85 | NTSTATUS ret;
|
|---|
| 86 | struct id_map map;
|
|---|
| 87 | bool expired;
|
|---|
| 88 |
|
|---|
| 89 | DEBUG(10,("idmap_gid_to_si: gid = [%lu], domain = '%s'\n",
|
|---|
| 90 | (unsigned long)gid, domname?domname:"NULL"));
|
|---|
| 91 |
|
|---|
| 92 | if (idmap_cache_find_gid2sid(gid, sid, &expired)) {
|
|---|
| 93 | DEBUG(10, ("idmap_cache_find_gid2sid found %d%s\n", gid,
|
|---|
| 94 | expired ? " (expired)": ""));
|
|---|
| 95 | if (expired && idmap_is_online()) {
|
|---|
| 96 | DEBUG(10, ("revalidating expired entry\n"));
|
|---|
| 97 | goto backend;
|
|---|
| 98 | }
|
|---|
| 99 | if (is_null_sid(sid)) {
|
|---|
| 100 | DEBUG(10, ("Returning negative cache entry\n"));
|
|---|
| 101 | return NT_STATUS_NONE_MAPPED;
|
|---|
| 102 | }
|
|---|
| 103 | DEBUG(10, ("Returning positive cache entry\n"));
|
|---|
| 104 | return NT_STATUS_OK;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | backend:
|
|---|
| 108 | map.sid = sid;
|
|---|
| 109 | map.xid.type = ID_TYPE_GID;
|
|---|
| 110 | map.xid.id = gid;
|
|---|
| 111 |
|
|---|
| 112 | ret = idmap_backends_unixid_to_sid(domname, &map);
|
|---|
| 113 | if ( ! NT_STATUS_IS_OK(ret)) {
|
|---|
| 114 | DEBUG(10, ("error mapping gid [%lu]\n", (unsigned long)gid));
|
|---|
| 115 | return ret;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | if (map.status != ID_MAPPED) {
|
|---|
| 119 | struct dom_sid null_sid;
|
|---|
| 120 | ZERO_STRUCT(null_sid);
|
|---|
| 121 | idmap_cache_set_sid2uid(&null_sid, gid);
|
|---|
| 122 | DEBUG(10, ("gid [%lu] not mapped\n", (unsigned long)gid));
|
|---|
| 123 | return NT_STATUS_NONE_MAPPED;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | idmap_cache_set_sid2gid(sid, gid);
|
|---|
| 127 |
|
|---|
| 128 | return NT_STATUS_OK;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /*****************************************************************
|
|---|
| 132 | Returns the UID mapped to the given SID.
|
|---|
| 133 | If mapping is not possible or SID maps to a GID returns an error.
|
|---|
| 134 | *****************************************************************/
|
|---|
| 135 |
|
|---|
| 136 | NTSTATUS idmap_sid_to_uid(const char *dom_name, DOM_SID *sid, uid_t *uid)
|
|---|
| 137 | {
|
|---|
| 138 | NTSTATUS ret;
|
|---|
| 139 | struct id_map map;
|
|---|
| 140 | bool expired;
|
|---|
| 141 |
|
|---|
| 142 | DEBUG(10,("idmap_sid_to_uid: sid = [%s], domain = '%s'\n",
|
|---|
| 143 | sid_string_dbg(sid), dom_name));
|
|---|
| 144 |
|
|---|
| 145 | if (idmap_cache_find_sid2uid(sid, uid, &expired)) {
|
|---|
| 146 | DEBUG(10, ("idmap_cache_find_sid2uid found %d%s\n",
|
|---|
| 147 | (int)(*uid), expired ? " (expired)": ""));
|
|---|
| 148 | if (expired && idmap_is_online()) {
|
|---|
| 149 | DEBUG(10, ("revalidating expired entry\n"));
|
|---|
| 150 | goto backend;
|
|---|
| 151 | }
|
|---|
| 152 | if ((*uid) == -1) {
|
|---|
| 153 | DEBUG(10, ("Returning negative cache entry\n"));
|
|---|
| 154 | return NT_STATUS_NONE_MAPPED;
|
|---|
| 155 | }
|
|---|
| 156 | DEBUG(10, ("Returning positive cache entry\n"));
|
|---|
| 157 | return NT_STATUS_OK;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | backend:
|
|---|
| 161 | map.sid = sid;
|
|---|
| 162 | map.xid.type = ID_TYPE_UID;
|
|---|
| 163 |
|
|---|
| 164 | ret = idmap_backends_sid_to_unixid(dom_name, &map);
|
|---|
| 165 |
|
|---|
| 166 | if (NT_STATUS_IS_OK(ret) && (map.status == ID_MAPPED)) {
|
|---|
| 167 | if (map.xid.type != ID_TYPE_UID) {
|
|---|
| 168 | DEBUG(10, ("sid [%s] not mapped to a uid "
|
|---|
| 169 | "[%u,%u,%u]\n",
|
|---|
| 170 | sid_string_dbg(sid),
|
|---|
| 171 | map.status,
|
|---|
| 172 | map.xid.type,
|
|---|
| 173 | map.xid.id));
|
|---|
| 174 | idmap_cache_set_sid2uid(sid, -1);
|
|---|
| 175 | return NT_STATUS_NONE_MAPPED;
|
|---|
| 176 | }
|
|---|
| 177 | goto done;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | if (dom_name[0] != '\0') {
|
|---|
| 181 | /*
|
|---|
| 182 | * We had the task to go to a specific domain which
|
|---|
| 183 | * could not answer our request. Fail.
|
|---|
| 184 | */
|
|---|
| 185 | idmap_cache_set_sid2uid(sid, -1);
|
|---|
| 186 | return NT_STATUS_NONE_MAPPED;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | ret = idmap_new_mapping(sid, ID_TYPE_UID, &map.xid);
|
|---|
| 190 |
|
|---|
| 191 | if (!NT_STATUS_IS_OK(ret)) {
|
|---|
| 192 | DEBUG(10, ("idmap_new_mapping failed: %s\n",
|
|---|
| 193 | nt_errstr(ret)));
|
|---|
| 194 | idmap_cache_set_sid2uid(sid, -1);
|
|---|
| 195 | return ret;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | done:
|
|---|
| 199 | *uid = (uid_t)map.xid.id;
|
|---|
| 200 | idmap_cache_set_sid2uid(sid, *uid);
|
|---|
| 201 | return NT_STATUS_OK;
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | /*****************************************************************
|
|---|
| 205 | Returns the GID mapped to the given SID.
|
|---|
| 206 | If mapping is not possible or SID maps to a UID returns an error.
|
|---|
| 207 | *****************************************************************/
|
|---|
| 208 |
|
|---|
| 209 | NTSTATUS idmap_sid_to_gid(const char *domname, DOM_SID *sid, gid_t *gid)
|
|---|
| 210 | {
|
|---|
| 211 | NTSTATUS ret;
|
|---|
| 212 | struct id_map map;
|
|---|
| 213 | bool expired;
|
|---|
| 214 |
|
|---|
| 215 | DEBUG(10,("idmap_sid_to_gid: sid = [%s], domain = '%s'\n",
|
|---|
| 216 | sid_string_dbg(sid), domname));
|
|---|
| 217 |
|
|---|
| 218 | if (idmap_cache_find_sid2gid(sid, gid, &expired)) {
|
|---|
| 219 | DEBUG(10, ("idmap_cache_find_sid2gid found %d%s\n",
|
|---|
| 220 | (int)(*gid), expired ? " (expired)": ""));
|
|---|
| 221 | if (expired && idmap_is_online()) {
|
|---|
| 222 | DEBUG(10, ("revalidating expired entry\n"));
|
|---|
| 223 | goto backend;
|
|---|
| 224 | }
|
|---|
| 225 | if ((*gid) == -1) {
|
|---|
| 226 | DEBUG(10, ("Returning negative cache entry\n"));
|
|---|
| 227 | return NT_STATUS_NONE_MAPPED;
|
|---|
| 228 | }
|
|---|
| 229 | DEBUG(10, ("Returning positive cache entry\n"));
|
|---|
| 230 | return NT_STATUS_OK;
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | backend:
|
|---|
|
|---|