f1896710dc930cba134d63c7f6f5fdced0a1dff3
[metze/samba/wip.git] / source3 / include / passdb.h
1 /* 
2    Unix SMB/CIFS implementation.
3    passdb structures and parameters
4    Copyright (C) Gerald Carter 2001
5    Copyright (C) Luke Kenneth Casson Leighton 1998 - 2000
6    Copyright (C) Andrew Bartlett 2002
7    Copyright (C) Simo Sorce 2003
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program 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
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #ifndef _PASSDB_H
25 #define _PASSDB_H
26
27
28 /*
29  * fields_present flags meanings
30  * same names as found in samba4 idl files
31  */
32
33 #define ACCT_USERNAME           0x00000001
34 #define ACCT_FULL_NAME          0x00000002
35 #define ACCT_RID                0x00000004
36 #define ACCT_PRIMARY_GID        0x00000008
37 #define ACCT_ADMIN_DESC         0x00000010
38 #define ACCT_DESCRIPTION        0x00000020
39 #define ACCT_HOME_DIR           0x00000040
40 #define ACCT_HOME_DRIVE         0x00000080
41 #define ACCT_LOGON_SCRIPT       0x00000100
42 #define ACCT_PROFILE            0x00000200
43 #define ACCT_WORKSTATIONS       0x00000400
44 #define ACCT_LAST_LOGON         0x00000800
45 #define ACCT_LAST_LOGOFF        0x00001000
46 #define ACCT_LOGON_HOURS        0x00002000
47 #define ACCT_BAD_PWD_COUNT      0x00004000
48 #define ACCT_NUM_LOGONS         0x00008000
49 #define ACCT_ALLOW_PWD_CHANGE   0x00010000
50 #define ACCT_FORCE_PWD_CHANGE   0x00020000
51 #define ACCT_LAST_PWD_CHANGE    0x00040000
52 #define ACCT_EXPIRY             0x00080000
53 #define ACCT_FLAGS              0x00100000
54 #define ACCT_CALLBACK           0x00200000
55 #define ACCT_COUNTRY_CODE       0x00400000
56 #define ACCT_CODE_PAGE          0x00800000
57 #define ACCT_NT_PWD_SET         0x01000000
58 #define ACCT_LM_PWD_SET         0x02000000
59 #define ACCT_PRIVATEDATA        0x04000000
60 #define ACCT_EXPIRED_FLAG       0x08000000
61 #define ACCT_SEC_DESC           0x10000000
62 #define ACCT_OWF_PWD            0x20000000
63
64 /*
65  * bit flags representing initialized fields in SAM_ACCOUNT
66  */
67 enum pdb_elements {
68         PDB_UNINIT,
69         PDB_SMBHOME,
70         PDB_PROFILE,
71         PDB_DRIVE,
72         PDB_LOGONSCRIPT,
73         PDB_LOGONTIME,
74         PDB_LOGOFFTIME,
75         PDB_KICKOFFTIME,
76         PDB_BAD_PASSWORD_TIME,
77         PDB_CANCHANGETIME,
78         PDB_MUSTCHANGETIME,
79         PDB_PLAINTEXT_PW,
80         PDB_USERNAME,
81         PDB_FULLNAME,
82         PDB_DOMAIN,
83         PDB_NTUSERNAME,
84         PDB_HOURSLEN,
85         PDB_LOGONDIVS,
86         PDB_USERSID,
87         PDB_GROUPSID,
88         PDB_ACCTCTRL,
89         PDB_PASSLASTSET,
90         PDB_UNIXHOMEDIR,
91         PDB_ACCTDESC,
92         PDB_WORKSTATIONS,
93         PDB_UNKNOWNSTR,
94         PDB_MUNGEDDIAL,
95         PDB_HOURS,
96         PDB_FIELDS_PRESENT,
97         PDB_BAD_PASSWORD_COUNT,
98         PDB_LOGON_COUNT,
99         PDB_UNKNOWN6,
100         PDB_LMPASSWD,
101         PDB_NTPASSWD,
102         PDB_PWHISTORY,
103         PDB_BACKEND_PRIVATE_DATA,
104
105         /* this must be the last element */
106         PDB_COUNT
107 };
108
109 enum pdb_group_elements {
110         PDB_GROUP_NAME,
111         PDB_GROUP_SID,
112         PDB_GROUP_SID_NAME_USE,
113         PDB_GROUP_MEMBERS,
114
115         /* this must be the last element */
116         PDB_GROUP_COUNT
117 };
118
119
120 enum pdb_value_state {
121         PDB_DEFAULT=0,
122         PDB_SET,
123         PDB_CHANGED
124 };
125
126 #define IS_SAM_SET(x, flag)     (pdb_get_init_flags(x, flag) == PDB_SET)
127 #define IS_SAM_CHANGED(x, flag) (pdb_get_init_flags(x, flag) == PDB_CHANGED)
128 #define IS_SAM_DEFAULT(x, flag) (pdb_get_init_flags(x, flag) == PDB_DEFAULT)
129
130 /* cache for bad password lockout data, to be used on replicated SAMs */
131 typedef struct logon_cache_struct {
132         time_t entry_timestamp;
133         uint16 acct_ctrl;
134         uint16 bad_password_count;
135         time_t bad_password_time;
136 } LOGIN_CACHE;
137                 
138 typedef struct sam_passwd {
139         TALLOC_CTX *mem_ctx;
140         
141         void (*free_fn)(struct sam_passwd **);
142
143         struct pdb_methods *methods;
144
145         struct user_data {
146                 /* initialization flags */
147                 struct bitmap *change_flags;
148                 struct bitmap *set_flags;
149
150                 time_t logon_time;            /* logon time */
151                 time_t logoff_time;           /* logoff time */
152                 time_t kickoff_time;          /* kickoff time */
153                 time_t bad_password_time;     /* last bad password entered */
154                 time_t pass_last_set_time;    /* password last set time */
155                 time_t pass_can_change_time;  /* password can change time */
156                 time_t pass_must_change_time; /* password must change time */
157                 
158                 const char * username;     /* UNIX username string */
159                 const char * domain;       /* Windows Domain name */
160                 const char * nt_username;  /* Windows username string */
161                 const char * full_name;    /* user's full name string */
162                 const char * unix_home_dir;     /* UNIX home directory string */
163                 const char * home_dir;     /* home directory string */
164                 const char * dir_drive;    /* home directory drive string */
165                 const char * logon_script; /* logon script string */
166                 const char * profile_path; /* profile path string */
167                 const char * acct_desc;    /* user description string */
168                 const char * workstations; /* login from workstations string */
169                 const char * unknown_str;  /* don't know what this is, yet. */
170                 const char * munged_dial;  /* munged path name and dial-back tel number */
171                 
172                 DOM_SID user_sid;    /* Primary User SID */
173                 DOM_SID group_sid;   /* Primary Group SID */
174                 
175                 DATA_BLOB lm_pw; /* .data is Null if no password */
176                 DATA_BLOB nt_pw; /* .data is Null if no password */
177                 DATA_BLOB nt_pw_his; /* nt hashed password history .data is Null if not available */
178                 char* plaintext_pw; /* is Null if not available */
179                 
180                 uint16 acct_ctrl; /* account info (ACB_xxxx bit-mask) */
181                 uint32 fields_present; /* 0x00ff ffff */
182                 
183                 uint16 logon_divs; /* 168 - number of hours in a week */
184                 uint32 hours_len; /* normally 21 bytes */
185                 uint8 hours[MAX_HOURS_LEN];
186                 
187                 /* Was unknown_5. */
188                 uint16 bad_password_count;
189                 uint16 logon_count;
190
191                 uint32 unknown_6; /* 0x0000 04ec */
192                 /* a tag for who added the private methods */
193                 const struct pdb_methods *backend_private_methods;
194                 void *backend_private_data; 
195                 void (*backend_private_data_free_fn)(void **);
196         } private_u;
197
198         /* Lets see if the remaining code can get the hint that you
199            are meant to use the pdb_...() functions. */
200         
201 } SAM_ACCOUNT;
202
203 struct acct_info {
204         fstring acct_name; /* account name */
205         fstring acct_desc; /* account name */
206         uint32 rid; /* domain-relative RID */
207 };
208
209 struct samr_displayentry {
210         uint32 idx;
211         uint32 rid;
212         uint16 acct_flags;
213         const char *account_name;
214         const char *fullname;
215         const char *description;
216 };
217
218 enum pdb_search_type {
219         PDB_USER_SEARCH,
220         PDB_GROUP_SEARCH,
221         PDB_ALIAS_SEARCH
222 };
223
224 struct pdb_search {
225         TALLOC_CTX *mem_ctx;
226         enum pdb_search_type type;
227         struct samr_displayentry *cache;
228         uint32 num_entries;
229         ssize_t cache_size;
230         BOOL search_ended;
231         void *private_data;
232         BOOL (*next_entry)(struct pdb_search *search,
233                            struct samr_displayentry *entry);
234         void (*search_end)(struct pdb_search *search);
235 };
236
237 /*****************************************************************
238  Functions to be implemented by the new (v2) passdb API 
239 ****************************************************************/
240
241 /*
242  * This next constant specifies the version number of the PASSDB interface
243  * this SAMBA will load. Increment this if *ANY* changes are made to the interface. 
244  * Changed interface to fix int -> size_t problems. JRA.
245  * There's no point in allocating arrays in
246  * samr_lookup_rids twice. It was done in the srv_samr_nt.c code as well as in
247  * the pdb module. Remove the latter, this might happen more often. VL.
248  */
249
250 #define PASSDB_INTERFACE_VERSION 12
251
252 typedef struct pdb_context 
253 {
254         struct pdb_methods *pdb_methods;
255         struct pdb_methods *pwent_methods;
256         
257         /* These functions are wrappers for the functions listed above.
258            They may do extra things like re-reading a SAM_ACCOUNT on update */
259
260         NTSTATUS (*pdb_setsampwent)(struct pdb_context *, BOOL update, uint16 acb_mask);
261         
262         void (*pdb_endsampwent)(struct pdb_context *);
263         
264         NTSTATUS (*pdb_getsampwent)(struct pdb_context *, SAM_ACCOUNT *user);
265         
266         NTSTATUS (*pdb_getsampwnam)(struct pdb_context *, SAM_ACCOUNT *sam_acct, const char *username);
267         
268         NTSTATUS (*pdb_getsampwsid)(struct pdb_context *, SAM_ACCOUNT *sam_acct, const DOM_SID *sid);
269
270         NTSTATUS (*pdb_add_sam_account)(struct pdb_context *, SAM_ACCOUNT *sampass);
271         
272         NTSTATUS (*pdb_update_sam_account)(struct pdb_context *, SAM_ACCOUNT *sampass);
273         
274         NTSTATUS (*pdb_delete_sam_account)(struct pdb_context *, SAM_ACCOUNT *username);
275         
276         NTSTATUS (*pdb_rename_sam_account)(struct pdb_context *, SAM_ACCOUNT *oldname, const char *newname);
277
278         NTSTATUS (*pdb_update_login_attempts)(struct pdb_context *context, SAM_ACCOUNT *sam_acct, BOOL success);
279
280         NTSTATUS (*pdb_getgrsid)(struct pdb_context *context, GROUP_MAP *map, DOM_SID sid);
281         
282         NTSTATUS (*pdb_getgrgid)(struct pdb_context *context, GROUP_MAP *map, gid_t gid);
283         
284         NTSTATUS (*pdb_getgrnam)(struct pdb_context *context, GROUP_MAP *map, const char *name);
285         
286         NTSTATUS (*pdb_add_group_mapping_entry)(struct pdb_context *context,
287                                                 GROUP_MAP *map);
288         
289         NTSTATUS (*pdb_update_group_mapping_entry)(struct pdb_context *context,
290                                                    GROUP_MAP *map);
291         
292         NTSTATUS (*pdb_delete_group_mapping_entry)(struct pdb_context *context,
293                                                    DOM_SID sid);
294         
295         NTSTATUS (*pdb_enum_group_mapping)(struct pdb_context *context,
296                                            enum SID_NAME_USE sid_name_use,
297                                            GROUP_MAP **pp_rmap, size_t *p_num_entries,
298                                            BOOL unix_only);
299
300         NTSTATUS (*pdb_enum_group_members)(struct pdb_context *context,
301                                            TALLOC_CTX *mem_ctx,
302                                            const DOM_SID *group,
303                                            uint32 **pp_member_rids,
304                                            size_t *p_num_members);
305
306         NTSTATUS (*pdb_enum_group_memberships)(struct pdb_context *context,
307                                                const char *username,
308                                                gid_t primary_gid,
309                                                DOM_SID **pp_sids, gid_t **pp_gids,
310                                                size_t *p_num_groups);
311
312         NTSTATUS (*pdb_find_alias)(struct pdb_context *context,
313                                    const char *name, DOM_SID *sid);
314
315         NTSTATUS (*pdb_create_alias)(struct pdb_context *context,
316                                      const char *name, uint32 *rid);
317
318         NTSTATUS (*pdb_delete_alias)(struct pdb_context *context,
319                                      const DOM_SID *sid);
320
321         NTSTATUS (*pdb_get_aliasinfo)(struct pdb_context *context,
322                                       const DOM_SID *sid,
323                                       struct acct_info *info);
324
325         NTSTATUS (*pdb_set_aliasinfo)(struct pdb_context *context,
326                                       const DOM_SID *sid,
327                                       struct acct_info *info);
328
329         NTSTATUS (*pdb_add_aliasmem)(struct pdb_context *context,
330                                      const DOM_SID *alias,
331                                      const DOM_SID *member);
332
333         NTSTATUS (*pdb_del_aliasmem)(struct pdb_context *context,
334                                      const DOM_SID *alias,
335                                      const DOM_SID *member);
336
337         NTSTATUS (*pdb_enum_aliasmem)(struct pdb_context *context,
338                                       const DOM_SID *alias,
339                                       DOM_SID **pp_members, size_t *p_num_members);
340
341         NTSTATUS (*pdb_enum_alias_memberships)(struct pdb_context *context,
342                                                TALLOC_CTX *mem_ctx,
343                                                const DOM_SID *domain_sid,
344                                                const DOM_SID *members,
345                                                size_t num_members,
346                                                uint32 **pp_alias_rids,
347                                                size_t *p_num_alias_rids);
348
349         NTSTATUS (*pdb_lookup_rids)(struct pdb_context *context,
350                                     const DOM_SID *domain_sid,
351                                     size_t num_rids,
352                                     uint32 *rids,
353                                     const char **pp_names,
354                                     uint32 *attrs);
355
356         NTSTATUS (*pdb_lookup_names)(struct pdb_context *context,
357                                      const DOM_SID *domain_sid,
358                                      size_t num_names,
359                                      const char **names,
360                                      uint32 *rids,
361                                      uint32 *attrs);
362
363         NTSTATUS (*pdb_get_account_policy)(struct pdb_context *context,
364                                            int policy_index, uint32 *value);
365
366         NTSTATUS (*pdb_set_account_policy)(struct pdb_context *context,
367                                            int policy_index, uint32 value);
368
369         NTSTATUS (*pdb_get_seq_num)(struct pdb_context *context, time_t *seq_num);
370
371         BOOL (*pdb_search_users)(struct pdb_context *context,
372                                  struct pdb_search *search,
373                                  uint16 acct_flags);
374         BOOL (*pdb_search_groups)(struct pdb_context *context,
375                                   struct pdb_search *search);
376         BOOL (*pdb_search_aliases)(struct pdb_context *context,
377                                    struct pdb_search *search,
378                                    const DOM_SID *sid);
379
380         void (*free_fn)(struct pdb_context **);
381         
382         TALLOC_CTX *mem_ctx;
383         
384 } PDB_CONTEXT;
385
386 typedef struct pdb_methods 
387 {
388         const char *name; /* What name got this module */
389         struct pdb_context *parent;
390
391         /* Use macros from dlinklist.h on these two */
392         struct pdb_methods *next;
393         struct pdb_methods *prev;
394
395         NTSTATUS (*setsampwent)(struct pdb_methods *, BOOL update, uint16 acb_mask);
396         
397         void (*endsampwent)(struct pdb_methods *);
398         
399         NTSTATUS (*getsampwent)(struct pdb_methods *, SAM_ACCOUNT *user);
400         
401         NTSTATUS (*getsampwnam)(struct pdb_methods *, SAM_ACCOUNT *sam_acct, const char *username);
402         
403         NTSTATUS (*getsampwsid)(struct pdb_methods *, SAM_ACCOUNT *sam_acct, const DOM_SID *sid);
404         
405         NTSTATUS (*add_sam_account)(struct pdb_methods *, SAM_ACCOUNT *sampass);
406         
407         NTSTATUS (*update_sam_account)(struct pdb_methods *, SAM_ACCOUNT *sampass);
408         
409         NTSTATUS (*delete_sam_account)(struct pdb_methods *, SAM_ACCOUNT *username);
410         
411         NTSTATUS (*rename_sam_account)(struct pdb_methods *, SAM_ACCOUNT *oldname, const char *newname);
412         
413         NTSTATUS (*update_login_attempts)(struct pdb_methods *methods, SAM_ACCOUNT *sam_acct, BOOL success);
414
415         NTSTATUS (*getgrsid)(struct pdb_methods *methods, GROUP_MAP *map, DOM_SID sid);
416
417         NTSTATUS (*getgrgid)(struct pdb_methods *methods, GROUP_MAP *map, gid_t gid);
418
419         NTSTATUS (*getgrnam)(struct pdb_methods *methods, GROUP_MAP *map, const char *name);
420
421         NTSTATUS (*add_group_mapping_entry)(struct pdb_methods *methods,
422                                             GROUP_MAP *map);
423
424         NTSTATUS (*update_group_mapping_entry)(struct pdb_methods *methods,
425                                                GROUP_MAP *map);
426
427         NTSTATUS (*delete_group_mapping_entry)(struct pdb_methods *methods,
428                                                DOM_SID sid);
429
430         NTSTATUS (*enum_group_mapping)(struct pdb_methods *methods,
431                                        enum SID_NAME_USE sid_name_use,
432                                        GROUP_MAP **pp_rmap, size_t *p_num_entries,
433                                        BOOL unix_only);
434
435         NTSTATUS (*enum_group_members)(struct pdb_methods *methods,
436                                        TALLOC_CTX *mem_ctx,
437                                        const DOM_SID *group,
438                                        uint32 **pp_member_rids,
439                                        size_t *p_num_members);
440
441         NTSTATUS (*enum_group_memberships)(struct pdb_methods *methods,
442                                            const char *username,
443                                            gid_t primary_gid,
444                                            DOM_SID **pp_sids, gid_t **pp_gids,
445                                            size_t *p_num_groups);
446
447         NTSTATUS (*find_alias)(struct pdb_methods *methods,
448                                const char *name, DOM_SID *sid);
449
450         NTSTATUS (*create_alias)(struct pdb_methods *methods,
451                                  const char *name, uint32 *rid);
452
453         NTSTATUS (*delete_alias)(struct pdb_methods *methods,
454                                  const DOM_SID *sid);
455
456         NTSTATUS (*get_aliasinfo)(struct pdb_methods *methods,
457                                   const DOM_SID *sid,
458                                   struct acct_info *info);
459
460         NTSTATUS (*set_aliasinfo)(struct pdb_methods *methods,
461                                   const DOM_SID *sid,
462                                   struct acct_info *info);
463
464         NTSTATUS (*add_aliasmem)(struct pdb_methods *methods,
465                                  const DOM_SID *alias, const DOM_SID *member);
466         NTSTATUS (*del_aliasmem)(struct pdb_methods *methods,
467                                  const DOM_SID *alias, const DOM_SID *member);
468         NTSTATUS (*enum_aliasmem)(struct pdb_methods *methods,
469                                   const DOM_SID *alias, DOM_SID **members,
470                                   size_t *p_num_members);
471         NTSTATUS (*enum_alias_memberships)(struct pdb_methods *methods,
472                                            TALLOC_CTX *mem_ctx,
473                                            const DOM_SID *domain_sid,
474                                            const DOM_SID *members,
475                                            size_t num_members,
476                                            uint32 **pp_alias_rids,
477                                            size_t *p_num_alias_rids);
478
479         NTSTATUS (*lookup_rids)(struct pdb_methods *methods,
480                                 const DOM_SID *domain_sid,
481                                 int num_rids,
482                                 uint32 *rids,
483                                 const char **pp_names,
484                                 uint32 *attrs);
485
486         NTSTATUS (*lookup_names)(struct pdb_methods *methods,
487                                  const DOM_SID *domain_sid,
488                                  int num_names,
489                                  const char **pp_names,
490                                  uint32 *rids,
491                                  uint32 *attrs);
492
493         NTSTATUS (*get_account_policy)(struct pdb_methods *methods,
494                                        int policy_index, uint32 *value);
495
496         NTSTATUS (*set_account_policy)(struct pdb_methods *methods,
497                                        int policy_index, uint32 value);
498
499         NTSTATUS (*get_seq_num)(struct pdb_methods *methods, time_t *seq_num);
500
501         BOOL (*search_users)(struct pdb_methods *methods,
502                              struct pdb_search *search,
503                              uint16 acct_flags);
504         BOOL (*search_groups)(struct pdb_methods *methods,
505                               struct pdb_search *search);
506         BOOL (*search_aliases)(struct pdb_methods *methods,
507                                struct pdb_search *search,
508                                const DOM_SID *sid);
509
510         void *private_data;  /* Private data of some kind */
511         
512         void (*free_private_data)(void **);
513
514 } PDB_METHODS;
515
516 typedef NTSTATUS (*pdb_init_function)(struct pdb_context *, 
517                          struct pdb_methods **, 
518                          const char *);
519
520 struct pdb_init_function_entry {
521         const char *name;
522         /* Function to create a member of the pdb_methods list */
523         pdb_init_function init;
524         struct pdb_init_function_entry *prev, *next;
525 };
526
527 enum sql_search_field { SQL_SEARCH_NONE = 0, SQL_SEARCH_USER_SID = 1, SQL_SEARCH_USER_NAME = 2};
528
529 #endif /* _PASSDB_H */