Apply some more of Derrell Lipman's changes.
[Samba/gebeck_regimport.git] / source3 / libads / ldap_user.c
blob56a0d8013b2b8ee6ddc32a0d37cc50dcf6907a8c
1 /*
2 Unix SMB/CIFS implementation.
3 ads (active directory) utility library
4 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2002
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
23 #ifdef HAVE_ADS
26 find a user account
28 ADS_STATUS ads_find_user_acct(ADS_STRUCT *ads, void **res, const char *user)
30 ADS_STATUS status;
31 char *ldap_exp;
32 const char *attrs[] = {"*", NULL};
33 char *escaped_user = escape_ldap_string_alloc(user);
34 if (!escaped_user) {
35 return ADS_ERROR(LDAP_NO_MEMORY);
38 asprintf(&ldap_exp, "(samAccountName=%s)", escaped_user);
39 status = ads_search(ads, res, ldap_exp, attrs);
40 SAFE_FREE(ldap_exp);
41 SAFE_FREE(escaped_user);
42 return status;
45 ADS_STATUS ads_add_user_acct(ADS_STRUCT *ads, const char *user,
46 const char *container, const char *fullname)
48 TALLOC_CTX *ctx;
49 ADS_MODLIST mods;
50 ADS_STATUS status;
51 const char *upn, *new_dn, *name, *controlstr;
52 const char *objectClass[] = {"top", "person", "organizationalPerson",
53 "user", NULL};
55 if (fullname && *fullname) name = fullname;
56 else name = user;
58 if (!(ctx = talloc_init("ads_add_user_acct")))
59 return ADS_ERROR(LDAP_NO_MEMORY);
61 status = ADS_ERROR(LDAP_NO_MEMORY);
63 if (!(upn = talloc_asprintf(ctx, "%s@%s", user, ads->config.realm)))
64 goto done;
65 if (!(new_dn = talloc_asprintf(ctx, "cn=%s,%s,%s", name, container,
66 ads->config.bind_path)))
67 goto done;
68 if (!(controlstr = talloc_asprintf(ctx, "%u", UF_NORMAL_ACCOUNT)))
69 goto done;
70 if (!(mods = ads_init_mods(ctx)))
71 goto done;
73 ads_mod_str(ctx, &mods, "cn", name);
74 ads_mod_strlist(ctx, &mods, "objectClass", objectClass);
75 ads_mod_str(ctx, &mods, "userPrincipalName", upn);
76 ads_mod_str(ctx, &mods, "name", name);
77 ads_mod_str(ctx, &mods, "displayName", name);
78 ads_mod_str(ctx, &mods, "sAMAccountName", user);
79 ads_mod_str(ctx, &mods, "userAccountControl", controlstr);
80 status = ads_gen_add(ads, new_dn, mods);
82 done:
83 talloc_destroy(ctx);
84 return status;
87 ADS_STATUS ads_add_group_acct(ADS_STRUCT *ads, const char *group,
88 const char *container, const char *comment)
90 TALLOC_CTX *ctx;
91 ADS_MODLIST mods;
92 ADS_STATUS status;
93 char *new_dn;
94 const char *objectClass[] = {"top", "group", NULL};
96 if (!(ctx = talloc_init("ads_add_group_acct")))
97 return ADS_ERROR(LDAP_NO_MEMORY);
99 status = ADS_ERROR(LDAP_NO_MEMORY);
101 if (!(new_dn = talloc_asprintf(ctx, "cn=%s,%s,%s", group, container,
102 ads->config.bind_path)))
103 goto done;
104 if (!(mods = ads_init_mods(ctx)))
105 goto done;
107 ads_mod_str(ctx, &mods, "cn", group);
108 ads_mod_strlist(ctx, &mods, "objectClass",objectClass);
109 ads_mod_str(ctx, &mods, "name", group);
110 if (comment && *comment)
111 ads_mod_str(ctx, &mods, "description", comment);
112 ads_mod_str(ctx, &mods, "sAMAccountName", group);
113 status = ads_gen_add(ads, new_dn, mods);
115 done:
116 talloc_destroy(ctx);
117 return status;
119 #endif