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/>.
22 #include "../libds/common/flags.h"
29 ADS_STATUS
ads_find_user_acct(ADS_STRUCT
*ads
, LDAPMessage
**res
,
34 const char *attrs
[] = {"*", NULL
};
35 char *escaped_user
= escape_ldap_string(talloc_tos(), 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
);
46 TALLOC_FREE(escaped_user
);
50 ADS_STATUS
ads_add_user_acct(ADS_STRUCT
*ads
, const char *user
,
51 const char *container
, const char *fullname
)
56 const char *upn
, *new_dn
, *name
, *controlstr
;
57 char *name_escaped
= NULL
;
58 const char *objectClass
[] = {"top", "person", "organizationalPerson",
61 if (fullname
&& *fullname
) name
= fullname
;
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
)))
71 if (!(name_escaped
= escape_rdn_val_string_alloc(name
)))
73 if (!(new_dn
= talloc_asprintf(ctx
, "cn=%s,%s,%s", name_escaped
, container
,
74 ads
->config
.bind_path
)))
76 if (!(controlstr
= talloc_asprintf(ctx
, "%u", (UF_NORMAL_ACCOUNT
| UF_ACCOUNTDISABLE
))))
78 if (!(mods
= ads_init_mods(ctx
)))
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
);
91 SAFE_FREE(name_escaped
);
96 ADS_STATUS
ads_add_group_acct(ADS_STRUCT
*ads
, const char *group
,
97 const char *container
, const char *comment
)
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
)))
113 if (!(new_dn
= talloc_asprintf(ctx
, "cn=%s,%s,%s", name_escaped
, container
,
114 ads
->config
.bind_path
)))
116 if (!(mods
= ads_init_mods(ctx
)))
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
);
128 SAFE_FREE(name_escaped
);