lib: Pass mem_ctx to state_path()
[amitay/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 #include "librpc/gen_ndr/dcerpc.h"
29 #include "librpc/gen_ndr/winbind.h"
30
31 #include "../lib/util/tevent_ntstatus.h"
32
33 #ifdef HAVE_LIBNSCD
34 #include <libnscd.h>
35 #endif
36
37 #ifdef HAVE_SYS_MMAN_H
38 #include <sys/mman.h>
39 #endif
40
41 #undef DBGC_CLASS
42 #define DBGC_CLASS DBGC_WINBIND
43
44 #define WB_REPLACE_CHAR         '_'
45
46 struct winbindd_cli_state {
47         struct winbindd_cli_state *prev, *next;   /* Linked list pointers */
48         int sock;                                 /* Open socket from client */
49         pid_t pid;                                /* pid of client */
50         time_t last_access;                       /* Time of last access (read or write) */
51         bool privileged;                           /* Is the client 'privileged' */
52
53         TALLOC_CTX *mem_ctx;                      /* memory per request */
54         const char *cmd_name;
55         NTSTATUS (*recv_fn)(struct tevent_req *req,
56                             struct winbindd_response *presp);
57         struct winbindd_request *request;         /* Request from client */
58         struct tevent_queue *out_queue;
59         struct winbindd_response *response;        /* Respose to client */
60         struct tevent_req *io_req; /* wb_req_read_* or wb_resp_write_* */
61
62         struct getpwent_state *pwent_state; /* State for getpwent() */
63         struct getgrent_state *grent_state; /* State for getgrent() */
64 };
65
66 struct getpwent_state {
67         struct winbindd_domain *domain;
68         uint32_t next_user;
69         struct wbint_RidArray rids;
70 };
71
72 struct getgrent_state {
73         struct winbindd_domain *domain;
74         int next_group;
75         int num_groups;
76         struct wbint_Principal *groups;
77 };
78
79 /* Our connection to the DC */
80
81 struct winbindd_cm_conn {
82         struct cli_state *cli;
83
84         enum dcerpc_AuthLevel auth_level;
85
86         struct rpc_pipe_client *samr_pipe;
87         struct policy_handle sam_connect_handle, sam_domain_handle;
88
89         struct rpc_pipe_client *lsa_pipe;
90         struct rpc_pipe_client *lsa_pipe_tcp;
91         struct policy_handle lsa_policy;
92
93         struct rpc_pipe_client *netlogon_pipe;
94         struct netlogon_creds_cli_context *netlogon_creds_ctx;
95         bool netlogon_force_reauth;
96 };
97
98 /* Async child */
99
100 struct winbindd_domain;
101
102 struct winbindd_child_dispatch_table {
103         const char *name;
104         enum winbindd_cmd struct_cmd;
105         enum winbindd_result (*struct_fn)(struct winbindd_domain *domain,
106                                           struct winbindd_cli_state *state);
107 };
108
109 struct winbindd_child {
110         pid_t pid;
111         struct winbindd_domain *domain;
112         char *logfilename;
113
114         int sock;
115         struct tevent_fd *monitor_fde; /* Watch for dead children/sockets */
116         struct tevent_queue *queue;
117         struct dcerpc_binding_handle *binding_handle;
118
119         struct tevent_timer *lockout_policy_event;
120         struct tevent_timer *machine_password_change_event;
121
122         const struct winbindd_child_dispatch_table *table;
123 };
124
125 /* Structures to hold per domain information */
126
127 struct winbindd_domain {
128         char *name;                            /* Domain name (NetBIOS) */
129         char *alt_name;                        /* alt Domain name, if any (FQDN for ADS) */
130         char *forest_name;                     /* Name of the AD forest we're in */
131         struct dom_sid sid;                           /* SID for this domain */
132         enum netr_SchannelType secure_channel_type;
133         uint32_t domain_flags;                   /* Domain flags from netlogon.h */
134         uint32_t domain_type;                    /* Domain type from netlogon.h */
135         uint32_t domain_trust_attribs;           /* Trust attribs from netlogon.h */
136         struct winbindd_domain *routing_domain;
137         bool initialized;                      /* Did we already ask for the domain mode? */
138         bool native_mode;                      /* is this a win2k domain in native mode ? */
139         bool active_directory;                 /* is this a win2k active directory ? */
140         bool primary;                          /* is this our primary domain ? */
141         bool internal;                         /* BUILTIN and member SAM */
142         bool rodc;                             /* Are we an RODC for this AD domain? (do some operations locally) */
143         bool online;                           /* is this domain available ? */
144         time_t startup_time;                   /* When we set "startup" true. monotonic clock */
145         bool startup;                          /* are we in the first 30 seconds after startup_time ? */
146
147         bool can_do_ncacn_ip_tcp;
148
149         /*
150          * Lookup methods for this domain (LDAP or RPC). The backend
151          * methods are used by the cache layer.
152          */
153         struct winbindd_methods *backend;
154
155         /* Private data for the backends (used for connection cache) */
156
157         void *private_data;
158
159         /* A working DC */
160         pid_t dc_probe_pid; /* Child we're using to detect the DC. */
161         char *dcname;
162         struct sockaddr_storage dcaddr;
163
164         /* Sequence number stuff */
165
166         time_t last_seq_check;
167         uint32_t sequence_number;
168         NTSTATUS last_status;
169
170         /* The smb connection */
171
172         struct winbindd_cm_conn conn;
173
174         /* The child pid we're talking to */
175
176         struct winbindd_child *children;
177
178         struct tevent_queue *queue;
179         struct dcerpc_binding_handle *binding_handle;
180
181         /* Callback we use to try put us back online. */
182
183         uint32_t check_online_timeout;
184         struct tevent_timer *check_online_event;
185
186         /* Linked list info */
187
188         struct winbindd_domain *prev, *next;
189 };
190
191 struct wb_acct_info {
192         fstring acct_name; /* account name */
193         fstring acct_desc; /* account name */
194         uint32_t rid; /* domain-relative RID */
195 };
196
197 /* per-domain methods. This is how LDAP vs RPC is selected
198  */
199 struct winbindd_methods {
200         /* does this backend provide a consistent view of the data? (ie. is the primary group
201            always correct) */
202         bool consistent;
203
204         /* get a list of users, returning a wbint_userinfo for each one */
205         NTSTATUS (*query_user_list)(struct winbindd_domain *domain,
206                                    TALLOC_CTX *mem_ctx,
207                                    uint32_t **rids);
208
209         /* get a list of domain groups */
210         NTSTATUS (*enum_dom_groups)(struct winbindd_domain *domain,
211                                     TALLOC_CTX *mem_ctx,
212                                     uint32_t *num_entries,
213                                     struct wb_acct_info **info);
214
215         /* get a list of domain local groups */
216         NTSTATUS (*enum_local_groups)(struct winbindd_domain *domain,
217                                     TALLOC_CTX *mem_ctx,
218                                     uint32_t *num_entries,
219                                     struct wb_acct_info **info);
220
221         /* convert one user or group name to a sid */
222         NTSTATUS (*name_to_sid)(struct winbindd_domain *domain,
223                                 TALLOC_CTX *mem_ctx,
224                                 const char *domain_name,
225                                 const char *name,
226                                 uint32_t flags,
227                                 struct dom_sid *sid,
228                                 enum lsa_SidType *type);
229
230         /* convert a sid to a user or group name */
231         NTSTATUS (*sid_to_name)(struct winbindd_domain *domain,
232                                 TALLOC_CTX *mem_ctx,
233                                 const struct dom_sid *sid,
234                                 char **domain_name,
235                                 char **name,
236                                 enum lsa_SidType *type);
237
238         NTSTATUS (*rids_to_names)(struct winbindd_domain *domain,
239                                   TALLOC_CTX *mem_ctx,
240                                   const struct dom_sid *domain_sid,
241                                   uint32_t *rids,
242                                   size_t num_rids,
243                                   char **domain_name,
244                                   char ***names,
245                                   enum lsa_SidType **types);
246
247         /* lookup all groups that a user is a member of. The backend
248            can also choose to lookup by username or rid for this
249            function */
250         NTSTATUS (*lookup_usergroups)(struct winbindd_domain *domain,
251                                       TALLOC_CTX *mem_ctx,
252                                       const struct dom_sid *user_sid,
253                                       uint32_t *num_groups, struct dom_sid **user_gids);
254
255         /* Lookup all aliases that the sids delivered are member of. This is
256          * to implement 'domain local groups' correctly */
257         NTSTATUS (*lookup_useraliases)(struct winbindd_domain *domain,
258                                        TALLOC_CTX *mem_ctx,
259                                        uint32_t num_sids,
260                                        const struct dom_sid *sids,
261                                        uint32_t *num_aliases,
262                                        uint32_t **alias_rids);
263
264         /* find all members of the group with the specified group_rid */
265         NTSTATUS (*lookup_groupmem)(struct winbindd_domain *domain,
266                                     TALLOC_CTX *mem_ctx,
267                                     const struct dom_sid *group_sid,
268                                     enum lsa_SidType type,
269                                     uint32_t *num_names,
270                                     struct dom_sid **sid_mem, char ***names,
271                                     uint32_t **name_types);
272
273         /* return the current global sequence number */
274         NTSTATUS (*sequence_number)(struct winbindd_domain *domain, uint32_t *seq);
275
276         /* return the lockout policy */
277         NTSTATUS (*lockout_policy)(struct winbindd_domain *domain,
278                                    TALLOC_CTX *mem_ctx,
279                                    struct samr_DomInfo12 *lockout_policy);
280
281         /* return the lockout policy */
282         NTSTATUS (*password_policy)(struct winbindd_domain *domain,
283                                     TALLOC_CTX *mem_ctx,
284                                     struct samr_DomInfo1 *password_policy);
285
286         /* enumerate trusted domains */
287         NTSTATUS (*trusted_domains)(struct winbindd_domain *domain,
288                                     TALLOC_CTX *mem_ctx,
289                                     struct netr_DomainTrustList *trusts);
290 };
291
292 /* Filled out by IDMAP backends */
293 struct winbindd_idmap_methods {
294   /* Called when backend is first loaded */
295   bool (*init)(void);
296
297   bool (*get_sid_from_uid)(uid_t uid, struct dom_sid *sid);
298   bool (*get_sid_from_gid)(gid_t gid, struct dom_sid *sid);
299
300   bool (*get_uid_from_sid)(struct dom_sid *sid, uid_t *uid);
301   bool (*get_gid_from_sid)(struct dom_sid *sid, gid_t *gid);
302
303   /* Called when backend is unloaded */
304   bool (*close)(void);
305   /* Called to dump backend status */
306   void (*status)(void);
307 };
308
309 /* Data structures for dealing with the trusted domain cache */
310
311 struct winbindd_tdc_domain {
312         const char *domain_name;
313         const char *dns_name;
314         struct dom_sid sid;
315         uint32_t trust_flags;
316         uint32_t trust_attribs;
317         uint32_t trust_type;
318 };
319
320 struct WINBINDD_MEMORY_CREDS {
321         struct WINBINDD_MEMORY_CREDS *next, *prev;
322         const char *username; /* lookup key. */
323         uid_t uid;
324         int ref_count;
325         size_t len;
326         uint8_t *nt_hash; /* Base pointer for the following 2 */
327         uint8_t *lm_hash;
328         char *pass;
329 };
330
331 struct WINBINDD_CCACHE_ENTRY {
332         struct WINBINDD_CCACHE_ENTRY *next, *prev;
333         const char *principal_name;
334         const char *ccname;
335         const char *service;
336         const char *username;
337         const char *realm;
338         struct WINBINDD_MEMORY_CREDS *cred_ptr;
339         int ref_count;
340         uid_t uid;
341         time_t create_time;
342         time_t renew_until;
343         time_t refresh_time;
344         struct tevent_timer *event;
345 };
346
347 #include "winbindd/winbindd_proto.h"
348
349 #define WINBINDD_ESTABLISH_LOOP 30
350 #define WINBINDD_RESCAN_FREQ lp_winbind_cache_time()
351 #define WINBINDD_PAM_AUTH_KRB5_RENEW_TIME 2592000 /* one month */
352 #define DOM_SEQUENCE_NONE ((uint32_t)-1)
353
354 #endif /* _WINBINDD_H */