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