29df0d2f3532abb5c3597053ca937015ed7b67b9
[nivanova/samba-autobuild/.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_RFC2307    = 2,
14         WB_POSIX_MAP_UNIXINFO   = 3
15 };
16
17 typedef struct {
18 #ifdef HAVE_LDAP
19         LDAP *ld;
20 #else
21         void *ld; /* the active ldap structure */
22 #endif
23         struct in_addr ldap_ip; /* the ip of the active connection, if any */
24         time_t last_attempt; /* last attempt to reconnect */
25         int ldap_port;
26         
27         int is_mine;    /* do I own this structure's memory? */
28         
29         /* info needed to find the server */
30         struct {
31                 char *realm;
32                 char *workgroup;
33                 char *ldap_server;
34                 int foreign; /* set to 1 if connecting to a foreign realm */
35         } server;
36
37         /* info needed to authenticate */
38         struct {
39                 char *realm;
40                 char *password;
41                 char *user_name;
42                 char *kdc_server;
43                 unsigned flags;
44                 int time_offset;
45                 time_t tgt_expire;
46                 time_t tgs_expire;
47                 time_t renewable;
48         } auth;
49
50         /* info derived from the servers config */
51         struct {
52                 uint32 flags; /* cldap flags identifying the services. */
53                 char *realm;
54                 char *bind_path;
55                 char *ldap_server_name;
56                 char *server_site_name;
57                 char *client_site_name;
58                 time_t current_time;
59                 int tried_closest_dc;
60         } config;
61 } ADS_STRUCT;
62
63 /* used to remember the names of the posix attributes in AD */
64 /* see the rfc2307 & sfu nss backends */
65
66 struct posix_schema {
67         char *posix_homedir_attr;
68         char *posix_shell_attr;
69         char *posix_uidnumber_attr;
70         char *posix_gidnumber_attr;
71         char *posix_gecos_attr;
72 };
73
74
75 /* there are 5 possible types of errors the ads subsystem can produce */
76 enum ads_error_type {ENUM_ADS_ERROR_KRB5, ENUM_ADS_ERROR_GSS, 
77                      ENUM_ADS_ERROR_LDAP, ENUM_ADS_ERROR_SYSTEM, ENUM_ADS_ERROR_NT};
78
79 typedef struct {
80         enum ads_error_type error_type;
81         union err_state{                
82                 int rc;
83                 NTSTATUS nt_status;
84         } err;
85         /* For error_type = ENUM_ADS_ERROR_GSS minor_status describe GSS API error */
86         /* Where rc represents major_status of GSS API error */
87         int minor_status;
88 } ADS_STATUS;
89
90 #ifdef HAVE_ADS
91 typedef LDAPMod **ADS_MODLIST;
92 #else
93 typedef void **ADS_MODLIST;
94 #endif
95
96 /* macros to simplify error returning */
97 #define ADS_ERROR(rc) ADS_ERROR_LDAP(rc)
98 #define ADS_ERROR_LDAP(rc) ads_build_error(ENUM_ADS_ERROR_LDAP, rc, 0)
99 #define ADS_ERROR_SYSTEM(rc) ads_build_error(ENUM_ADS_ERROR_SYSTEM, rc?rc:EINVAL, 0)
100 #define ADS_ERROR_KRB5(rc) ads_build_error(ENUM_ADS_ERROR_KRB5, rc, 0)
101 #define ADS_ERROR_GSS(rc, minor) ads_build_error(ENUM_ADS_ERROR_GSS, rc, minor)
102 #define ADS_ERROR_NT(rc) ads_build_nt_error(ENUM_ADS_ERROR_NT,rc)
103
104 #define ADS_ERR_OK(status) ((status.error_type == ENUM_ADS_ERROR_NT) ? NT_STATUS_IS_OK(status.err.nt_status):(status.err.rc == 0))
105 #define ADS_SUCCESS ADS_ERROR(0)
106
107 #define ADS_ERROR_HAVE_NO_MEMORY(x) do { \
108         if (!(x)) {\
109                 return ADS_ERROR(LDAP_NO_MEMORY);\
110         }\
111 } while (0)
112
113
114 /* time between reconnect attempts */
115 #define ADS_RECONNECT_TIME 5
116
117 /* ldap control oids */
118 #define ADS_PAGE_CTL_OID        "1.2.840.113556.1.4.319"
119 #define ADS_NO_REFERRALS_OID    "1.2.840.113556.1.4.1339"
120 #define ADS_SERVER_SORT_OID     "1.2.840.113556.1.4.473"
121 #define ADS_PERMIT_MODIFY_OID   "1.2.840.113556.1.4.1413"
122 #define ADS_ASQ_OID             "1.2.840.113556.1.4.1504"
123 #define ADS_EXTENDED_DN_OID     "1.2.840.113556.1.4.529"
124
125 /* ldap attribute oids (Services for Unix) */
126 #define ADS_ATTR_SFU_UIDNUMBER_OID      "1.2.840.113556.1.6.18.1.310"
127 #define ADS_ATTR_SFU_GIDNUMBER_OID      "1.2.840.113556.1.6.18.1.311"
128 #define ADS_ATTR_SFU_HOMEDIR_OID        "1.2.840.113556.1.6.18.1.344"
129 #define ADS_ATTR_SFU_SHELL_OID          "1.2.840.113556.1.6.18.1.312"
130 #define ADS_ATTR_SFU_GECOS_OID          "1.2.840.113556.1.6.18.1.337"
131
132 /* ldap attribute oids (RFC2307) */
133 #define ADS_ATTR_RFC2307_UIDNUMBER_OID  "1.3.6.1.1.1.1.0"
134 #define ADS_ATTR_RFC2307_GIDNUMBER_OID  "1.3.6.1.1.1.1.1"
135 #define ADS_ATTR_RFC2307_HOMEDIR_OID    "1.3.6.1.1.1.1.3"
136 #define ADS_ATTR_RFC2307_SHELL_OID      "1.3.6.1.1.1.1.4"
137 #define ADS_ATTR_RFC2307_GECOS_OID      "1.3.6.1.1.1.1.2"
138
139 /* ldap bitwise searches */
140 #define ADS_LDAP_MATCHING_RULE_BIT_AND  "1.2.840.113556.1.4.803"
141 #define ADS_LDAP_MATCHING_RULE_BIT_OR   "1.2.840.113556.1.4.804"
142
143 /* UserFlags for userAccountControl */
144 #define UF_SCRIPT                               0x00000001
145 #define UF_ACCOUNTDISABLE                       0x00000002
146 #define UF_UNUSED_1                             0x00000004
147 #define UF_HOMEDIR_REQUIRED                     0x00000008
148
149 #define UF_LOCKOUT                              0x00000010
150 #define UF_PASSWD_NOTREQD                       0x00000020
151 #define UF_PASSWD_CANT_CHANGE                   0x00000040
152 #define UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED      0x00000080
153
154 #define UF_TEMP_DUPLICATE_ACCOUNT               0x00000100
155 #define UF_NORMAL_ACCOUNT                       0x00000200
156 #define UF_UNUSED_2                             0x00000400
157 #define UF_INTERDOMAIN_TRUST_ACCOUNT            0x00000800
158
159 #define UF_WORKSTATION_TRUST_ACCOUNT            0x00001000
160 #define UF_SERVER_TRUST_ACCOUNT                 0x00002000
161 #define UF_UNUSED_3                             0x00004000
162 #define UF_UNUSED_4                             0x00008000
163
164 #define UF_DONT_EXPIRE_PASSWD                   0x00010000
165 #define UF_MNS_LOGON_ACCOUNT                    0x00020000
166 #define UF_SMARTCARD_REQUIRED                   0x00040000
167 #define UF_TRUSTED_FOR_DELEGATION               0x00080000
168
169 #define UF_NOT_DELEGATED                        0x00100000
170 #define UF_USE_DES_KEY_ONLY                     0x00200000
171 #define UF_DONT_REQUIRE_PREAUTH                 0x00400000
172 #define UF_PASSWORD_EXPIRED                     0x00800000
173
174 #define UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION 0x01000000
175 #define UF_NO_AUTH_DATA_REQUIRED                0x02000000
176 #define UF_UNUSED_8                             0x04000000
177 #define UF_UNUSED_9                             0x08000000
178
179 #define UF_UNUSED_10                            0x10000000
180 #define UF_UNUSED_11                            0x20000000
181 #define UF_UNUSED_12                            0x40000000
182 #define UF_UNUSED_13                            0x80000000
183
184 #define UF_MACHINE_ACCOUNT_MASK (\
185                 UF_INTERDOMAIN_TRUST_ACCOUNT |\
186                 UF_WORKSTATION_TRUST_ACCOUNT |\
187                 UF_SERVER_TRUST_ACCOUNT \
188                 )
189
190 #define UF_ACCOUNT_TYPE_MASK (\
191                 UF_TEMP_DUPLICATE_ACCOUNT |\
192                 UF_NORMAL_ACCOUNT |\
193                 UF_INTERDOMAIN_TRUST_ACCOUNT |\
194                 UF_WORKSTATION_TRUST_ACCOUNT |\
195                 UF_SERVER_TRUST_ACCOUNT \
196                 )
197
198 #define UF_SETTABLE_BITS (\
199                 UF_SCRIPT |\
200                 UF_ACCOUNTDISABLE |\
201                 UF_HOMEDIR_REQUIRED  |\
202                 UF_LOCKOUT |\
203                 UF_PASSWD_NOTREQD |\
204                 UF_PASSWD_CANT_CHANGE |\
205                 UF_ACCOUNT_TYPE_MASK | \
206                 UF_DONT_EXPIRE_PASSWD | \
207                 UF_MNS_LOGON_ACCOUNT |\
208                 UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED |\
209                 UF_SMARTCARD_REQUIRED |\
210                 UF_TRUSTED_FOR_DELEGATION |\
211                 UF_NOT_DELEGATED |\
212                 UF_USE_DES_KEY_ONLY  |\
213                 UF_DONT_REQUIRE_PREAUTH \
214                 )
215
216 /* sAMAccountType */
217 #define ATYPE_NORMAL_ACCOUNT                    0x30000000 /* 805306368 */
218 #define ATYPE_WORKSTATION_TRUST                 0x30000001 /* 805306369 */
219 #define ATYPE_INTERDOMAIN_TRUST                 0x30000002 /* 805306370 */ 
220 #define ATYPE_SECURITY_GLOBAL_GROUP             0x10000000 /* 268435456 */
221 #define ATYPE_DISTRIBUTION_GLOBAL_GROUP         0x10000001 /* 268435457 */
222 #define ATYPE_DISTRIBUTION_UNIVERSAL_GROUP      ATYPE_DISTRIBUTION_GLOBAL_GROUP
223 #define ATYPE_SECURITY_LOCAL_GROUP              0x20000000 /* 536870912 */
224 #define ATYPE_DISTRIBUTION_LOCAL_GROUP          0x20000001 /* 536870913 */
225
226 #define ATYPE_ACCOUNT           ATYPE_NORMAL_ACCOUNT            /* 0x30000000 805306368 */
227 #define ATYPE_GLOBAL_GROUP      ATYPE_SECURITY_GLOBAL_GROUP     /* 0x10000000 268435456 */
228 #define ATYPE_LOCAL_GROUP       ATYPE_SECURITY_LOCAL_GROUP      /* 0x20000000 536870912 */
229
230 /* groupType */
231 #define GROUP_TYPE_BUILTIN_LOCAL_GROUP          0x00000001
232 #define GROUP_TYPE_ACCOUNT_GROUP                0x00000002
233 #define GROUP_TYPE_RESOURCE_GROUP               0x00000004
234 #define GROUP_TYPE_UNIVERSAL_GROUP              0x00000008
235 #define GROUP_TYPE_APP_BASIC_GROUP              0x00000010
236 #define GROUP_TYPE_APP_QUERY_GROUP              0x00000020
237 #define GROUP_TYPE_SECURITY_ENABLED             0x80000000
238
239 #define GTYPE_SECURITY_BUILTIN_LOCAL_GROUP (    /* 0x80000005 -2147483643 */ \
240                 GROUP_TYPE_BUILTIN_LOCAL_GROUP| \
241                 GROUP_TYPE_RESOURCE_GROUP| \
242                 GROUP_TYPE_SECURITY_ENABLED \
243                 )
244 #define GTYPE_SECURITY_DOMAIN_LOCAL_GROUP (     /* 0x80000004 -2147483644 */ \
245                 GROUP_TYPE_RESOURCE_GROUP| \
246                 GROUP_TYPE_SECURITY_ENABLED \
247                 )
248 #define GTYPE_SECURITY_GLOBAL_GROUP (           /* 0x80000002 -2147483646 */ \
249                 GROUP_TYPE_ACCOUNT_GROUP| \
250                 GROUP_TYPE_SECURITY_ENABLED \
251                 )
252 #define GTYPE_DISTRIBUTION_GLOBAL_GROUP         0x00000002      /* 2 */
253 #define GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP   0x00000004      /* 4 */
254 #define GTYPE_DISTRIBUTION_UNIVERSAL_GROUP      0x00000008      /* 8 */
255
256 #define ADS_PINGS          0x0000FFFF  /* Ping response */
257 #define ADS_DNS_CONTROLLER 0x20000000  /* DomainControllerName is a DNS name*/
258 #define ADS_DNS_DOMAIN     0x40000000  /* DomainName is a DNS name */
259 #define ADS_DNS_FOREST     0x80000000  /* DnsForestName is a DNS name */
260
261 /* DomainCntrollerAddressType */
262 #define ADS_INET_ADDRESS      0x00000001
263 #define ADS_NETBIOS_ADDRESS   0x00000002
264
265
266 /* ads auth control flags */
267 #define ADS_AUTH_DISABLE_KERBEROS 0x01
268 #define ADS_AUTH_NO_BIND          0x02
269 #define ADS_AUTH_ANON_BIND        0x04
270 #define ADS_AUTH_SIMPLE_BIND      0x08
271 #define ADS_AUTH_ALLOW_NTLMSSP    0x10
272
273 /* Kerberos environment variable names */
274 #define KRB5_ENV_CCNAME "KRB5CCNAME"
275
276 /* Heimdal uses a slightly different name */
277 #if defined(HAVE_ENCTYPE_ARCFOUR_HMAC_MD5)
278 #define ENCTYPE_ARCFOUR_HMAC ENCTYPE_ARCFOUR_HMAC_MD5
279 #endif
280
281 /* The older versions of heimdal that don't have this
282    define don't seem to use it anyway.  I'm told they
283    always use a subkey */
284 #ifndef HAVE_AP_OPTS_USE_SUBKEY
285 #define AP_OPTS_USE_SUBKEY 0
286 #endif
287
288 #define WELL_KNOWN_GUID_COMPUTERS       "AA312825768811D1ADED00C04FD8D5CD" 
289 #define WELL_KNOWN_GUID_USERS           "A9D1CA15768811D1ADED00C04FD8D5CD"
290
291 #ifndef KRB5_ADDR_NETBIOS
292 #define KRB5_ADDR_NETBIOS 0x14
293 #endif
294
295 #ifndef KRB5KRB_ERR_RESPONSE_TOO_BIG
296 #define KRB5KRB_ERR_RESPONSE_TOO_BIG (-1765328332L)
297 #endif
298
299 #ifdef HAVE_KRB5
300 typedef struct {
301 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
302         krb5_address **addrs;
303 #elif defined(HAVE_KRB5_ADDRESSES) /* Heimdal */
304         krb5_addresses *addrs;
305 #else
306 #error UNKNOWN_KRB5_ADDRESS_TYPE
307 #endif
308 } smb_krb5_addresses;
309 #endif
310
311 enum ads_extended_dn_flags {
312         ADS_EXTENDED_DN_HEX_STRING      = 0,
313         ADS_EXTENDED_DN_STRING          = 1 /* not supported on win2k */
314 };
315
316 /* this is probably not very well suited to pass other controls generically but
317  * is good enough for the extended dn control where it is only used for atm */
318
319 typedef struct {
320         const char *control;
321         int val;
322         int critical;
323 } ads_control;
324 #endif