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