3 * Unix SMB/CIFS implementation.
4 * MS-RPC client internal functions
5 * Copyright (C) Chris Nicholls 2005.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "libmsrpc_internal.h"
25 char *cac_unistr_to_str( TALLOC_CTX * mem_ctx, uint16 * src, int num_bytes );
26 char *talloc_unistr2_to_ascii( TALLOC_CTX * mem_ctx, UNISTR2 str );
27 char *cac_unistr_ascii( TALLOC_CTX * mem_ctx, UNISTR src );
29 /*used to get a struct rpc_pipe_client* to be passed into rpccli* calls*/
30 struct rpc_pipe_client *cac_GetPipe( CacServerHandle * hnd, int pi_idx )
33 struct rpc_pipe_client *pipe_hnd = NULL;
39 if ( hnd->_internal.pipes[pi_idx] == False ) {
40 hnd->status = NT_STATUS_INVALID_HANDLE;
44 srv = cac_GetServer( hnd );
46 hnd->status = NT_STATUS_INVALID_CONNECTION;
50 pipe_hnd = srv->cli->pipe_list;
52 while ( pipe_hnd != NULL && pipe_hnd->pipe_idx != pi_idx ) {
53 pipe_hnd = pipe_hnd->next;
59 /*takes a string like HKEY_LOCAL_MACHINE\HARDWARE\ACPI and returns the reg_type code and then a pointer to the start of the path (HARDWARE)*/
60 int cac_ParseRegPath( char *path, uint32 * reg_type, char **key_name )
66 if ( strncmp( path, "HKLM", 4 ) == 0 ) {
67 *reg_type = HKEY_LOCAL_MACHINE;
68 *key_name = ( path[4] == '\\' ) ? path + 5 : NULL;
69 } else if ( strncmp( path, "HKEY_LOCAL_MACHINE", 18 ) == 0 ) {
70 *reg_type = HKEY_LOCAL_MACHINE;
71 *key_name = ( path[18] == '\\' ) ? path + 19 : NULL;
72 } else if ( strncmp( path, "HKCR", 4 ) == 0 ) {
73 *reg_type = HKEY_CLASSES_ROOT;
74 *key_name = ( path[4] == '\\' ) ? path + 5 : NULL;
75 } else if ( strncmp( path, "HKEY_CLASSES_ROOT", 17 ) == 0 ) {
76 *reg_type = HKEY_CLASSES_ROOT;
77 *key_name = ( path[17] == '\\' ) ? path + 18 : NULL;
78 } else if ( strncmp( path, "HKU", 3 ) == 0 ) {
79 *reg_type = HKEY_USERS;
80 *key_name = ( path[3] == '\\' ) ? path + 4 : NULL;
81 } else if ( strncmp( path, "HKEY_USERS", 10 ) == 0 ) {
82 *reg_type = HKEY_USERS;
83 *key_name = ( path[10] == '\\' ) ? path + 11 : NULL;
84 } else if ( strncmp( path, "HKPD", 4 ) == 0 ) {
85 *reg_type = HKEY_PERFORMANCE_DATA;
86 *key_name = ( path[4] == '\\' ) ? path + 5 : NULL;
87 } else if ( strncmp( path, "HKEY_PERFORMANCE_DATA", 21 ) == 0 ) {
88 *reg_type = HKEY_PERFORMANCE_DATA;
89 *key_name = ( path[21] == '\\' ) ? path + 22 : NULL;
99 RPC_DATA_BLOB *cac_MakeRpcDataBlob( TALLOC_CTX * mem_ctx, uint32 data_type,
100 REG_VALUE_DATA data )
102 RPC_DATA_BLOB *blob = NULL;
106 uint32 multi_idx = 0;
108 blob = talloc( mem_ctx, RPC_DATA_BLOB );
115 switch ( data_type ) {
117 init_rpc_blob_str( blob, data.reg_sz,
118 strlen( data.reg_sz ) + 1 );
122 init_rpc_blob_str( blob, data.reg_expand_sz,
123 strlen( data.reg_sz ) + 1 );
127 init_rpc_blob_bytes( blob, data.reg_binary.data,
128 data.reg_binary.data_length );
132 init_rpc_blob_uint32( blob, data.reg_dword );
135 case REG_DWORD_BIG_ENDIAN:
136 init_rpc_blob_uint32( blob, data.reg_dword_be );
140 /*need to find the size */
141 for ( i = 0; i < data.reg_multi_sz.num_strings; i++ ) {
142 size += strlen( data.reg_multi_sz.strings[i] ) + 1;
145 /**need a whole bunch of unicode strings in a row (seperated by null characters), with an extra null-character on the end*/
147 multi = TALLOC_ZERO_ARRAY( mem_ctx, uint8, ( size + 1 ) * 2 ); /*size +1 for the extra null character */
153 /*do it using rpcstr_push() */
155 for ( i = 0; i < data.reg_multi_sz.num_strings; i++ ) {
157 strlen( data.reg_multi_sz.strings[i] ) + 1;
159 rpcstr_push( ( multi + multi_idx ),
160 data.reg_multi_sz.strings[i], len * 2,
163 /* x2 becuase it is a uint8 buffer */
164 multi_idx += len * 2;
167 /*now initialize the buffer as binary data */
168 init_rpc_blob_bytes( blob, multi, ( size + 1 ) * 2 );
178 if ( !( blob->buffer ) ) {
186 /*turns a string in a uint16 array to a char array*/
187 char *cac_unistr_to_str( TALLOC_CTX * mem_ctx, uint16 * src, int num_bytes )
195 /*don't allocate more space than we need */
196 while ( ( str_len ) < num_bytes / 2 && src[str_len] != 0x0000 )
199 /*need room for a '\0' */
202 buf = TALLOC_ARRAY( mem_ctx, char, str_len );
208 for ( i = 0; i < num_bytes / 2; i++ ) {
209 buf[i] = ( ( char * ) src )[2 * i];
212 buf[str_len - 1] = '\0';
217 REG_VALUE_DATA *cac_MakeRegValueData( TALLOC_CTX * mem_ctx, uint32 data_type,
220 REG_VALUE_DATA *data;
224 /*all of the following used for MULTI_SZ data */
227 uint32 multi_idx = 0;
228 uint32 num_strings = 0;
229 char **strings = NULL;
231 data = talloc( mem_ctx, REG_VALUE_DATA );
237 switch ( data_type ) {
240 cac_unistr_to_str( mem_ctx, buf.buffer, buf.buf_len );
241 if ( !data->reg_sz ) {
250 data->reg_expand_sz =
251 cac_unistr_to_str( mem_ctx, buf.buffer, buf.buf_len );
253 if ( !data->reg_expand_sz ) {
264 data->reg_binary.data_length = size;
267 data->reg_binary.data =
268 ( uint8 * ) TALLOC_MEMDUP( mem_ctx, buf.buffer, size );
269 if ( !data->reg_binary.data ) {
275 data->reg_binary.data = NULL;
280 data->reg_dword = *( ( uint32 * ) buf.buffer );
283 case REG_DWORD_BIG_ENDIAN:
284 data->reg_dword_be = *( ( uint32 * ) buf.buffer );
290 /*find out how many strings there are. size is # of bytes and we want to work uint16 */
291 for ( i = 0; i < ( size / 2 - 1 ); i++ ) {
292 if ( buf.buffer[i] == 0x0000 )
295 /*buffer is suppsed to be terminated with \0\0, but it might not be */
296 if ( buf.buffer[i] == 0x0000
297 && buf.buffer[i + 1] == 0x0000 )
302 strings = TALLOC_ARRAY( mem_ctx, char *, num_strings );
313 if ( num_strings == 0 ) /*then our work here is done */
316 for ( i = 0; i < num_strings; i++ ) {
317 /*find out how many characters are in this string */
319 /*make sure we don't go past the end of the buffer and keep looping until we have a uni \0 */
320 while ( multi_idx + len < size / 2
321 && buf.buffer[multi_idx + len] != 0x0000 )
324 /*stay aware of the \0\0 */
327 strings[i] = TALLOC_ZERO_ARRAY( mem_ctx, char, len );
329 /*pull out the unicode string */
330 rpcstr_pull( strings[i], ( buf.buffer + multi_idx ),
331 len, -1, STR_TERMINATE );
333 /*keep track of where we are in the bigger array */
337 data->reg_multi_sz.num_strings = num_strings;
338 data->reg_multi_sz.strings = strings;
350 SAM_USERINFO_CTR *cac_MakeUserInfoCtr( TALLOC_CTX * mem_ctx,
353 SAM_USERINFO_CTR *ctr = NULL;
355 /*the flags we are 'setting'- include/passdb.h */
357 ACCT_USERNAME | ACCT_FULL_NAME | ACCT_PRIMARY_GID |
358 ACCT_DESCRIPTION | ACCT_COMMENT | ACCT_HOME_DIR |
359 ACCT_HOME_DRIVE | ACCT_LOGON_SCRIPT | ACCT_PROFILE |
360 ACCT_WORKSTATIONS | ACCT_FLAGS;
365 NTTIME pass_last_set_time;
366 NTTIME pass_can_change_time;
367 NTTIME pass_must_change_time;
380 ctr = talloc( mem_ctx, SAM_USERINFO_CTR );
384 ZERO_STRUCTP( ctr->info.id23 );
386 ctr->info.id21 = talloc( mem_ctx, SAM_USER_INFO_21 );
387 if ( !ctr->info.id21 )
390 ctr->switch_value = 21;
392 ZERO_STRUCTP( ctr->info.id21 );
394 unix_to_nt_time( &logon_time, info->logon_time );
395 unix_to_nt_time( &logoff_time, info->logoff_time );
396 unix_to_nt_time( &kickoff_time, info->kickoff_time );
397 unix_to_nt_time( &pass_last_set_time, info->pass_last_set_time );
398 unix_to_nt_time( &pass_can_change_time, info->pass_can_change_time );
399 unix_to_nt_time( &pass_must_change_time,
400 info->pass_must_change_time );
402 /*initialize the strings */
403 init_unistr2( &user_name, info->username, UNI_STR_TERMINATE );
404 init_unistr2( &full_name, info->full_name, UNI_STR_TERMINATE );
405 init_unistr2( &home_dir, info->home_dir, UNI_STR_TERMINATE );
406 init_unistr2( &dir_drive, info->home_drive, UNI_STR_TERMINATE );
407 init_unistr2( &log_scr, info->logon_script, UNI_STR_TERMINATE );
408 init_unistr2( &prof_path, info->profile_path, UNI_STR_TERMINATE );
409 init_unistr2( &desc, info->description, UNI_STR_TERMINATE );
410 init_unistr2( &wkstas, info->workstations, UNI_STR_TERMINATE );
411 init_unistr2( &unk, "\0", UNI_STR_TERMINATE );
412 init_unistr2( &mung_dial, info->dial, UNI_STR_TERMINATE );
414 /*manually set passmustchange */
415 ctr->info.id21->passmustchange =
416 ( info->pass_must_change ) ? 0x01 : 0x00;
418 init_sam_user_info21W( ctr->info.id21, &logon_time, &logoff_time, &kickoff_time, &pass_last_set_time, &pass_can_change_time, &pass_must_change_time, &user_name, &full_name, &home_dir, &dir_drive, &log_scr, &prof_path, &desc, &wkstas, &unk, &mung_dial, info->lm_password, info->nt_password, info->rid, info->group_rid, info->acb_mask, flags, 168, /*logon divs */
420 info->bad_passwd_count, info->logon_count );
426 char *talloc_unistr2_to_ascii( TALLOC_CTX * mem_ctx, UNISTR2 str )
433 buf = TALLOC_ARRAY( mem_ctx, char, ( str.uni_str_len + 1 ) );
437 unistr2_to_ascii( buf, &str, str.uni_str_len + 1 );
442 CacUserInfo *cac_MakeUserInfo( TALLOC_CTX * mem_ctx, SAM_USERINFO_CTR * ctr )
444 CacUserInfo *info = NULL;
445 SAM_USER_INFO_21 *id21 = NULL;
447 if ( !ctr || ctr->switch_value != 21 )
450 info = talloc( mem_ctx, CacUserInfo );
454 id21 = ctr->info.id21;
456 ZERO_STRUCTP( info );
458 info->logon_time = nt_time_to_unix( id21->logon_time );
459 info->logoff_time = nt_time_to_unix( id21->logoff_time );
460 info->kickoff_time = nt_time_to_unix( id21->kickoff_time );
461 info->pass_last_set_time =
462 nt_time_to_unix( id21->pass_last_set_time );
463 info->pass_can_change_time =
464 nt_time_to_unix( id21->pass_can_change_time );
465 info->pass_must_change_time =
466 nt_time_to_unix( id21->pass_must_change_time );
469 talloc_unistr2_to_ascii( mem_ctx, id21->uni_user_name );
470 if ( !info->username )
474 talloc_unistr2_to_ascii( mem_ctx, id21->uni_full_name );
475 if ( !info->full_name )
479 talloc_unistr2_to_ascii( mem_ctx, id21->uni_home_dir );
480 if ( !info->home_dir )
484 talloc_unistr2_to_ascii( mem_ctx, id21->uni_dir_drive );
485 if ( !info->home_drive )
489 talloc_unistr2_to_ascii( mem_ctx, id21->uni_logon_script );
490 if ( !info->logon_script )
494 talloc_unistr2_to_ascii( mem_ctx, id21->uni_profile_path );
495 if ( !info->profile_path )
499 talloc_unistr2_to_ascii( mem_ctx, id21->uni_acct_desc );
500 if ( !info->description )
504 talloc_unistr2_to_ascii( mem_ctx, id21->uni_workstations );
505 if ( !info->workstations )
509 talloc_unistr2_to_ascii( mem_ctx, id21->uni_munged_dial );
513 info->rid = id21->user_rid;
514 info->group_rid = id21->group_rid;
515 info->acb_mask = id21->acb_info;
516 info->bad_passwd_count = id21->bad_password_count;
517 info->logon_count = id21->logon_count;
519 memcpy( info->nt_password, id21->nt_pwd, 8 );
520 memcpy( info->lm_password, id21->lm_pwd, 8 );
523 ( LOGON_HRS * ) TALLOC_MEMDUP( mem_ctx, &( id21->logon_hrs ),
524 sizeof( LOGON_HRS ) );
525 if ( !info->logon_hours )
528 info->pass_must_change = ( id21->passmustchange ) ? True : False;
533 CacGroupInfo *cac_MakeGroupInfo( TALLOC_CTX * mem_ctx, GROUP_INFO_CTR * ctr )
535 CacGroupInfo *info = NULL;
537 if ( !mem_ctx || !ctr || ctr->switch_value1 != 1 )
540 info = talloc( mem_ctx, CacGroupInfo );
545 talloc_unistr2_to_ascii( mem_ctx,
546 ctr->group.info1.uni_acct_name );
551 talloc_unistr2_to_ascii( mem_ctx,
552 ctr->group.info1.uni_acct_desc );
553 if ( !info->description )
556 info->num_members = ctr->group.info1.num_members;
561 GROUP_INFO_CTR *cac_MakeGroupInfoCtr( TALLOC_CTX * mem_ctx,
562 CacGroupInfo * info )
564 GROUP_INFO_CTR *ctr = NULL;
566 if ( !mem_ctx || !info )
569 ctr = talloc( mem_ctx, GROUP_INFO_CTR );
573 ctr->switch_value1 = 1;
575 init_samr_group_info1( &( ctr->group.info1 ), info->name,
576 info->description, info->num_members );
581 CacAliasInfo *cac_MakeAliasInfo( TALLOC_CTX * mem_ctx, ALIAS_INFO_CTR ctr )
583 CacGroupInfo *info = NULL;
585 if ( !mem_ctx || ctr.level != 1 )
588 info = talloc( mem_ctx, CacAliasInfo );
593 talloc_unistr2_to_ascii( mem_ctx,
594 *( ctr.alias.info1.name.string ) );
599 talloc_unistr2_to_ascii( mem_ctx,
600 *( ctr.alias.info1.description.
605 info->num_members = ctr.alias.info1.num_member;
610 ALIAS_INFO_CTR *cac_MakeAliasInfoCtr( TALLOC_CTX * mem_ctx,
611 CacAliasInfo * info )
613 ALIAS_INFO_CTR *ctr = NULL;
615 if ( !mem_ctx || !info )
618 ctr = talloc( mem_ctx, ALIAS_INFO_CTR );
624 init_samr_alias_info1( &( ctr->alias.info1 ), info->name,
625 info->num_members, info->description );
630 CacDomainInfo *cac_MakeDomainInfo( TALLOC_CTX * mem_ctx,
631 SAM_UNK_INFO_1 * info1,
632 SAM_UNK_INFO_2 * info2,
633 SAM_UNK_INFO_12 * info12 )
635 CacDomainInfo *info = NULL;
637 if ( !mem_ctx || !info1 || !info2 || !info12 )
640 info = talloc( mem_ctx, CacDomainInfo );
644 info->min_pass_length = info1->min_length_password;
645 info->pass_history = info1->password_history;
647 cac_InitCacTime( &( info->expire ), info1->expire );
648 cac_InitCacTime( &( info->min_pass_age ), info1->min_passwordage );
650 info->server_role = info2->server_role;
651 info->num_users = info2->num_domain_usrs;
652 info->num_domain_groups = info2->num_domain_grps;
653 info->num_local_groups = info2->num_local_grps;
655 /*if these have been ZERO'd out we need to know. uni_str_len will be 0 */
656 if ( info2->uni_comment.uni_str_len == 0 ) {
657 info->comment = talloc_strdup( mem_ctx, "\0" );
660 talloc_unistr2_to_ascii( mem_ctx,
661 info2->uni_comment );
664 if ( info2->uni_domain.uni_str_len == 0 ) {
665 info->domain_name = talloc_strdup( mem_ctx, "\0" );
668 talloc_unistr2_to_ascii( mem_ctx, info2->uni_domain );
671 if ( info2->uni_server.uni_str_len == 0 ) {
672 info->server_name = talloc_strdup( mem_ctx, "\0" );
675 talloc_unistr2_to_ascii( mem_ctx, info2->uni_server );
679 cac_InitCacTime( &( info->lockout_duration ), info12->duration );
680 cac_InitCacTime( &( info->lockout_reset ), info12->reset_count );
681 info->num_bad_attempts = info12->bad_attempt_lockout;
686 char *cac_unistr_ascii( TALLOC_CTX * mem_ctx, UNISTR src )
691 if ( !mem_ctx || !src.buffer )
694 len = unistrlen( src.buffer ) + 1;
696 buf = TALLOC_ZERO_ARRAY( mem_ctx, char, len );
700 rpcstr_pull( buf, src.buffer, len, -1, STR_TERMINATE );
705 CacService *cac_MakeServiceArray( TALLOC_CTX * mem_ctx,
706 ENUM_SERVICES_STATUS * svc,
707 uint32 num_services )
710 CacService *services = NULL;
712 if ( !mem_ctx || !svc )
716 services = TALLOC_ZERO_ARRAY( mem_ctx, CacService, num_services );
723 for ( i = 0; i < num_services; i++ ) {
724 services[i].service_name =
725 cac_unistr_ascii( mem_ctx, svc[i].servicename );
726 services[i].display_name =
727 cac_unistr_ascii( mem_ctx, svc[i].displayname );
729 if ( !services[i].service_name || !services[i].display_name )
732 services[i].status = svc[i].status;
738 int cac_InitCacServiceConfig( TALLOC_CTX * mem_ctx, SERVICE_CONFIG * src,
739 CacServiceConfig * dest )
745 talloc_unistr2_to_ascii( mem_ctx, *src->executablepath );
746 if ( !dest->exe_path )
749 dest->load_order_group =
750 talloc_unistr2_to_ascii( mem_ctx, *src->loadordergroup );
751 if ( !dest->load_order_group )
755 talloc_unistr2_to_ascii( mem_ctx, *src->dependencies );
756 if ( !dest->dependencies )
760 talloc_unistr2_to_ascii( mem_ctx, *src->startname );
761 if ( !dest->start_name )
765 talloc_unistr2_to_ascii( mem_ctx, *src->displayname );
766 if ( !dest->display_name )
769 dest->type = src->service_type;
770 dest->start_type = src->start_type;
771 dest->error_control = src->error_control;
772 dest->tag_id = src->tag_id;