r23792: convert Samba4 to GPLv3
[kai/samba-autobuild/.git] / source4 / lib / samba3 / tdbsam.c
1 /* 
2    Unix SMB/CIFS implementation.
3    tdb passdb backend format routines
4
5         Copyright (C) Simo Sorce        2000-2003
6     Copyright (C) Jelmer Vernooij       2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "lib/tdb/include/tdb.h"
25 #include "lib/util/util_tdb.h"
26 #include "lib/samba3/samba3.h"
27
28 #define TDB_FORMAT_STRING_V0       "ddddddBBBBBBBBBBBBddBBwdwdBwwd"
29 #define TDB_FORMAT_STRING_V1       "dddddddBBBBBBBBBBBBddBBwdwdBwwd"
30 #define TDB_FORMAT_STRING_V2       "dddddddBBBBBBBBBBBBddBBBwwdBwwd"
31 #define TDBSAM_VERSION_STRING      "INFO/version"
32
33 static BOOL init_sam_from_buffer_v0(TDB_CONTEXT *tdb, struct samba3_samaccount *sampass, TDB_DATA buf)
34 {
35         uint32_t        username_len, domain_len, nt_username_len,
36                 dir_drive_len, unknown_str_len, munged_dial_len,
37                 fullname_len, homedir_len, logon_script_len,
38                 profile_path_len, acct_desc_len, workstations_len;
39                 
40         uint32_t        remove_me;
41         uint32_t                len = 0;
42         uint32_t                lm_pw_len, nt_pw_len, hourslen;
43         
44         if(sampass == NULL || buf.dptr == NULL) {
45                 DEBUG(0, ("init_sam_from_buffer_v0: NULL parameters found!\n"));
46                 return False;
47         }
48
49         /* unpack the buffer into variables */
50         len = tdb_unpack (tdb, (char *)buf.dptr, buf.dsize, TDB_FORMAT_STRING_V0,
51                 &sampass->logon_time,                                   /* d */
52                 &sampass->logoff_time,                                  /* d */
53                 &sampass->kickoff_time,                                 /* d */
54                 &sampass->pass_last_set_time,                           /* d */
55                 &sampass->pass_can_change_time,                         /* d */
56                 &sampass->pass_must_change_time,                        /* d */
57                 &username_len, &sampass->username,                      /* B */
58                 &domain_len, &sampass->domain,                          /* B */
59                 &nt_username_len, &sampass->nt_username,                /* B */
60                 &fullname_len, &sampass->fullname,                      /* B */
61                 &homedir_len, &sampass->homedir,                        /* B */
62                 &dir_drive_len, &sampass->dir_drive,                    /* B */
63                 &logon_script_len, &sampass->logon_script,              /* B */
64                 &profile_path_len, &sampass->profile_path,              /* B */
65                 &acct_desc_len, &sampass->acct_desc,                    /* B */
66                 &workstations_len, &sampass->workstations,              /* B */
67                 &unknown_str_len, &sampass->unknown_str,                /* B */
68                 &munged_dial_len, &sampass->munged_dial,                /* B */
69                 &sampass->user_rid,                                     /* d */
70                 &sampass->group_rid,                                    /* d */
71                 &lm_pw_len, sampass->lm_pw.hash,                        /* B */
72                 &nt_pw_len, sampass->nt_pw.hash,                        /* B */
73                 &sampass->acct_ctrl,                                    /* w */
74                 &remove_me, /* remove on the next TDB_FORMAT upgarde */ /* d */
75                 &sampass->logon_divs,                                   /* w */
76                 &sampass->hours_len,                                    /* d */
77                 &hourslen, &sampass->hours,                             /* B */
78                 &sampass->bad_password_count,                           /* w */
79                 &sampass->logon_count,                                  /* w */
80                 &sampass->unknown_6);                                   /* d */
81                 
82         if (len == (uint32_t) -1)  {
83                 return False;
84         }
85
86         return True;
87 }
88
89 static BOOL init_sam_from_buffer_v1(TDB_CONTEXT *tdb, struct samba3_samaccount *sampass, TDB_DATA buf)
90 {
91         uint32_t        username_len, domain_len, nt_username_len,
92                 dir_drive_len, unknown_str_len, munged_dial_len,
93                 fullname_len, homedir_len, logon_script_len,
94                 profile_path_len, acct_desc_len, workstations_len;
95                 
96         uint32_t        remove_me;
97         uint32_t                len = 0;
98         uint32_t                lm_pw_len, nt_pw_len, hourslen;
99         
100         if(sampass == NULL || buf.dptr == NULL) {
101                 DEBUG(0, ("init_sam_from_buffer_v1: NULL parameters found!\n"));
102                 return False;
103         }
104
105         /* unpack the buffer into variables */
106         len = tdb_unpack (tdb, (char *)buf.dptr, buf.dsize, TDB_FORMAT_STRING_V1,
107                 &sampass->logon_time,                                   /* d */
108                 &sampass->logoff_time,                                  /* d */
109                 &sampass->kickoff_time,                         /* d */
110                 /* Change from V0 is addition of bad_password_time field. */
111                 &sampass->bad_password_time,                            /* d */
112                 &sampass->pass_last_set_time,                           /* d */
113                 &sampass->pass_can_change_time,                 /* d */
114                 &sampass->pass_must_change_time,                        /* d */
115                 &username_len, &sampass->username,                      /* B */
116                 &domain_len, &sampass->domain,          /* B */
117                 &nt_username_len, &sampass->nt_username,        /* B */
118                 &fullname_len, &sampass->fullname,                      /* B */
119                 &homedir_len, &sampass->homedir,                        /* B */
120                 &dir_drive_len, &sampass->dir_drive,                    /* B */
121                 &logon_script_len, &sampass->logon_script,              /* B */
122                 &profile_path_len, &sampass->profile_path,              /* B */
123                 &acct_desc_len, &sampass->acct_desc,                    /* B */
124                 &workstations_len, &sampass->workstations,              /* B */
125                 &unknown_str_len, &sampass->unknown_str,                /* B */
126                 &munged_dial_len, &sampass->munged_dial,                /* B */
127                 &sampass->user_rid,                                     /* d */
128                 &sampass->group_rid,                                    /* d */
129                 &lm_pw_len, sampass->lm_pw.hash,                        /* B */
130                 &nt_pw_len, sampass->nt_pw.hash,                        /* B */
131                 &sampass->acct_ctrl,                                    /* w */
132                 &remove_me,                                             /* d */
133                 &sampass->logon_divs,                                   /* w */
134                 &sampass->hours_len,                                    /* d */
135                 &hourslen, &sampass->hours,                             /* B */
136                 &sampass->bad_password_count,                           /* w */
137                 &sampass->logon_count,                                  /* w */
138                 &sampass->unknown_6);                                   /* d */
139                 
140         if (len == (uint32_t) -1)  {
141                 return False;
142         }
143
144         return True;
145 }
146
147 static BOOL init_sam_from_buffer_v2(TDB_CONTEXT *tdb, struct samba3_samaccount *sampass, TDB_DATA buf)
148 {
149         uint32_t        username_len, domain_len, nt_username_len,
150                 dir_drive_len, unknown_str_len, munged_dial_len,
151                 fullname_len, homedir_len, logon_script_len,
152                 profile_path_len, acct_desc_len, workstations_len;
153                 
154         uint32_t                len = 0;
155         uint32_t                lm_pw_len, nt_pw_len, nt_pw_hist_len, hourslen;
156         
157         if(sampass == NULL || buf.dptr == NULL) {
158                 DEBUG(0, ("init_sam_from_buffer_v2: NULL parameters found!\n"));
159                 return False;
160         }
161
162         /* unpack the buffer into variables */
163         len = tdb_unpack (tdb, (char *)buf.dptr, buf.dsize, TDB_FORMAT_STRING_V2,
164                 &sampass->logon_time,                                   /* d */
165                 &sampass->logoff_time,                                  /* d */
166                 &sampass->kickoff_time,                                 /* d */
167                 &sampass->bad_password_time,                            /* d */
168                 &sampass->pass_last_set_time,                           /* d */
169                 &sampass->pass_can_change_time,                         /* d */
170                 &sampass->pass_must_change_time,                        /* d */
171                 &username_len, &sampass->username,                      /* B */
172                 &domain_len, &sampass->domain,                          /* B */
173                 &nt_username_len, &sampass->nt_username,                /* B */
174                 &fullname_len, &sampass->fullname,                      /* B */
175                 &homedir_len, &sampass->homedir,                        /* B */
176                 &dir_drive_len, &sampass->dir_drive,                    /* B */
177                 &logon_script_len, &sampass->logon_script,              /* B */
178                 &profile_path_len, &sampass->profile_path,              /* B */
179                 &acct_desc_len, &sampass->acct_desc,                    /* B */
180                 &workstations_len, &sampass->workstations,              /* B */
181                 &unknown_str_len, &sampass->unknown_str,                /* B */
182                 &munged_dial_len, &sampass->munged_dial,                /* B */
183                 &sampass->user_rid,                                     /* d */
184                 &sampass->group_rid,                                    /* d */
185                 &lm_pw_len, sampass->lm_pw.hash,                        /* B */
186                 &nt_pw_len, sampass->nt_pw.hash,                        /* B */
187                 /* Change from V1 is addition of password history field. */
188                 &nt_pw_hist_len, &sampass->nt_pw_hist_ptr,              /* B */
189                 &sampass->acct_ctrl,                                    /* w */
190                 /* Also "remove_me" field was removed. */
191                 &sampass->logon_divs,                                   /* w */
192                 &sampass->hours_len,                                    /* d */
193                 &hourslen, &sampass->hours,                             /* B */
194                 &sampass->bad_password_count,                           /* w */
195                 &sampass->logon_count,                                  /* w */
196                 &sampass->unknown_6);                                   /* d */
197                 
198         if (len == (uint32_t) -1)  {
199                 return False;
200         }
201
202         return True;
203 }
204
205 NTSTATUS samba3_read_tdbsam(const char *filename, TALLOC_CTX *ctx, struct samba3_samaccount **accounts, uint32_t *count)
206 {
207         int32_t version;
208         TDB_CONTEXT *tdb;
209         TDB_DATA key, val;
210
211         /* Try to open tdb passwd */
212         if (!(tdb = tdb_open(filename, 0, TDB_DEFAULT, O_RDONLY, 0600))) {
213                 DEBUG(0, ("Unable to open TDB passwd file '%s'\n", filename));
214                 return NT_STATUS_UNSUCCESSFUL;
215         }
216
217         /* Check the version */
218         version = tdb_fetch_int32(tdb, 
219                                                 TDBSAM_VERSION_STRING);
220         if (version == -1)
221                 version = 0;    /* Version not found, assume version 0 */
222         
223         /* Compare the version */
224         if (version > 2) {
225                 /* Version more recent than the latest known */ 
226                 DEBUG(0, ("TDBSAM version unknown: %d\n", version));
227                 tdb_close(tdb);
228                 return NT_STATUS_NOT_SUPPORTED;
229         } 
230         
231         *accounts = NULL;
232         *count = 0;
233
234         for (key = tdb_firstkey(tdb); key.dptr; key = tdb_nextkey(tdb, key))
235         {
236                 BOOL ret;
237                 if (strncmp((const char *)key.dptr, "USER_", 5) != 0) 
238                         continue;
239
240                 val = tdb_fetch(tdb, key);
241
242                 *accounts = talloc_realloc(ctx, *accounts, struct samba3_samaccount, (*count)+1);
243
244                 switch (version) 
245                 {
246                         case 0: ret = init_sam_from_buffer_v0(tdb, &(*accounts)[*count], val); break;
247                         case 1: ret = init_sam_from_buffer_v1(tdb, &(*accounts)[*count], val); break;
248                         case 2: ret = init_sam_from_buffer_v2(tdb, &(*accounts)[*count], val); break;
249                         default: ret = False; break;
250
251                 }
252
253                 if (!ret) {
254                         DEBUG(0, ("Unable to parse SAM account %s\n", key.dptr));
255                 }
256
257                 (*count)++;
258         }
259         
260         tdb_close(tdb);
261         
262         return NT_STATUS_OK;
263 }