99265594b96810e5935c9be12768eb3d92e97804
[nivanova/samba-autobuild/.git] / source3 / winbindd / idmap_ldap.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    idmap LDAP backend
5
6    Copyright (C) Tim Potter             2000
7    Copyright (C) Jim McDonough <jmcd@us.ibm.com>        2003
8    Copyright (C) Gerald Carter          2003
9    Copyright (C) Simo Sorce             2003-2007
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 #include "winbindd.h"
27
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_IDMAP
30
31 #include <lber.h>
32 #include <ldap.h>
33
34 #include "smbldap.h"
35
36 static char *idmap_fetch_secret(const char *backend, bool alloc,
37                                 const char *domain, const char *identity)
38 {
39         char *tmp, *ret;
40         int r;
41
42         if (alloc) {
43                 r = asprintf(&tmp, "IDMAP_ALLOC_%s", backend);
44         } else {
45                 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
46         }
47
48         if (r < 0)
49                 return NULL;
50
51         strupper_m(tmp); /* make sure the key is case insensitive */
52         ret = secrets_fetch_generic(tmp, identity);
53
54         SAFE_FREE(tmp);
55
56         return ret;
57 }
58
59 struct idmap_ldap_context {
60         struct smbldap_state *smbldap_state;
61         char *url;
62         char *suffix;
63         char *user_dn;
64         uint32_t filter_low_id, filter_high_id;         /* Filter range */
65         bool anon;
66 };
67
68 struct idmap_ldap_alloc_context {
69         struct smbldap_state *smbldap_state;
70         char *url;
71         char *suffix;
72         char *user_dn;
73         uid_t low_uid, high_uid;      /* Range of uids */
74         gid_t low_gid, high_gid;      /* Range of gids */
75
76 };
77
78 #define CHECK_ALLOC_DONE(mem) do { \
79         if (!mem) { \
80                 DEBUG(0, ("Out of memory!\n")); \
81                 ret = NT_STATUS_NO_MEMORY; \
82                 goto done; \
83         } } while (0)
84
85 /**********************************************************************
86  IDMAP ALLOC TDB BACKEND
87 **********************************************************************/
88
89 static struct idmap_ldap_alloc_context *idmap_alloc_ldap;
90
91 /*********************************************************************
92  ********************************************************************/
93
94 static NTSTATUS get_credentials( TALLOC_CTX *mem_ctx,
95                                  struct smbldap_state *ldap_state,
96                                  const char *config_option,
97                                  struct idmap_domain *dom,
98                                  char **dn )
99 {
100         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
101         char *secret = NULL;
102         const char *tmp = NULL;
103         char *user_dn = NULL;
104         bool anon = False;
105
106         /* assume anonymous if we don't have a specified user */
107
108         tmp = lp_parm_const_string(-1, config_option, "ldap_user_dn", NULL);
109
110         if ( tmp ) {
111                 if (!dom) {
112                         /* only the alloc backend can pass in a NULL dom */
113                         secret = idmap_fetch_secret("ldap", True,
114                                                     NULL, tmp);
115                 } else {
116                         secret = idmap_fetch_secret("ldap", False,
117                                                     dom->name, tmp);
118                 }
119
120                 if (!secret) {
121                         DEBUG(0, ("get_credentials: Unable to fetch "
122                                   "auth credentials for %s in %s\n",
123                                   tmp, (dom==NULL)?"ALLOC":dom->name));
124                         ret = NT_STATUS_ACCESS_DENIED;
125                         goto done;
126                 }
127                 *dn = talloc_strdup(mem_ctx, tmp);
128                 CHECK_ALLOC_DONE(*dn);
129         } else {
130                 if (!fetch_ldap_pw(&user_dn, &secret)) {
131                         DEBUG(2, ("get_credentials: Failed to lookup ldap "
132                                   "bind creds. Using anonymous connection.\n"));
133                         anon = True;
134                         *dn = NULL;
135                 } else {
136                         *dn = talloc_strdup(mem_ctx, user_dn);
137                         SAFE_FREE( user_dn );
138                         CHECK_ALLOC_DONE(*dn);
139                 }
140         }
141
142         smbldap_set_creds(ldap_state, anon, *dn, secret);
143         ret = NT_STATUS_OK;
144
145 done:
146         SAFE_FREE(secret);
147
148         return ret;
149 }
150
151
152 /**********************************************************************
153  Verify the sambaUnixIdPool entry in the directory.
154 **********************************************************************/
155
156 static NTSTATUS verify_idpool(void)
157 {
158         NTSTATUS ret;
159         TALLOC_CTX *ctx;
160         LDAPMessage *result = NULL;
161         LDAPMod **mods = NULL;
162         const char **attr_list;
163         char *filter;
164         int count;
165         int rc;
166
167         if ( ! idmap_alloc_ldap) {
168                 return NT_STATUS_UNSUCCESSFUL;
169         }
170
171         ctx = talloc_new(idmap_alloc_ldap);
172         if ( ! ctx) {
173                 DEBUG(0, ("Out of memory!\n"));
174                 return NT_STATUS_NO_MEMORY;
175         }
176
177         filter = talloc_asprintf(ctx, "(objectclass=%s)", LDAP_OBJ_IDPOOL);
178         CHECK_ALLOC_DONE(filter);
179
180         attr_list = get_attr_list(ctx, idpool_attr_list);
181         CHECK_ALLOC_DONE(attr_list);
182
183         rc = smbldap_search(idmap_alloc_ldap->smbldap_state,
184                                 idmap_alloc_ldap->suffix,
185                                 LDAP_SCOPE_SUBTREE,
186                                 filter,
187                                 attr_list,
188                                 0,
189                                 &result);
190
191         if (rc != LDAP_SUCCESS) {
192                 DEBUG(1, ("Unable to verify the idpool, "
193                           "cannot continue initialization!\n"));
194                 return NT_STATUS_UNSUCCESSFUL;
195         }
196
197         count = ldap_count_entries(idmap_alloc_ldap->smbldap_state->ldap_struct,
198                                    result);
199
200         ldap_msgfree(result);
201
202         if ( count > 1 ) {
203                 DEBUG(0,("Multiple entries returned from %s (base == %s)\n",
204                         filter, idmap_alloc_ldap->suffix));
205                 ret = NT_STATUS_UNSUCCESSFUL;
206                 goto done;
207         }
208         else if (count == 0) {
209                 char *uid_str, *gid_str;
210
211                 uid_str = talloc_asprintf(ctx, "%lu",
212                                 (unsigned long)idmap_alloc_ldap->low_uid);
213                 gid_str = talloc_asprintf(ctx, "%lu",
214                                 (unsigned long)idmap_alloc_ldap->low_gid);
215
216                 smbldap_set_mod(&mods, LDAP_MOD_ADD,
217                                 "objectClass", LDAP_OBJ_IDPOOL);
218                 smbldap_set_mod(&mods, LDAP_MOD_ADD,
219                                 get_attr_key2string(idpool_attr_list,
220                                                     LDAP_ATTR_UIDNUMBER),
221                                 uid_str);
222                 smbldap_set_mod(&mods, LDAP_MOD_ADD,
223                                 get_attr_key2string(idpool_attr_list,
224                                                     LDAP_ATTR_GIDNUMBER),
225                                 gid_str);
226                 if (mods) {
227                         rc = smbldap_modify(idmap_alloc_ldap->smbldap_state,
228                                                 idmap_alloc_ldap->suffix,
229                                                 mods);
230                         ldap_mods_free(mods, True);
231                 } else {
232                         ret = NT_STATUS_UNSUCCESSFUL;
233                         goto done;
234                 }
235         }
236
237         ret = (rc == LDAP_SUCCESS)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
238 done:
239         talloc_free(ctx);
240         return ret;
241 }
242
243 /*****************************************************************************
244  Initialise idmap database.
245 *****************************************************************************/
246
247 static NTSTATUS idmap_ldap_alloc_init(const char *params)
248 {
249         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
250         const char *tmp;
251         uid_t low_uid = 0;
252         uid_t high_uid = 0;
253         gid_t low_gid = 0;
254         gid_t high_gid = 0;
255
256         /* Only do init if we are online */
257         if (idmap_is_offline()) {
258                 return NT_STATUS_FILE_IS_OFFLINE;
259         }
260
261         idmap_alloc_ldap = TALLOC_ZERO_P(NULL, struct idmap_ldap_alloc_context);
262         CHECK_ALLOC_DONE( idmap_alloc_ldap );
263
264         /* load ranges */
265
266         if (!lp_idmap_uid(&low_uid, &high_uid)
267             || !lp_idmap_gid(&low_gid, &high_gid)) {
268                 DEBUG(1, ("idmap uid or idmap gid missing\n"));
269                 ret = NT_STATUS_UNSUCCESSFUL;
270                 goto done;
271         }
272
273         idmap_alloc_ldap->low_uid = low_uid;
274         idmap_alloc_ldap->high_uid = high_uid;
275         idmap_alloc_ldap->low_gid = low_gid;
276         idmap_alloc_ldap->high_gid= high_gid;
277
278         if (idmap_alloc_ldap->high_uid <= idmap_alloc_ldap->low_uid) {
279                 DEBUG(1, ("idmap uid range invalid\n"));
280                 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
281                 ret = NT_STATUS_UNSUCCESSFUL;
282                 goto done;
283         }
284
285         if (idmap_alloc_ldap->high_gid <= idmap_alloc_ldap->low_gid) {
286                 DEBUG(1, ("idmap gid range invalid\n"));
287                 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
288                 ret = NT_STATUS_UNSUCCESSFUL;
289                 goto done;
290         }
291
292         if (params && *params) {
293                 /* assume location is the only parameter */
294                 idmap_alloc_ldap->url = talloc_strdup(idmap_alloc_ldap, params);
295         } else {
296                 tmp = lp_parm_const_string(-1, "idmap alloc config",
297                                            "ldap_url", NULL);
298
299                 if ( ! tmp) {
300                         DEBUG(1, ("ERROR: missing idmap ldap url\n"));
301                         ret = NT_STATUS_UNSUCCESSFUL;
302                         goto done;
303                 }
304
305                 idmap_alloc_ldap->url = talloc_strdup(idmap_alloc_ldap, tmp);
306         }
307         CHECK_ALLOC_DONE( idmap_alloc_ldap->url );
308
309         trim_char(idmap_alloc_ldap->url, '\"', '\"');
310
311         tmp = lp_parm_const_string(-1, "idmap alloc config",
312                                    "ldap_base_dn", NULL);
313         if ( ! tmp || ! *tmp) {
314                 tmp = lp_ldap_idmap_suffix();
315                 if ( ! tmp) {
316                         DEBUG(1, ("ERROR: missing idmap ldap suffix\n"));
317                         ret = NT_STATUS_UNSUCCESSFUL;
318                         goto done;
319                 }
320         }
321
322         idmap_alloc_ldap->suffix = talloc_strdup(idmap_alloc_ldap, tmp);
323         CHECK_ALLOC_DONE( idmap_alloc_ldap->suffix );
324
325         ret = smbldap_init(idmap_alloc_ldap, winbind_event_context(),
326                            idmap_alloc_ldap->url,
327                            &idmap_alloc_ldap->smbldap_state);
328         if (!NT_STATUS_IS_OK(ret)) {
329                 DEBUG(1, ("ERROR: smbldap_init (%s) failed!\n",
330                           idmap_alloc_ldap->url));
331                 goto done;
332         }
333
334         ret = get_credentials( idmap_alloc_ldap,
335                                idmap_alloc_ldap->smbldap_state,
336                                "idmap alloc config", NULL,
337                                &idmap_alloc_ldap->user_dn );
338         if ( !NT_STATUS_IS_OK(ret) ) {
339                 DEBUG(1,("idmap_ldap_alloc_init: Failed to get connection "
340                          "credentials (%s)\n", nt_errstr(ret)));
341                 goto done;
342         }
343
344         /* see if the idmap suffix and sub entries exists */
345
346         ret = verify_idpool();
347
348  done:
349         if ( !NT_STATUS_IS_OK( ret ) )
350                 TALLOC_FREE( idmap_alloc_ldap );
351
352         return ret;
353 }
354
355 /********************************
356  Allocate a new uid or gid
357 ********************************/
358
359 static NTSTATUS idmap_ldap_allocate_id(struct unixid *xid)
360 {
361         TALLOC_CTX *ctx;
362         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
363         int rc = LDAP_SERVER_DOWN;
364         int count = 0;
365         LDAPMessage *result = NULL;
366         LDAPMessage *entry = NULL;
367         LDAPMod **mods = NULL;
368         char *id_str;
369         char *new_id_str;
370         char *filter = NULL;
371         const char *dn = NULL;
372         const char **attr_list;
373         const char *type;
374
375         /* Only do query if we are online */
376         if (idmap_is_offline()) {
377                 return NT_STATUS_FILE_IS_OFFLINE;
378         }
379
380         if ( ! idmap_alloc_ldap) {
381                 return NT_STATUS_UNSUCCESSFUL;
382         }
383
384         ctx = talloc_new(idmap_alloc_ldap);
385         if ( ! ctx) {
386                 DEBUG(0, ("Out of memory!\n"));
387                 return NT_STATUS_NO_MEMORY;
388         }
389
390         /* get type */
391         switch (xid->type) {
392
393         case ID_TYPE_UID:
394                 type = get_attr_key2string(idpool_attr_list,
395                                            LDAP_ATTR_UIDNUMBER);
396                 break;
397
398         case ID_TYPE_GID:
399                 type = get_attr_key2string(idpool_attr_list,
400                                            LDAP_ATTR_GIDNUMBER);
401                 break;
402
403         default:
404                 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
405                 return NT_STATUS_INVALID_PARAMETER;
406         }
407
408         filter = talloc_asprintf(ctx, "(objectClass=%s)", LDAP_OBJ_IDPOOL);
409         CHECK_ALLOC_DONE(filter);
410
411         attr_list = get_attr_list(ctx, idpool_attr_list);
412         CHECK_ALLOC_DONE(attr_list);
413
414         DEBUG(10, ("Search of the id pool (filter: %s)\n", filter));
415
416         rc = smbldap_search(idmap_alloc_ldap->smbldap_state,
417                                 idmap_alloc_ldap->suffix,
418                                LDAP_SCOPE_SUBTREE, filter,
419                                attr_list, 0, &result);
420
421         if (rc != LDAP_SUCCESS) {
422                 DEBUG(0,("%s object not found\n", LDAP_OBJ_IDPOOL));
423                 goto done;
424         }
425
426         talloc_autofree_ldapmsg(ctx, result);
427
428         count = ldap_count_entries(idmap_alloc_ldap->smbldap_state->ldap_struct,
429                                    result);
430         if (count != 1) {
431                 DEBUG(0,("Single %s object not found\n", LDAP_OBJ_IDPOOL));
432                 goto done;
433         }
434
435         entry = ldap_first_entry(idmap_alloc_ldap->smbldap_state->ldap_struct,
436                                  result);
437
438         dn = smbldap_talloc_dn(ctx,
439                                idmap_alloc_ldap->smbldap_state->ldap_struct,
440                                entry);
441         if ( ! dn) {
442                 goto done;
443         }
444
445         if ( ! (id_str = smbldap_talloc_single_attribute(idmap_alloc_ldap->smbldap_state->ldap_struct,
446                                 entry, type, ctx))) {
447                 DEBUG(0,("%s attribute not found\n", type));
448                 goto done;
449         }
450         if ( ! id_str) {
451                 DEBUG(0,("Out of memory\n"));
452                 ret = NT_STATUS_NO_MEMORY;
453                 goto done;
454         }
455
456         xid->id = strtoul(id_str, NULL, 10);
457
458         /* make sure we still have room to grow */
459
460         switch (xid->type) {
461         case ID_TYPE_UID:
462                 if (xid->id > idmap_alloc_ldap->high_uid) {
463                         DEBUG(0,("Cannot allocate uid above %lu!\n",
464                                  (unsigned long)idmap_alloc_ldap->high_uid));
465                         goto done;
466                 }
467                 break;
468
469         case ID_TYPE_GID:
470                 if (xid->id > idmap_alloc_ldap->high_gid) {
471                         DEBUG(0,("Cannot allocate gid above %lu!\n",
472                                  (unsigned long)idmap_alloc_ldap->high_uid));
473                         goto done;
474                 }
475                 break;
476
477         default:
478                 /* impossible */
479                 goto done;
480         }
481
482         new_id_str = talloc_asprintf(ctx, "%lu", (unsigned long)xid->id + 1);
483         if ( ! new_id_str) {
484                 DEBUG(0,("Out of memory\n"));
485                 ret = NT_STATUS_NO_MEMORY;
486                 goto done;
487         }
488
489         smbldap_set_mod(&mods, LDAP_MOD_DELETE, type, id_str);
490         smbldap_set_mod(&mods, LDAP_MOD_ADD, type, new_id_str);
491
492         if (mods == NULL) {
493                 DEBUG(0,("smbldap_set_mod() failed.\n"));
494                 goto done;
495         }
496
497         DEBUG(10, ("Try to atomically increment the id (%s -> %s)\n",
498                    id_str, new_id_str));
499
500         rc = smbldap_modify(idmap_alloc_ldap->smbldap_state, dn, mods);
501
502         ldap_mods_free(mods, True);
503
504         if (rc != LDAP_SUCCESS) {
505                 DEBUG(1,("Failed to allocate new %s. "
506                          "smbldap_modify() failed.\n", type));
507                 goto done;
508         }
509
510         ret = NT_STATUS_OK;
511
512 done:
513         talloc_free(ctx);
514         return ret;
515 }
516
517 /**********************************
518  Get current highest id.
519 **********************************/
520
521 static NTSTATUS idmap_ldap_get_hwm(struct unixid *xid)
522 {
523         TALLOC_CTX *memctx;
524         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
525         int rc = LDAP_SERVER_DOWN;
526         int count = 0;
527         LDAPMessage *result = NULL;
528         LDAPMessage *entry = NULL;
529         char *id_str;
530         char *filter = NULL;
531         const char **attr_list;
532         const char *type;
533
534         /* Only do query if we are online */
535         if (idmap_is_offline()) {
536                 return NT_STATUS_FILE_IS_OFFLINE;
537         }
538
539         if ( ! idmap_alloc_ldap) {
540                 return NT_STATUS_UNSUCCESSFUL;
541         }
542
543         memctx = talloc_new(idmap_alloc_ldap);
544         if ( ! memctx) {
545                 DEBUG(0, ("Out of memory!\n"));
546                 return NT_STATUS_NO_MEMORY;
547         }
548
549         /* get type */
550         switch (xid->type) {
551
552         case ID_TYPE_UID:
553                 type = get_attr_key2string(idpool_attr_list,
554                                            LDAP_ATTR_UIDNUMBER);
555                 break;
556
557         case ID_TYPE_GID:
558                 type = get_attr_key2string(idpool_attr_list,
559                                            LDAP_ATTR_GIDNUMBER);
560                 break;
561
562         default:
563                 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
564                 return NT_STATUS_INVALID_PARAMETER;
565         }
566
567         filter = talloc_asprintf(memctx, "(objectClass=%s)", LDAP_OBJ_IDPOOL);
568         CHECK_ALLOC_DONE(filter);
569
570         attr_list = get_attr_list(memctx, idpool_attr_list);
571         CHECK_ALLOC_DONE(attr_list);
572
573         rc = smbldap_search(idmap_alloc_ldap->smbldap_state,
574                                 idmap_alloc_ldap->suffix,
575                                LDAP_SCOPE_SUBTREE, filter,
576                                attr_list, 0, &result);
577
578         if (rc != LDAP_SUCCESS) {
579                 DEBUG(0,("%s object not found\n", LDAP_OBJ_IDPOOL));
580                 goto done;
581         }
582
583         talloc_autofree_ldapmsg(memctx, result);
584
585         count = ldap_count_entries(idmap_alloc_ldap->smbldap_state->ldap_struct,
586                                    result);
587         if (count != 1) {
588                 DEBUG(0,("Single %s object not found\n", LDAP_OBJ_IDPOOL));
589                 goto done;
590         }
591
592         entry = ldap_first_entry(idmap_alloc_ldap->smbldap_state->ldap_struct,
593                                  result);
594
595         id_str = smbldap_talloc_single_attribute(idmap_alloc_ldap->smbldap_state->ldap_struct,
596                         entry, type, memctx);
597         if ( ! id_str) {
598                 DEBUG(0,("%s attribute not found\n", type));
599                 goto done;
600         }
601         if ( ! id_str) {
602                 DEBUG(0,("Out of memory\n"));
603                 ret = NT_STATUS_NO_MEMORY;
604                 goto done;
605         }
606
607         xid->id = strtoul(id_str, NULL, 10);
608
609         ret = NT_STATUS_OK;
610 done:
611         talloc_free(memctx);
612         return ret;
613 }
614 /**********************************
615  Set highest id.
616 **********************************/
617
618 static NTSTATUS idmap_ldap_set_hwm(struct unixid *xid)
619 {
620         TALLOC_CTX *ctx;
621         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
622         int rc = LDAP_SERVER_DOWN;
623         int count = 0;
624         LDAPMessage *result = NULL;
625         LDAPMessage *entry = NULL;
626         LDAPMod **mods = NULL;
627         char *new_id_str;
628         char *filter = NULL;
629         const char *dn = NULL;
630         const char **attr_list;
631         const char *type;
632
633         /* Only do query if we are online */
634         if (idmap_is_offline()) {
635                 return NT_STATUS_FILE_IS_OFFLINE;
636         }
637
638         if ( ! idmap_alloc_ldap) {
639                 return NT_STATUS_UNSUCCESSFUL;
640         }
641
642         ctx = talloc_new(idmap_alloc_ldap);
643         if ( ! ctx) {
644                 DEBUG(0, ("Out of memory!\n"));
645                 return NT_STATUS_NO_MEMORY;
646         }
647
648         /* get type */
649         switch (xid->type) {
650
651         case ID_TYPE_UID:
652                 type = get_attr_key2string(idpool_attr_list,
653                                            LDAP_ATTR_UIDNUMBER);
654                 break;
655
656         case ID_TYPE_GID:
657                 type = get_attr_key2string(idpool_attr_list,
658                                            LDAP_ATTR_GIDNUMBER);
659                 break;
660
661         default:
662                 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
663                 return NT_STATUS_INVALID_PARAMETER;
664         }
665
666         filter = talloc_asprintf(ctx, "(objectClass=%s)", LDAP_OBJ_IDPOOL);
667         CHECK_ALLOC_DONE(filter);
668
669         attr_list = get_attr_list(ctx, idpool_attr_list);
670         CHECK_ALLOC_DONE(attr_list);
671
672         rc = smbldap_search(idmap_alloc_ldap->smbldap_state,
673                                 idmap_alloc_ldap->suffix,
674                                LDAP_SCOPE_SUBTREE, filter,
675                                attr_list, 0, &result);
676
677         if (rc != LDAP_SUCCESS) {
678                 DEBUG(0,("%s object not found\n", LDAP_OBJ_IDPOOL));
679                 goto done;
680         }
681
682         talloc_autofree_ldapmsg(ctx, result);
683
684         count = ldap_count_entries(idmap_alloc_ldap->smbldap_state->ldap_struct,
685                                    result);
686         if (count != 1) {
687                 DEBUG(0,("Single %s object not found\n", LDAP_OBJ_IDPOOL));
688                 goto done;
689         }
690
691         entry = ldap_first_entry(idmap_alloc_ldap->smbldap_state->ldap_struct,
692                                  result);
693
694         dn = smbldap_talloc_dn(ctx,
695                                 idmap_alloc_ldap->smbldap_state->ldap_struct,
696                                 entry);
697         if ( ! dn) {
698                 goto done;
699         }
700
701         new_id_str = talloc_asprintf(ctx, "%lu", (unsigned long)xid->id);
702         if ( ! new_id_str) {
703                 DEBUG(0,("Out of memory\n"));
704                 ret = NT_STATUS_NO_MEMORY;
705                 goto done;
706         }
707
708         smbldap_set_mod(&mods, LDAP_MOD_REPLACE, type, new_id_str);
709
710         if (mods == NULL) {
711                 DEBUG(0,("smbldap_set_mod() failed.\n"));
712                 goto done;
713         }
714
715         rc = smbldap_modify(idmap_alloc_ldap->smbldap_state, dn, mods);
716
717         ldap_mods_free(mods, True);
718
719         if (rc != LDAP_SUCCESS) {
720                 DEBUG(1,("Failed to allocate new %s. "
721                          "smbldap_modify() failed.\n", type));
722                 goto done;
723         }
724
725         ret = NT_STATUS_OK;
726
727 done:
728         talloc_free(ctx);
729         return ret;
730 }
731
732 /**********************************
733  Close idmap ldap alloc
734 **********************************/
735
736 static NTSTATUS idmap_ldap_alloc_close(void)
737 {
738         if (idmap_alloc_ldap) {
739                 smbldap_free_struct(&idmap_alloc_ldap->smbldap_state);
740                 DEBUG(5,("The connection to the LDAP server was closed\n"));
741                 /* maybe free the results here --metze */
742                 TALLOC_FREE(idmap_alloc_ldap);
743         }
744         return NT_STATUS_OK;
745 }
746
747
748 /**********************************************************************
749  IDMAP MAPPING LDAP BACKEND
750 **********************************************************************/
751
752 static int idmap_ldap_close_destructor(struct idmap_ldap_context *ctx)
753 {
754         smbldap_free_struct(&ctx->smbldap_state);
755         DEBUG(5,("The connection to the LDAP server was closed\n"));
756         /* maybe free the results here --metze */
757
758         return 0;
759 }
760
761 /********************************
762  Initialise idmap database.
763 ********************************/
764
765 static NTSTATUS idmap_ldap_db_init(struct idmap_domain *dom,
766                                    const char *params)
767 {
768         NTSTATUS ret;
769         struct idmap_ldap_context *ctx = NULL;
770         char *config_option = NULL;
771         const char *tmp = NULL;
772
773         /* Only do init if we are online */
774         if (idmap_is_offline()) {
775                 return NT_STATUS_FILE_IS_OFFLINE;
776         }
777
778         ctx = TALLOC_ZERO_P(dom, struct idmap_ldap_context);
779         if ( ! ctx) {
780                 DEBUG(0, ("Out of memory!\n"));
781                 return NT_STATUS_NO_MEMORY;
782         }
783
784         if (strequal(dom->name, "*")) {
785                 uid_t low_uid = 0;
786                 uid_t high_uid = 0;
787                 gid_t low_gid = 0;
788                 gid_t high_gid = 0;
789
790                 ctx->filter_low_id = 0;
791                 ctx->filter_high_id = 0;
792
793                 if (lp_idmap_uid(&low_uid, &high_uid)) {
794                         ctx->filter_low_id = low_uid;
795                         ctx->filter_high_id = high_uid;
796                 } else {
797                         DEBUG(3, ("Warning: 'idmap uid' not set!\n"));
798                 }
799
800                 if (lp_idmap_gid(&low_gid, &high_gid)) {
801                         if ((low_gid != low_uid) || (high_gid != high_uid)) {
802                                 DEBUG(1, ("Warning: 'idmap uid' and 'idmap gid'"
803                                       " ranges do not agree -- building "
804                                       "intersection\n"));
805                                 ctx->filter_low_id = MAX(ctx->filter_low_id,
806                                                          low_gid);
807                                 ctx->filter_high_id = MIN(ctx->filter_high_id,
808                                                           high_gid);
809                         }
810                 } else {
811                         DEBUG(3, ("Warning: 'idmap gid' not set!\n"));
812                 }
813         } else {
814                 const char *range = NULL;
815
816                 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
817                 if ( ! config_option) {
818                         DEBUG(0, ("Out of memory!\n"));
819                         ret = NT_STATUS_NO_MEMORY;
820                         goto done;
821                 }
822
823                 /* load ranges */
824                 range = lp_parm_const_string(-1, config_option, "range", NULL);
825                 if (range && range[0]) {
826                         if ((sscanf(range, "%u - %u", &ctx->filter_low_id,
827                                                         &ctx->filter_high_id) != 2))
828                         {
829                                 DEBUG(1, ("ERROR: invalid filter range [%s]", range));
830                                 ctx->filter_low_id = 0;
831                                 ctx->filter_high_id = 0;
832                         }
833                 }
834         }
835
836         if (ctx->filter_low_id > ctx->filter_high_id) {
837                 DEBUG(1, ("ERROR: invalid filter range [%u-%u]",
838                       ctx->filter_low_id, ctx->filter_high_id));
839                 ctx->filter_low_id = 0;
840                 ctx->filter_high_id = 0;
841         }
842
843         if (params != NULL) {
844                 /* assume location is the only parameter */
845                 ctx->url = talloc_strdup(ctx, params);
846         } else {
847                 tmp = lp_parm_const_string(-1, config_option, "ldap_url", NULL);
848
849                 if ( ! tmp) {
850                         DEBUG(1, ("ERROR: missing idmap ldap url\n"));
851                         ret = NT_STATUS_UNSUCCESSFUL;
852                         goto done;
853                 }
854
855                 ctx->url = talloc_strdup(ctx, tmp);
856         }
857         CHECK_ALLOC_DONE(ctx->url);
858
859         tmp = lp_parm_const_string(-1, config_option, "ldap_base_dn", NULL);
860         if ( ! tmp || ! *tmp) {
861                 tmp = lp_ldap_idmap_suffix();
862                 if ( ! tmp) {
863                         DEBUG(1, ("ERROR: missing idmap ldap suffix\n"));
864                         ret = NT_STATUS_UNSUCCESSFUL;
865                         goto done;
866                 }
867         }
868
869         ctx->suffix = talloc_strdup(ctx, tmp);
870         CHECK_ALLOC_DONE(ctx->suffix);
871
872         ret = smbldap_init(ctx, winbind_event_context(), ctx->url,
873                            &ctx->smbldap_state);
874         if (!NT_STATUS_IS_OK(ret)) {
875                 DEBUG(1, ("ERROR: smbldap_init (%s) failed!\n", ctx->url));
876                 goto done;
877         }
878
879         ret = get_credentials( ctx, ctx->smbldap_state, config_option,
880                                dom, &ctx->user_dn );
881         if ( !NT_STATUS_IS_OK(ret) ) {
882                 DEBUG(1,("idmap_ldap_db_init: Failed to get connection "
883                          "credentials (%s)\n", nt_errstr(ret)));
884                 goto done;
885         }
886
887         /* set the destructor on the context, so that resource are properly
888            freed if the contexts is released */
889
890         talloc_set_destructor(ctx, idmap_ldap_close_destructor);
891
892         dom->private_data = ctx;
893
894         talloc_free(config_option);
895         return NT_STATUS_OK;
896
897 /*failed */
898 done:
899         talloc_free(ctx);
900         return ret;
901 }
902
903 /* max number of ids requested per batch query */
904 #define IDMAP_LDAP_MAX_IDS 30
905
906 /**********************************
907  lookup a set of unix ids.
908 **********************************/
909
910 /* this function searches up to IDMAP_LDAP_MAX_IDS entries
911  * in maps for a match */
912 static struct id_map *find_map_by_id(struct id_map **maps,
913                                      enum id_type type,
914                                      uint32_t id)
915 {
916         int i;
917
918         for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
919                 if (maps[i] == NULL) { /* end of the run */
920                         return NULL;
921                 }
922                 if ((maps[i]->xid.type == type) && (maps[i]->xid.id == id)) {
923                         return maps[i];
924                 }
925         }
926
927         return NULL;
928 }
929
930 static NTSTATUS idmap_ldap_unixids_to_sids(struct idmap_domain *dom,
931                                            struct id_map **ids)
932 {
933         NTSTATUS ret;
934         TALLOC_CTX *memctx;
935         struct idmap_ldap_context *ctx;
936         LDAPMessage *result = NULL;
937         LDAPMessage *entry = NULL;
938         const char *uidNumber;
939         const char *gidNumber;
940         const char **attr_list;
941         char *filter = NULL;
942         bool multi = False;
943         int idx = 0;
944         int bidx = 0;
945         int count;
946         int rc;
947         int i;
948
949         /* Only do query if we are online */
950         if (idmap_is_offline()) {
951                 return NT_STATUS_FILE_IS_OFFLINE;
952         }
953
954         ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
955
956         memctx = talloc_new(ctx);
957         if ( ! memctx) {
958                 DEBUG(0, ("Out of memory!\n"));
959                 return NT_STATUS_NO_MEMORY;
960         }
961
962         uidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_UIDNUMBER);
963         gidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_GIDNUMBER);
964
965         attr_list = get_attr_list(memctx, sidmap_attr_list);
966
967         if ( ! ids[1]) {
968                 /* if we are requested just one mapping use the simple filter */
969
970                 filter = talloc_asprintf(memctx, "(&(objectClass=%s)(%s=%lu))",
971                                 LDAP_OBJ_IDMAP_ENTRY,
972                                 (ids[0]->xid.type==ID_TYPE_UID)?uidNumber:gidNumber,
973                                 (unsigned long)ids[0]->xid.id);
974                 CHECK_ALLOC_DONE(filter);
975                 DEBUG(10, ("Filter: [%s]\n", filter));
976         } else {
977                 /* multiple mappings */
978                 multi = True;
979         }
980
981         for (i = 0; ids[i]; i++) {
982                 ids[i]->status = ID_UNKNOWN;
983         }
984
985 again:
986         if (multi) {
987
988                 talloc_free(filter);
989                 filter = talloc_asprintf(memctx,
990                                          "(&(objectClass=%s)(|",
991                                          LDAP_OBJ_IDMAP_ENTRY);
992                 CHECK_ALLOC_DONE(filter);
993
994                 bidx = idx;
995                 for (i = 0; (i < IDMAP_LDAP_MAX_IDS) && ids[idx]; i++, idx++) {
996                         filter = talloc_asprintf_append_buffer(filter, "(%s=%lu)",
997                                         (ids[idx]->xid.type==ID_TYPE_UID)?uidNumber:gidNumber,
998                                         (unsigned long)ids[idx]->xid.id);
999                         CHECK_ALLOC_DONE(filter);
1000                 }
1001                 filter = talloc_asprintf_append_buffer(filter, "))");
1002                 CHECK_ALLOC_DONE(filter);
1003                 DEBUG(10, ("Filter: [%s]\n", filter));
1004         } else {
1005                 bidx = 0;
1006                 idx = 1;
1007         }
1008
1009         rc = smbldap_search(ctx->smbldap_state, ctx->suffix, LDAP_SCOPE_SUBTREE,
1010                 filter, attr_list, 0, &result);
1011
1012         if (rc != LDAP_SUCCESS) {
1013                 DEBUG(3,("Failure looking up ids (%s)\n", ldap_err2string(rc)));
1014                 ret = NT_STATUS_UNSUCCESSFUL;
1015                 goto done;
1016         }
1017
1018         count = ldap_count_entries(ctx->smbldap_state->ldap_struct, result);
1019
1020         if (count == 0) {
1021                 DEBUG(10, ("NO SIDs found\n"));
1022         }
1023
1024         for (i = 0; i < count; i++) {
1025                 char *sidstr = NULL;
1026                 char *tmp = NULL;
1027                 enum id_type type;
1028                 struct id_map *map;
1029                 uint32_t id;
1030
1031                 if (i == 0) { /* first entry */
1032                         entry = ldap_first_entry(ctx->smbldap_state->ldap_struct,
1033                                                  result);
1034                 } else { /* following ones */
1035                         entry = ldap_next_entry(ctx->smbldap_state->ldap_struct,
1036                                                 entry);
1037                 }
1038                 if ( ! entry) {
1039                         DEBUG(2, ("ERROR: Unable to fetch ldap entries "
1040                                   "from results\n"));
1041                         break;
1042                 }
1043
1044                 /* first check if the SID is present */
1045                 sidstr = smbldap_talloc_single_attribute(
1046                                 ctx->smbldap_state->ldap_struct,
1047                                 entry, LDAP_ATTRIBUTE_SID, memctx);
1048                 if ( ! sidstr) { /* no sid, skip entry */
1049                         DEBUG(2, ("WARNING SID not found on entry\n"));
1050                         continue;
1051                 }
1052
1053                 /* now try to see if it is a uid, if not try with a gid
1054                  * (gid is more common, but in case both uidNumber and
1055                  * gidNumber are returned the SID is mapped to the uid
1056                  *not the gid) */
1057                 type = ID_TYPE_UID;
1058                 tmp = smbldap_talloc_single_attribute(
1059                                 ctx->smbldap_state->ldap_struct,
1060                                 entry, uidNumber, memctx);
1061                 if ( ! tmp) {
1062                         type = ID_TYPE_GID;
1063                         tmp = smbldap_talloc_single_attribute(
1064                                         ctx->smbldap_state->ldap_struct,
1065                                         entry, gidNumber, memctx);
1066                 }
1067                 if ( ! tmp) { /* wow very strange entry, how did it match ? */
1068                         DEBUG(5, ("Unprobable match on (%s), no uidNumber, "
1069                                   "nor gidNumber returned\n", sidstr));
1070                         TALLOC_FREE(sidstr);
1071                         continue;
1072                 }
1073
1074                 id = strtoul(tmp, NULL, 10);
1075                 if ((id == 0) ||
1076                     (ctx->filter_low_id && (id < ctx->filter_low_id)) ||
1077                     (ctx->filter_high_id && (id > ctx->filter_high_id))) {
1078                         DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
1079                                   "Filtered!\n", id,
1080                                   ctx->filter_low_id, ctx->filter_high_id));
1081                         TALLOC_FREE(sidstr);
1082                         TALLOC_FREE(tmp);
1083                         continue;
1084                 }
1085                 TALLOC_FREE(tmp);
1086
1087                 map = find_map_by_id(&ids[bidx], type, id);
1088                 if (!map) {
1089                         DEBUG(2, ("WARNING: couldn't match sid (%s) "
1090                                   "with requested ids\n", sidstr));
1091                         TALLOC_FREE(sidstr);
1092                         continue;
1093                 }
1094
1095                 if ( ! string_to_sid(map->sid, sidstr)) {
1096                         DEBUG(2, ("ERROR: Invalid SID on entry\n"));
1097                         TALLOC_FREE(sidstr);
1098                         continue;
1099                 }
1100
1101                 if (map->status == ID_MAPPED) {
1102                         DEBUG(1, ("WARNING: duplicate %s mapping in LDAP. "
1103                               "overwriting mapping %u -> %s with %u -> %s\n",
1104                               (type == ID_TYPE_UID) ? "UID" : "GID",
1105                               id, sid_string_dbg(map->sid), id, sidstr));
1106                 }
1107
1108                 TALLOC_FREE(sidstr);
1109
1110                 /* mapped */
1111                 map->status = ID_MAPPED;
1112
1113                 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
1114                            (unsigned long)map->xid.id, map->xid.type));
1115         }
1116
1117         /* free the ldap results */
1118         if (result) {
1119                 ldap_msgfree(result);
1120                 result = NULL;
1121         }
1122
1123         if (multi && ids[idx]) { /* still some values to map */
1124                 goto again;
1125         }
1126
1127         ret = NT_STATUS_OK;
1128
1129         /* mark all unknwon/expired ones as unmapped */
1130         for (i = 0; ids[i]; i++) {
1131                 if (ids[i]->status != ID_MAPPED)
1132                         ids[i]->status = ID_UNMAPPED;
1133         }
1134
1135 done:
1136         talloc_free(memctx);
1137         return ret;
1138 }
1139
1140 /**********************************
1141  lookup a set of sids.
1142 **********************************/
1143
1144 /* this function searches up to IDMAP_LDAP_MAX_IDS entries
1145  * in maps for a match */
1146 static struct id_map *find_map_by_sid(struct id_map **maps, DOM_SID *sid)
1147 {
1148         int i;
1149
1150         for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
1151                 if (maps[i] == NULL) { /* end of the run */
1152                         return NULL;
1153                 }
1154                 if (sid_equal(maps[i]->sid, sid)) {
1155                         return maps[i];
1156                 }
1157         }
1158
1159         return NULL;
1160 }
1161
1162 static NTSTATUS idmap_ldap_sids_to_unixids(struct idmap_domain *dom,
1163                                            struct id_map **ids)
1164 {
1165         LDAPMessage *entry = NULL;
1166         NTSTATUS ret;
1167         TALLOC_CTX *memctx;
1168         struct idmap_ldap_context *ctx;
1169         LDAPMessage *result = NULL;
1170         const char *uidNumber;
1171         const char *gidNumber;
1172         const char **attr_list;
1173         char *filter = NULL;
1174         bool multi = False;
1175         int idx = 0;
1176         int bidx = 0;
1177         int count;
1178         int rc;
1179         int i;
1180
1181         /* Only do query if we are online */
1182         if (idmap_is_offline()) {
1183                 return NT_STATUS_FILE_IS_OFFLINE;
1184         }
1185
1186         ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
1187
1188         memctx = talloc_new(ctx);
1189         if ( ! memctx) {
1190                 DEBUG(0, ("Out of memory!\n"));
1191                 return NT_STATUS_NO_MEMORY;
1192         }
1193
1194         uidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_UIDNUMBER);
1195         gidNumber = get_attr_key2string(idpool_attr_list, LDAP_ATTR_GIDNUMBER);
1196
1197         attr_list = get_attr_list(memctx, sidmap_attr_list);
1198
1199         if ( ! ids[1]) {
1200                 /* if we are requested just one mapping use the simple filter */
1201
1202                 filter = talloc_asprintf(memctx, "(&(objectClass=%s)(%s=%s))",
1203                                 LDAP_OBJ_IDMAP_ENTRY,
1204                                 LDAP_ATTRIBUTE_SID,
1205                                 sid_string_talloc(memctx, ids[0]->sid));
1206                 CHECK_ALLOC_DONE(filter);
1207                 DEBUG(10, ("Filter: [%s]\n", filter));
1208         } else {
1209                 /* multiple mappings */
1210                 multi = True;
1211         }
1212
1213         for (i = 0; ids[i]; i++) {
1214                 ids[i]->status = ID_UNKNOWN;
1215         }
1216
1217 again:
1218         if (multi) {
1219
1220                 TALLOC_FREE(filter);
1221                 filter = talloc_asprintf(memctx,
1222                                          "(&(objectClass=%s)(|",
1223                                          LDAP_OBJ_IDMAP_ENTRY);
1224                 CHECK_ALLOC_DONE(filter);
1225
1226                 bidx = idx;
1227                 for (i = 0; (i < IDMAP_LDAP_MAX_IDS) && ids[idx]; i++, idx++) {
1228                         filter = talloc_asprintf_append_buffer(filter, "(%s=%s)",
1229                                         LDAP_ATTRIBUTE_SID,
1230                                         sid_string_talloc(memctx,
1231                                                           ids[idx]->sid));
1232                         CHECK_ALLOC_DONE(filter);
1233                 }
1234                 filter = talloc_asprintf_append_buffer(filter, "))");
1235                 CHECK_ALLOC_DONE(filter);
1236                 DEBUG(10, ("Filter: [%s]", filter));
1237         } else {
1238                 bidx = 0;
1239                 idx = 1;
1240         }
1241
1242         rc = smbldap_search(ctx->smbldap_state, ctx->suffix, LDAP_SCOPE_SUBTREE,
1243                 filter, attr_list, 0, &result);
1244
1245         if (rc != LDAP_SUCCESS) {
1246                 DEBUG(3,("Failure looking up sids (%s)\n",
1247                          ldap_err2string(rc)));
1248                 ret = NT_STATUS_UNSUCCESSFUL;
1249                 goto done;
1250         }
1251
1252         count = ldap_count_entries(ctx->smbldap_state->ldap_struct, result);
1253
1254         if (count == 0) {
1255                 DEBUG(10, ("NO SIDs found\n"));
1256         }
1257
1258         for (i = 0; i < count; i++) {
1259                 char *sidstr = NULL;
1260                 char *tmp = NULL;
1261                 enum id_type type;
1262                 struct id_map *map;
1263                 DOM_SID sid;
1264                 uint32_t id;
1265
1266                 if (i == 0) { /* first entry */
1267                         entry = ldap_first_entry(ctx->smbldap_state->ldap_struct,
1268                                                  result);
1269                 } else { /* following ones */
1270                         entry = ldap_next_entry(ctx->smbldap_state->ldap_struct,
1271                                                 entry);
1272                 }
1273                 if ( ! entry) {
1274                         DEBUG(2, ("ERROR: Unable to fetch ldap entries "
1275                                   "from results\n"));
1276                         break;
1277                 }
1278
1279                 /* first check if the SID is present */
1280                 sidstr = smbldap_talloc_single_attribute(
1281                                 ctx->smbldap_state->ldap_struct,
1282                                 entry, LDAP_ATTRIBUTE_SID, memctx);
1283                 if ( ! sidstr) { /* no sid ??, skip entry */
1284                         DEBUG(2, ("WARNING SID not found on entry\n"));
1285                         continue;
1286                 }
1287
1288                 if ( ! string_to_sid(&sid, sidstr)) {
1289                         DEBUG(2, ("ERROR: Invalid SID on entry\n"));
1290                         TALLOC_FREE(sidstr);
1291                         continue;
1292                 }
1293
1294                 map = find_map_by_sid(&ids[bidx], &sid);
1295                 if (!map) {
1296                         DEBUG(2, ("WARNING: couldn't find entry sid (%s) "
1297                                   "in ids", sidstr));
1298                         TALLOC_FREE(sidstr);
1299                         continue;
1300                 }
1301
1302                 /* now try to see if it is a uid, if not try with a gid
1303                  * (gid is more common, but in case both uidNumber and
1304                  * gidNumber are returned the SID is mapped to the uid
1305                  * not the gid) */
1306                 type = ID_TYPE_UID;
1307                 tmp = smbldap_talloc_single_attribute(
1308                                 ctx->smbldap_state->ldap_struct,
1309                                 entry, uidNumber, memctx);
1310                 if ( ! tmp) {
1311                         type = ID_TYPE_GID;
1312                         tmp = smbldap_talloc_single_attribute(
1313                                         ctx->smbldap_state->ldap_struct,
1314                                         entry, gidNumber, memctx);
1315                 }
1316                 if ( ! tmp) { /* no ids ?? */
1317                         DEBUG(5, ("no uidNumber, "
1318                                   "nor gidNumber attributes found\n"));
1319                         TALLOC_FREE(sidstr);
1320                         continue;
1321                 }
1322
1323                 id = strtoul(tmp, NULL, 10);
1324                 if ((id == 0) ||
1325                     (ctx->filter_low_id && (id < ctx->filter_low_id)) ||
1326                     (ctx->filter_high_id && (id > ctx->filter_high_id))) {
1327                         DEBUG(5, ("Requested id (%u) out of range (%u - %u). "
1328                                   "Filtered!\n", id,
1329                                   ctx->filter_low_id, ctx->filter_high_id));
1330                         TALLOC_FREE(sidstr);
1331                         TALLOC_FREE(tmp);
1332                         continue;
1333                 }
1334                 TALLOC_FREE(tmp);
1335
1336                 if (map->status == ID_MAPPED) {
1337                         DEBUG(1, ("WARNING: duplicate %s mapping in LDAP. "
1338                               "overwriting mapping %s -> %u with %s -> %u\n",
1339                               (type == ID_TYPE_UID) ? "UID" : "GID",
1340                               sidstr, map->xid.id, sidstr, id));
1341                 }
1342
1343                 TALLOC_FREE(sidstr);
1344
1345                 /* mapped */
1346                 map->xid.type = type;
1347                 map->xid.id = id;
1348                 map->status = ID_MAPPED;
1349
1350                 DEBUG(10, ("Mapped %s -> %lu (%d)\n", sid_string_dbg(map->sid),
1351                            (unsigned long)map->xid.id, map->xid.type));
1352         }
1353
1354         /* free the ldap results */
1355         if (result) {
1356                 ldap_msgfree(result);
1357                 result = NULL;
1358         }
1359
1360         if (multi && ids[idx]) { /* still some values to map */
1361                 goto again;
1362         }
1363
1364         ret = NT_STATUS_OK;
1365
1366         /* mark all unknwon/expired ones as unmapped */
1367         for (i = 0; ids[i]; i++) {
1368                 if (ids[i]->status != ID_MAPPED)
1369                         ids[i]->status = ID_UNMAPPED;
1370         }
1371
1372 done:
1373         talloc_free(memctx);
1374         return ret;
1375 }
1376
1377 /**********************************
1378  set a mapping.
1379 **********************************/
1380
1381 /* TODO: change this:  This function cannot be called to modify a mapping,
1382  * only set a new one */
1383
1384 static NTSTATUS idmap_ldap_set_mapping(struct idmap_domain *dom,
1385                                        const struct id_map *map)
1386 {
1387         NTSTATUS ret;
1388         TALLOC_CTX *memctx;
1389         struct idmap_ldap_context *ctx;
1390         LDAPMessage *entry = NULL;
1391         LDAPMod **mods = NULL;
1392         const char *type;
1393         char *id_str;
1394         char *sid;
1395         char *dn;
1396         int rc = -1;
1397
1398         /* Only do query if we are online */
1399         if (idmap_is_offline()) {
1400                 return NT_STATUS_FILE_IS_OFFLINE;
1401         }
1402
1403         ctx = talloc_get_type(dom->private_data, struct idmap_ldap_context);
1404
1405         switch(map->xid.type) {
1406         case ID_TYPE_UID:
1407                 type = get_attr_key2string(sidmap_attr_list,
1408                                            LDAP_ATTR_UIDNUMBER);
1409                 break;
1410
1411         case ID_TYPE_GID:
1412                 type = get_attr_key2string(sidmap_attr_list,
1413                                            LDAP_ATTR_GIDNUMBER);
1414                 break;
1415
1416         default:
1417                 return NT_STATUS_INVALID_PARAMETER;
1418         }
1419
1420         memctx = talloc_new(ctx);
1421         if ( ! memctx) {
1422                 DEBUG(0, ("Out of memory!\n"));
1423                 return NT_STATUS_NO_MEMORY;
1424         }
1425
1426         id_str = talloc_asprintf(memctx, "%lu", (unsigned long)map->xid.id);
1427         CHECK_ALLOC_DONE(id_str);
1428
1429         sid = talloc_strdup(memctx, sid_string_talloc(memctx, map->sid));
1430         CHECK_ALLOC_DONE(sid);
1431
1432         dn = talloc_asprintf(memctx, "%s=%s,%s",
1433                         get_attr_key2string(sidmap_attr_list, LDAP_ATTR_SID),
1434                         sid,
1435                         ctx->suffix);
1436         CHECK_ALLOC_DONE(dn);
1437
1438         smbldap_set_mod(&mods, LDAP_MOD_ADD,
1439                         "objectClass", LDAP_OBJ_IDMAP_ENTRY);
1440
1441         smbldap_make_mod(ctx->smbldap_state->ldap_struct,
1442                          entry, &mods, type, id_str);
1443
1444         smbldap_make_mod(ctx->smbldap_state->ldap_struct, entry, &mods,
1445                          get_attr_key2string(sidmap_attr_list, LDAP_ATTR_SID),
1446                          sid);
1447
1448         if ( ! mods) {
1449                 DEBUG(2, ("ERROR: No mods?\n"));
1450                 ret = NT_STATUS_UNSUCCESSFUL;
1451                 goto done;
1452         }
1453
1454         /* TODO: remove conflicting mappings! */
1455
1456         smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_SID_ENTRY);
1457
1458         DEBUG(10, ("Set DN %s (%s -> %s)\n", dn, sid, id_str));
1459
1460         rc = smbldap_add(ctx->smbldap_state, dn, mods);
1461         ldap_mods_free(mods, True);
1462
1463         if (rc != LDAP_SUCCESS) {
1464                 char *ld_error = NULL;
1465                 ldap_get_option(ctx->smbldap_state->ldap_struct,
1466                                 LDAP_OPT_ERROR_STRING, &ld_error);
1467                 DEBUG(0,("ldap_set_mapping_internals: Failed to add %s to %lu "
1468                          "mapping [%s]\n", sid,
1469                          (unsigned long)map->xid.id, type));
1470                 DEBUG(0, ("ldap_set_mapping_internals: Error was: %s (%s)\n",
1471                         ld_error ? ld_error : "(NULL)", ldap_err2string (rc)));
1472                 if (ld_error) {
1473                         ldap_memfree(ld_error);
1474                 }
1475                 ret = NT_STATUS_UNSUCCESSFUL;
1476                 goto done;
1477         }
1478
1479         DEBUG(10,("ldap_set_mapping: Successfully created mapping from %s to "
1480                   "%lu [%s]\n", sid, (unsigned long)map->xid.id, type));
1481
1482         ret = NT_STATUS_OK;
1483
1484 done:
1485         talloc_free(memctx);
1486         return ret;
1487 }
1488
1489 /**********************************
1490  Close the idmap ldap instance
1491 **********************************/
1492
1493 static NTSTATUS idmap_ldap_close(struct idmap_domain *dom)
1494 {
1495         struct idmap_ldap_context *ctx;
1496
1497         if (dom->private_data) {
1498                 ctx = talloc_get_type(dom->private_data,
1499                                       struct idmap_ldap_context);
1500
1501                 talloc_free(ctx);
1502                 dom->private_data = NULL;
1503         }
1504
1505         return NT_STATUS_OK;
1506 }
1507
1508 static struct idmap_methods idmap_ldap_methods = {
1509
1510         .init = idmap_ldap_db_init,
1511         .unixids_to_sids = idmap_ldap_unixids_to_sids,
1512         .sids_to_unixids = idmap_ldap_sids_to_unixids,
1513         .set_mapping = idmap_ldap_set_mapping,
1514         .close_fn = idmap_ldap_close
1515 };
1516
1517 static struct idmap_alloc_methods idmap_ldap_alloc_methods = {
1518
1519         .init = idmap_ldap_alloc_init,
1520         .allocate_id = idmap_ldap_allocate_id,
1521         .get_id_hwm = idmap_ldap_get_hwm,
1522         .set_id_hwm = idmap_ldap_set_hwm,
1523         .close_fn = idmap_ldap_alloc_close,
1524         /* .dump_data = TODO */
1525 };
1526
1527 static NTSTATUS idmap_alloc_ldap_init(void)
1528 {
1529         return smb_register_idmap_alloc(SMB_IDMAP_INTERFACE_VERSION, "ldap",
1530                                         &idmap_ldap_alloc_methods);
1531 }
1532
1533 NTSTATUS idmap_ldap_init(void);
1534 NTSTATUS idmap_ldap_init(void)
1535 {
1536         NTSTATUS ret;
1537
1538         /* FIXME: bad hack to actually register also the alloc_ldap module
1539          * without changining configure.in */
1540         ret = idmap_alloc_ldap_init();
1541         if (! NT_STATUS_IS_OK(ret)) {
1542                 return ret;
1543         }
1544         return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "ldap",
1545                                   &idmap_ldap_methods);
1546 }
1547