This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[bbaumbach/samba-autobuild/.git] / source3 / libads / ads_ldap.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind ADS backend functions
5
6    Copyright (C) Andrew Tridgell 2001
7    Copyright (C) Andrew Bartlett 2002
8    
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.
13    
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.
18    
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.
22 */
23
24 #include "includes.h"
25 #ifdef HAVE_LDAP
26
27 /* convert a single name to a sid in a domain */
28 NTSTATUS ads_name_to_sid(ADS_STRUCT *ads,
29                          const char *name,
30                          DOM_SID *sid,
31                          enum SID_NAME_USE *type)
32 {
33         const char *attrs[] = {"objectSid", "sAMAccountType", NULL};
34         int count;
35         ADS_STATUS rc;
36         void *res = NULL;
37         char *exp;
38         uint32 t;
39         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
40
41         if (asprintf(&exp, "(|(sAMAccountName=%s)(userPrincipalName=%s@%s))", 
42                      name, name, ads->config.realm) == -1) {
43                 DEBUG(1,("ads_name_to_sid: asprintf failed!\n"));
44                 status = NT_STATUS_NO_MEMORY;
45                 goto done;
46         }
47
48         rc = ads_search_retry(ads, &res, exp, attrs);
49         free(exp);
50         if (!ADS_ERR_OK(rc)) {
51                 DEBUG(1,("name_to_sid ads_search: %s\n", ads_errstr(rc)));
52                 goto done;
53         }
54
55         count = ads_count_replies(ads, res);
56         if (count != 1) {
57                 DEBUG(1,("name_to_sid: %s not found\n", name));
58                 goto done;
59         }
60
61         if (!ads_pull_sid(ads, res, "objectSid", sid)) {
62                 DEBUG(1,("No sid for %s !?\n", name));
63                 goto done;
64         }
65
66         if (!ads_pull_uint32(ads, res, "sAMAccountType", &t)) {
67                 DEBUG(1,("No sAMAccountType for %s !?\n", name));
68                 goto done;
69         }
70
71         *type = ads_atype_map(t);
72
73         status = NT_STATUS_OK;
74
75         DEBUG(3,("ads name_to_sid mapped %s\n", name));
76
77 done:
78         if (res) ads_msgfree(ads, res);
79
80         return status;
81 }
82
83 /* convert a sid to a user or group name */
84 NTSTATUS ads_sid_to_name(ADS_STRUCT *ads,
85                          TALLOC_CTX *mem_ctx,
86                          const DOM_SID *sid,
87                          char **name,
88                          enum SID_NAME_USE *type)
89 {
90         const char *attrs[] = {"userPrincipalName", 
91                                "sAMAccountName",
92                                "sAMAccountType", NULL};
93         ADS_STATUS rc;
94         void *msg = NULL;
95         char *exp = NULL;
96         char *sidstr = NULL;
97         uint32 atype;
98         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
99
100         if (!(sidstr = sid_binstring(sid))) {
101                 DEBUG(1,("ads_sid_to_name: sid_binstring failed!\n"));
102                 status = NT_STATUS_NO_MEMORY;
103                 goto done;
104         }
105
106         if (asprintf(&exp, "(objectSid=%s)", sidstr) == -1) {
107                 DEBUG(1,("ads_sid_to_name: asprintf failed!\n"));
108                 status = NT_STATUS_NO_MEMORY;
109                 goto done;
110         }
111
112         rc = ads_search_retry(ads, &msg, exp, attrs);
113         if (!ADS_ERR_OK(rc)) {
114                 status = ads_ntstatus(rc);
115                 DEBUG(1,("ads_sid_to_name ads_search: %s\n", ads_errstr(rc)));
116                 goto done;
117         }
118
119         if (!ads_pull_uint32(ads, msg, "sAMAccountType", &atype)) {
120                 goto done;
121         }
122
123         *name = ads_pull_username(ads, mem_ctx, msg);
124         if (!*name) {
125                 DEBUG(1,("ads_sid_to_name: ads_pull_username retuned NULL!\n"));
126                 status = NT_STATUS_NO_MEMORY;
127                 goto done;
128         }
129                 
130         *type = ads_atype_map(atype);
131
132         status = NT_STATUS_OK;
133
134         DEBUG(3,("ads sid_to_name mapped %s\n", *name));
135
136 done:
137         if (msg) ads_msgfree(ads, msg);
138
139         SAFE_FREE(exp);
140         SAFE_FREE(sidstr);
141
142         return status;
143 }
144
145 #endif