b6e3d189c514e0146182558d496d9e922f44b9d4
[tprouty/samba.git] / source3 / libads / ldap_user.c
1 /* 
2    Unix SMB/CIFS implementation.
3    ads (active directory) utility library
4    Copyright (C) Jim McDonough 2002
5    
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.
10    
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.
15    
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.
19 */
20
21 #include "includes.h"
22
23 #ifdef HAVE_ADS
24
25 /*
26   find a user account
27 */
28 ADS_STATUS ads_find_user_acct(ADS_STRUCT *ads, void **res, const char *user)
29 {
30         ADS_STATUS status;
31         char *exp;
32         const char *attrs[] = {"*", NULL};
33
34         asprintf(&exp, "(samAccountName=%s)", user);
35         status = ads_search(ads, res, exp, attrs);
36         free(exp);
37         return status;
38 }
39
40 ADS_STATUS ads_add_user_acct(ADS_STRUCT *ads, const char *user, 
41                              const char *fullname)
42 {
43         TALLOC_CTX *ctx;
44         ADS_MODLIST mods;
45         ADS_STATUS status;
46         const char *upn, *new_dn, *name, *controlstr;
47         const char *objectClass[] = {"top", "person", "organizationalPerson",
48                                      "user", NULL};
49
50         if (fullname && *fullname) name = fullname;
51         else name = user;
52
53         if (!(ctx = talloc_init_named("ads_add_user_acct")))
54                 return ADS_ERROR(LDAP_NO_MEMORY);
55
56         status = ADS_ERROR(LDAP_NO_MEMORY);
57
58         if (!(upn = talloc_asprintf(ctx, "%s@%s", user, ads->realm)))
59                 goto done;
60         if (!(new_dn = talloc_asprintf(ctx, "cn=%s,cn=Users,%s", name, 
61                                        ads->bind_path)))
62                 goto done;
63         if (!(controlstr = talloc_asprintf(ctx, "%u", UF_NORMAL_ACCOUNT)))
64                 goto done;
65         if (!(mods = ads_init_mods(ctx)))
66                 goto done;
67
68         ads_mod_str(ctx, &mods, "cn", name);
69         ads_mod_strlist(ctx, &mods, "objectClass", objectClass);
70         ads_mod_str(ctx, &mods, "userPrincipalName", upn);
71         ads_mod_str(ctx, &mods, "name", name);
72         ads_mod_str(ctx, &mods, "displayName", name);
73         ads_mod_str(ctx, &mods, "sAMAccountName", user);
74         ads_mod_str(ctx, &mods, "userAccountControl", controlstr);
75         status = ads_gen_add(ads, new_dn, mods);
76
77  done:
78         talloc_destroy(ctx);
79         return status;
80 }
81
82 ADS_STATUS ads_add_group_acct(ADS_STRUCT *ads, const char *group, 
83                               const char *comment)
84 {
85         TALLOC_CTX *ctx;
86         ADS_MODLIST mods;
87         ADS_STATUS status;
88         char *new_dn;
89         const char *objectClass[] = {"top", "group", NULL};
90
91         if (!(ctx = talloc_init_named("ads_add_group_acct")))
92                 return ADS_ERROR(LDAP_NO_MEMORY);
93
94         status = ADS_ERROR(LDAP_NO_MEMORY);
95
96         if (!(new_dn = talloc_asprintf(ctx, "cn=%s,cn=Users,%s", group, 
97                                        ads->bind_path)))
98                 goto done;
99         if (!(mods = ads_init_mods(ctx)))
100                 goto done;
101
102         ads_mod_str(ctx, &mods, "cn", group);
103         ads_mod_strlist(ctx, &mods, "objectClass",objectClass);
104         ads_mod_str(ctx, &mods, "name", group);
105         if (comment)
106                 ads_mod_str(ctx, &mods, "description", comment);
107         ads_mod_str(ctx, &mods, "sAMAccountName", group);
108         status = ads_gen_add(ads, new_dn, mods);
109
110  done:
111         talloc_destroy(ctx);
112         return status;
113 }
114 #endif