Second attempt at fix for bug 6529 - Offline files conflict with Vista and Office...
[samba.git] / source3 / rpc_client / cli_lsarpc.c
1 /*
2    Unix SMB/CIFS implementation.
3    RPC pipe client
4    Copyright (C) Tim Potter                        2000-2001,
5    Copyright (C) Andrew Tridgell              1992-1997,2000,
6    Copyright (C) Rafal Szczesniak                       2002
7    Copyright (C) Jeremy Allison                         2005.
8    Copyright (C) Michael Adam                           2007.
9    Copyright (C) Guenther Deschner                      2008.
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
26
27 /** @defgroup lsa LSA - Local Security Architecture
28  *  @ingroup rpc_client
29  *
30  * @{
31  **/
32
33 /**
34  * @file cli_lsarpc.c
35  *
36  * RPC client routines for the LSA RPC pipe.  LSA means "local
37  * security authority", which is half of a password database.
38  **/
39
40 /** Open a LSA policy handle
41  *
42  * @param cli Handle on an initialised SMB connection */
43
44 NTSTATUS rpccli_lsa_open_policy(struct rpc_pipe_client *cli,
45                                 TALLOC_CTX *mem_ctx,
46                                 bool sec_qos, uint32 des_access,
47                                 struct policy_handle *pol)
48 {
49         struct lsa_ObjectAttribute attr;
50         struct lsa_QosInfo qos;
51         uint16_t system_name = '\\';
52
53         ZERO_STRUCT(attr);
54
55         attr.len        = 0x18;
56
57         if (sec_qos) {
58                 qos.len                 = 0xc;
59                 qos.impersonation_level = 2;
60                 qos.context_mode        = 1;
61                 qos.effective_only      = 0;
62
63                 attr.sec_qos            = &qos;
64         }
65
66         return rpccli_lsa_OpenPolicy(cli, mem_ctx,
67                                      &system_name,
68                                      &attr,
69                                      des_access,
70                                      pol);
71 }
72
73 /** Open a LSA policy handle
74   *
75   * @param cli Handle on an initialised SMB connection
76   */
77
78 NTSTATUS rpccli_lsa_open_policy2(struct rpc_pipe_client *cli,
79                                  TALLOC_CTX *mem_ctx, bool sec_qos,
80                                  uint32 des_access, struct policy_handle *pol)
81 {
82         struct lsa_ObjectAttribute attr;
83         struct lsa_QosInfo qos;
84
85         ZERO_STRUCT(attr);
86
87         attr.len        = 0x18;
88
89         if (sec_qos) {
90                 qos.len                 = 0xc;
91                 qos.impersonation_level = 2;
92                 qos.context_mode        = 1;
93                 qos.effective_only      = 0;
94
95                 attr.sec_qos            = &qos;
96         }
97
98         return rpccli_lsa_OpenPolicy2(cli, mem_ctx,
99                                       cli->srv_name_slash,
100                                       &attr,
101                                       des_access,
102                                       pol);
103 }
104
105 /* Lookup a list of sids
106  *
107  * internal version withOUT memory allocation of the target arrays.
108  * this assumes suffciently sized arrays to store domains, names and types. */
109
110 static NTSTATUS rpccli_lsa_lookup_sids_noalloc(struct rpc_pipe_client *cli,
111                                                TALLOC_CTX *mem_ctx,
112                                                struct policy_handle *pol,
113                                                int num_sids,
114                                                const DOM_SID *sids,
115                                                char **domains,
116                                                char **names,
117                                                enum lsa_SidType *types)
118 {
119         NTSTATUS result = NT_STATUS_OK;
120         TALLOC_CTX *tmp_ctx = NULL;
121         int i;
122         struct lsa_SidArray sid_array;
123         struct lsa_RefDomainList *ref_domains = NULL;
124         struct lsa_TransNameArray lsa_names;
125         uint32_t count = 0;
126         uint16_t level = 1;
127
128         ZERO_STRUCT(lsa_names);
129
130         tmp_ctx = talloc_new(mem_ctx);
131         if (!tmp_ctx) {
132                 DEBUG(0, ("rpccli_lsa_lookup_sids_noalloc: out of memory!\n"));
133                 result = NT_STATUS_UNSUCCESSFUL;
134                 goto done;
135         }
136
137         sid_array.num_sids = num_sids;
138         sid_array.sids = TALLOC_ARRAY(mem_ctx, struct lsa_SidPtr, num_sids);
139         if (!sid_array.sids) {
140                 return NT_STATUS_NO_MEMORY;
141         }
142
143         for (i = 0; i<num_sids; i++) {
144                 sid_array.sids[i].sid = sid_dup_talloc(mem_ctx, &sids[i]);
145                 if (!sid_array.sids[i].sid) {
146                         return NT_STATUS_NO_MEMORY;
147                 }
148         }
149
150         result = rpccli_lsa_LookupSids(cli, mem_ctx,
151                                        pol,
152                                        &sid_array,
153                                        &ref_domains,
154                                        &lsa_names,
155                                        level,
156                                        &count);
157
158         DEBUG(10, ("LSA_LOOKUPSIDS returned '%s', mapped count = %d'\n",
159                    nt_errstr(result), count));
160
161         if (!NT_STATUS_IS_OK(result) &&
162             !NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
163             !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
164         {
165                 /* An actual error occured */
166                 goto done;
167         }
168
169         /* Return output parameters */
170
171         if (NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) ||
172             (count == 0))
173         {
174                 for (i = 0; i < num_sids; i++) {
175                         (names)[i] = NULL;
176                         (domains)[i] = NULL;
177                         (types)[i] = SID_NAME_UNKNOWN;
178                 }
179                 result = NT_STATUS_NONE_MAPPED;
180                 goto done;
181         }
182
183         for (i = 0; i < num_sids; i++) {
184                 const char *name, *dom_name;
185                 uint32_t dom_idx = lsa_names.names[i].sid_index;
186
187                 /* Translate optimised name through domain index array */
188
189                 if (dom_idx != 0xffffffff) {
190
191                         dom_name = ref_domains->domains[dom_idx].name.string;
192                         name = lsa_names.names[i].name.string;
193
194                         if (name) {
195                                 (names)[i] = talloc_strdup(mem_ctx, name);
196                                 if ((names)[i] == NULL) {
197                                         DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
198                                         result = NT_STATUS_UNSUCCESSFUL;
199                                         goto done;
200                                 }
201                         } else {
202                                 (names)[i] = NULL;
203                         }
204                         (domains)[i] = talloc_strdup(mem_ctx, dom_name);
205                         (types)[i] = lsa_names.names[i].sid_type;
206                         if (((domains)[i] == NULL)) {
207                                 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
208                                 result = NT_STATUS_UNSUCCESSFUL;
209                                 goto done;
210                         }
211
212                 } else {
213                         (names)[i] = NULL;
214                         (domains)[i] = NULL;
215                         (types)[i] = SID_NAME_UNKNOWN;
216                 }
217         }
218
219 done:
220         TALLOC_FREE(tmp_ctx);
221         return result;
222 }
223
224 /* Lookup a list of sids
225  *
226  * do it the right way: there is a limit (of 20480 for w2k3) entries
227  * returned by this call. when the sids list contains more entries,
228  * empty lists are returned. This version of lsa_lookup_sids passes
229  * the list of sids in hunks of LOOKUP_SIDS_HUNK_SIZE to the lsa call. */
230
231 /* This constant defines the limit of how many sids to look up
232  * in one call (maximum). the limit from the server side is
233  * at 20480 for win2k3, but we keep it at a save 1000 for now. */
234 #define LOOKUP_SIDS_HUNK_SIZE 1000
235
236 NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
237                                 TALLOC_CTX *mem_ctx,
238                                 struct policy_handle *pol,
239                                 int num_sids,
240                                 const DOM_SID *sids,
241                                 char ***pdomains,
242                                 char ***pnames,
243                                 enum lsa_SidType **ptypes)
244 {
245         NTSTATUS result = NT_STATUS_OK;
246         int sids_left = 0;
247         int sids_processed = 0;
248         const DOM_SID *hunk_sids = sids;
249         char **hunk_domains;
250         char **hunk_names;
251         enum lsa_SidType *hunk_types;
252         char **domains = NULL;
253         char **names = NULL;
254         enum lsa_SidType *types = NULL;
255
256         if (num_sids) {
257                 if (!(domains = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
258                         DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
259                         result = NT_STATUS_NO_MEMORY;
260                         goto fail;
261                 }
262
263                 if (!(names = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
264                         DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
265                         result = NT_STATUS_NO_MEMORY;
266                         goto fail;
267                 }
268
269                 if (!(types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_sids))) {
270                         DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
271                         result = NT_STATUS_NO_MEMORY;
272                         goto fail;
273                 }
274         }
275
276         sids_left = num_sids;
277         hunk_domains = domains;
278         hunk_names = names;
279         hunk_types = types;
280
281         while (sids_left > 0) {
282                 int hunk_num_sids;
283                 NTSTATUS hunk_result = NT_STATUS_OK;
284
285                 hunk_num_sids = ((sids_left > LOOKUP_SIDS_HUNK_SIZE)
286                                 ? LOOKUP_SIDS_HUNK_SIZE
287                                 : sids_left);
288
289                 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
290                            "%d -- %d of %d.\n",
291                            sids_processed,
292                            sids_processed + hunk_num_sids - 1,
293                            num_sids));
294
295                 hunk_result = rpccli_lsa_lookup_sids_noalloc(cli,
296                                                              mem_ctx,
297                                                              pol,
298                                                              hunk_num_sids,
299                                                              hunk_sids,
300                                                              hunk_domains,
301                                                              hunk_names,
302                                                              hunk_types);
303
304                 if (!NT_STATUS_IS_OK(hunk_result) &&
305                     !NT_STATUS_EQUAL(hunk_result, STATUS_SOME_UNMAPPED) &&
306                     !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED))
307                 {
308                         /* An actual error occured */
309                         result = hunk_result;
310                         goto fail;
311                 }
312
313                 /* adapt overall result */
314                 if (( NT_STATUS_IS_OK(result) &&
315                      !NT_STATUS_IS_OK(hunk_result))
316                     ||
317                     ( NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
318                      !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED)))
319                 {
320                         result = STATUS_SOME_UNMAPPED;
321                 }
322
323                 sids_left -= hunk_num_sids;
324                 sids_processed += hunk_num_sids; /* only used in DEBUG */
325                 hunk_sids += hunk_num_sids;
326                 hunk_domains += hunk_num_sids;
327                 hunk_names += hunk_num_sids;
328                 hunk_types += hunk_num_sids;
329         }
330
331         *pdomains = domains;
332         *pnames = names;
333         *ptypes = types;
334         return result;
335
336 fail:
337         TALLOC_FREE(domains);
338         TALLOC_FREE(names);
339         TALLOC_FREE(types);
340         return result;
341 }
342
343 /** Lookup a list of names */
344
345 NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
346                                  TALLOC_CTX *mem_ctx,
347                                  struct policy_handle *pol, int num_names,
348                                  const char **names,
349                                  const char ***dom_names,
350                                  int level,
351                                  DOM_SID **sids,
352                                  enum lsa_SidType **types)
353 {
354         NTSTATUS result;
355         int i;
356         struct lsa_String *lsa_names = NULL;
357         struct lsa_RefDomainList *domains = NULL;
358         struct lsa_TransSidArray sid_array;
359         uint32_t count = 0;
360
361         ZERO_STRUCT(sid_array);
362
363         lsa_names = TALLOC_ARRAY(mem_ctx, struct lsa_String, num_names);
364         if (!lsa_names) {
365                 return NT_STATUS_NO_MEMORY;
366         }
367
368         for (i=0; i<num_names; i++) {
369                 init_lsa_String(&lsa_names[i], names[i]);
370         }
371
372         result = rpccli_lsa_LookupNames(cli, mem_ctx,
373                                         pol,
374                                         num_names,
375                                         lsa_names,
376                                         &domains,
377                                         &sid_array,
378                                         level,
379                                         &count);
380
381         if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
382             NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
383
384                 /* An actual error occured */
385
386                 goto done;
387         }
388
389         /* Return output parameters */
390
391         if (count == 0) {
392                 result = NT_STATUS_NONE_MAPPED;
393                 goto done;
394         }
395
396         if (num_names) {
397                 if (!((*sids = TALLOC_ARRAY(mem_ctx, DOM_SID, num_names)))) {
398                         DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
399                         result = NT_STATUS_NO_MEMORY;
400                         goto done;
401                 }
402
403                 if (!((*types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_names)))) {
404                         DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
405                         result = NT_STATUS_NO_MEMORY;
406                         goto done;
407                 }
408
409                 if (dom_names != NULL) {
410                         *dom_names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
411                         if (*dom_names == NULL) {
412                                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
413                                 result = NT_STATUS_NO_MEMORY;
414                                 goto done;
415                         }
416                 }
417         } else {
418                 *sids = NULL;
419                 *types = NULL;
420                 if (dom_names != NULL) {
421                         *dom_names = NULL;
422                 }
423         }
424
425         for (i = 0; i < num_names; i++) {
426                 uint32_t dom_idx = sid_array.sids[i].sid_index;
427                 uint32_t dom_rid = sid_array.sids[i].rid;
428                 DOM_SID *sid = &(*sids)[i];
429
430                 /* Translate optimised sid through domain index array */
431
432                 if (dom_idx == 0xffffffff) {
433                         /* Nothing to do, this is unknown */
434                         ZERO_STRUCTP(sid);
435                         (*types)[i] = SID_NAME_UNKNOWN;
436                         continue;
437                 }
438
439                 sid_copy(sid, domains->domains[dom_idx].sid);
440
441                 if (dom_rid != 0xffffffff) {
442                         sid_append_rid(sid, dom_rid);
443                 }
444
445                 (*types)[i] = sid_array.sids[i].sid_type;
446
447                 if (dom_names == NULL) {
448                         continue;
449                 }
450
451                 (*dom_names)[i] = domains->domains[dom_idx].name.string;
452         }
453
454  done:
455
456         return result;
457 }