2 ldb database library - Samba3 SAM compatibility backend
4 Copyright (C) Jelmer Vernooij 2005
5 Copyright (C) Martin Kuehl <mkhl@samba.org> 2006
9 #include "ldb/include/ldb.h"
10 #include "ldb/include/ldb_private.h"
11 #include "ldb/include/ldb_errors.h"
12 #include "ldb/ldb_map/ldb_map.h"
13 #include "system/passwd.h"
15 #include "librpc/gen_ndr/ndr_security.h"
16 #include "librpc/gen_ndr/ndr_samr.h"
17 #include "librpc/ndr/libndr.h"
18 #include "libcli/security/security.h"
19 #include "libcli/security/proto.h"
20 #include "lib/samba3/samba3.h"
23 * sambaSID -> member (dn!)
24 * sambaSIDList -> member (dn!)
25 * sambaDomainName -> name
31 * sambaAcctFlags -> systemFlags ?
32 * sambaPasswordHistory -> ntPwdHistory*/
40 * sambaAlgorithmicRidBase
51 * sambaUserWorkstations
55 /* In Samba4 but not in Samba3:
58 /* From a sambaPrimaryGroupSID, generate a primaryGroupID (integer) attribute */
59 static struct ldb_message_element *generate_primaryGroupID(struct ldb_module *module, TALLOC_CTX *ctx, const char *local_attr, const struct ldb_message *remote)
61 struct ldb_message_element *el;
62 const char *sid = ldb_msg_find_attr_as_string(remote, "sambaPrimaryGroupSID", NULL);
68 p = strrchr(sid, '-');
72 el = talloc_zero(ctx, struct ldb_message_element);
73 el->name = talloc_strdup(ctx, "primaryGroupID");
75 el->values = talloc_array(ctx, struct ldb_val, 1);
76 el->values[0].data = (uint8_t *)talloc_strdup(el->values, p+1);
77 el->values[0].length = strlen((char *)el->values[0].data);
82 static void generate_sambaPrimaryGroupSID(struct ldb_module *module, const char *local_attr, const struct ldb_message *local, struct ldb_message *remote_mp, struct ldb_message *remote_fb)
84 const struct ldb_val *sidval;
87 enum ndr_err_code ndr_err;
89 /* We need the domain, so we get it from the objectSid that we hope is here... */
90 sidval = ldb_msg_find_ldb_val(local, "objectSid");
93 return; /* Sorry, no SID today.. */
95 sid = talloc(remote_mp, struct dom_sid);
100 ndr_err = ndr_pull_struct_blob(sidval, sid, NULL, sid, (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
101 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
106 if (!ldb_msg_find_ldb_val(local, "primaryGroupID"))
107 return; /* Sorry, no SID today.. */
111 sidstring = dom_sid_string(remote_mp, sid);
113 ldb_msg_add_fmt(remote_mp, "sambaPrimaryGroupSID", "%s-%d", sidstring, ldb_msg_find_attr_as_uint(local, "primaryGroupID", 0));
114 talloc_free(sidstring);
117 /* Just copy the old value. */
118 static struct ldb_val convert_uid_samaccount(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
120 struct ldb_val out = data_blob(NULL, 0);
121 ldb_handler_copy(module->ldb, ctx, val, &out);
126 static struct ldb_val lookup_homedir(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
129 struct ldb_val retval;
131 pwd = getpwnam((char *)val->data);
134 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "Unable to lookup '%s' in passwd", (char *)val->data);
135 return *talloc_zero(ctx, struct ldb_val);
138 retval.data = (uint8_t *)talloc_strdup(ctx, pwd->pw_dir);
139 retval.length = strlen((char *)retval.data);
144 static struct ldb_val lookup_gid(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
147 struct ldb_val retval;
149 pwd = getpwnam((char *)val->data);
152 return *talloc_zero(ctx, struct ldb_val);
155 retval.data = (uint8_t *)talloc_asprintf(ctx, "%ld", (unsigned long)pwd->pw_gid);
156 retval.length = strlen((char *)retval.data);
161 static struct ldb_val lookup_uid(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
164 struct ldb_val retval;
166 pwd = getpwnam((char *)val->data);
169 return *talloc_zero(ctx, struct ldb_val);
172 retval.data = (uint8_t *)talloc_asprintf(ctx, "%ld", (unsigned long)pwd->pw_uid);
173 retval.length = strlen((char *)retval.data);
178 /* Encode a sambaSID to an objectSid. */
179 static struct ldb_val encode_sid(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
181 struct ldb_val out = data_blob(NULL, 0);
183 enum ndr_err_code ndr_err;
185 sid = dom_sid_parse_talloc(ctx, (char *)val->data);
190 ndr_err = ndr_push_struct_blob(&out, ctx,
192 sid, (ndr_push_flags_fn_t)ndr_push_dom_sid);
194 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
201 /* Decode an objectSid to a sambaSID. */
202 static struct ldb_val decode_sid(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
204 struct ldb_val out = data_blob(NULL, 0);
206 enum ndr_err_code ndr_err;
208 sid = talloc(ctx, struct dom_sid);
213 ndr_err = ndr_pull_struct_blob(val, sid, NULL, sid,
214 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
215 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
219 out.data = (uint8_t *)dom_sid_string(ctx, sid);
220 if (out.data == NULL) {
223 out.length = strlen((const char *)out.data);
230 /* Convert 16 bytes to 32 hex digits. */
231 static struct ldb_val bin2hex(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
234 struct samr_Password pwd;
235 if (val->length != sizeof(pwd.hash)) {
236 return data_blob(NULL, 0);
238 memcpy(pwd.hash, val->data, sizeof(pwd.hash));
239 out = data_blob_string_const(smbpasswd_sethexpwd(ctx, &pwd, 0));
241 return data_blob(NULL, 0);
246 /* Convert 32 hex digits to 16 bytes. */
247 static struct ldb_val hex2bin(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
250 struct samr_Password *pwd;
251 pwd = smbpasswd_gethexpwd(ctx, (const char *)val->data);
253 return data_blob(NULL, 0);
255 out = data_blob_talloc(ctx, pwd->hash, sizeof(pwd->hash));
259 const struct ldb_map_objectclass samba3_objectclasses[] = {
261 .local_name = "user",
262 .remote_name = "posixAccount",
263 .base_classes = { "top", NULL },
264 .musts = { "cn", "uid", "uidNumber", "gidNumber", "homeDirectory", NULL },
265 .mays = { "userPassword", "loginShell", "gecos", "description", NULL },
268 .local_name = "group",
269 .remote_name = "posixGroup",
270 .base_classes = { "top", NULL },
271 .musts = { "cn", "gidNumber", NULL },
272 .mays = { "userPassword", "memberUid", "description", NULL },
275 .local_name = "group",
276 .remote_name = "sambaGroupMapping",
277 .base_classes = { "top", "posixGroup", NULL },
278 .musts = { "gidNumber", "sambaSID", "sambaGroupType", NULL },
279 .mays = { "displayName", "description", "sambaSIDList", NULL },
282 .local_name = "user",
283 .remote_name = "sambaSAMAccount",
284 .base_classes = { "top", "posixAccount", NULL },
285 .musts = { "uid", "sambaSID", NULL },
286 .mays = { "cn", "sambaLMPassword", "sambaNTPassword",
287 "sambaPwdLastSet", "sambaLogonTime", "sambaLogoffTime",
288 "sambaKickoffTime", "sambaPwdCanChange", "sambaPwdMustChange",
289 "sambaAcctFlags", "displayName", "sambaHomePath", "sambaHomeDrive",
290 "sambaLogonScript", "sambaProfilePath", "description", "sambaUserWorkstations",
291 "sambaPrimaryGroupSID", "sambaDomainName", "sambaMungedDial",
292 "sambaBadPasswordCount", "sambaBadPasswordTime",
293 "sambaPasswordHistory", "sambaLogonHours", NULL }
297 .local_name = "domain",
298 .remote_name = "sambaDomain",
299 .base_classes = { "top", NULL },
300 .musts = { "sambaDomainName", "sambaSID", NULL },
301 .mays = { "sambaNextRid", "sambaNextGroupRid", "sambaNextUserRid", "sambaAlgorithmicRidBase", NULL },
306 const struct ldb_map_attribute samba3_attributes[] =
308 /* sambaNextRid -> nextRid */
310 .local_name = "nextRid",
314 .remote_name = "sambaNextRid",
319 /* sambaBadPasswordTime -> badPasswordtime*/
321 .local_name = "badPasswordTime",
325 .remote_name = "sambaBadPasswordTime",
330 /* sambaLMPassword -> lmPwdHash*/
332 .local_name = "dBCSPwd",
336 .remote_name = "sambaLMPassword",
337 .convert_local = bin2hex,
338 .convert_remote = hex2bin,
343 /* sambaGroupType -> groupType */
345 .local_name = "groupType",
349 .remote_name = "sambaGroupType",
354 /* sambaNTPassword -> ntPwdHash*/
356 .local_name = "ntpwdhash",
360 .remote_name = "sambaNTPassword",
361 .convert_local = bin2hex,
362 .convert_remote = hex2bin,
367 /* sambaPrimaryGroupSID -> primaryGroupID */
369 .local_name = "primaryGroupID",
370 .type = MAP_GENERATE,
373 .remote_names = { "sambaPrimaryGroupSID", NULL },
374 .generate_local = generate_primaryGroupID,
375 .generate_remote = generate_sambaPrimaryGroupSID,
380 /* sambaBadPasswordCount -> badPwdCount */
382 .local_name = "badPwdCount",
386 .remote_name = "sambaBadPasswordCount",
391 /* sambaLogonTime -> lastLogon*/
393 .local_name = "lastLogon",
397 .remote_name = "sambaLogonTime",
402 /* sambaLogoffTime -> lastLogoff*/
404 .local_name = "lastLogoff",
408 .remote_name = "sambaLogoffTime",
413 /* uid -> unixName */
415 .local_name = "unixName",
419 .remote_name = "uid",
424 /* displayName -> name */
426 .local_name = "name",
430 .remote_name = "displayName",
441 /* sAMAccountName -> cn */
443 .local_name = "sAMAccountName",
447 .remote_name = "uid",
448 .convert_remote = convert_uid_samaccount,
455 .local_name = "objectCategory",
461 .local_name = "objectGUID",
467 .local_name = "objectVersion",
473 .local_name = "codePage",
479 .local_name = "dNSHostName",
486 .local_name = "dnsDomain",
492 .local_name = "dnsRoot",
498 .local_name = "countryCode",
504 .local_name = "nTMixedDomain",
508 /* operatingSystem */
510 .local_name = "operatingSystem",
514 /* operatingSystemVersion */
516 .local_name = "operatingSystemVersion",
521 /* servicePrincipalName */
523 .local_name = "servicePrincipalName",
527 /* msDS-Behavior-Version */
529 .local_name = "msDS-Behavior-Version",
533 /* msDS-KeyVersionNumber */
535 .local_name = "msDS-KeyVersionNumber",
539 /* msDs-masteredBy */
541 .local_name = "msDs-masteredBy",
559 .local_name = "description",
563 /* sambaSID -> objectSid*/
565 .local_name = "objectSid",
569 .remote_name = "sambaSID",
570 .convert_local = decode_sid,
571 .convert_remote = encode_sid,
576 /* sambaPwdLastSet -> pwdLastSet */
578 .local_name = "pwdLastSet",
582 .remote_name = "sambaPwdLastSet",
589 .local_name = "accountExpires",
595 .local_name = "adminCount",
601 .local_name = "canonicalName",
605 /* createTimestamp */
607 .local_name = "createTimestamp",
613 .local_name = "creationTime",
619 .local_name = "dMDLocation",
625 .local_name = "fSMORoleOwner",
631 .local_name = "forceLogoff",
637 .local_name = "instanceType",
643 .local_name = "invocationId",
647 /* isCriticalSystemObject */
649 .local_name = "isCriticalSystemObject",
653 /* localPolicyFlags */
655 .local_name = "localPolicyFlags",
659 /* lockOutObservationWindow */
661 .local_name = "lockOutObservationWindow",
665 /* lockoutDuration */
667 .local_name = "lockoutDuration",
671 /* lockoutThreshold */
673 .local_name = "lockoutThreshold",
679 .local_name = "logonCount",
685 .local_name = "masteredBy",
691 .local_name = "maxPwdAge",
697 .local_name = "member",
703 .local_name = "memberOf",
709 .local_name = "minPwdAge",
715 .local_name = "minPwdLength",
721 .local_name = "modifiedCount",
725 /* modifiedCountAtLastProm */
727 .local_name = "modifiedCountAtLastProm",
731 /* modifyTimestamp */
733 .local_name = "modifyTimestamp",
739 .local_name = "nCName",
745 .local_name = "nETBIOSName",
751 .local_name = "oEMInformation",
757 .local_name = "privilege",
761 /* pwdHistoryLength */
763 .local_name = "pwdHistoryLength",
769 .local_name = "pwdProperties",
773 /* rIDAvailablePool */
775 .local_name = "rIDAvailablePool",
781 .local_name = "revision",
785 /* ridManagerReference */
787 .local_name = "ridManagerReference",
793 .local_name = "sAMAccountType",
799 .local_name = "sPNMappings",
803 /* serverReference */
805 .local_name = "serverReference",
811 .local_name = "serverState",
815 /* showInAdvancedViewOnly */
817 .local_name = "showInAdvancedViewOnly",
823 .local_name = "subRefs",
829 .local_name = "systemFlags",
835 .local_name = "uASCompat",
841 .local_name = "uSNChanged",
847 .local_name = "uSNCreated",
853 .local_name = "sambaPassword",
857 /* userAccountControl */
859 .local_name = "userAccountControl",
865 .local_name = "whenChanged",
871 .local_name = "whenCreated",
877 .local_name = "unixName",
881 .remote_name = "uidNumber",
882 .convert_local = lookup_uid,
887 /* gidNumber. Perhaps make into generate so we can distinguish between
888 * groups and accounts? */
890 .local_name = "unixName",
894 .remote_name = "gidNumber",
895 .convert_local = lookup_gid,
902 .local_name = "unixName",
906 .remote_name = "homeDirectory",
907 .convert_local = lookup_homedir,
916 /* the context init function */
917 static int samba3sam_init(struct ldb_module *module)
921 ret = ldb_map_init(module, samba3_attributes, samba3_objectclasses, NULL, NULL, "samba3sam");
922 if (ret != LDB_SUCCESS)
925 return ldb_next_init(module);
928 _PUBLIC_ const struct ldb_module_ops ldb_samba3sam_module_ops = {
931 .init_context = samba3sam_init,