7f48b1fdf9614e011cd83b6bc86aaba84166453a
[jra/samba/.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_UserInfo5 structure.
257 ********************************************************************/
258
259 void init_samr_user_info5(struct samr_UserInfo5 *r,
260                           const char *account_name,
261                           const char *full_name,
262                           uint32_t rid,
263                           uint32_t primary_gid,
264                           const char *home_directory,
265                           const char *home_drive,
266                           const char *logon_script,
267                           const char *profile_path,
268                           const char *description,
269                           const char *workstations,
270                           NTTIME last_logon,
271                           NTTIME last_logoff,
272                           struct samr_LogonHours logon_hours,
273                           uint16_t bad_password_count,
274                           uint16_t logon_count,
275                           NTTIME last_password_change,
276                           NTTIME acct_expiry,
277                           uint32_t acct_flags)
278 {
279         DEBUG(5, ("init_samr_user_info5\n"));
280
281         init_lsa_String(&r->account_name, account_name);
282         init_lsa_String(&r->full_name, full_name);
283         r->rid = rid;
284         r->primary_gid = primary_gid;
285         init_lsa_String(&r->home_directory, home_directory);
286         init_lsa_String(&r->home_drive, home_drive);
287         init_lsa_String(&r->logon_script, logon_script);
288         init_lsa_String(&r->profile_path, profile_path);
289         init_lsa_String(&r->description, description);
290         init_lsa_String(&r->workstations, workstations);
291         r->last_logon = last_logon;
292         r->last_logoff = last_logoff;
293         r->logon_hours = logon_hours;
294         r->bad_password_count = bad_password_count;
295         r->logon_count = logon_count;
296         r->last_password_change = last_password_change;
297         r->acct_expiry = acct_expiry;
298         r->acct_flags = acct_flags;
299 }
300
301
302 /*******************************************************************
303  inits a samr_UserInfo7 structure.
304 ********************************************************************/
305
306 void init_samr_user_info7(struct samr_UserInfo7 *r,
307                           const char *account_name)
308 {
309         DEBUG(5, ("init_samr_user_info7\n"));
310
311         init_lsa_String(&r->account_name, account_name);
312 }
313
314 /*******************************************************************
315  inits a samr_UserInfo9 structure.
316 ********************************************************************/
317
318 void init_samr_user_info9(struct samr_UserInfo9 *r,
319                           uint32_t primary_gid)
320 {
321         DEBUG(5, ("init_samr_user_info9\n"));
322
323         r->primary_gid = primary_gid;
324 }
325
326 /*******************************************************************
327  inits a SAM_USER_INFO_16 structure.
328 ********************************************************************/
329
330 void init_samr_user_info16(struct samr_UserInfo16 *r,
331                            uint32_t acct_flags)
332 {
333         DEBUG(5, ("init_samr_user_info16\n"));
334
335         r->acct_flags = acct_flags;
336 }
337
338 /*******************************************************************
339  inits a samr_UserInfo18 structure.
340 ********************************************************************/
341
342 void init_samr_user_info18(struct samr_UserInfo18 *r,
343                            const uint8 lm_pwd[16],
344                            const uint8 nt_pwd[16])
345 {
346         DEBUG(5, ("init_samr_user_info18\n"));
347
348         r->lm_pwd_active =
349                 memcpy(r->lm_pwd.hash, lm_pwd, sizeof(r->lm_pwd.hash)) ? true : false;
350         r->nt_pwd_active =
351                 memcpy(r->nt_pwd.hash, nt_pwd, sizeof(r->nt_pwd.hash)) ? true : false;
352 }
353
354 /*******************************************************************
355  inits a samr_UserInfo20 structure.
356 ********************************************************************/
357
358 void init_samr_user_info20(struct samr_UserInfo20 *r,
359                            struct lsa_BinaryString *parameters)
360 {
361         r->parameters = *parameters;
362 }
363
364 /*************************************************************************
365  inits a samr_UserInfo21 structure
366  *************************************************************************/
367
368 void init_samr_user_info21(struct samr_UserInfo21 *r,
369                            NTTIME last_logon,
370                            NTTIME last_logoff,
371                            NTTIME last_password_change,
372                            NTTIME acct_expiry,
373                            NTTIME allow_password_change,
374                            NTTIME force_password_change,
375                            const char *account_name,
376                            const char *full_name,
377                            const char *home_directory,
378                            const char *home_drive,
379                            const char *logon_script,
380                            const char *profile_path,
381                            const char *description,
382                            const char *workstations,
383                            const char *comment,
384                            struct lsa_BinaryString *parameters,
385                            uint32_t rid,
386                            uint32_t primary_gid,
387                            uint32_t acct_flags,
388                            uint32_t fields_present,
389                            struct samr_LogonHours logon_hours,
390                            uint16_t bad_password_count,
391                            uint16_t logon_count,
392                            uint16_t country_code,
393                            uint16_t code_page,
394                            uint8_t lm_password_set,
395                            uint8_t nt_password_set,
396                            uint8_t password_expired)
397 {
398         r->last_logon = last_logon;
399         r->last_logoff = last_logoff;
400         r->last_password_change = last_password_change;
401         r->acct_expiry = acct_expiry;
402         r->allow_password_change = allow_password_change;
403         r->force_password_change = force_password_change;
404         init_lsa_String(&r->account_name, account_name);
405         init_lsa_String(&r->full_name, full_name);
406         init_lsa_String(&r->home_directory, home_directory);
407         init_lsa_String(&r->home_drive, home_drive);
408         init_lsa_String(&r->logon_script, logon_script);
409         init_lsa_String(&r->profile_path, profile_path);
410         init_lsa_String(&r->description, description);
411         init_lsa_String(&r->workstations, workstations);
412         init_lsa_String(&r->comment, comment);
413         r->parameters = *parameters;
414         r->rid = rid;
415         r->primary_gid = primary_gid;
416         r->acct_flags = acct_flags;
417         r->fields_present = fields_present;
418         r->logon_hours = logon_hours;
419         r->bad_password_count = bad_password_count;
420         r->logon_count = logon_count;
421         r->country_code = country_code;
422         r->code_page = code_page;
423         r->lm_password_set = lm_password_set;
424         r->nt_password_set = nt_password_set;
425         r->password_expired = password_expired;
426 }
427
428 /*************************************************************************
429  init_samr_user_info23
430  *************************************************************************/
431
432 void init_samr_user_info23(struct samr_UserInfo23 *r,
433                            NTTIME last_logon,
434                            NTTIME last_logoff,
435                            NTTIME last_password_change,
436                            NTTIME acct_expiry,
437                            NTTIME allow_password_change,
438                            NTTIME force_password_change,
439                            const char *account_name,
440                            const char *full_name,
441                            const char *home_directory,
442                            const char *home_drive,
443                            const char *logon_script,
444                            const char *profile_path,
445                            const char *description,
446                            const char *workstations,
447                            const char *comment,
448                            struct lsa_BinaryString *parameters,
449                            uint32_t rid,
450                            uint32_t primary_gid,
451                            uint32_t acct_flags,
452                            uint32_t fields_present,
453                            struct samr_LogonHours logon_hours,
454                            uint16_t bad_password_count,
455                            uint16_t logon_count,
456                            uint16_t country_code,
457                            uint16_t code_page,
458                            uint8_t lm_password_set,
459                            uint8_t nt_password_set,
460                            uint8_t password_expired,
461                            struct samr_CryptPassword *pwd_buf)
462 {
463         memset(r, '\0', sizeof(*r));
464         init_samr_user_info21(&r->info,
465                               last_logon,
466                               last_logoff,
467                               last_password_change,
468                               acct_expiry,
469                               allow_password_change,
470                               force_password_change,
471                               account_name,
472                               full_name,
473                               home_directory,
474                               home_drive,
475                               logon_script,
476                               profile_path,
477                               description,
478                               workstations,
479                               comment,
480                               parameters,
481                               rid,
482                               primary_gid,
483                               acct_flags,
484                               fields_present,
485                               logon_hours,
486                               bad_password_count,
487                               logon_count,
488                               country_code,
489                               code_page,
490                               lm_password_set,
491                               nt_password_set,
492                               password_expired);
493
494         r->password = *pwd_buf;
495 }
496
497 /*************************************************************************
498  init_samr_user_info24
499  *************************************************************************/
500
501 void init_samr_user_info24(struct samr_UserInfo24 *r,
502                            struct samr_CryptPassword *pwd_buf,
503                            uint8_t password_expired)
504 {
505         DEBUG(10, ("init_samr_user_info24:\n"));
506
507         r->password = *pwd_buf;
508         r->password_expired = password_expired;
509 }
510
511 /*************************************************************************
512  init_samr_user_info25
513  *************************************************************************/
514
515 void init_samr_user_info25(struct samr_UserInfo25 *r,
516                            NTTIME last_logon,
517                            NTTIME last_logoff,
518                            NTTIME last_password_change,
519                            NTTIME acct_expiry,
520                            NTTIME allow_password_change,
521                            NTTIME force_password_change,
522                            const char *account_name,
523                            const char *full_name,
524                            const char *home_directory,
525                            const char *home_drive,
526                            const char *logon_script,
527                            const char *profile_path,
528                            const char *description,
529                            const char *workstations,
530                            const char *comment,
531                            struct lsa_BinaryString *parameters,
532                            uint32_t rid,
533                            uint32_t primary_gid,
534                            uint32_t acct_flags,
535                            uint32_t fields_present,
536                            struct samr_LogonHours logon_hours,
537                            uint16_t bad_password_count,
538                            uint16_t logon_count,
539                            uint16_t country_code,
540                            uint16_t code_page,
541                            uint8_t lm_password_set,
542                            uint8_t nt_password_set,
543                            uint8_t password_expired,
544                            struct samr_CryptPasswordEx *pwd_buf)
545 {
546         DEBUG(10, ("init_samr_user_info25:\n"));
547
548         memset(r, '\0', sizeof(*r));
549         init_samr_user_info21(&r->info,
550                               last_logon,
551                               last_logoff,
552                               last_password_change,
553                               acct_expiry,
554                               allow_password_change,
555                               force_password_change,
556                               account_name,
557                               full_name,
558                               home_directory,
559                               home_drive,
560                               logon_script,
561                               profile_path,
562                               description,
563                               workstations,
564                               comment,
565                               parameters,
566                               rid,
567                               primary_gid,
568                               acct_flags,
569                               fields_present,
570                               logon_hours,
571                               bad_password_count,
572                               logon_count,
573                               country_code,
574                               code_page,
575                               lm_password_set,
576                               nt_password_set,
577                               password_expired);
578
579         r->password = *pwd_buf;
580 }
581
582 /*************************************************************************
583  init_samr_user_info26
584  *************************************************************************/
585
586 void init_samr_user_info26(struct samr_UserInfo26 *r,
587                            struct samr_CryptPasswordEx *pwd_buf,
588                            uint8_t password_expired)
589 {
590         DEBUG(10, ("init_samr_user_info26:\n"));
591
592         r->password = *pwd_buf;
593         r->password_expired = password_expired;
594 }
595
596 /*************************************************************************
597  inits a samr_CryptPasswordEx structure
598  *************************************************************************/
599
600 void init_samr_CryptPasswordEx(const char *pwd,
601                                DATA_BLOB *session_key,
602                                struct samr_CryptPasswordEx *pwd_buf)
603 {
604         /* samr_CryptPasswordEx */
605
606         uchar pwbuf[532];
607         struct MD5Context md5_ctx;
608         uint8_t confounder[16];
609         DATA_BLOB confounded_session_key = data_blob(NULL, 16);
610
611         encode_pw_buffer(pwbuf, pwd, STR_UNICODE);
612
613         generate_random_buffer((uint8_t *)confounder, 16);
614
615         MD5Init(&md5_ctx);
616         MD5Update(&md5_ctx, confounder, 16);
617         MD5Update(&md5_ctx, session_key->data,
618                             session_key->length);
619         MD5Final(confounded_session_key.data, &md5_ctx);
620
621         SamOEMhashBlob(pwbuf, 516, &confounded_session_key);
622         memcpy(&pwbuf[516], confounder, 16);
623
624         memcpy(pwd_buf->data, pwbuf, sizeof(pwbuf));
625         data_blob_free(&confounded_session_key);
626 }
627
628 /*************************************************************************
629  inits a samr_CryptPassword structure
630  *************************************************************************/
631
632 void init_samr_CryptPassword(const char *pwd,
633                              DATA_BLOB *session_key,
634                              struct samr_CryptPassword *pwd_buf)
635 {
636         /* samr_CryptPassword */
637
638         encode_pw_buffer(pwd_buf->data, pwd, STR_UNICODE);
639         SamOEMhashBlob(pwd_buf->data, 516, session_key);
640 }