s3-ipasam: implement {get,set,del}_trusted_domain
[garming/samba-autobuild/.git] / source3 / include / smbldap.h
1 /* 
2    Unix SMB/CIFS mplementation.
3    LDAP protocol helper functions for SAMBA
4    Copyright (C) Gerald Carter                  2001-2003
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 3 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, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #ifndef _SMBLDAP_H
22 #define _SMBLDAP_H
23
24 struct smbldap_state;
25
26 #ifdef HAVE_LDAP
27
28 /* specify schema versions between 2.2. and 3.0 */
29
30 #define SCHEMAVER_SAMBAACCOUNT          1
31 #define SCHEMAVER_SAMBASAMACCOUNT       2
32
33 /* objectclass names */
34
35 #define LDAP_OBJ_SAMBASAMACCOUNT        "sambaSamAccount"
36 #define LDAP_OBJ_SAMBAACCOUNT           "sambaAccount"
37 #define LDAP_OBJ_GROUPMAP               "sambaGroupMapping"
38 #define LDAP_OBJ_DOMINFO                "sambaDomain"
39 #define LDAP_OBJ_IDPOOL                 "sambaUnixIdPool"
40 #define LDAP_OBJ_IDMAP_ENTRY            "sambaIdmapEntry"
41 #define LDAP_OBJ_SID_ENTRY              "sambaSidEntry"
42 #define LDAP_OBJ_TRUST_PASSWORD         "sambaTrustPassword"
43 #define LDAP_OBJ_TRUSTDOM_PASSWORD      "sambaTrustedDomainPassword"
44 #define LDAP_OBJ_TRUSTED_DOMAIN         "sambaTrustedDomain"
45
46 #define LDAP_OBJ_ACCOUNT                "account"
47 #define LDAP_OBJ_POSIXACCOUNT           "posixAccount"
48 #define LDAP_OBJ_POSIXGROUP             "posixGroup"
49 #define LDAP_OBJ_OU                     "organizationalUnit"
50
51 /* some generic attributes that get reused a lot */
52
53 #define LDAP_ATTRIBUTE_SID              "sambaSID"
54 #define LDAP_ATTRIBUTE_UIDNUMBER        "uidNumber"
55 #define LDAP_ATTRIBUTE_GIDNUMBER        "gidNumber"
56 #define LDAP_ATTRIBUTE_SID_LIST         "sambaSIDList"
57
58 /* attribute map table indexes */
59
60 #define LDAP_ATTR_LIST_END              0
61 #define LDAP_ATTR_UID                   1
62 #define LDAP_ATTR_UIDNUMBER             2
63 #define LDAP_ATTR_GIDNUMBER             3
64 #define LDAP_ATTR_UNIX_HOME             4
65 #define LDAP_ATTR_PWD_LAST_SET          5
66 #define LDAP_ATTR_PWD_CAN_CHANGE        6
67 #define LDAP_ATTR_PWD_MUST_CHANGE       7
68 #define LDAP_ATTR_LOGON_TIME            8
69 #define LDAP_ATTR_LOGOFF_TIME           9
70 #define LDAP_ATTR_KICKOFF_TIME          10
71 #define LDAP_ATTR_CN                    11
72 #define LDAP_ATTR_DISPLAY_NAME          12
73 #define LDAP_ATTR_HOME_PATH             13
74 #define LDAP_ATTR_LOGON_SCRIPT          14
75 #define LDAP_ATTR_PROFILE_PATH          15
76 #define LDAP_ATTR_DESC                  16
77 #define LDAP_ATTR_USER_WKS              17
78 #define LDAP_ATTR_USER_SID              18
79 #define LDAP_ATTR_USER_RID              18
80 #define LDAP_ATTR_PRIMARY_GROUP_SID     19
81 #define LDAP_ATTR_PRIMARY_GROUP_RID     20
82 #define LDAP_ATTR_LMPW                  21
83 #define LDAP_ATTR_NTPW                  22
84 #define LDAP_ATTR_DOMAIN                23
85 #define LDAP_ATTR_OBJCLASS              24
86 #define LDAP_ATTR_ACB_INFO              25
87 #define LDAP_ATTR_NEXT_USERRID          26
88 #define LDAP_ATTR_NEXT_GROUPRID         27
89 #define LDAP_ATTR_DOM_SID               28
90 #define LDAP_ATTR_HOME_DRIVE            29
91 #define LDAP_ATTR_GROUP_SID             30
92 #define LDAP_ATTR_GROUP_TYPE            31
93 #define LDAP_ATTR_SID                   32
94 #define LDAP_ATTR_ALGORITHMIC_RID_BASE  33
95 #define LDAP_ATTR_NEXT_RID              34
96 #define LDAP_ATTR_BAD_PASSWORD_COUNT    35
97 #define LDAP_ATTR_LOGON_COUNT           36
98 #define LDAP_ATTR_MUNGED_DIAL           37
99 #define LDAP_ATTR_BAD_PASSWORD_TIME     38
100 #define LDAP_ATTR_PWD_HISTORY           39
101 #define LDAP_ATTR_SID_LIST              40
102 #define LDAP_ATTR_MOD_TIMESTAMP         41
103 #define LDAP_ATTR_LOGON_HOURS           42 
104 #define LDAP_ATTR_TRUST_PASSWD_FLAGS    43
105 #define LDAP_ATTR_SN                    44
106
107
108 typedef struct _attrib_map_entry {
109         int             attrib;
110         const char      *name;
111 } ATTRIB_MAP_ENTRY;
112
113
114 /* structures */
115
116 extern ATTRIB_MAP_ENTRY attrib_map_v22[];
117 extern ATTRIB_MAP_ENTRY attrib_map_to_delete_v22[];
118 extern ATTRIB_MAP_ENTRY attrib_map_v30[];
119 extern ATTRIB_MAP_ENTRY attrib_map_to_delete_v30[];
120 extern ATTRIB_MAP_ENTRY dominfo_attr_list[];
121 extern ATTRIB_MAP_ENTRY groupmap_attr_list[];
122 extern ATTRIB_MAP_ENTRY groupmap_attr_list_to_delete[];
123 extern ATTRIB_MAP_ENTRY idpool_attr_list[];
124 extern ATTRIB_MAP_ENTRY sidmap_attr_list[];
125 extern ATTRIB_MAP_ENTRY trustpw_attr_list[];
126
127
128 /* Function declarations -- not included in proto.h so we don't
129    have to worry about LDAP structure types */
130
131 NTSTATUS smbldap_init(TALLOC_CTX *mem_ctx,
132                       struct event_context *event_ctx,
133                       const char *location,
134                       struct smbldap_state **smbldap_state);
135
136 const char* get_attr_key2string( ATTRIB_MAP_ENTRY table[], int key );
137 const char** get_attr_list( TALLOC_CTX *mem_ctx, ATTRIB_MAP_ENTRY table[] );
138 void smbldap_set_mod (LDAPMod *** modlist, int modop, const char *attribute, const char *value);
139 void smbldap_make_mod(LDAP *ldap_struct, LDAPMessage *existing,
140                       LDAPMod ***mods,
141                       const char *attribute, const char *newval);
142 bool smbldap_get_single_attribute (LDAP * ldap_struct, LDAPMessage * entry,
143                                    const char *attribute, char *value,
144                                    int max_len);
145 int smbldap_modify(struct smbldap_state *ldap_state,
146                    const char *dn,
147                    LDAPMod *attrs[]);
148
149 /**
150  * Struct to keep the state for all the ldap stuff 
151  *
152  */
153
154 struct smbldap_state {
155         LDAP *ldap_struct;
156         pid_t pid;
157         time_t last_ping; /* monotonic */
158         /* retrive-once info */
159         const char *uri;
160
161         /* credentials */
162         bool anonymous;
163         char *bind_dn;
164         char *bind_secret;
165
166         bool paged_results;
167
168         unsigned int num_failures;
169
170         time_t last_use; /* monotonic */
171         struct event_context *event_context;
172         struct timed_event *idle_event;
173
174         struct timeval last_rebind; /* monotonic */
175 };
176
177 /* struct used by both pdb_ldap.c and pdb_nds.c */
178
179 struct ldapsam_privates {
180         struct smbldap_state *smbldap_state;
181
182         /* Former statics */
183         LDAPMessage *result;
184         LDAPMessage *entry;
185         int index;
186
187         const char *domain_name;
188         struct dom_sid domain_sid;
189
190         /* configuration items */
191         int schema_ver;
192
193         char *domain_dn;
194
195         /* Is this NDS ldap? */
196         int is_nds_ldap;
197
198         /* Is this IPA ldap? */
199         int is_ipa_ldap;
200
201         /* ldap server location parameter */
202         char *location;
203
204         struct {
205                 char *filter;
206                 LDAPMessage *result;
207         } search_cache;
208 };
209
210 /* Functions shared between pdb_ldap.c and pdb_nds.c. */
211 NTSTATUS pdb_init_ldapsam_compat( struct pdb_methods **pdb_method, const char *location);
212 void private_data_free_fn(void **result);
213 int ldapsam_search_suffix_by_name(struct ldapsam_privates *ldap_state,
214                                   const char *user,
215                                   LDAPMessage ** result,
216                                   const char **attr);
217 NTSTATUS pdb_init_ldapsam( struct pdb_methods **pdb_method, const char *location);
218 const char** get_userattr_list( TALLOC_CTX *mem_ctx, int schema_ver );
219
220 char * smbldap_talloc_single_attribute(LDAP *ldap_struct, LDAPMessage *entry,
221                                        const char *attribute,
222                                        TALLOC_CTX *mem_ctx);
223 char * smbldap_talloc_first_attribute(LDAP *ldap_struct, LDAPMessage *entry,
224                                       const char *attribute,
225                                       TALLOC_CTX *mem_ctx);
226 char * smbldap_talloc_smallest_attribute(LDAP *ldap_struct, LDAPMessage *entry,
227                                          const char *attribute,
228                                          TALLOC_CTX *mem_ctx);
229 bool smbldap_talloc_single_blob(TALLOC_CTX *mem_ctx, LDAP *ld,
230                                 LDAPMessage *msg, const char *attrib,
231                                 DATA_BLOB *blob);
232 bool smbldap_pull_sid(LDAP *ld, LDAPMessage *msg, const char *attrib,
233                       struct dom_sid *sid);
234 void talloc_autofree_ldapmsg(TALLOC_CTX *mem_ctx, LDAPMessage *result);
235 void talloc_autofree_ldapmod(TALLOC_CTX *mem_ctx, LDAPMod **mod);
236 char *smbldap_talloc_dn(TALLOC_CTX *mem_ctx, LDAP *ld,
237                               LDAPMessage *entry);
238
239
240 /* The following definitions come from lib/smbldap.c  */
241
242 int smb_ldap_start_tls(LDAP *ldap_struct, int version);
243 int smb_ldap_setup_full_conn(LDAP **ldap_struct, const char *uri);
244 int smbldap_search(struct smbldap_state *ldap_state,
245                    const char *base, int scope, const char *filter,
246                    const char *attrs[], int attrsonly,
247                    LDAPMessage **res);
248 int smbldap_search_paged(struct smbldap_state *ldap_state,
249                          const char *base, int scope, const char *filter,
250                          const char **attrs, int attrsonly, int pagesize,
251                          LDAPMessage **res, void **cookie);
252 int smbldap_modify(struct smbldap_state *ldap_state, const char *dn, LDAPMod *attrs[]);
253 int smbldap_add(struct smbldap_state *ldap_state, const char *dn, LDAPMod *attrs[]);
254 int smbldap_delete(struct smbldap_state *ldap_state, const char *dn);
255 int smbldap_extended_operation(struct smbldap_state *ldap_state,
256                                LDAP_CONST char *reqoid, struct berval *reqdata,
257                                LDAPControl **serverctrls, LDAPControl **clientctrls,
258                                char **retoidp, struct berval **retdatap);
259 int smbldap_search_suffix (struct smbldap_state *ldap_state,
260                            const char *filter, const char **search_attr,
261                            LDAPMessage ** result);
262 void smbldap_free_struct(struct smbldap_state **ldap_state) ;
263 NTSTATUS smbldap_init(TALLOC_CTX *mem_ctx, struct event_context *event_ctx,
264                       const char *location,
265                       struct smbldap_state **smbldap_state);
266 bool smbldap_has_control(LDAP *ld, const char *control);
267 bool smbldap_has_extension(LDAP *ld, const char *extension);
268 bool smbldap_has_naming_context(LDAP *ld, const char *naming_context);
269 bool smbldap_set_creds(struct smbldap_state *ldap_state, bool anon, const char *dn, const char *secret);
270
271 /* The following definitions come from lib/smbldap_util.c  */
272
273 NTSTATUS smbldap_search_domain_info(struct smbldap_state *ldap_state,
274                                     LDAPMessage ** result, const char *domain_name,
275                                     bool try_add);
276
277 #else
278 #define LDAP void
279 #define LDAPMessage void
280 #define LDAPMod void
281 #define LDAP_CONST const
282 #define LDAPControl void
283 struct berval;
284 struct ldapsam_privates;
285 #endif  /* HAVE_LDAP */
286
287 #define LDAP_DEFAULT_TIMEOUT   15
288 #define LDAP_CONNECTION_DEFAULT_TIMEOUT 2
289 #define LDAP_PAGE_SIZE 1024
290
291 #ifndef LDAP_OPT_SUCCESS
292 #define LDAP_OPT_SUCCESS 0
293 #endif
294
295 #define ADS_PAGE_CTL_OID        "1.2.840.113556.1.4.319"
296
297 #endif  /* _SMBLDAP_H */