2 Unix SMB/CIFS implementation.
4 mapping routines for SID <-> unix uid/gid
6 Copyright (C) Andrew Tridgell 2004
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "system/passwd.h"
25 #include "dsdb/common/flags.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "auth/auth.h"
28 #include "libcli/ldap/ldap.h"
30 #include "libcli/security/security.h"
33 these are used for the fallback local uid/gid to sid mapping
36 #define SIDMAP_LOCAL_USER_BASE 0x80000000
37 #define SIDMAP_LOCAL_GROUP_BASE 0xC0000000
38 #define SIDMAP_MAX_LOCAL_UID 0x3fffffff
39 #define SIDMAP_MAX_LOCAL_GID 0x3fffffff
42 private context for sid mapping routines
44 struct sidmap_context {
45 struct ldb_context *samctx;
49 open a sidmap context - use talloc_free to close
51 _PUBLIC_ struct sidmap_context *sidmap_open(TALLOC_CTX *mem_ctx)
53 struct sidmap_context *sidmap;
54 sidmap = talloc(mem_ctx, struct sidmap_context);
58 sidmap->samctx = samdb_connect(sidmap, system_session(sidmap));
59 if (sidmap->samctx == NULL) {
69 check the sAMAccountType field of a search result to see if
70 the account is a user account
72 static BOOL is_user_account(struct ldb_message *res)
74 uint_t atype = samdb_result_uint(res, "sAMAccountType", 0);
75 if (atype && (!(atype & ATYPE_ACCOUNT))) {
82 check the sAMAccountType field of a search result to see if
83 the account is a group account
85 static BOOL is_group_account(struct ldb_message *res)
87 uint_t atype = samdb_result_uint(res, "sAMAccountType", 0);
88 if (atype && atype == ATYPE_NORMAL_ACCOUNT) {
97 return the dom_sid of our primary domain
99 static NTSTATUS sidmap_primary_domain_sid(struct sidmap_context *sidmap,
100 TALLOC_CTX *mem_ctx, struct dom_sid **sid)
102 const char *attrs[] = { "objectSid", NULL };
104 struct ldb_message **res = NULL;
106 ret = gendb_search_dn(sidmap->samctx, mem_ctx, NULL, &res, attrs);
109 return NT_STATUS_NO_SUCH_DOMAIN;
112 *sid = samdb_result_dom_sid(mem_ctx, res[0], "objectSid");
115 return NT_STATUS_NO_MEMORY;
123 map a sid to a unix uid
125 _PUBLIC_ NTSTATUS sidmap_sid_to_unixuid(struct sidmap_context *sidmap,
126 struct dom_sid *sid, uid_t *uid)
128 const char *attrs[] = { "sAMAccountName", "uidNumber",
129 "sAMAccountType", NULL };
133 struct ldb_message **res;
134 struct dom_sid *domain_sid;
137 tmp_ctx = talloc_new(sidmap);
139 ret = gendb_search(sidmap->samctx, tmp_ctx, NULL, &res, attrs,
140 "objectSid=%s", ldap_encode_ndr_dom_sid(tmp_ctx, sid));
145 /* make sure its a user, not a group */
146 if (!is_user_account(res[0])) {
147 DEBUG(0,("sid_to_unixuid: sid %s is not an account!\n",
148 dom_sid_string(tmp_ctx, sid)));
149 talloc_free(tmp_ctx);
150 return NT_STATUS_INVALID_SID;
153 /* first try to get the uid directly */
154 s = samdb_result_string(res[0], "uidNumber", NULL);
156 *uid = strtoul(s, NULL, 0);
157 talloc_free(tmp_ctx);
161 /* next try via the UnixName attribute */
162 s = samdb_result_string(res[0], "unixName", NULL);
164 struct passwd *pwd = getpwnam(s);
166 DEBUG(0,("unixName %s for sid %s does not exist as a local user\n", s,
167 dom_sid_string(tmp_ctx, sid)));
168 talloc_free(tmp_ctx);
169 return NT_STATUS_NO_SUCH_USER;
172 talloc_free(tmp_ctx);
176 /* finally try via the sAMAccountName attribute */
177 s = samdb_result_string(res[0], "sAMAccountName", NULL);
179 struct passwd *pwd = getpwnam(s);
181 DEBUG(0,("sAMAccountName '%s' for sid %s does not exist as a local user\n",
182 s, dom_sid_string(tmp_ctx, sid)));
183 talloc_free(tmp_ctx);
184 return NT_STATUS_NO_SUCH_USER;
187 talloc_free(tmp_ctx);
193 status = sidmap_primary_domain_sid(sidmap, tmp_ctx, &domain_sid);
194 if (!NT_STATUS_IS_OK(status)) {
195 talloc_free(tmp_ctx);
196 return NT_STATUS_NO_SUCH_DOMAIN;
199 if (dom_sid_in_domain(domain_sid, sid)) {
200 uint32_t rid = sid->sub_auths[sid->num_auths-1];
201 if (rid >= SIDMAP_LOCAL_USER_BASE &&
202 rid < SIDMAP_LOCAL_GROUP_BASE) {
203 *uid = rid - SIDMAP_LOCAL_USER_BASE;
204 talloc_free(tmp_ctx);
210 DEBUG(0,("sid_to_unixuid: no uidNumber, unixName or sAMAccountName for sid %s\n",
211 dom_sid_string(tmp_ctx, sid)));
213 talloc_free(tmp_ctx);
214 return NT_STATUS_INVALID_SID;
219 map a sid to a unix gid
221 _PUBLIC_ NTSTATUS sidmap_sid_to_unixgid(struct sidmap_context *sidmap,
222 struct dom_sid *sid, gid_t *gid)
224 const char *attrs[] = { "sAMAccountName", "gidNumber",
225 "unixName", "sAMAccountType", NULL };
229 struct ldb_message **res;
231 struct dom_sid *domain_sid;
233 tmp_ctx = talloc_new(sidmap);
235 ret = gendb_search(sidmap->samctx, tmp_ctx, NULL, &res, attrs,
236 "objectSid=%s", ldap_encode_ndr_dom_sid(tmp_ctx, sid));
241 /* make sure its not a user */
242 if (!is_group_account(res[0])) {
243 DEBUG(0,("sid_to_unixgid: sid %s is a ATYPE_NORMAL_ACCOUNT\n",
244 dom_sid_string(tmp_ctx, sid)));
245 talloc_free(tmp_ctx);
246 return NT_STATUS_INVALID_SID;
249 /* first try to get the gid directly */
250 s = samdb_result_string(res[0], "gidNumber", NULL);
252 *gid = strtoul(s, NULL, 0);
253 talloc_free(tmp_ctx);
257 /* next try via the UnixName attribute */
258 s = samdb_result_string(res[0], "unixName", NULL);
260 struct group *grp = getgrnam(s);
262 DEBUG(0,("unixName '%s' for sid %s does not exist as a local group\n",
263 s, dom_sid_string(tmp_ctx, sid)));
264 talloc_free(tmp_ctx);
265 return NT_STATUS_NO_SUCH_GROUP;
268 talloc_free(tmp_ctx);
272 /* finally try via the sAMAccountName attribute */
273 s = samdb_result_string(res[0], "sAMAccountName", NULL);
275 struct group *grp = getgrnam(s);
277 DEBUG(0,("sAMAccountName '%s' for sid %s does not exist as a local group\n", s, dom_sid_string(tmp_ctx, sid)));
278 talloc_free(tmp_ctx);
279 return NT_STATUS_NO_SUCH_GROUP;
282 talloc_free(tmp_ctx);
287 status = sidmap_primary_domain_sid(sidmap, tmp_ctx, &domain_sid);
288 if (!NT_STATUS_IS_OK(status)) {
289 talloc_free(tmp_ctx);
290 return NT_STATUS_NO_SUCH_DOMAIN;
293 if (dom_sid_in_domain(domain_sid, sid)) {
294 uint32_t rid = sid->sub_auths[sid->num_auths-1];
295 if (rid >= SIDMAP_LOCAL_GROUP_BASE) {
296 *gid = rid - SIDMAP_LOCAL_GROUP_BASE;
297 talloc_free(tmp_ctx);
302 DEBUG(0,("sid_to_unixgid: no gidNumber, unixName or sAMAccountName for sid %s\n",
303 dom_sid_string(tmp_ctx, sid)));
305 talloc_free(tmp_ctx);
306 return NT_STATUS_INVALID_SID;
311 map a unix uid to a dom_sid
312 the returned sid is allocated in the supplied mem_ctx
314 _PUBLIC_ NTSTATUS sidmap_uid_to_sid(struct sidmap_context *sidmap,
316 uid_t uid, struct dom_sid **sid)
318 const char *attrs[] = { "sAMAccountName", "objectSid", "sAMAccountType", NULL };
321 struct ldb_message **res;
323 struct dom_sid *domain_sid;
327 we search for the mapping in the following order:
329 - check if the uid is in the dynamic uid range assigned for winbindd
330 use. If it is, then look in winbindd sid mapping
331 database (not implemented yet)
332 - look for a user account in samdb that has uidNumber set to the
334 - look for a user account in samdb that has unixName or
335 sAMAccountName set to the name given by getpwuid()
336 - assign a SID by adding the uid to SIDMAP_LOCAL_USER_BASE in the local
341 tmp_ctx = talloc_new(mem_ctx);
345 step 2: look for a user account in samdb that has uidNumber set to the
349 ret = gendb_search(sidmap->samctx, tmp_ctx, NULL, &res, attrs,
350 "uidNumber=%u", (unsigned int)uid);
351 for (i=0;i<ret;i++) {
352 if (!is_user_account(res[i])) continue;
354 *sid = samdb_result_dom_sid(mem_ctx, res[i], "objectSid");
355 talloc_free(tmp_ctx);
356 NT_STATUS_HAVE_NO_MEMORY(*sid);
361 step 3: look for a user account in samdb that has unixName
362 or sAMAccountName set to the name given by getpwuid()
369 ret = gendb_search(sidmap->samctx, tmp_ctx, NULL, &res, attrs,
370 "(|(unixName=%s)(sAMAccountName=%s))",
371 pwd->pw_name, pwd->pw_name);
372 for (i=0;i<ret;i++) {
373 if (!is_user_account(res[i])) continue;
375 *sid = samdb_result_dom_sid(mem_ctx, res[i], "objectSid");
376 talloc_free(tmp_ctx);
377 NT_STATUS_HAVE_NO_MEMORY(*sid);
383 step 4: assign a SID by adding the uid to
384 SIDMAP_LOCAL_USER_BASE in the local domain
387 if (uid > SIDMAP_MAX_LOCAL_UID) {
388 return NT_STATUS_INVALID_SID;
391 status = sidmap_primary_domain_sid(sidmap, tmp_ctx, &domain_sid);
392 if (!NT_STATUS_IS_OK(status)) {
393 talloc_free(tmp_ctx);
397 *sid = dom_sid_add_rid(mem_ctx, domain_sid, SIDMAP_LOCAL_USER_BASE + uid);
398 talloc_free(tmp_ctx);
401 return NT_STATUS_NO_MEMORY;
409 map a unix gid to a dom_sid
410 the returned sid is allocated in the supplied mem_ctx
412 _PUBLIC_ NTSTATUS sidmap_gid_to_sid(struct sidmap_context *sidmap,
414 gid_t gid, struct dom_sid **sid)
416 const char *attrs[] = { "sAMAccountName", "objectSid", "sAMAccountType", NULL };
419 struct ldb_message **res;
421 struct dom_sid *domain_sid;
425 we search for the mapping in the following order:
427 - check if the gid is in the dynamic gid range assigned for winbindd
428 use. If it is, then look in winbindd sid mapping
429 database (not implemented yet)
430 - look for a group account in samdb that has gidNumber set to the
432 - look for a group account in samdb that has unixName or
433 sAMAccountName set to the name given by getgrgid()
434 - assign a SID by adding the gid to SIDMAP_LOCAL_GROUP_BASE in the local
439 tmp_ctx = talloc_new(sidmap);
443 step 2: look for a group account in samdb that has gidNumber set to the
447 ret = gendb_search(sidmap->samctx, tmp_ctx, NULL, &res, attrs,
448 "gidNumber=%u", (unsigned int)gid);
449 for (i=0;i<ret;i++) {
450 if (!is_group_account(res[i])) continue;
452 *sid = samdb_result_dom_sid(mem_ctx, res[i], "objectSid");
453 talloc_free(tmp_ctx);
454 NT_STATUS_HAVE_NO_MEMORY(*sid);
459 step 3: look for a group account in samdb that has unixName
460 or sAMAccountName set to the name given by getgrgid()
467 ret = gendb_search(sidmap->samctx, tmp_ctx, NULL, &res, attrs,
468 "(|(unixName=%s)(sAMAccountName=%s))",
469 grp->gr_name, grp->gr_name);
470 for (i=0;i<ret;i++) {
471 if (!is_group_account(res[i])) continue;
473 *sid = samdb_result_dom_sid(mem_ctx, res[i], "objectSid");
474 talloc_free(tmp_ctx);
475 NT_STATUS_HAVE_NO_MEMORY(*sid);
481 step 4: assign a SID by adding the gid to
482 SIDMAP_LOCAL_GROUP_BASE in the local domain
485 if (gid > SIDMAP_MAX_LOCAL_GID) {
486 return NT_STATUS_INVALID_SID;
489 status = sidmap_primary_domain_sid(sidmap, tmp_ctx, &domain_sid);
490 if (!NT_STATUS_IS_OK(status)) {
491 talloc_free(tmp_ctx);
495 *sid = dom_sid_add_rid(mem_ctx, domain_sid, SIDMAP_LOCAL_GROUP_BASE + gid);
496 talloc_free(tmp_ctx);
499 return NT_STATUS_NO_MEMORY;
506 check if a sid is in the range of auto-allocated SIDs from our primary domain,
507 and if it is, then return the name and atype
509 _PUBLIC_ NTSTATUS sidmap_allocated_sid_lookup(struct sidmap_context *sidmap,
511 const struct dom_sid *sid,
516 struct dom_sid *domain_sid;
517 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
520 status = sidmap_primary_domain_sid(sidmap, tmp_ctx, &domain_sid);
521 if (!NT_STATUS_IS_OK(status)) {
522 return NT_STATUS_NO_SUCH_DOMAIN;
525 if (!dom_sid_in_domain(domain_sid, sid)) {
526 talloc_free(tmp_ctx);
527 return NT_STATUS_INVALID_SID;
530 talloc_free(tmp_ctx);
532 rid = sid->sub_auths[sid->num_auths-1];
533 if (rid < SIDMAP_LOCAL_USER_BASE) {
534 return NT_STATUS_INVALID_SID;
537 if (rid < SIDMAP_LOCAL_GROUP_BASE) {
539 uid_t uid = rid - SIDMAP_LOCAL_USER_BASE;
540 *atype = ATYPE_NORMAL_ACCOUNT;
543 *name = talloc_asprintf(mem_ctx, "uid%u", uid);
545 *name = talloc_strdup(mem_ctx, pwd->pw_name);
549 gid_t gid = rid - SIDMAP_LOCAL_GROUP_BASE;
550 *atype = ATYPE_LOCAL_GROUP;
553 *name = talloc_asprintf(mem_ctx, "gid%u", gid);
555 *name = talloc_strdup(mem_ctx, grp->gr_name);
560 return NT_STATUS_NO_MEMORY;