2 Unix SMB/CIFS implementation.
4 Winbind ADS backend functions
6 Copyright (C) Andrew Tridgell 2001
7 Copyright (C) Andrew Bartlett 2002
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 /* convert a single name to a sid in a domain */
28 NTSTATUS ads_name_to_sid(ADS_STRUCT *ads,
31 enum SID_NAME_USE *type)
33 const char *attrs[] = {"objectSid", "sAMAccountType", NULL};
39 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
40 char *escaped_name = escape_ldap_string_alloc(name);
41 char *escaped_realm = escape_ldap_string_alloc(ads->config.realm);
43 if (!escaped_name || !escaped_realm) {
44 status = NT_STATUS_NO_MEMORY;
48 if (asprintf(&exp, "(|(sAMAccountName=%s)(userPrincipalName=%s@%s))",
49 escaped_name, escaped_name, escaped_realm) == -1) {
50 DEBUG(1,("ads_name_to_sid: asprintf failed!\n"));
51 status = NT_STATUS_NO_MEMORY;
55 rc = ads_search_retry(ads, &res, exp, attrs);
57 if (!ADS_ERR_OK(rc)) {
58 DEBUG(1,("name_to_sid ads_search: %s\n", ads_errstr(rc)));
62 count = ads_count_replies(ads, res);
64 DEBUG(1,("name_to_sid: %s not found\n", name));
68 if (!ads_pull_sid(ads, res, "objectSid", sid)) {
69 DEBUG(1,("No sid for %s !?\n", name));
73 if (!ads_pull_uint32(ads, res, "sAMAccountType", &t)) {
74 DEBUG(1,("No sAMAccountType for %s !?\n", name));
78 *type = ads_atype_map(t);
80 status = NT_STATUS_OK;
82 DEBUG(3,("ads name_to_sid mapped %s\n", name));
85 if (res) ads_msgfree(ads, res);
87 SAFE_FREE(escaped_name);
88 SAFE_FREE(escaped_realm);
93 /* convert a sid to a user or group name */
94 NTSTATUS ads_sid_to_name(ADS_STRUCT *ads,
98 enum SID_NAME_USE *type)
100 const char *attrs[] = {"userPrincipalName",
102 "sAMAccountType", NULL};
108 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
110 if (!(sidstr = sid_binstring(sid))) {
111 DEBUG(1,("ads_sid_to_name: sid_binstring failed!\n"));
112 status = NT_STATUS_NO_MEMORY;
116 if (asprintf(&exp, "(objectSid=%s)", sidstr) == -1) {
117 DEBUG(1,("ads_sid_to_name: asprintf failed!\n"));
118 status = NT_STATUS_NO_MEMORY;
122 rc = ads_search_retry(ads, &msg, exp, attrs);
123 if (!ADS_ERR_OK(rc)) {
124 status = ads_ntstatus(rc);
125 DEBUG(1,("ads_sid_to_name ads_search: %s\n", ads_errstr(rc)));
129 if (!ads_pull_uint32(ads, msg, "sAMAccountType", &atype)) {
133 *name = ads_pull_username(ads, mem_ctx, msg);
135 DEBUG(1,("ads_sid_to_name: ads_pull_username retuned NULL!\n"));
136 status = NT_STATUS_NO_MEMORY;
140 *type = ads_atype_map(atype);
142 status = NT_STATUS_OK;
144 DEBUG(3,("ads sid_to_name mapped %s\n", *name));
147 if (msg) ads_msgfree(ads, msg);