Convert the winbind parent->child communication to wb_reqtrans
[samba.git] / source3 / winbindd / winbindd.h
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind daemon for ntdom nss module
5
6    Copyright (C) Tim Potter 2000
7    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
8    
9    This library is free software; you can redistribute it and/or
10    modify it under the terms of the GNU Lesser General Public
11    License as published by the Free Software Foundation; either
12    version 3 of the License, or (at your option) any later version.
13    
14    This library is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    Library General Public License for more details.
18    
19    You should have received a copy of the GNU Lesser General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #ifndef _WINBINDD_H
24 #define _WINBINDD_H
25
26 #include "nsswitch/winbind_struct_protocol.h"
27 #include "nsswitch/libwbclient/wbclient.h"
28
29 #ifdef HAVE_LIBNSCD
30 #include <libnscd.h>
31 #endif
32
33 #ifdef HAVE_SYS_MMAN_H
34 #include <sys/mman.h>
35 #endif
36
37 #undef DBGC_CLASS
38 #define DBGC_CLASS DBGC_WINBIND
39
40 #define WB_REPLACE_CHAR         '_'
41
42 struct winbindd_fd_event {
43         struct winbindd_fd_event *next, *prev;
44         int fd;
45         int flags; /* see EVENT_FD_* flags */
46         void (*handler)(struct winbindd_fd_event *fde, int flags);
47         void *data;
48         size_t length, done;
49         void (*finished)(void *private_data, bool success);
50         void *private_data;
51 };
52
53 struct sid_ctr {
54         DOM_SID *sid;
55         bool finished;
56         const char *domain;
57         const char *name;
58         enum lsa_SidType type;
59 };
60
61 struct winbindd_cli_state {
62         struct winbindd_cli_state *prev, *next;   /* Linked list pointers */
63         int sock;                                 /* Open socket from client */
64         pid_t pid;                                /* pid of client */
65         bool finished;                            /* Can delete from list */
66         bool write_extra_data;                    /* Write extra_data field */
67         time_t last_access;                       /* Time of last access (read or write) */
68         bool privileged;                           /* Is the client 'privileged' */
69
70         TALLOC_CTX *mem_ctx;                      /* memory per request */
71         struct winbindd_request *request;         /* Request from client */
72         struct winbindd_request _request;
73         struct tevent_queue *out_queue;
74         struct winbindd_response response;        /* Respose to client */
75         bool getpwent_initialized;                /* Has getpwent_state been
76                                                    * initialized? */
77         bool getgrent_initialized;                /* Has getgrent_state been
78                                                    * initialized? */
79         struct getent_state *getpwent_state;      /* State for getpwent() */
80         struct getent_state *getgrent_state;      /* State for getgrent() */
81 };
82
83 /* State between get{pw,gr}ent() calls */
84
85 struct getent_state {
86         struct getent_state *prev, *next;
87         void *sam_entries;
88         uint32 sam_entry_index, num_sam_entries;
89         bool got_sam_entries;
90         fstring domain_name;
91 };
92
93 /* Storage for cached getpwent() user entries */
94
95 struct getpwent_user {
96         fstring name;                        /* Account name */
97         fstring gecos;                       /* User information */
98         fstring homedir;                     /* User Home Directory */
99         fstring shell;                       /* User Login Shell */
100         DOM_SID user_sid;                    /* NT user and primary group SIDs */
101         DOM_SID group_sid;
102 };
103
104 /* Server state structure */
105
106 typedef struct {
107         char *acct_name;
108         char *full_name;
109         char *homedir;
110         char *shell;
111         gid_t primary_gid;                   /* allow the nss_info
112                                                 backend to set the primary group */
113         DOM_SID user_sid;                    /* NT user and primary group SIDs */
114         DOM_SID group_sid;
115 } WINBIND_USERINFO;
116
117 /* Our connection to the DC */
118
119 struct winbindd_cm_conn {
120         struct cli_state *cli;
121
122         struct rpc_pipe_client *samr_pipe;
123         struct policy_handle sam_connect_handle, sam_domain_handle;
124
125         struct rpc_pipe_client *lsa_pipe;
126         struct policy_handle lsa_policy;
127
128         struct rpc_pipe_client *netlogon_pipe;
129 };
130
131 struct winbindd_async_request;
132
133 /* Async child */
134
135 struct winbindd_domain;
136
137 struct winbindd_child_dispatch_table {
138         const char *name;
139         enum winbindd_cmd struct_cmd;
140         enum winbindd_result (*struct_fn)(struct winbindd_domain *domain,
141                                           struct winbindd_cli_state *state);
142 };
143
144 struct winbindd_child {
145         struct winbindd_child *next, *prev;
146
147         pid_t pid;
148         struct winbindd_domain *domain;
149         char *logfilename;
150
151         int sock;
152         struct tevent_queue *queue;
153
154         struct timed_event *lockout_policy_event;
155         struct timed_event *machine_password_change_event;
156
157         const struct winbindd_child_dispatch_table *table;
158 };
159
160 /* Structures to hold per domain information */
161
162 struct winbindd_domain {
163         fstring name;                          /* Domain name (NetBIOS) */
164         fstring alt_name;                      /* alt Domain name, if any (FQDN for ADS) */
165         fstring forest_name;                   /* Name of the AD forest we're in */
166         DOM_SID sid;                           /* SID for this domain */
167         uint32 domain_flags;                   /* Domain flags from netlogon.h */
168         uint32 domain_type;                    /* Domain type from netlogon.h */
169         uint32 domain_trust_attribs;           /* Trust attribs from netlogon.h */
170         bool initialized;                      /* Did we already ask for the domain mode? */
171         bool native_mode;                      /* is this a win2k domain in native mode ? */
172         bool active_directory;                 /* is this a win2k active directory ? */
173         bool primary;                          /* is this our primary domain ? */
174         bool internal;                         /* BUILTIN and member SAM */
175         bool online;                           /* is this domain available ? */
176         time_t startup_time;                   /* When we set "startup" true. */
177         bool startup;                          /* are we in the first 30 seconds after startup_time ? */
178
179         bool can_do_samlogon_ex; /* Due to the lack of finer control what type
180                                   * of DC we have, let us try to do a
181                                   * credential-chain less samlogon_ex call
182                                   * with AD and schannel. If this fails with
183                                   * DCERPC_FAULT_OP_RNG_ERROR, then set this
184                                   * to False. This variable is around so that
185                                   * we don't have to try _ex every time. */
186
187         /* Lookup methods for this domain (LDAP or RPC) */
188         struct winbindd_methods *methods;
189
190         /* the backend methods are used by the cache layer to find the right
191            backend */
192         struct winbindd_methods *backend;
193
194         /* Private data for the backends (used for connection cache) */
195
196         void *private_data;
197
198         /*
199          * idmap config settings, used to tell the idmap child which
200          * special domain config to use for a mapping
201          */
202         bool have_idmap_config;
203         uint32_t id_range_low, id_range_high;
204
205         /* A working DC */
206         pid_t dc_probe_pid; /* Child we're using to detect the DC. */
207         fstring dcname;
208         struct sockaddr_storage dcaddr;
209
210         /* Sequence number stuff */
211
212         time_t last_seq_check;
213         uint32 sequence_number;
214         NTSTATUS last_status;
215
216         /* The smb connection */
217
218         struct winbindd_cm_conn conn;
219
220         /* The child pid we're talking to */
221
222         struct winbindd_child child;
223
224         /* Callback we use to try put us back online. */
225
226         uint32 check_online_timeout;
227         struct timed_event *check_online_event;
228
229         /* Linked list info */
230
231         struct winbindd_domain *prev, *next;
232 };
233
234 /* per-domain methods. This is how LDAP vs RPC is selected
235  */
236 struct winbindd_methods {
237         /* does this backend provide a consistent view of the data? (ie. is the primary group
238            always correct) */
239         bool consistent;
240
241         /* get a list of users, returning a WINBIND_USERINFO for each one */
242         NTSTATUS (*query_user_list)(struct winbindd_domain *domain,
243                                    TALLOC_CTX *mem_ctx,
244                                    uint32 *num_entries, 
245                                    WINBIND_USERINFO **info);
246
247         /* get a list of domain groups */
248         NTSTATUS (*enum_dom_groups)(struct winbindd_domain *domain,
249                                     TALLOC_CTX *mem_ctx,
250                                     uint32 *num_entries, 
251                                     struct acct_info **info);
252
253         /* get a list of domain local groups */
254         NTSTATUS (*enum_local_groups)(struct winbindd_domain *domain,
255                                     TALLOC_CTX *mem_ctx,
256                                     uint32 *num_entries, 
257                                     struct acct_info **info);
258                                     
259         /* convert one user or group name to a sid */
260         NTSTATUS (*name_to_sid)(struct winbindd_domain *domain,
261                                 TALLOC_CTX *mem_ctx,
262                                 enum winbindd_cmd orig_cmd,
263                                 const char *domain_name,
264                                 const char *name,
265                                 DOM_SID *sid,
266                                 enum lsa_SidType *type);
267
268         /* convert a sid to a user or group name */
269         NTSTATUS (*sid_to_name)(struct winbindd_domain *domain,
270                                 TALLOC_CTX *mem_ctx,
271                                 const DOM_SID *sid,
272                                 char **domain_name,
273                                 char **name,
274                                 enum lsa_SidType *type);
275
276         NTSTATUS (*rids_to_names)(struct winbindd_domain *domain,
277                                   TALLOC_CTX *mem_ctx,
278                                   const DOM_SID *domain_sid,
279                                   uint32 *rids,
280                                   size_t num_rids,
281                                   char **domain_name,
282                                   char ***names,
283                                   enum lsa_SidType **types);
284
285         /* lookup user info for a given SID */
286         NTSTATUS (*query_user)(struct winbindd_domain *domain, 
287                                TALLOC_CTX *mem_ctx, 
288                                const DOM_SID *user_sid,
289                                WINBIND_USERINFO *user_info);
290
291         /* lookup all groups that a user is a member of. The backend
292            can also choose to lookup by username or rid for this
293            function */
294         NTSTATUS (*lookup_usergroups)(struct winbindd_domain *domain,
295                                       TALLOC_CTX *mem_ctx,
296                                       const DOM_SID *user_sid,
297                                       uint32 *num_groups, DOM_SID **user_gids);
298
299         /* Lookup all aliases that the sids delivered are member of. This is
300          * to implement 'domain local groups' correctly */
301         NTSTATUS (*lookup_useraliases)(struct winbindd_domain *domain,
302                                        TALLOC_CTX *mem_ctx,
303                                        uint32 num_sids,
304                                        const DOM_SID *sids,
305                                        uint32 *num_aliases,
306                                        uint32 **alias_rids);
307
308         /* find all members of the group with the specified group_rid */
309         NTSTATUS (*lookup_groupmem)(struct winbindd_domain *domain,
310                                     TALLOC_CTX *mem_ctx,
311                                     const DOM_SID *group_sid,
312                                     uint32 *num_names, 
313                                     DOM_SID **sid_mem, char ***names, 
314                                     uint32 **name_types);
315
316         /* return the current global sequence number */
317         NTSTATUS (*sequence_number)(struct winbindd_domain *domain, uint32 *seq);
318
319         /* return the lockout policy */
320         NTSTATUS (*lockout_policy)(struct winbindd_domain *domain,
321                                    TALLOC_CTX *mem_ctx,
322                                    struct samr_DomInfo12 *lockout_policy);
323
324         /* return the lockout policy */
325         NTSTATUS (*password_policy)(struct winbindd_domain *domain,
326                                     TALLOC_CTX *mem_ctx,
327                                     struct samr_DomInfo1 *password_policy);
328
329         /* enumerate trusted domains */
330         NTSTATUS (*trusted_domains)(struct winbindd_domain *domain,
331                                     TALLOC_CTX *mem_ctx,
332                                     uint32 *num_domains,
333                                     char ***names,
334                                     char ***alt_names,
335                                     DOM_SID **dom_sids);
336 };
337
338 /* Filled out by IDMAP backends */
339 struct winbindd_idmap_methods {
340   /* Called when backend is first loaded */
341   bool (*init)(void);
342
343   bool (*get_sid_from_uid)(uid_t uid, DOM_SID *sid);
344   bool (*get_sid_from_gid)(gid_t gid, DOM_SID *sid);
345
346   bool (*get_uid_from_sid)(DOM_SID *sid, uid_t *uid);
347   bool (*get_gid_from_sid)(DOM_SID *sid, gid_t *gid);
348
349   /* Called when backend is unloaded */
350   bool (*close)(void);
351   /* Called to dump backend status */
352   void (*status)(void);
353 };
354
355 /* Data structures for dealing with the trusted domain cache */
356
357 struct winbindd_tdc_domain {
358         const char *domain_name;
359         const char *dns_name;
360         DOM_SID sid;
361         uint32 trust_flags;
362         uint32 trust_attribs;
363         uint32 trust_type;
364 };
365
366 /* Switch for listing users or groups */
367 enum ent_type {
368         LIST_USERS = 0,
369         LIST_GROUPS,
370 };
371
372 struct WINBINDD_MEMORY_CREDS {
373         struct WINBINDD_MEMORY_CREDS *next, *prev;
374         const char *username; /* lookup key. */
375         uid_t uid;
376         int ref_count;
377         size_t len;
378         uint8_t *nt_hash; /* Base pointer for the following 2 */
379         uint8_t *lm_hash;
380         char *pass;
381 };
382
383 struct WINBINDD_CCACHE_ENTRY {
384         struct WINBINDD_CCACHE_ENTRY *next, *prev;
385         const char *principal_name;
386         const char *ccname;
387         const char *service;
388         const char *username;
389         const char *realm;
390         struct WINBINDD_MEMORY_CREDS *cred_ptr;
391         int ref_count;
392         uid_t uid;
393         time_t create_time;
394         time_t renew_until;
395         time_t refresh_time;
396         struct timed_event *event;
397 };
398
399 #include "winbindd/winbindd_proto.h"
400
401 #define WINBINDD_ESTABLISH_LOOP 30
402 #define WINBINDD_RESCAN_FREQ lp_winbind_cache_time()
403 #define WINBINDD_PAM_AUTH_KRB5_RENEW_TIME 2592000 /* one month */
404 #define DOM_SEQUENCE_NONE ((uint32)-1)
405
406 #define IS_DOMAIN_OFFLINE(x) ( lp_winbind_offline_logon() && \
407                                ( get_global_winbindd_state_offline() \
408                                  || !(x)->online ) )
409 #define IS_DOMAIN_ONLINE(x) (!IS_DOMAIN_OFFLINE(x))
410
411 #endif /* _WINBINDD_H */