spnego: share spnego_parse.
[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 #include "../libds/common/flags.h"
10
11 #define TOK_ID_KRB_AP_REQ       ((const uint8_t *)"\x01\x00")
12 #define TOK_ID_KRB_AP_REP       ((const uint8_t *)"\x02\x00")
13 #define TOK_ID_KRB_ERROR        ((const uint8_t *)"\x03\x00")
14 #define TOK_ID_GSS_GETMIC       ((const uint8_t *)"\x01\x01")
15 #define TOK_ID_GSS_WRAP         ((const uint8_t *)"\x02\x01")
16
17 enum wb_posix_mapping {
18         WB_POSIX_MAP_UNKNOWN    = -1,
19         WB_POSIX_MAP_TEMPLATE   = 0, 
20         WB_POSIX_MAP_SFU        = 1, 
21         WB_POSIX_MAP_SFU20      = 2, 
22         WB_POSIX_MAP_RFC2307    = 3,
23         WB_POSIX_MAP_UNIXINFO   = 4
24 };
25
26 /* there are 5 possible types of errors the ads subsystem can produce */
27 enum ads_error_type {ENUM_ADS_ERROR_KRB5, ENUM_ADS_ERROR_GSS, 
28                      ENUM_ADS_ERROR_LDAP, ENUM_ADS_ERROR_SYSTEM, ENUM_ADS_ERROR_NT};
29
30 typedef struct {
31         enum ads_error_type error_type;
32         union err_state{                
33                 int rc;
34                 NTSTATUS nt_status;
35         } err;
36         /* For error_type = ENUM_ADS_ERROR_GSS minor_status describe GSS API error */
37         /* Where rc represents major_status of GSS API error */
38         int minor_status;
39 } ADS_STATUS;
40
41 struct ads_struct;
42
43 struct ads_saslwrap_ops {
44         const char *name;
45         ADS_STATUS (*wrap)(struct ads_struct *, uint8 *buf, uint32 len);
46         ADS_STATUS (*unwrap)(struct ads_struct *);
47         void (*disconnect)(struct ads_struct *);
48 };
49
50 enum ads_saslwrap_type {
51         ADS_SASLWRAP_TYPE_PLAIN = 1,
52         ADS_SASLWRAP_TYPE_SIGN = 2,
53         ADS_SASLWRAP_TYPE_SEAL = 4
54 };
55
56 typedef struct ads_struct {
57         int is_mine;    /* do I own this structure's memory? */
58         
59         /* info needed to find the server */
60         struct {
61                 char *realm;
62                 char *workgroup;
63                 char *ldap_server;
64                 int foreign; /* set to 1 if connecting to a foreign
65                               * realm */
66                 bool gc;     /* Is this a global catalog server? */
67         } server;
68
69         /* info needed to authenticate */
70         struct {
71                 char *realm;
72                 char *password;
73                 char *user_name;
74                 char *kdc_server;
75                 unsigned flags;
76                 int time_offset;
77                 time_t tgt_expire;
78                 time_t tgs_expire;
79                 time_t renewable;
80         } auth;
81
82         /* info derived from the servers config */
83         struct {
84                 uint32 flags; /* cldap flags identifying the services. */
85                 char *realm;
86                 char *bind_path;
87                 char *ldap_server_name;
88                 char *server_site_name;
89                 char *client_site_name;
90                 time_t current_time;
91                 char *schema_path;
92                 char *config_path;
93         } config;
94
95         /* info about the current LDAP connection */
96 #ifdef HAVE_LDAP
97         struct {
98                 LDAP *ld;
99                 struct sockaddr_storage ss; /* the ip of the active connection, if any */
100                 time_t last_attempt; /* last attempt to reconnect */
101                 int port;
102
103                 enum ads_saslwrap_type wrap_type;
104
105 #ifdef HAVE_LDAP_SASL_WRAPPING
106                 Sockbuf_IO_Desc *sbiod; /* lowlevel state for LDAP wrapping */
107 #endif /* HAVE_LDAP_SASL_WRAPPING */
108                 TALLOC_CTX *mem_ctx;
109                 const struct ads_saslwrap_ops *wrap_ops;
110                 void *wrap_private_data;
111                 struct {
112                         uint32 ofs;
113                         uint32 needed;
114                         uint32 left;
115 #define        ADS_SASL_WRAPPING_IN_MAX_WRAPPED        0x0FFFFFFF
116                         uint32 max_wrapped;
117                         uint32 min_wrapped;
118                         uint32 size;
119                         uint8 *buf;
120                 } in;
121                 struct {
122                         uint32 ofs;
123                         uint32 left;
124 #define        ADS_SASL_WRAPPING_OUT_MAX_WRAPPED       0x00A00000
125                         uint32 max_unwrapped;
126                         uint32 sig_size;
127                         uint32 size;
128                         uint8 *buf;
129                 } out;
130         } ldap;
131 #endif /* HAVE_LDAP */
132 } ADS_STRUCT;
133
134 /* used to remember the names of the posix attributes in AD */
135 /* see the rfc2307 & sfu nss backends */
136
137 struct posix_schema {
138         char *posix_homedir_attr;
139         char *posix_shell_attr;
140         char *posix_uidnumber_attr;
141         char *posix_gidnumber_attr;
142         char *posix_gecos_attr;
143         char *posix_uid_attr;
144 };
145
146
147
148 #ifdef HAVE_ADS
149 typedef LDAPMod **ADS_MODLIST;
150 #else
151 typedef void **ADS_MODLIST;
152 #endif
153
154 /* macros to simplify error returning */
155 #define ADS_ERROR(rc) ADS_ERROR_LDAP(rc)
156 #define ADS_ERROR_LDAP(rc) ads_build_error(ENUM_ADS_ERROR_LDAP, rc, 0)
157 #define ADS_ERROR_SYSTEM(rc) ads_build_error(ENUM_ADS_ERROR_SYSTEM, rc?rc:EINVAL, 0)
158 #define ADS_ERROR_KRB5(rc) ads_build_error(ENUM_ADS_ERROR_KRB5, rc, 0)
159 #define ADS_ERROR_GSS(rc, minor) ads_build_error(ENUM_ADS_ERROR_GSS, rc, minor)
160 #define ADS_ERROR_NT(rc) ads_build_nt_error(ENUM_ADS_ERROR_NT,rc)
161
162 #define ADS_ERR_OK(status) ((status.error_type == ENUM_ADS_ERROR_NT) ? NT_STATUS_IS_OK(status.err.nt_status):(status.err.rc == 0))
163 #define ADS_SUCCESS ADS_ERROR(0)
164
165 #define ADS_ERROR_HAVE_NO_MEMORY(x) do { \
166         if (!(x)) {\
167                 return ADS_ERROR(LDAP_NO_MEMORY);\
168         }\
169 } while (0)
170
171
172 /* time between reconnect attempts */
173 #define ADS_RECONNECT_TIME 5
174
175 /* ldap control oids */
176 #define ADS_PAGE_CTL_OID        "1.2.840.113556.1.4.319"
177 #define ADS_NO_REFERRALS_OID    "1.2.840.113556.1.4.1339"
178 #define ADS_SERVER_SORT_OID     "1.2.840.113556.1.4.473"
179 #define ADS_PERMIT_MODIFY_OID   "1.2.840.113556.1.4.1413"
180 #define ADS_ASQ_OID             "1.2.840.113556.1.4.1504"
181 #define ADS_EXTENDED_DN_OID     "1.2.840.113556.1.4.529"
182 #define ADS_SD_FLAGS_OID        "1.2.840.113556.1.4.801"
183
184 /* ldap attribute oids (Services for Unix 3.0, 3.5) */
185 #define ADS_ATTR_SFU_UIDNUMBER_OID      "1.2.840.113556.1.6.18.1.310"
186 #define ADS_ATTR_SFU_GIDNUMBER_OID      "1.2.840.113556.1.6.18.1.311"
187 #define ADS_ATTR_SFU_HOMEDIR_OID        "1.2.840.113556.1.6.18.1.344"
188 #define ADS_ATTR_SFU_SHELL_OID          "1.2.840.113556.1.6.18.1.312"
189 #define ADS_ATTR_SFU_GECOS_OID          "1.2.840.113556.1.6.18.1.337"
190 #define ADS_ATTR_SFU_UID_OID            "1.2.840.113556.1.6.18.1.309"
191
192 /* ldap attribute oids (Services for Unix 2.0) */
193 #define ADS_ATTR_SFU20_UIDNUMBER_OID    "1.2.840.113556.1.4.7000.187.70"
194 #define ADS_ATTR_SFU20_GIDNUMBER_OID    "1.2.840.113556.1.4.7000.187.71"
195 #define ADS_ATTR_SFU20_HOMEDIR_OID      "1.2.840.113556.1.4.7000.187.106"
196 #define ADS_ATTR_SFU20_SHELL_OID        "1.2.840.113556.1.4.7000.187.72"
197 #define ADS_ATTR_SFU20_GECOS_OID        "1.2.840.113556.1.4.7000.187.97"
198 #define ADS_ATTR_SFU20_UID_OID          "1.2.840.113556.1.4.7000.187.102"
199
200
201 /* ldap attribute oids (RFC2307) */
202 #define ADS_ATTR_RFC2307_UIDNUMBER_OID  "1.3.6.1.1.1.1.0"
203 #define ADS_ATTR_RFC2307_GIDNUMBER_OID  "1.3.6.1.1.1.1.1"
204 #define ADS_ATTR_RFC2307_HOMEDIR_OID    "1.3.6.1.1.1.1.3"
205 #define ADS_ATTR_RFC2307_SHELL_OID      "1.3.6.1.1.1.1.4"
206 #define ADS_ATTR_RFC2307_GECOS_OID      "1.3.6.1.1.1.1.2"
207 #define ADS_ATTR_RFC2307_UID_OID        "0.9.2342.19200300.100.1.1"
208
209 /* ldap bitwise searches */
210 #define ADS_LDAP_MATCHING_RULE_BIT_AND  "1.2.840.113556.1.4.803"
211 #define ADS_LDAP_MATCHING_RULE_BIT_OR   "1.2.840.113556.1.4.804"
212
213 #define ADS_PINGS          0x0000FFFF  /* Ping response */
214 #define ADS_DNS_CONTROLLER 0x20000000  /* DomainControllerName is a DNS name*/
215 #define ADS_DNS_DOMAIN     0x40000000  /* DomainName is a DNS name */
216 #define ADS_DNS_FOREST     0x80000000  /* DnsForestName is a DNS name */
217
218 /* ads auth control flags */
219 #define ADS_AUTH_DISABLE_KERBEROS 0x0001
220 #define ADS_AUTH_NO_BIND          0x0002
221 #define ADS_AUTH_ANON_BIND        0x0004
222 #define ADS_AUTH_SIMPLE_BIND      0x0008
223 #define ADS_AUTH_ALLOW_NTLMSSP    0x0010
224 #define ADS_AUTH_SASL_SIGN        0x0020
225 #define ADS_AUTH_SASL_SEAL        0x0040
226 #define ADS_AUTH_SASL_FORCE       0x0080
227 #define ADS_AUTH_USER_CREDS       0x0100
228
229 /* Kerberos environment variable names */
230 #define KRB5_ENV_CCNAME "KRB5CCNAME"
231
232 /* Heimdal uses a slightly different name */
233 #if defined(HAVE_ENCTYPE_ARCFOUR_HMAC_MD5)
234 #define ENCTYPE_ARCFOUR_HMAC ENCTYPE_ARCFOUR_HMAC_MD5
235 #endif
236
237 /* The older versions of heimdal that don't have this
238    define don't seem to use it anyway.  I'm told they
239    always use a subkey */
240 #ifndef HAVE_AP_OPTS_USE_SUBKEY
241 #define AP_OPTS_USE_SUBKEY 0
242 #endif
243
244 #define WELL_KNOWN_GUID_COMPUTERS       "AA312825768811D1ADED00C04FD8D5CD" 
245 #define WELL_KNOWN_GUID_USERS           "A9D1CA15768811D1ADED00C04FD8D5CD"
246
247 #ifndef KRB5_ADDR_NETBIOS
248 #define KRB5_ADDR_NETBIOS 0x14
249 #endif
250
251 #ifndef KRB5KRB_ERR_RESPONSE_TOO_BIG
252 #define KRB5KRB_ERR_RESPONSE_TOO_BIG (-1765328332L)
253 #endif
254
255 #ifdef HAVE_KRB5
256 typedef struct {
257 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
258         krb5_address **addrs;
259 #elif defined(HAVE_KRB5_ADDRESSES) /* Heimdal */
260         krb5_addresses *addrs;
261 #else
262 #error UNKNOWN_KRB5_ADDRESS_TYPE
263 #endif /* defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) */
264 } smb_krb5_addresses;
265
266 #ifdef HAVE_KRB5_KEYBLOCK_KEYVALUE /* Heimdal */
267 #define KRB5_KEY_TYPE(k)        ((k)->keytype)
268 #define KRB5_KEY_LENGTH(k)      ((k)->keyvalue.length)
269 #define KRB5_KEY_DATA(k)        ((k)->keyvalue.data)
270 #define KRB5_KEY_DATA_CAST      void
271 #else /* MIT */
272 #define KRB5_KEY_TYPE(k)        ((k)->enctype)
273 #define KRB5_KEY_LENGTH(k)      ((k)->length)
274 #define KRB5_KEY_DATA(k)        ((k)->contents)
275 #define KRB5_KEY_DATA_CAST      krb5_octet
276 #endif /* HAVE_KRB5_KEYBLOCK_KEYVALUE */
277
278 #ifdef HAVE_KRB5_KEYTAB_ENTRY_KEY               /* MIT */
279 #define KRB5_KT_KEY(k)          (&(k)->key)
280 #elif HAVE_KRB5_KEYTAB_ENTRY_KEYBLOCK          /* Heimdal */
281 #define KRB5_KT_KEY(k)          (&(k)->keyblock)
282 #else
283 #error krb5_keytab_entry has no key or keyblock member
284 #endif /* HAVE_KRB5_KEYTAB_ENTRY_KEY */
285
286 #endif /* HAVE_KRB5 */
287
288 enum ads_extended_dn_flags {
289         ADS_EXTENDED_DN_HEX_STRING      = 0,
290         ADS_EXTENDED_DN_STRING          = 1 /* not supported on win2k */
291 };
292
293 /* this is probably not very well suited to pass other controls generically but
294  * is good enough for the extended dn control where it is only used for atm */
295
296 typedef struct {
297         const char *control;
298         int val;
299         int critical;
300 } ads_control;
301
302 #define ADS_IGNORE_PRINCIPAL "not_defined_in_RFC4178@please_ignore"
303
304 #endif  /* _INCLUDE_ADS_H_ */