edf24203a80e768c540ea9ff560d8c6feed06a28
[metze/samba/wip.git] / source3 / rpc_client / init_samr.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Guenther Deschner                  2008.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21
22 /*******************************************************************
23  inits a structure.
24 ********************************************************************/
25
26 void init_samr_DomInfo1(struct samr_DomInfo1 *r,
27                         uint16_t min_password_length,
28                         uint16_t password_history_length,
29                         uint32_t password_properties,
30                         int64_t max_password_age,
31                         int64_t min_password_age)
32 {
33         r->min_password_length = min_password_length;
34         r->password_history_length = password_history_length;
35         r->password_properties = password_properties;
36         r->max_password_age = max_password_age;
37         r->min_password_age = min_password_age;
38 }
39
40 /*******************************************************************
41  inits a structure.
42 ********************************************************************/
43
44 void init_samr_DomGeneralInformation(struct samr_DomGeneralInformation *r,
45                                      NTTIME force_logoff_time,
46                                      const char *oem_information,
47                                      const char *domain_name,
48                                      const char *primary,
49                                      uint64_t sequence_num,
50                                      enum samr_DomainServerState domain_server_state,
51                                      enum samr_Role role,
52                                      uint32_t unknown3,
53                                      uint32_t num_users,
54                                      uint32_t num_groups,
55                                      uint32_t num_aliases)
56 {
57         r->force_logoff_time = force_logoff_time;
58         init_lsa_String(&r->oem_information, oem_information);
59         init_lsa_String(&r->domain_name, domain_name);
60         init_lsa_String(&r->primary, primary);
61         r->sequence_num = sequence_num;
62         r->domain_server_state = domain_server_state;
63         r->role = role;
64         r->unknown3 = unknown3;
65         r->num_users = num_users;
66         r->num_groups = num_groups;
67         r->num_aliases = num_aliases;
68 }
69
70 /*******************************************************************
71  inits a structure.
72 ********************************************************************/
73
74 void init_samr_DomInfo3(struct samr_DomInfo3 *r,
75                         NTTIME force_logoff_time)
76 {
77         r->force_logoff_time = force_logoff_time;
78 }
79
80 /*******************************************************************
81  inits a structure.
82 ********************************************************************/
83
84 void init_samr_DomOEMInformation(struct samr_DomOEMInformation *r,
85                                  const char *oem_information)
86 {
87         init_lsa_String(&r->oem_information, oem_information);
88 }
89
90 /*******************************************************************
91  inits a structure.
92 ********************************************************************/
93
94 void init_samr_DomInfo5(struct samr_DomInfo5 *r,
95                         const char *domain_name)
96 {
97         init_lsa_String(&r->domain_name, domain_name);
98 }
99
100 /*******************************************************************
101  inits a structure.
102 ********************************************************************/
103
104 void init_samr_DomInfo6(struct samr_DomInfo6 *r,
105                         const char *primary)
106 {
107         init_lsa_String(&r->primary, primary);
108 }
109
110 /*******************************************************************
111  inits a structure.
112 ********************************************************************/
113
114 void init_samr_DomInfo7(struct samr_DomInfo7 *r,
115                         enum samr_Role role)
116 {
117         r->role = role;
118 }
119
120 /*******************************************************************
121  inits a structure.
122 ********************************************************************/
123
124 void init_samr_DomInfo8(struct samr_DomInfo8 *r,
125                         uint64_t sequence_num,
126                         NTTIME domain_create_time)
127 {
128         r->sequence_num = sequence_num;
129         r->domain_create_time = domain_create_time;
130 }
131
132 /*******************************************************************
133  inits a structure.
134 ********************************************************************/
135
136 void init_samr_DomInfo9(struct samr_DomInfo9 *r,
137                         enum samr_DomainServerState domain_server_state)
138 {
139         r->domain_server_state = domain_server_state;
140 }
141
142
143 /*******************************************************************
144  inits a structure.
145 ********************************************************************/
146
147 void init_samr_DomInfo12(struct samr_DomInfo12 *r,
148                          uint64_t lockout_duration,
149                          uint64_t lockout_window,
150                          uint16_t lockout_threshold)
151 {
152         r->lockout_duration = lockout_duration;
153         r->lockout_window = lockout_window;
154         r->lockout_threshold = lockout_threshold;
155 }
156
157 /*******************************************************************
158  inits a samr_GroupInfoAll structure.
159 ********************************************************************/
160
161 void init_samr_group_info1(struct samr_GroupInfoAll *r,
162                            const char *name,
163                            uint32_t attributes,
164                            uint32_t num_members,
165                            const char *description)
166 {
167         DEBUG(5, ("init_samr_group_info1\n"));
168
169         init_lsa_String(&r->name, name);
170         r->attributes = attributes;
171         r->num_members = num_members;
172         init_lsa_String(&r->description, description);
173 }
174
175 /*******************************************************************
176  inits a lsa_String structure
177 ********************************************************************/
178
179 void init_samr_group_info2(struct lsa_String *r, const char *group_name)
180 {
181         DEBUG(5, ("init_samr_group_info2\n"));
182
183         init_lsa_String(r, group_name);
184 }
185
186 /*******************************************************************
187  inits a samr_GroupInfoAttributes structure.
188 ********************************************************************/
189
190 void init_samr_group_info3(struct samr_GroupInfoAttributes *r,
191                            uint32_t attributes)
192 {
193         DEBUG(5, ("init_samr_group_info3\n"));
194
195         r->attributes = attributes;
196 }
197
198 /*******************************************************************
199  inits a lsa_String structure
200 ********************************************************************/
201
202 void init_samr_group_info4(struct lsa_String *r, const char *description)
203 {
204         DEBUG(5, ("init_samr_group_info4\n"));
205
206         init_lsa_String(r, description);
207 }
208
209 /*******************************************************************
210  inits a samr_GroupInfoAll structure.
211 ********************************************************************/
212
213 void init_samr_group_info5(struct samr_GroupInfoAll *r,
214                            const char *name,
215                            uint32_t attributes,
216                            uint32_t num_members,
217                            const char *description)
218 {
219         DEBUG(5, ("init_samr_group_info5\n"));
220
221         init_lsa_String(&r->name, name);
222         r->attributes = attributes;
223         r->num_members = num_members;
224         init_lsa_String(&r->description, description);
225 }
226
227 /*******************************************************************
228  inits a samr_AliasInfoAll structure.
229 ********************************************************************/
230
231 void init_samr_alias_info1(struct samr_AliasInfoAll *r,
232                            const char *name,
233                            uint32_t num_members,
234                            const char *description)
235 {
236         DEBUG(5, ("init_samr_alias_info1\n"));
237
238         init_lsa_String(&r->name, name);
239         r->num_members = num_members;
240         init_lsa_String(&r->description, description);
241 }
242
243 /*******************************************************************
244 inits a lsa_String structure.
245 ********************************************************************/
246
247 void init_samr_alias_info3(struct lsa_String *r,
248                            const char *description)
249 {
250         DEBUG(5, ("init_samr_alias_info3\n"));
251
252         init_lsa_String(r, description);
253 }
254
255 /*************************************************************************
256  inits a samr_CryptPasswordEx structure
257  *************************************************************************/
258
259 void init_samr_CryptPasswordEx(const char *pwd,
260                                DATA_BLOB *session_key,
261                                struct samr_CryptPasswordEx *pwd_buf)
262 {
263         /* samr_CryptPasswordEx */
264
265         uchar pwbuf[532];
266         struct MD5Context md5_ctx;
267         uint8_t confounder[16];
268         DATA_BLOB confounded_session_key = data_blob(NULL, 16);
269
270         encode_pw_buffer(pwbuf, pwd, STR_UNICODE);
271
272         generate_random_buffer((uint8_t *)confounder, 16);
273
274         MD5Init(&md5_ctx);
275         MD5Update(&md5_ctx, confounder, 16);
276         MD5Update(&md5_ctx, session_key->data,
277                             session_key->length);
278         MD5Final(confounded_session_key.data, &md5_ctx);
279
280         SamOEMhashBlob(pwbuf, 516, &confounded_session_key);
281         memcpy(&pwbuf[516], confounder, 16);
282
283         memcpy(pwd_buf->data, pwbuf, sizeof(pwbuf));
284         data_blob_free(&confounded_session_key);
285 }
286
287 /*************************************************************************
288  inits a samr_CryptPassword structure
289  *************************************************************************/
290
291 void init_samr_CryptPassword(const char *pwd,
292                              DATA_BLOB *session_key,
293                              struct samr_CryptPassword *pwd_buf)
294 {
295         /* samr_CryptPassword */
296
297         encode_pw_buffer(pwd_buf->data, pwd, STR_UNICODE);
298         SamOEMhashBlob(pwd_buf->data, 516, session_key);
299 }