fixed the ldb blackbox test to work with non-bourne shells (as needed
[Samba.git] / source3 / libads / ldap_user.c
blobbef2c912928d8c8dac73d23baa9fd4dac051902d
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 3 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, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
22 #ifdef HAVE_ADS
25 find a user account
27 ADS_STATUS ads_find_user_acct(ADS_STRUCT *ads, LDAPMessage **res,
28 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 char *name_escaped = NULL;
53 const char *objectClass[] = {"top", "person", "organizationalPerson",
54 "user", NULL};
56 if (fullname && *fullname) name = fullname;
57 else name = user;
59 if (!(ctx = talloc_init("ads_add_user_acct")))
60 return ADS_ERROR(LDAP_NO_MEMORY);
62 status = ADS_ERROR(LDAP_NO_MEMORY);
64 if (!(upn = talloc_asprintf(ctx, "%s@%s", user, ads->config.realm)))
65 goto done;
66 if (!(name_escaped = escape_rdn_val_string_alloc(name)))
67 goto done;
68 if (!(new_dn = talloc_asprintf(ctx, "cn=%s,%s,%s", name_escaped, container,
69 ads->config.bind_path)))
70 goto done;
71 if (!(controlstr = talloc_asprintf(ctx, "%u", (UF_NORMAL_ACCOUNT | UF_ACCOUNTDISABLE))))
72 goto done;
73 if (!(mods = ads_init_mods(ctx)))
74 goto done;
76 ads_mod_str(ctx, &mods, "cn", name);
77 ads_mod_strlist(ctx, &mods, "objectClass", objectClass);
78 ads_mod_str(ctx, &mods, "userPrincipalName", upn);
79 ads_mod_str(ctx, &mods, "name", name);
80 ads_mod_str(ctx, &mods, "displayName", name);
81 ads_mod_str(ctx, &mods, "sAMAccountName", user);
82 ads_mod_str(ctx, &mods, "userAccountControl", controlstr);
83 status = ads_gen_add(ads, new_dn, mods);
85 done:
86 SAFE_FREE(name_escaped);
87 talloc_destroy(ctx);
88 return status;
91 ADS_STATUS ads_add_group_acct(ADS_STRUCT *ads, const char *group,
92 const char *container, const char *comment)
94 TALLOC_CTX *ctx;
95 ADS_MODLIST mods;
96 ADS_STATUS status;
97 char *new_dn;
98 char *name_escaped = NULL;
99 const char *objectClass[] = {"top", "group", NULL};
101 if (!(ctx = talloc_init("ads_add_group_acct")))
102 return ADS_ERROR(LDAP_NO_MEMORY);
104 status = ADS_ERROR(LDAP_NO_MEMORY);
106 if (!(name_escaped = escape_rdn_val_string_alloc(group)))
107 goto done;
108 if (!(new_dn = talloc_asprintf(ctx, "cn=%s,%s,%s", name_escaped, container,
109 ads->config.bind_path)))
110 goto done;
111 if (!(mods = ads_init_mods(ctx)))
112 goto done;
114 ads_mod_str(ctx, &mods, "cn", group);
115 ads_mod_strlist(ctx, &mods, "objectClass",objectClass);
116 ads_mod_str(ctx, &mods, "name", group);
117 if (comment && *comment)
118 ads_mod_str(ctx, &mods, "description", comment);
119 ads_mod_str(ctx, &mods, "sAMAccountName", group);
120 status = ads_gen_add(ads, new_dn, mods);
122 done:
123 SAFE_FREE(name_escaped);
124 talloc_destroy(ctx);
125 return status;
127 #endif