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