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