Move sys_readlink() to libreplace.
[ira/wip.git] / source3 / include / ads.h
1 #ifndef _INCLUDE_ADS_H_
2 #define _INCLUDE_ADS_H_
3 /*
4   header for ads (active directory) library routines
5
6   basically this is a wrapper around ldap
7 */
8
9 enum wb_posix_mapping {
10         WB_POSIX_MAP_UNKNOWN    = -1,
11         WB_POSIX_MAP_TEMPLATE   = 0, 
12         WB_POSIX_MAP_SFU        = 1, 
13         WB_POSIX_MAP_SFU20      = 2, 
14         WB_POSIX_MAP_RFC2307    = 3,
15         WB_POSIX_MAP_UNIXINFO   = 4
16 };
17
18 /* there are 5 possible types of errors the ads subsystem can produce */
19 enum ads_error_type {ENUM_ADS_ERROR_KRB5, ENUM_ADS_ERROR_GSS, 
20                      ENUM_ADS_ERROR_LDAP, ENUM_ADS_ERROR_SYSTEM, ENUM_ADS_ERROR_NT};
21
22 typedef struct {
23         enum ads_error_type error_type;
24         union err_state{                
25                 int rc;
26                 NTSTATUS nt_status;
27         } err;
28         /* For error_type = ENUM_ADS_ERROR_GSS minor_status describe GSS API error */
29         /* Where rc represents major_status of GSS API error */
30         int minor_status;
31 } ADS_STATUS;
32
33 struct ads_struct;
34
35 struct ads_saslwrap_ops {
36         const char *name;
37         ADS_STATUS (*wrap)(struct ads_struct *, uint8 *buf, uint32 len);
38         ADS_STATUS (*unwrap)(struct ads_struct *);
39         void (*disconnect)(struct ads_struct *);
40 };
41
42 enum ads_saslwrap_type {
43         ADS_SASLWRAP_TYPE_PLAIN = 1,
44         ADS_SASLWRAP_TYPE_SIGN = 2,
45         ADS_SASLWRAP_TYPE_SEAL = 4
46 };
47
48 typedef struct ads_struct {
49         int is_mine;    /* do I own this structure's memory? */
50         
51         /* info needed to find the server */
52         struct {
53                 char *realm;
54                 char *workgroup;
55                 char *ldap_server;
56                 int foreign; /* set to 1 if connecting to a foreign
57                               * realm */
58                 bool gc;     /* Is this a global catalog server? */
59         } server;
60
61         /* info needed to authenticate */
62         struct {
63                 char *realm;
64                 char *password;
65                 char *user_name;
66                 char *kdc_server;
67                 unsigned flags;
68                 int time_offset;
69                 time_t tgt_expire;
70                 time_t tgs_expire;
71                 time_t renewable;
72         } auth;
73
74         /* info derived from the servers config */
75         struct {
76                 uint32 flags; /* cldap flags identifying the services. */
77                 char *realm;
78                 char *bind_path;
79                 char *ldap_server_name;
80                 char *server_site_name;
81                 char *client_site_name;
82                 time_t current_time;
83                 int tried_closest_dc;
84                 char *schema_path;
85                 char *config_path;
86         } config;
87
88         /* info about the current LDAP connection */
89 #ifdef HAVE_LDAP
90         struct {
91                 LDAP *ld;
92                 struct sockaddr_storage ss; /* the ip of the active connection, if any */
93                 time_t last_attempt; /* last attempt to reconnect */
94                 int port;
95
96                 enum ads_saslwrap_type wrap_type;
97
98 #ifdef HAVE_LDAP_SASL_WRAPPING
99                 Sockbuf_IO_Desc *sbiod; /* lowlevel state for LDAP wrapping */
100 #endif /* HAVE_LDAP_SASL_WRAPPING */
101                 TALLOC_CTX *mem_ctx;
102                 const struct ads_saslwrap_ops *wrap_ops;
103                 void *wrap_private_data;
104                 struct {
105                         uint32 ofs;
106                         uint32 needed;
107                         uint32 left;
108 #define        ADS_SASL_WRAPPING_IN_MAX_WRAPPED        0x0FFFFFFF
109                         uint32 max_wrapped;
110                         uint32 min_wrapped;
111                         uint32 size;
112                         uint8 *buf;
113                 } in;
114                 struct {
115                         uint32 ofs;
116                         uint32 left;
117 #define        ADS_SASL_WRAPPING_OUT_MAX_WRAPPED       0x00A00000
118                         uint32 max_unwrapped;
119                         uint32 sig_size;
120                         uint32 size;
121                         uint8 *buf;
122                 } out;
123         } ldap;
124 #endif /* HAVE_LDAP */
125 } ADS_STRUCT;
126
127 /* used to remember the names of the posix attributes in AD */
128 /* see the rfc2307 & sfu nss backends */
129
130 struct posix_schema {
131         char *posix_homedir_attr;
132         char *posix_shell_attr;
133         char *posix_uidnumber_attr;
134         char *posix_gidnumber_attr;
135         char *posix_gecos_attr;
136         char *posix_uid_attr;
137 };
138
139
140
141 #ifdef HAVE_ADS
142 typedef LDAPMod **ADS_MODLIST;
143 #else
144 typedef void **ADS_MODLIST;
145 #endif
146
147 /* macros to simplify error returning */
148 #define ADS_ERROR(rc) ADS_ERROR_LDAP(rc)
149 #define ADS_ERROR_LDAP(rc) ads_build_error(ENUM_ADS_ERROR_LDAP, rc, 0)
150 #define ADS_ERROR_SYSTEM(rc) ads_build_error(ENUM_ADS_ERROR_SYSTEM, rc?rc:EINVAL, 0)
151 #define ADS_ERROR_KRB5(rc) ads_build_error(ENUM_ADS_ERROR_KRB5, rc, 0)
152 #define ADS_ERROR_GSS(rc, minor) ads_build_error(ENUM_ADS_ERROR_GSS, rc, minor)
153 #define ADS_ERROR_NT(rc) ads_build_nt_error(ENUM_ADS_ERROR_NT,rc)
154
155 #define ADS_ERR_OK(status) ((status.error_type == ENUM_ADS_ERROR_NT) ? NT_STATUS_IS_OK(status.err.nt_status):(status.err.rc == 0))
156 #define ADS_SUCCESS ADS_ERROR(0)
157
158 #define ADS_ERROR_HAVE_NO_MEMORY(x) do { \
159         if (!(x)) {\
160                 return ADS_ERROR(LDAP_NO_MEMORY);\
161         }\
162 } while (0)
163
164
165 /* time between reconnect attempts */
166 #define ADS_RECONNECT_TIME 5
167
168 /* ldap control oids */
169 #define ADS_PAGE_CTL_OID        "1.2.840.113556.1.4.319"
170 #define ADS_NO_REFERRALS_OID    "1.2.840.113556.1.4.1339"
171 #define ADS_SERVER_SORT_OID     "1.2.840.113556.1.4.473"
172 #define ADS_PERMIT_MODIFY_OID   "1.2.840.113556.1.4.1413"
173 #define ADS_ASQ_OID             "1.2.840.113556.1.4.1504"
174 #define ADS_EXTENDED_DN_OID     "1.2.840.113556.1.4.529"
175 #define ADS_SD_FLAGS_OID        "1.2.840.113556.1.4.801"
176
177 /* ldap attribute oids (Services for Unix 3.0, 3.5) */
178 #define ADS_ATTR_SFU_UIDNUMBER_OID      "1.2.840.113556.1.6.18.1.310"
179 #define ADS_ATTR_SFU_GIDNUMBER_OID      "1.2.840.113556.1.6.18.1.311"
180 #define ADS_ATTR_SFU_HOMEDIR_OID        "1.2.840.113556.1.6.18.1.344"
181 #define ADS_ATTR_SFU_SHELL_OID          "1.2.840.113556.1.6.18.1.312"
182 #define ADS_ATTR_SFU_GECOS_OID          "1.2.840.113556.1.6.18.1.337"
183 #define ADS_ATTR_SFU_UID_OID            "1.2.840.113556.1.6.18.1.309"
184
185 /* ldap attribute oids (Services for Unix 2.0) */
186 #define ADS_ATTR_SFU20_UIDNUMBER_OID    "1.2.840.113556.1.4.7000.187.70"
187 #define ADS_ATTR_SFU20_GIDNUMBER_OID    "1.2.840.113556.1.4.7000.187.71"
188 #define ADS_ATTR_SFU20_HOMEDIR_OID      "1.2.840.113556.1.4.7000.187.106"
189 #define ADS_ATTR_SFU20_SHELL_OID        "1.2.840.113556.1.4.7000.187.72"
190 #define ADS_ATTR_SFU20_GECOS_OID        "1.2.840.113556.1.4.7000.187.97"
191 #define ADS_ATTR_SFU20_UID_OID          "1.2.840.113556.1.4.7000.187.102"
192
193
194 /* ldap attribute oids (RFC2307) */
195 #define ADS_ATTR_RFC2307_UIDNUMBER_OID  "1.3.6.1.1.1.1.0"
196 #define ADS_ATTR_RFC2307_GIDNUMBER_OID  "1.3.6.1.1.1.1.1"
197 #define ADS_ATTR_RFC2307_HOMEDIR_OID    "1.3.6.1.1.1.1.3"
198 #define ADS_ATTR_RFC2307_SHELL_OID      "1.3.6.1.1.1.1.4"
199 #define ADS_ATTR_RFC2307_GECOS_OID      "1.3.6.1.1.1.1.2"
200 #define ADS_ATTR_RFC2307_UID_OID        "0.9.2342.19200300.100.1.1"
201
202 /* ldap bitwise searches */
203 #define ADS_LDAP_MATCHING_RULE_BIT_AND  "1.2.840.113556.1.4.803"
204 #define ADS_LDAP_MATCHING_RULE_BIT_OR   "1.2.840.113556.1.4.804"
205
206 /* UserFlags for userAccountControl */
207 #define UF_SCRIPT                               0x00000001
208 #define UF_ACCOUNTDISABLE                       0x00000002
209 #define UF_UNUSED_1                             0x00000004
210 #define UF_HOMEDIR_REQUIRED                     0x00000008
211
212 #define UF_LOCKOUT                              0x00000010
213 #define UF_PASSWD_NOTREQD                       0x00000020
214 #define UF_PASSWD_CANT_CHANGE                   0x00000040
215 #define UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED      0x00000080
216
217 #define UF_TEMP_DUPLICATE_ACCOUNT               0x00000100
218 #define UF_NORMAL_ACCOUNT                       0x00000200
219 #define UF_UNUSED_2                             0x00000400
220 #define UF_INTERDOMAIN_TRUST_ACCOUNT            0x00000800
221
222 #define UF_WORKSTATION_TRUST_ACCOUNT            0x00001000
223 #define UF_SERVER_TRUST_ACCOUNT                 0x00002000
224 #define UF_UNUSED_3                             0x00004000
225 #define UF_UNUSED_4                             0x00008000
226
227 #define UF_DONT_EXPIRE_PASSWD                   0x00010000
228 #define UF_MNS_LOGON_ACCOUNT                    0x00020000
229 #define UF_SMARTCARD_REQUIRED                   0x00040000
230 #define UF_TRUSTED_FOR_DELEGATION               0x00080000
231
232 #define UF_NOT_DELEGATED                        0x00100000
233 #define UF_USE_DES_KEY_ONLY                     0x00200000
234 #define UF_DONT_REQUIRE_PREAUTH                 0x00400000
235 #define UF_PASSWORD_EXPIRED                     0x00800000
236
237 #define UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION 0x01000000
238 #define UF_NO_AUTH_DATA_REQUIRED                0x02000000
239 #define UF_UNUSED_8                             0x04000000
240 #define UF_UNUSED_9                             0x08000000
241
242 #define UF_UNUSED_10                            0x10000000
243 #define UF_UNUSED_11                            0x20000000
244 #define UF_UNUSED_12                            0x40000000
245 #define UF_UNUSED_13                            0x80000000
246
247 #define UF_MACHINE_ACCOUNT_MASK (\
248                 UF_INTERDOMAIN_TRUST_ACCOUNT |\
249                 UF_WORKSTATION_TRUST_ACCOUNT |\
250                 UF_SERVER_TRUST_ACCOUNT \
251                 )
252
253 #define UF_ACCOUNT_TYPE_MASK (\
254                 UF_TEMP_DUPLICATE_ACCOUNT |\
255                 UF_NORMAL_ACCOUNT |\
256                 UF_INTERDOMAIN_TRUST_ACCOUNT |\
257                 UF_WORKSTATION_TRUST_ACCOUNT |\
258                 UF_SERVER_TRUST_ACCOUNT \
259                 )
260
261 #define UF_SETTABLE_BITS (\
262                 UF_SCRIPT |\
263                 UF_ACCOUNTDISABLE |\
264                 UF_HOMEDIR_REQUIRED  |\
265                 UF_LOCKOUT |\
266                 UF_PASSWD_NOTREQD |\
267                 UF_PASSWD_CANT_CHANGE |\
268                 UF_ACCOUNT_TYPE_MASK | \
269                 UF_DONT_EXPIRE_PASSWD | \
270                 UF_MNS_LOGON_ACCOUNT |\
271                 UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED |\
272                 UF_SMARTCARD_REQUIRED |\
273                 UF_TRUSTED_FOR_DELEGATION |\
274                 UF_NOT_DELEGATED |\
275                 UF_USE_DES_KEY_ONLY  |\
276                 UF_DONT_REQUIRE_PREAUTH \
277                 )
278
279 /* sAMAccountType */
280 #define ATYPE_NORMAL_ACCOUNT                    0x30000000 /* 805306368 */
281 #define ATYPE_WORKSTATION_TRUST                 0x30000001 /* 805306369 */
282 #define ATYPE_INTERDOMAIN_TRUST                 0x30000002 /* 805306370 */ 
283 #define ATYPE_SECURITY_GLOBAL_GROUP             0x10000000 /* 268435456 */
284 #define ATYPE_DISTRIBUTION_GLOBAL_GROUP         0x10000001 /* 268435457 */
285 #define ATYPE_DISTRIBUTION_UNIVERSAL_GROUP      ATYPE_DISTRIBUTION_GLOBAL_GROUP
286 #define ATYPE_SECURITY_LOCAL_GROUP              0x20000000 /* 536870912 */
287 #define ATYPE_DISTRIBUTION_LOCAL_GROUP          0x20000001 /* 536870913 */
288
289 #define ATYPE_ACCOUNT           ATYPE_NORMAL_ACCOUNT            /* 0x30000000 805306368 */
290 #define ATYPE_GLOBAL_GROUP      ATYPE_SECURITY_GLOBAL_GROUP     /* 0x10000000 268435456 */
291 #define ATYPE_LOCAL_GROUP       ATYPE_SECURITY_LOCAL_GROUP      /* 0x20000000 536870912 */
292
293 /* groupType */
294 #define GROUP_TYPE_BUILTIN_LOCAL_GROUP          0x00000001
295 #define GROUP_TYPE_ACCOUNT_GROUP                0x00000002
296 #define GROUP_TYPE_RESOURCE_GROUP               0x00000004
297 #define GROUP_TYPE_UNIVERSAL_GROUP              0x00000008
298 #define GROUP_TYPE_APP_BASIC_GROUP              0x00000010
299 #define GROUP_TYPE_APP_QUERY_GROUP              0x00000020
300 #define GROUP_TYPE_SECURITY_ENABLED             0x80000000
301
302 #define GTYPE_SECURITY_BUILTIN_LOCAL_GROUP (    /* 0x80000005 -2147483643 */ \
303                 GROUP_TYPE_BUILTIN_LOCAL_GROUP| \
304                 GROUP_TYPE_RESOURCE_GROUP| \
305                 GROUP_TYPE_SECURITY_ENABLED \
306                 )
307 #define GTYPE_SECURITY_DOMAIN_LOCAL_GROUP (     /* 0x80000004 -2147483644 */ \
308                 GROUP_TYPE_RESOURCE_GROUP| \
309                 GROUP_TYPE_SECURITY_ENABLED \
310                 )
311 #define GTYPE_SECURITY_GLOBAL_GROUP (           /* 0x80000002 -2147483646 */ \
312                 GROUP_TYPE_ACCOUNT_GROUP| \
313                 GROUP_TYPE_SECURITY_ENABLED \
314                 )
315 #define GTYPE_SECURITY_UNIVERSAL_GROUP (        /* 0x80000008 -2147483656 */ \
316                 GROUP_TYPE_UNIVERSAL_GROUP| \
317                 GROUP_TYPE_SECURITY_ENABLED \
318                 )
319
320 #define GTYPE_DISTRIBUTION_GLOBAL_GROUP         0x00000002      /* 2 */
321 #define GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP   0x00000004      /* 4 */
322 #define GTYPE_DISTRIBUTION_UNIVERSAL_GROUP      0x00000008      /* 8 */
323
324 #define ADS_PINGS          0x0000FFFF  /* Ping response */
325 #define ADS_DNS_CONTROLLER 0x20000000  /* DomainControllerName is a DNS name*/
326 #define ADS_DNS_DOMAIN     0x40000000  /* DomainName is a DNS name */
327 #define ADS_DNS_FOREST     0x80000000  /* DnsForestName is a DNS name */
328
329 /* ads auth control flags */
330 #define ADS_AUTH_DISABLE_KERBEROS 0x0001
331 #define ADS_AUTH_NO_BIND          0x0002
332 #define ADS_AUTH_ANON_BIND        0x0004
333 #define ADS_AUTH_SIMPLE_BIND      0x0008
334 #define ADS_AUTH_ALLOW_NTLMSSP    0x0010
335 #define ADS_AUTH_SASL_SIGN        0x0020
336 #define ADS_AUTH_SASL_SEAL        0x0040
337 #define ADS_AUTH_SASL_FORCE       0x0080
338 #define ADS_AUTH_USER_CREDS       0x0100
339
340 /* Kerberos environment variable names */
341 #define KRB5_ENV_CCNAME "KRB5CCNAME"
342
343 /* Heimdal uses a slightly different name */
344 #if defined(HAVE_ENCTYPE_ARCFOUR_HMAC_MD5)
345 #define ENCTYPE_ARCFOUR_HMAC ENCTYPE_ARCFOUR_HMAC_MD5
346 #endif
347
348 /* The older versions of heimdal that don't have this
349    define don't seem to use it anyway.  I'm told they
350    always use a subkey */
351 #ifndef HAVE_AP_OPTS_USE_SUBKEY
352 #define AP_OPTS_USE_SUBKEY 0
353 #endif
354
355 #define WELL_KNOWN_GUID_COMPUTERS       "AA312825768811D1ADED00C04FD8D5CD" 
356 #define WELL_KNOWN_GUID_USERS           "A9D1CA15768811D1ADED00C04FD8D5CD"
357
358 #ifndef KRB5_ADDR_NETBIOS
359 #define KRB5_ADDR_NETBIOS 0x14
360 #endif
361
362 #ifndef KRB5KRB_ERR_RESPONSE_TOO_BIG
363 #define KRB5KRB_ERR_RESPONSE_TOO_BIG (-1765328332L)
364 #endif
365
366 #ifdef HAVE_KRB5
367 typedef struct {
368         NTSTATUS ntstatus;
369         uint32 unknown1;
370         uint32 unknown2; /* 0x00000001 */
371 } KRB5_EDATA_NTSTATUS;
372
373 typedef struct {
374 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
375         krb5_address **addrs;
376 #elif defined(HAVE_KRB5_ADDRESSES) /* Heimdal */
377         krb5_addresses *addrs;
378 #else
379 #error UNKNOWN_KRB5_ADDRESS_TYPE
380 #endif /* defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) */
381 } smb_krb5_addresses;
382
383 #ifdef HAVE_KRB5_KEYBLOCK_KEYVALUE /* Heimdal */
384 #define KRB5_KEY_TYPE(k)        ((k)->keytype)
385 #define KRB5_KEY_LENGTH(k)      ((k)->keyvalue.length)
386 #define KRB5_KEY_DATA(k)        ((k)->keyvalue.data)
387 #define KRB5_KEY_DATA_CAST      void
388 #else /* MIT */
389 #define KRB5_KEY_TYPE(k)        ((k)->enctype)
390 #define KRB5_KEY_LENGTH(k)      ((k)->length)
391 #define KRB5_KEY_DATA(k)        ((k)->contents)
392 #define KRB5_KEY_DATA_CAST      krb5_octet
393 #endif /* HAVE_KRB5_KEYBLOCK_KEYVALUE */
394
395 #ifdef HAVE_KRB5_KEYTAB_ENTRY_KEY               /* MIT */
396 #define KRB5_KT_KEY(k)          (&(k)->key)
397 #elif HAVE_KRB5_KEYTAB_ENTRY_KEYBLOCK          /* Heimdal */
398 #define KRB5_KT_KEY(k)          (&(k)->keyblock)
399 #else
400 #error krb5_keytab_entry has no key or keyblock member
401 #endif /* HAVE_KRB5_KEYTAB_ENTRY_KEY */
402
403 #endif /* HAVE_KRB5 */
404
405 enum ads_extended_dn_flags {
406         ADS_EXTENDED_DN_HEX_STRING      = 0,
407         ADS_EXTENDED_DN_STRING          = 1 /* not supported on win2k */
408 };
409
410 /* this is probably not very well suited to pass other controls generically but
411  * is good enough for the extended dn control where it is only used for atm */
412
413 typedef struct {
414         const char *control;
415         int val;
416         int critical;
417 } ads_control;
418
419 #define ADS_EXTENDED_RIGHT_APPLY_GROUP_POLICY "edacfd8f-ffb3-11d1-b41d-00a0c968f939"
420
421 #define ADS_IGNORE_PRINCIPAL "not_defined_in_RFC4178@please_ignore"
422
423 /* Settings for the domainFunctionality attribute in the rootDSE */
424
425 #define DS_DOMAIN_FUNCTION_2000         0
426 #define DS_DOMAIN_FUCNTION_2003_MIXED   1
427 #define DS_DOMAIN_FUNCTION_2003         2
428 #define DS_DOMAIN_FUNCTION_2008         3
429
430 #endif  /* _INCLUDE_ADS_H_ */