This fixes some bugs for NT4 usrmgr.exe
[abartlet/samba.git/.git] / source3 / rpc_server / srv_samr_util.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SAMR Pipe utility functions.
4    
5    Copyright (C) Luke Kenneth Casson Leighton   1996-1998
6    Copyright (C) Gerald (Jerry) Carter          2000-2001
7    Copyright (C) Andrew Bartlett                2001-2002
8    Copyright (C) Stefan (metze) Metzmacher      2002
9       
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_RPC_SRV
29
30 #define STRING_CHANGED (old_string && !new_string) ||\
31                     (!old_string && new_string) ||\
32                 (old_string && new_string && (strcmp(old_string, new_string) != 0))
33
34 /*************************************************************
35  Copies a SAM_USER_INFO_21 to a SAM_ACCOUNT
36 **************************************************************/
37
38 void copy_id21_to_sam_passwd(SAM_ACCOUNT *to, SAM_USER_INFO_21 *from)
39 {
40         time_t unix_time, stored_time;
41         const char *old_string, *new_string;
42
43         if (from == NULL || to == NULL) 
44                 return;
45         if (!nt_time_is_zero(&from->logon_time)) {
46                 unix_time=nt_time_to_unix(&from->logon_time);
47                 stored_time = pdb_get_logon_time(to);
48                 DEBUG(10,("INFO_21 LOGON_TIME: %lu -> %lu\n",(long unsigned int)stored_time, (long unsigned int)unix_time));
49                 if (stored_time != unix_time) 
50                         pdb_set_logon_time(to, unix_time, PDB_CHANGED);
51         }       
52         if (!nt_time_is_zero(&from->logoff_time)) {
53                 unix_time=nt_time_to_unix(&from->logoff_time);
54                 stored_time = pdb_get_logoff_time(to);
55                 DEBUG(10,("INFO_21 LOGOFF_TIME: %lu -> %lu\n",(long unsigned int)stored_time, (long unsigned int)unix_time));
56                 if (stored_time != unix_time) 
57                         pdb_set_logoff_time(to, unix_time, PDB_CHANGED);
58         }
59         
60         if (!nt_time_is_zero(&from->kickoff_time)) {
61                 unix_time=nt_time_to_unix(&from->kickoff_time);
62                 stored_time = pdb_get_kickoff_time(to);
63                 DEBUG(10,("INFO_21 KICKOFF_TIME: %lu -> %lu\n",(long unsigned int)stored_time, (long unsigned int)unix_time));
64                 if (stored_time != unix_time) 
65                         pdb_set_kickoff_time(to, unix_time , PDB_CHANGED);
66         }       
67
68         if (!nt_time_is_zero(&from->pass_can_change_time)) {
69                 unix_time=nt_time_to_unix(&from->pass_can_change_time);
70                 stored_time = pdb_get_pass_can_change_time(to);
71                 DEBUG(10,("INFO_21 PASS_CAN_CH: %lu -> %lu\n",(long unsigned int)stored_time, (long unsigned int)unix_time));
72                 if (stored_time != unix_time) 
73                         pdb_set_pass_can_change_time(to, unix_time, PDB_CHANGED);
74         }
75         if (!nt_time_is_zero(&from->pass_last_set_time)) {
76                 unix_time=nt_time_to_unix(&from->pass_last_set_time);
77                 stored_time = pdb_get_pass_last_set_time(to);
78                 DEBUG(10,("INFO_21 PASS_LAST_SET: %lu -> %lu\n",(long unsigned int)stored_time, (long unsigned int)unix_time));
79                 if (stored_time != unix_time) 
80                         pdb_set_pass_last_set_time(to, unix_time, PDB_CHANGED);
81         }
82
83         if (!nt_time_is_zero(&from->pass_must_change_time)) {
84                 unix_time=nt_time_to_unix(&from->pass_must_change_time);
85                 stored_time=pdb_get_pass_must_change_time(to);
86                 DEBUG(10,("INFO_21 PASS_MUST_CH: %lu -> %lu\n",(long unsigned int)stored_time, (long unsigned int)unix_time));
87                 if (stored_time != unix_time) 
88                         pdb_set_pass_must_change_time(to, unix_time, PDB_CHANGED);
89         }
90
91         /* Backend should check this for sainity */
92         if (from->hdr_user_name.buffer) {
93                 old_string = pdb_get_username(to);
94                 new_string = pdb_unistr2_convert(&from->uni_user_name);
95                 DEBUG(10,("INFO_21 UNI_USER_NAME: %s -> %s\n", old_string, new_string));
96                 if (STRING_CHANGED)
97                     pdb_set_username(to      , new_string, PDB_CHANGED);
98         }
99
100         if (from->hdr_full_name.buffer) {
101                 old_string = pdb_get_fullname(to);
102                 new_string = pdb_unistr2_convert(&from->uni_full_name);
103                 DEBUG(10,("INFO_21 UNI_FULL_NAME: %s -> %s\n",old_string, new_string));
104                 if (STRING_CHANGED)
105                         pdb_set_fullname(to      , new_string, PDB_CHANGED);
106         }
107         
108         if (from->hdr_home_dir.buffer) {
109                 old_string = pdb_get_homedir(to);
110                 new_string = pdb_unistr2_convert(&from->uni_home_dir);
111                 DEBUG(10,("INFO_21 UNI_HOME_DIR: %s -> %s\n",old_string,new_string));
112                 if (STRING_CHANGED)
113                         pdb_set_homedir(to       , new_string, PDB_CHANGED);
114         }
115
116         if (from->hdr_dir_drive.buffer) {
117                 old_string = pdb_get_dir_drive(to);
118                 new_string = pdb_unistr2_convert(&from->uni_dir_drive);
119                 DEBUG(10,("INFO_21 UNI_DIR_DRIVE: %s -> %s\n",old_string,new_string));
120                 if (STRING_CHANGED)
121                         pdb_set_dir_drive(to     , new_string, PDB_CHANGED);
122         }
123
124         if (from->hdr_logon_script.buffer) {
125                 old_string = pdb_get_logon_script(to);
126                 new_string = pdb_unistr2_convert(&from->uni_logon_script);
127                 DEBUG(10,("INFO_21 UNI_LOGON_SCRIPT: %s -> %s\n",old_string,new_string));
128                 if (STRING_CHANGED)
129                         pdb_set_logon_script(to  , new_string, PDB_CHANGED);
130         }
131
132         if (from->hdr_profile_path.buffer) {
133                 old_string = pdb_get_profile_path(to);
134                 new_string = pdb_unistr2_convert(&from->uni_profile_path);
135                 DEBUG(10,("INFO_21 UNI_PROFILE_PATH: %s -> %s\n",old_string, new_string));
136                 if (STRING_CHANGED)
137                         pdb_set_profile_path(to  , new_string, PDB_CHANGED);
138         }
139         
140         if (from->hdr_acct_desc.buffer) {
141                 old_string = pdb_get_acct_desc(to);
142                 new_string = pdb_unistr2_convert(&from->uni_acct_desc);
143                 DEBUG(10,("INFO_21 UNI_ACCT_DESC: %s -> %s\n",old_string,new_string));
144                 if (STRING_CHANGED)
145                         pdb_set_acct_desc(to     , new_string, PDB_CHANGED);
146         }
147         
148         if (from->hdr_workstations.buffer) {
149                 old_string = pdb_get_workstations(to);
150                 new_string = pdb_unistr2_convert(&from->uni_workstations);
151                 DEBUG(10,("INFO_21 UNI_WORKSTATIONS: %s -> %s\n",old_string, new_string));
152                 if (STRING_CHANGED)
153                         pdb_set_workstations(to  , new_string, PDB_CHANGED);
154         }
155
156         if (from->hdr_unknown_str.buffer) {
157                 old_string = pdb_get_unknown_str(to);
158                 new_string = pdb_unistr2_convert(&from->uni_unknown_str);
159                 DEBUG(10,("INFO_21 UNI_UNKNOWN_STR: %s -> %s\n",old_string, new_string));
160                 if (STRING_CHANGED)
161                         pdb_set_unknown_str(to   , new_string, PDB_CHANGED);
162         }
163         
164         if (from->hdr_munged_dial.buffer) {
165                 old_string = pdb_get_munged_dial(to);
166                 new_string = pdb_unistr2_convert(&from->uni_munged_dial);
167                 DEBUG(10,("INFO_21 UNI_MUNGED_DIAL: %s -> %s\n",old_string, new_string));
168                 if (STRING_CHANGED)
169                         pdb_set_munged_dial(to   , new_string, PDB_CHANGED);
170         }
171         
172         if (from->user_rid != pdb_get_user_rid(to)) {
173                 DEBUG(10,("INFO_21 USER_RID: %u -> %u NOT UPDATED!\n",pdb_get_user_rid(to),from->user_rid));
174                 /* we really allow this ??? metze */
175                 /* pdb_set_user_sid_from_rid(to, from->user_rid, PDB_CHANGED);*/
176         }
177         
178         if (from->group_rid != pdb_get_group_rid(to)) {
179                 DEBUG(10,("INFO_21 GROUP_RID: %u -> %u\n",pdb_get_group_rid(to),from->group_rid));
180                 pdb_set_group_sid_from_rid(to, from->group_rid, PDB_CHANGED);
181         }
182         
183         DEBUG(10,("INFO_21 ACCT_CTRL: %08X -> %08X\n",pdb_get_acct_ctrl(to),from->acb_info));
184         if (from->acb_info != pdb_get_acct_ctrl(to)) {
185                 pdb_set_acct_ctrl(to, from->acb_info, PDB_CHANGED);
186         }
187
188         DEBUG(10,("INFO_21 UNKOWN_3: %08X -> %08X\n",pdb_get_unknown_3(to),from->unknown_3));
189         if (from->unknown_3 != pdb_get_unknown_3(to)) {
190                 pdb_set_unknown_3(to, from->unknown_3, PDB_CHANGED);
191         }
192
193         DEBUG(15,("INFO_21 LOGON_DIVS: %08X -> %08X\n",pdb_get_logon_divs(to),from->logon_divs));
194         if (from->logon_divs != pdb_get_logon_divs(to)) {
195                 pdb_set_logon_divs(to, from->logon_divs, PDB_CHANGED);
196         }
197
198         DEBUG(15,("INFO_21 LOGON_HRS.LEN: %08X -> %08X\n",pdb_get_hours_len(to),from->logon_hrs.len));
199         if (from->logon_hrs.len != pdb_get_hours_len(to)) {
200                 pdb_set_hours_len(to, from->logon_hrs.len, PDB_CHANGED);
201         }
202
203         DEBUG(15,("INFO_21 LOGON_HRS.HOURS: %s -> %s\n",pdb_get_hours(to),from->logon_hrs.hours));
204 /* Fix me: only update if it changes --metze */
205         pdb_set_hours(to, from->logon_hrs.hours, PDB_CHANGED);
206
207         DEBUG(10,("INFO_21 UNKOWN_5: %08X -> %08X\n",pdb_get_unknown_5(to),from->unknown_5));
208         if (from->unknown_5 != pdb_get_unknown_5(to)) {
209                 pdb_set_unknown_5(to, from->unknown_5, PDB_CHANGED);
210         }
211
212         DEBUG(10,("INFO_21 UNKOWN_6: %08X -> %08X\n",pdb_get_unknown_6(to),from->unknown_6));
213         if (from->unknown_6 != pdb_get_unknown_6(to)) {
214                 pdb_set_unknown_6(to, from->unknown_6, PDB_CHANGED);
215         }
216
217         DEBUG(10,("INFO_21 PADDING1 %02X %02X %02X %02X %02X %02X\n",
218                   from->padding1[0],
219                   from->padding1[1],
220                   from->padding1[2],
221                   from->padding1[3],
222                   from->padding1[4],
223                   from->padding1[5]));
224
225         DEBUG(10,("INFO_21 PASS_MUST_CHANGE_AT_NEXT_LOGON: %02X\n",from->passmustchange));
226         if (from->passmustchange==PASS_MUST_CHANGE_AT_NEXT_LOGON) {
227                 pdb_set_pass_must_change_time(to,0, PDB_CHANGED);               
228         }
229
230         DEBUG(10,("INFO_21 PADDING_2: %02X\n",from->padding2));
231
232         DEBUG(10,("INFO_21 PADDING_4: %08X\n",from->padding4));
233 }
234
235
236 /*************************************************************
237  Copies a SAM_USER_INFO_23 to a SAM_ACCOUNT
238 **************************************************************/
239
240 void copy_id23_to_sam_passwd(SAM_ACCOUNT *to, SAM_USER_INFO_23 *from)
241 {
242         time_t unix_time, stored_time;
243         const char *old_string, *new_string;
244
245         if (from == NULL || to == NULL) 
246                 return;
247         if (!nt_time_is_zero(&from->logon_time)) {
248                 unix_time=nt_time_to_unix(&from->logon_time);
249                 stored_time = pdb_get_logon_time(to);
250                 DEBUG(10,("INFO_23 LOGON_TIME: %lu -> %lu\n",(long unsigned int)stored_time, (long unsigned int)unix_time));
251                 if (stored_time != unix_time) 
252                         pdb_set_logon_time(to, unix_time, PDB_CHANGED);
253         }       
254         if (!nt_time_is_zero(&from->logoff_time)) {
255                 unix_time=nt_time_to_unix(&from->logoff_time);
256                 stored_time = pdb_get_logoff_time(to);
257                 DEBUG(10,("INFO_23 LOGOFF_TIME: %lu -> %lu\n",(long unsigned int)stored_time, (long unsigned int)unix_time));
258                 if (stored_time != unix_time) 
259                         pdb_set_logoff_time(to, unix_time, PDB_CHANGED);
260         }
261         
262         if (!nt_time_is_zero(&from->kickoff_time)) {
263                 unix_time=nt_time_to_unix(&from->kickoff_time);
264                 stored_time = pdb_get_kickoff_time(to);
265                 DEBUG(10,("INFO_23 KICKOFF_TIME: %lu -> %lu\n",(long unsigned int)stored_time, (long unsigned int)unix_time));
266                 if (stored_time != unix_time) 
267                         pdb_set_kickoff_time(to, unix_time , PDB_CHANGED);
268         }       
269
270         if (!nt_time_is_zero(&from->pass_can_change_time)) {
271                 unix_time=nt_time_to_unix(&from->pass_can_change_time);
272                 stored_time = pdb_get_pass_can_change_time(to);
273                 DEBUG(10,("INFO_23 PASS_CAN_CH: %lu -> %lu\n",(long unsigned int)stored_time, (long unsigned int)unix_time));
274                 if (stored_time != unix_time) 
275                         pdb_set_pass_can_change_time(to, unix_time, PDB_CHANGED);
276         }
277         if (!nt_time_is_zero(&from->pass_last_set_time)) {
278                 unix_time=nt_time_to_unix(&from->pass_last_set_time);
279                 stored_time = pdb_get_pass_last_set_time(to);
280                 DEBUG(10,("INFO_23 PASS_LAST_SET: %lu -> %lu\n",(long unsigned int)stored_time, (long unsigned int)unix_time));
281                 if (stored_time != unix_time) 
282                         pdb_set_pass_last_set_time(to, unix_time, PDB_CHANGED);
283         }
284
285         if (!nt_time_is_zero(&from->pass_must_change_time)) {
286                 unix_time=nt_time_to_unix(&from->pass_must_change_time);
287                 stored_time=pdb_get_pass_must_change_time(to);
288                 DEBUG(10,("INFO_23 PASS_MUST_CH: %lu -> %lu\n",(long unsigned int)stored_time, (long unsigned int)unix_time));
289                 if (stored_time != unix_time) 
290                         pdb_set_pass_must_change_time(to, unix_time, PDB_CHANGED);
291         }
292
293         /* Backend should check this for sainity */
294         if (from->hdr_user_name.buffer) {
295                 old_string = pdb_get_username(to);
296                 new_string = pdb_unistr2_convert(&from->uni_user_name);
297                 DEBUG(10,("INFO_23 UNI_USER_NAME: %s -> %s\n", old_string, new_string));
298                 if (STRING_CHANGED)
299                     pdb_set_username(to      , new_string, PDB_CHANGED);
300         }
301
302         if (from->hdr_full_name.buffer) {
303                 old_string = pdb_get_fullname(to);
304                 new_string = pdb_unistr2_convert(&from->uni_user_name);
305                 DEBUG(10,("INFO_23 UNI_FULL_NAME: %s -> %s\n",old_string, new_string));
306                 if (STRING_CHANGED)
307                         pdb_set_fullname(to      , new_string, PDB_CHANGED);
308         }
309         
310         if (from->hdr_home_dir.buffer) {
311                 old_string = pdb_get_homedir(to);
312                 new_string = pdb_unistr2_convert(&from->uni_home_dir);
313                 DEBUG(10,("INFO_23 UNI_HOME_DIR: %s -> %s\n",old_string,new_string));
314                 if (STRING_CHANGED)
315                         pdb_set_homedir(to       , new_string, PDB_CHANGED);
316         }
317
318         if (from->hdr_dir_drive.buffer) {
319                 old_string = pdb_get_dir_drive(to);
320                 new_string = pdb_unistr2_convert(&from->uni_dir_drive);
321                 DEBUG(10,("INFO_23 UNI_DIR_DRIVE: %s -> %s\n",old_string,new_string));
322                 if (STRING_CHANGED)
323                         pdb_set_dir_drive(to     , new_string, PDB_CHANGED);
324         }
325
326         if (from->hdr_logon_script.buffer) {
327                 old_string = pdb_get_logon_script(to);
328                 new_string = pdb_unistr2_convert(&from->uni_logon_script);
329                 DEBUG(10,("INFO_23 UNI_LOGON_SCRIPT: %s -> %s\n",old_string,new_string));
330                 if (STRING_CHANGED)
331                         pdb_set_logon_script(to  , new_string, PDB_CHANGED);
332         }
333
334         if (from->hdr_profile_path.buffer) {
335                 old_string = pdb_get_profile_path(to);
336                 new_string = pdb_unistr2_convert(&from->uni_profile_path);
337                 DEBUG(10,("INFO_23 UNI_PROFILE_PATH: %s -> %s\n",old_string, new_string));
338                 if (STRING_CHANGED)
339                         pdb_set_profile_path(to  , new_string, PDB_CHANGED);
340         }
341         
342         if (from->hdr_acct_desc.buffer) {
343                 old_string = pdb_get_acct_desc(to);
344                 new_string = pdb_unistr2_convert(&from->uni_acct_desc);
345                 DEBUG(10,("INFO_23 UNI_ACCT_DESC: %s -> %s\n",old_string,new_string));
346                 if (STRING_CHANGED)
347                         pdb_set_acct_desc(to     , new_string, PDB_CHANGED);
348         }
349         
350         if (from->hdr_workstations.buffer) {
351                 old_string = pdb_get_workstations(to);
352                 new_string = pdb_unistr2_convert(&from->uni_workstations);
353                 DEBUG(10,("INFO_23 UNI_WORKSTATIONS: %s -> %s\n",old_string, new_string));
354                 if (STRING_CHANGED)
355                         pdb_set_workstations(to  , new_string, PDB_CHANGED);
356         }
357
358         if (from->hdr_unknown_str.buffer) {
359                 old_string = pdb_get_unknown_str(to);
360                 new_string = pdb_unistr2_convert(&from->uni_unknown_str);
361                 DEBUG(10,("INFO_23 UNI_UNKNOWN_STR: %s -> %s\n",old_string, new_string));
362                 if (STRING_CHANGED)
363                         pdb_set_unknown_str(to   , new_string, PDB_CHANGED);
364         }
365         
366         if (from->hdr_munged_dial.buffer) {
367                 old_string = pdb_get_munged_dial(to);
368                 new_string = pdb_unistr2_convert(&from->uni_munged_dial);
369                 DEBUG(10,("INFO_23 UNI_MUNGED_DIAL: %s -> %s\n",old_string, new_string));
370                 if (STRING_CHANGED)
371                         pdb_set_munged_dial(to   , new_string, PDB_CHANGED);
372         }
373         
374         if (from->user_rid != pdb_get_user_rid(to)) {
375                 DEBUG(10,("INFO_23 USER_RID: %u -> %u NOT UPDATED!\n",pdb_get_user_rid(to),from->user_rid));
376                 /* we really allow this ??? metze */
377                 /* pdb_set_user_sid_from_rid(to, from->user_rid, PDB_CHANGED);*/
378         }
379         
380         if (from->group_rid != pdb_get_group_rid(to)) {
381                 DEBUG(10,("INFO_23 GROUP_RID: %u -> %u\n",pdb_get_group_rid(to),from->group_rid));
382                 pdb_set_group_sid_from_rid(to, from->group_rid, PDB_CHANGED);
383         }
384         
385         DEBUG(10,("INFO_23 ACCT_CTRL: %08X -> %08X\n",pdb_get_acct_ctrl(to),from->acb_info));
386         if (from->acb_info != pdb_get_acct_ctrl(to)) {
387                 pdb_set_acct_ctrl(to, from->acb_info, PDB_CHANGED);
388         }
389
390         DEBUG(10,("INFO_23 UNKOWN_3: %08X -> %08X\n",pdb_get_unknown_3(to),from->unknown_3));
391         if (from->unknown_3 != pdb_get_unknown_3(to)) {
392                 pdb_set_unknown_3(to, from->unknown_3, PDB_CHANGED);
393         }
394
395         DEBUG(15,("INFO_23 LOGON_DIVS: %08X -> %08X\n",pdb_get_logon_divs(to),from->logon_divs));
396         if (from->logon_divs != pdb_get_logon_divs(to)) {
397                 pdb_set_logon_divs(to, from->logon_divs, PDB_CHANGED);
398         }
399
400         DEBUG(15,("INFO_23 LOGON_HRS.LEN: %08X -> %08X\n",pdb_get_hours_len(to),from->logon_hrs.len));
401         if (from->logon_hrs.len != pdb_get_hours_len(to)) {
402                 pdb_set_hours_len(to, from->logon_hrs.len, PDB_CHANGED);
403         }
404
405         DEBUG(15,("INFO_23 LOGON_HRS.HOURS: %s -> %s\n",pdb_get_hours(to),from->logon_hrs.hours));
406 /* Fix me: only update if it changes --metze */
407         pdb_set_hours(to, from->logon_hrs.hours, PDB_CHANGED);
408
409         DEBUG(10,("INFO_23 UNKOWN_5: %08X -> %08X\n",pdb_get_unknown_5(to),from->unknown_5));
410         if (from->unknown_5 != pdb_get_unknown_5(to)) {
411                 pdb_set_unknown_5(to, from->unknown_5, PDB_CHANGED);
412         }
413
414         DEBUG(10,("INFO_23 UNKOWN_6: %08X -> %08X\n",pdb_get_unknown_6(to),from->unknown_6));
415         if (from->unknown_6 != pdb_get_unknown_6(to)) {
416                 pdb_set_unknown_6(to, from->unknown_6, PDB_CHANGED);
417         }
418
419         DEBUG(10,("INFO_23 PADDING1 %02X %02X %02X %02X %02X %02X\n",
420                   from->padding1[0],
421                   from->padding1[1],
422                   from->padding1[2],
423                   from->padding1[3],
424                   from->padding1[4],
425                   from->padding1[5]));
426
427         DEBUG(10,("INFO_23 PASS_MUST_CHANGE_AT_NEXT_LOGON: %02X\n",from->passmustchange));
428         if (from->passmustchange==PASS_MUST_CHANGE_AT_NEXT_LOGON) {
429                 pdb_set_pass_must_change_time(to,0, PDB_CHANGED);               
430         }
431
432         DEBUG(10,("INFO_23 PADDING_2: %02X\n",from->padding2));
433
434         DEBUG(10,("INFO_23 PADDING_4: %08X\n",from->padding4));
435 }
436
437