vfs: Allocate SMB4ACL_T on an explict memory context
[Samba/vl.git] / source3 / libads / ldap_user.c
blob55421009a7b9ac348e25f9ac7a2d047c1a8cfcc9
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"
21 #include "ads.h"
22 #include "../libds/common/flags.h"
24 #ifdef HAVE_ADS
27 find a user account
29 ADS_STATUS ads_find_user_acct(ADS_STRUCT *ads, LDAPMessage **res,
30 const char *user)
32 ADS_STATUS status;
33 char *ldap_exp;
34 const char *attrs[] = {"*", NULL};
35 char *escaped_user = escape_ldap_string(talloc_tos(), user);
36 if (!escaped_user) {
37 return ADS_ERROR(LDAP_NO_MEMORY);
40 if (asprintf(&ldap_exp, "(samAccountName=%s)", escaped_user) == -1) {
41 TALLOC_FREE(escaped_user);
42 return ADS_ERROR(LDAP_NO_MEMORY);
44 status = ads_search(ads, res, ldap_exp, attrs);
45 SAFE_FREE(ldap_exp);
46 TALLOC_FREE(escaped_user);
47 return status;
50 ADS_STATUS ads_add_user_acct(ADS_STRUCT *ads, const char *user,
51 const char *container, const char *fullname)
53 TALLOC_CTX *ctx;
54 ADS_MODLIST mods;
55 ADS_STATUS status;
56 const char *upn, *new_dn, *name, *controlstr;
57 char *name_escaped = NULL;
58 const char *objectClass[] = {"top", "person", "organizationalPerson",
59 "user", NULL};
61 if (fullname && *fullname) name = fullname;
62 else name = user;
64 if (!(ctx = talloc_init("ads_add_user_acct")))
65 return ADS_ERROR(LDAP_NO_MEMORY);
67 status = ADS_ERROR(LDAP_NO_MEMORY);
69 if (!(upn = talloc_asprintf(ctx, "%s@%s", user, ads->config.realm)))
70 goto done;
71 if (!(name_escaped = escape_rdn_val_string_alloc(name)))
72 goto done;
73 if (!(new_dn = talloc_asprintf(ctx, "cn=%s,%s,%s", name_escaped, container,
74 ads->config.bind_path)))
75 goto done;
76 if (!(controlstr = talloc_asprintf(ctx, "%u", (UF_NORMAL_ACCOUNT | UF_ACCOUNTDISABLE))))
77 goto done;
78 if (!(mods = ads_init_mods(ctx)))
79 goto done;
81 ads_mod_str(ctx, &mods, "cn", name);
82 ads_mod_strlist(ctx, &mods, "objectClass", objectClass);
83 ads_mod_str(ctx, &mods, "userPrincipalName", upn);
84 ads_mod_str(ctx, &mods, "name", name);
85 ads_mod_str(ctx, &mods, "displayName", name);
86 ads_mod_str(ctx, &mods, "sAMAccountName", user);
87 ads_mod_str(ctx, &mods, "userAccountControl", controlstr);
88 status = ads_gen_add(ads, new_dn, mods);
90 done:
91 SAFE_FREE(name_escaped);
92 talloc_destroy(ctx);
93 return status;
96 ADS_STATUS ads_add_group_acct(ADS_STRUCT *ads, const char *group,
97 const char *container, const char *comment)
99 TALLOC_CTX *ctx;
100 ADS_MODLIST mods;
101 ADS_STATUS status;
102 char *new_dn;
103 char *name_escaped = NULL;
104 const char *objectClass[] = {"top", "group", NULL};
106 if (!(ctx = talloc_init("ads_add_group_acct")))
107 return ADS_ERROR(LDAP_NO_MEMORY);
109 status = ADS_ERROR(LDAP_NO_MEMORY);
111 if (!(name_escaped = escape_rdn_val_string_alloc(group)))
112 goto done;
113 if (!(new_dn = talloc_asprintf(ctx, "cn=%s,%s,%s", name_escaped, container,
114 ads->config.bind_path)))
115 goto done;
116 if (!(mods = ads_init_mods(ctx)))
117 goto done;
119 ads_mod_str(ctx, &mods, "cn", group);
120 ads_mod_strlist(ctx, &mods, "objectClass",objectClass);
121 ads_mod_str(ctx, &mods, "name", group);
122 if (comment && *comment)
123 ads_mod_str(ctx, &mods, "description", comment);
124 ads_mod_str(ctx, &mods, "sAMAccountName", group);
125 status = ads_gen_add(ads, new_dn, mods);
127 done:
128 SAFE_FREE(name_escaped);
129 talloc_destroy(ctx);
130 return status;
132 #endif