previous commit added an abstraction function that didn't even have
[ira/wip.git] / source3 / rpc_client / cli_netlogon_sync.c
1 /* 
2  *  Unix SMB/Netbios implementation.
3  *  Version 1.9.
4  *  RPC Pipe client / server routines
5  *  Copyright (C) Andrew Tridgell              1992-1999,
6  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1999,
7  *  Copyright (C) Matthew Chapman                   1999,
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
25 #ifdef SYSLOG
26 #undef SYSLOG
27 #endif
28
29 #include "includes.h"
30
31 extern int DEBUGLEVEL;
32 extern pstring global_myname;
33
34 BOOL synchronise_passdb(void)
35 {
36         struct cli_state cli;
37         SAM_DELTA_HDR hdr_deltas[MAX_SAM_DELTAS];
38         SAM_DELTA_CTR deltas[MAX_SAM_DELTAS];
39         uint32 num;
40
41         SAM_ACCOUNT_INFO *acc;
42         struct smb_passwd pwd;
43         fstring nt_name;
44         unsigned char smb_passwd[16];
45         unsigned char smb_nt_passwd[16];
46         uchar trust_passwd[16];
47
48         char *mode;
49         BOOL success;
50         BOOL ret;
51         int i;
52
53         if (!cli_connect_serverlist(&cli, lp_passwordserver()))
54         {
55                 return False;
56         }
57
58         if (!trust_get_passwd(trust_passwd, lp_workgroup(), global_myname))
59         {
60                 return False;
61         }
62
63         ret = do_sam_sync(&cli, trust_passwd, cli.mach_acct, global_myname,
64                           hdr_deltas, deltas, &num);
65
66         if (ret)
67         {
68                 for (i = 0; i < num; i++)
69                 {
70                         /* Currently only interested in accounts */
71                         if (hdr_deltas[i].type != 5)
72                         {
73                                 continue;
74                         }
75
76                         acc = &deltas[i].account_info;
77                         pwdb_init_smb(&pwd);
78
79                         pwd.user_rid = acc->user_rid;
80                         unistr2_to_ascii(nt_name, &(acc->uni_acct_name), sizeof(fstring)-1);
81                         pwd.nt_name = nt_name;
82                         pwd.acct_ctrl = acc->acb_info;
83                         pwd.pass_last_set_time = nt_time_to_unix(&(acc->pwd_last_set_time));
84                         
85                         sam_pwd_hash(acc->user_rid, smb_passwd, acc->pass.buf_lm_pwd, 0);
86                         sam_pwd_hash(acc->user_rid, smb_nt_passwd, acc->pass.buf_nt_pwd, 0);
87                         pwd.smb_passwd = smb_passwd;
88                         pwd.smb_nt_passwd = smb_nt_passwd;
89
90                         mode = "modify";
91                         success = mod_smbpwd_entry(&pwd, True);
92
93                         if (!success)
94                         {
95                                 mode = "add";
96                                 success = add_smbpwd_entry(&pwd);
97                         }
98
99                         DEBUG(0, ("Attempted to %s account for %s: %s\n", mode,
100                                   nt_name, success ? "OK" : "FAILED"));
101                 }
102         }
103
104         cli_ulogoff(&cli);
105         cli_shutdown(&cli);
106         return ret;
107 }