r18963: * Move parts of registry headers that were still in
[jra/samba/.git] / source3 / libmsrpc / libmsrpc_internal.c
1
2 /* 
3  *  Unix SMB/CIFS implementation.
4  *  MS-RPC client internal functions
5  *  Copyright (C) Chris Nicholls              2005.
6  *  
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 2 of the License, or
10  *  (at your option) any later version.
11  *  
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.
16  *  
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22
23 #include "libmsrpc.h"
24 #include "libmsrpc_internal.h"
25
26 /*used to get a struct rpc_pipe_client* to be passed into rpccli* calls*/
27 struct rpc_pipe_client *cac_GetPipe( CacServerHandle * hnd, int pi_idx )
28 {
29         SMBCSRV *srv = NULL;
30         struct rpc_pipe_client *pipe_hnd = NULL;
31
32         if ( !hnd ) {
33                 return NULL;
34         }
35
36         if ( hnd->_internal.pipes[pi_idx] == False ) {
37                 hnd->status = NT_STATUS_INVALID_HANDLE;
38                 return NULL;
39         }
40
41         srv = cac_GetServer( hnd );
42         if ( !srv ) {
43                 hnd->status = NT_STATUS_INVALID_CONNECTION;
44                 return NULL;
45         }
46
47         pipe_hnd = srv->cli->pipe_list;
48
49         while ( pipe_hnd != NULL && pipe_hnd->pipe_idx != pi_idx ) {
50                 pipe_hnd = pipe_hnd->next;
51         }
52
53         return pipe_hnd;
54 }
55
56 /*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)*/
57 int cac_ParseRegPath( char *path, uint32 * reg_type, char **key_name )
58 {
59
60         if ( !path )
61                 return CAC_FAILURE;
62
63         if ( strncmp( path, "HKLM", 4 ) == 0 ) {
64                 *reg_type = HKEY_LOCAL_MACHINE;
65                 *key_name = ( path[4] == '\\' ) ? path + 5 : NULL;
66         } else if ( strncmp( path, "HKEY_LOCAL_MACHINE", 18 ) == 0 ) {
67                 *reg_type = HKEY_LOCAL_MACHINE;
68                 *key_name = ( path[18] == '\\' ) ? path + 19 : NULL;
69         } else if ( strncmp( path, "HKCR", 4 ) == 0 ) {
70                 *reg_type = HKEY_CLASSES_ROOT;
71                 *key_name = ( path[4] == '\\' ) ? path + 5 : NULL;
72         } else if ( strncmp( path, "HKEY_CLASSES_ROOT", 17 ) == 0 ) {
73                 *reg_type = HKEY_CLASSES_ROOT;
74                 *key_name = ( path[17] == '\\' ) ? path + 18 : NULL;
75         } else if ( strncmp( path, "HKU", 3 ) == 0 ) {
76                 *reg_type = HKEY_USERS;
77                 *key_name = ( path[3] == '\\' ) ? path + 4 : NULL;
78         } else if ( strncmp( path, "HKEY_USERS", 10 ) == 0 ) {
79                 *reg_type = HKEY_USERS;
80                 *key_name = ( path[10] == '\\' ) ? path + 11 : NULL;
81         } else if ( strncmp( path, "HKPD", 4 ) == 0 ) {
82                 *reg_type = HKEY_PERFORMANCE_DATA;
83                 *key_name = ( path[4] == '\\' ) ? path + 5 : NULL;
84         } else if ( strncmp( path, "HKEY_PERFORMANCE_DATA", 21 ) == 0 ) {
85                 *reg_type = HKEY_PERFORMANCE_DATA;
86                 *key_name = ( path[21] == '\\' ) ? path + 22 : NULL;
87         } else {
88                 return CAC_FAILURE;
89         }
90
91         return CAC_SUCCESS;
92 }
93
94
95
96 RPC_DATA_BLOB *cac_MakeRpcDataBlob( TALLOC_CTX * mem_ctx, uint32 data_type,
97                                     REG_VALUE_DATA data )
98 {
99         RPC_DATA_BLOB *blob = NULL;
100         int i;
101         uint32 size = 0;
102         uint8 *multi = NULL;
103         uint32 multi_idx = 0;
104
105         blob = talloc( mem_ctx, RPC_DATA_BLOB );
106
107         if ( !blob ) {
108                 errno = ENOMEM;
109                 return NULL;
110         }
111
112         switch ( data_type ) {
113         case REG_SZ:
114                 init_rpc_blob_str( blob, data.reg_sz,
115                                    strlen( data.reg_sz ) + 1 );
116                 break;
117
118         case REG_EXPAND_SZ:
119                 init_rpc_blob_str( blob, data.reg_expand_sz,
120                                    strlen( data.reg_sz ) + 1 );
121                 break;
122
123         case REG_BINARY:
124                 init_rpc_blob_bytes( blob, data.reg_binary.data,
125                                      data.reg_binary.data_length );
126                 break;
127
128         case REG_DWORD:
129                 init_rpc_blob_uint32( blob, data.reg_dword );
130                 break;
131
132         case REG_DWORD_BIG_ENDIAN:
133                 init_rpc_blob_uint32( blob, data.reg_dword_be );
134                 break;
135
136         case REG_MULTI_SZ:
137                 /*need to find the size */
138                 for ( i = 0; i < data.reg_multi_sz.num_strings; i++ ) {
139                         size += strlen( data.reg_multi_sz.strings[i] ) + 1;
140                 }
141
142          /**need a whole bunch of unicode strings in a row (seperated by null characters), with an extra null-character on the end*/
143
144                 multi = TALLOC_ZERO_ARRAY( mem_ctx, uint8, ( size + 1 ) * 2 );  /*size +1 for the extra null character */
145                 if ( !multi ) {
146                         errno = ENOMEM;
147                         break;
148                 }
149
150                 /*do it using rpcstr_push() */
151                 multi_idx = 0;
152                 for ( i = 0; i < data.reg_multi_sz.num_strings; i++ ) {
153                         size_t len =
154                                 strlen( data.reg_multi_sz.strings[i] ) + 1;
155
156                         rpcstr_push( ( multi + multi_idx ),
157                                      data.reg_multi_sz.strings[i], len * 2,
158                                      STR_TERMINATE );
159
160                         /* x2 becuase it is a uint8 buffer */
161                         multi_idx += len * 2;
162                 }
163
164                 /*now initialize the buffer as binary data */
165                 init_rpc_blob_bytes( blob, multi, ( size + 1 ) * 2 );
166
167                 break;
168
169         default:
170                 TALLOC_FREE( blob );
171                 blob = NULL;
172                 return NULL;
173         }
174
175         if ( !( blob->buffer ) ) {
176                 TALLOC_FREE( blob );
177                 return NULL;
178         }
179
180         return blob;
181 }
182
183 /*turns a string in a uint16 array to a char array*/
184 char *cac_unistr_to_str( TALLOC_CTX * mem_ctx, uint16 * src, int num_bytes )
185 {
186         char *buf;
187
188         int i = 0;
189
190         uint32 str_len = 0;
191
192         /*don't allocate more space than we need */
193         while ( ( str_len ) < num_bytes / 2 && src[str_len] != 0x0000 )
194                 str_len++;
195
196         /*need room for a '\0' */
197         str_len++;
198
199         buf = talloc_array( mem_ctx, char, str_len );
200
201         if ( !buf ) {
202                 return NULL;
203         }
204
205         for ( i = 0; i < num_bytes / 2; i++ ) {
206                 buf[i] = ( ( char * ) src )[2 * i];
207         }
208
209         buf[str_len - 1] = '\0';
210
211         return buf;
212 }
213
214 REG_VALUE_DATA *cac_MakeRegValueData( TALLOC_CTX * mem_ctx, uint32 data_type,
215                                       REGVAL_BUFFER buf )
216 {
217         REG_VALUE_DATA *data;
218
219         uint32 i;
220
221         /*all of the following used for MULTI_SZ data */
222         uint32 size = 0;
223         uint32 len = 0;
224         uint32 multi_idx = 0;
225         uint32 num_strings = 0;
226         char **strings = NULL;
227
228         data = talloc( mem_ctx, REG_VALUE_DATA );
229         if ( !data ) {
230                 errno = ENOMEM;
231                 return NULL;
232         }
233
234         switch ( data_type ) {
235         case REG_SZ:
236                 data->reg_sz =
237                         cac_unistr_to_str( mem_ctx, buf.buffer, buf.buf_len );
238                 if ( !data->reg_sz ) {
239                         TALLOC_FREE( data );
240                         errno = ENOMEM;
241                         data = NULL;
242                 }
243
244                 break;
245
246         case REG_EXPAND_SZ:
247                 data->reg_expand_sz =
248                         cac_unistr_to_str( mem_ctx, buf.buffer, buf.buf_len );
249
250                 if ( !data->reg_expand_sz ) {
251                         TALLOC_FREE( data );
252                         errno = ENOMEM;
253                         data = NULL;
254                 }
255
256                 break;
257
258         case REG_BINARY:
259                 size = buf.buf_len;
260
261                 data->reg_binary.data_length = size;
262
263                 data->reg_binary.data =
264                         ( uint8 * ) talloc_memdup( mem_ctx, buf.buffer,
265                                                    size );
266                 if ( !data->reg_binary.data ) {
267                         TALLOC_FREE( data );
268                         errno = ENOMEM;
269                         data = NULL;
270                 }
271                 break;
272
273         case REG_DWORD:
274                 data->reg_dword = *( ( uint32 * ) buf.buffer );
275                 break;
276
277         case REG_DWORD_BIG_ENDIAN:
278                 data->reg_dword_be = *( ( uint32 * ) buf.buffer );
279                 break;
280
281         case REG_MULTI_SZ:
282                 size = buf.buf_len;
283
284                 /*find out how many strings there are. size is # of bytes and we want to work uint16 */
285                 for ( i = 0; i < ( size / 2 - 1 ); i++ ) {
286                         if ( buf.buffer[i] == 0x0000 )
287                                 num_strings++;
288
289                         /*buffer is suppsed to be terminated with \0\0, but it might not be */
290                         if ( buf.buffer[i] == 0x0000
291                              && buf.buffer[i + 1] == 0x0000 )
292                                 break;
293                 }
294
295                 strings = talloc_array( mem_ctx, char *, num_strings );
296
297                 if ( !strings ) {
298                         errno = ENOMEM;
299                         TALLOC_FREE( data );
300                         break;
301                 }
302
303                 if ( num_strings == 0 ) /*then our work here is done */
304                         break;
305
306                 for ( i = 0; i < num_strings; i++ ) {
307                         /*find out how many characters are in this string */
308                         len = 0;
309                         /*make sure we don't go past the end of the buffer and keep looping until we have a uni \0 */
310                         while ( multi_idx + len < size / 2
311                                 && buf.buffer[multi_idx + len] != 0x0000 )
312                                 len++;
313
314                         /*stay aware of the \0\0 */
315                         len++;
316
317                         strings[i] = TALLOC_ZERO_ARRAY( mem_ctx, char, len );
318
319                         /*pull out the unicode string */
320                         rpcstr_pull( strings[i], ( buf.buffer + multi_idx ),
321                                      len, -1, STR_TERMINATE );
322
323                         /*keep track of where we are in the bigger array */
324                         multi_idx += len;
325                 }
326
327                 data->reg_multi_sz.num_strings = num_strings;
328                 data->reg_multi_sz.strings = strings;
329
330                 break;
331
332         default:
333                 TALLOC_FREE( data );
334                 data = NULL;
335         }
336
337         return data;
338 }
339
340 SAM_USERINFO_CTR *cac_MakeUserInfoCtr( TALLOC_CTX * mem_ctx,
341                                        CacUserInfo * info )
342 {
343         SAM_USERINFO_CTR *ctr = NULL;
344
345         /*the flags we are 'setting'- include/passdb.h */
346         uint32 flags =
347                 ACCT_USERNAME | ACCT_FULL_NAME | ACCT_PRIMARY_GID |
348                 ACCT_DESCRIPTION | ACCT_COMMENT | ACCT_HOME_DIR |
349                 ACCT_HOME_DRIVE | ACCT_LOGON_SCRIPT | ACCT_PROFILE |
350                 ACCT_WORKSTATIONS | ACCT_FLAGS;
351
352         NTTIME logon_time;
353         NTTIME logoff_time;
354         NTTIME kickoff_time;
355         NTTIME pass_last_set_time;
356         NTTIME pass_can_change_time;
357         NTTIME pass_must_change_time;
358
359         UNISTR2 user_name;
360         UNISTR2 full_name;
361         UNISTR2 home_dir;
362         UNISTR2 dir_drive;
363         UNISTR2 log_scr;
364         UNISTR2 prof_path;
365         UNISTR2 desc;
366         UNISTR2 wkstas;
367         UNISTR2 mung_dial;
368         UNISTR2 unk;
369
370         ctr = talloc( mem_ctx, SAM_USERINFO_CTR );
371         if ( !ctr )
372                 return NULL;
373
374         ZERO_STRUCTP( ctr->info.id23 );
375
376         ctr->info.id21 = talloc( mem_ctx, SAM_USER_INFO_21 );
377         if ( !ctr->info.id21 )
378                 return NULL;
379
380         ctr->switch_value = 21;
381
382         ZERO_STRUCTP( ctr->info.id21 );
383
384         unix_to_nt_time( &logon_time, info->logon_time );
385         unix_to_nt_time( &logoff_time, info->logoff_time );
386         unix_to_nt_time( &kickoff_time, info->kickoff_time );
387         unix_to_nt_time( &pass_last_set_time, info->pass_last_set_time );
388         unix_to_nt_time( &pass_can_change_time, info->pass_can_change_time );
389         unix_to_nt_time( &pass_must_change_time,
390                          info->pass_must_change_time );
391
392         /*initialize the strings */
393         init_unistr2( &user_name, info->username, UNI_STR_TERMINATE );
394         init_unistr2( &full_name, info->full_name, UNI_STR_TERMINATE );
395         init_unistr2( &home_dir, info->home_dir, UNI_STR_TERMINATE );
396         init_unistr2( &dir_drive, info->home_drive, UNI_STR_TERMINATE );
397         init_unistr2( &log_scr, info->logon_script, UNI_STR_TERMINATE );
398         init_unistr2( &prof_path, info->profile_path, UNI_STR_TERMINATE );
399         init_unistr2( &desc, info->description, UNI_STR_TERMINATE );
400         init_unistr2( &wkstas, info->workstations, UNI_STR_TERMINATE );
401         init_unistr2( &unk, "\0", UNI_STR_TERMINATE );
402         init_unistr2( &mung_dial, info->dial, UNI_STR_TERMINATE );
403
404         /*manually set passmustchange */
405         ctr->info.id21->passmustchange =
406                 ( info->pass_must_change ) ? 0x01 : 0x00;
407
408         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 */
409                                info->logon_hours,
410                                info->bad_passwd_count, info->logon_count );
411
412         return ctr;
413
414 }
415
416 char *talloc_unistr2_to_ascii( TALLOC_CTX * mem_ctx, UNISTR2 str )
417 {
418         char *buf = NULL;
419
420         if ( !mem_ctx )
421                 return NULL;
422
423         buf = talloc_array( mem_ctx, char, ( str.uni_str_len + 1 ) );
424         if ( !buf )
425                 return NULL;
426
427         unistr2_to_ascii( buf, &str, str.uni_str_len + 1 );
428
429         return buf;
430 }
431
432 CacUserInfo *cac_MakeUserInfo( TALLOC_CTX * mem_ctx, SAM_USERINFO_CTR * ctr )
433 {
434         CacUserInfo *info = NULL;
435         SAM_USER_INFO_21 *id21 = NULL;
436
437         if ( !ctr || ctr->switch_value != 21 )
438                 return NULL;
439
440         info = talloc( mem_ctx, CacUserInfo );
441         if ( !info )
442                 return NULL;
443
444         id21 = ctr->info.id21;
445
446         ZERO_STRUCTP( info );
447
448         info->logon_time = nt_time_to_unix( id21->logon_time );
449         info->logoff_time = nt_time_to_unix( id21->logoff_time );
450         info->kickoff_time = nt_time_to_unix( id21->kickoff_time );
451         info->pass_last_set_time =
452                 nt_time_to_unix( id21->pass_last_set_time );
453         info->pass_can_change_time =
454                 nt_time_to_unix( id21->pass_can_change_time );
455         info->pass_must_change_time =
456                 nt_time_to_unix( id21->pass_must_change_time );
457
458         info->username =
459                 talloc_unistr2_to_ascii( mem_ctx, id21->uni_user_name );
460         if ( !info->username )
461                 return NULL;
462
463         info->full_name =
464                 talloc_unistr2_to_ascii( mem_ctx, id21->uni_full_name );
465         if ( !info->full_name )
466                 return NULL;
467
468         info->home_dir =
469                 talloc_unistr2_to_ascii( mem_ctx, id21->uni_home_dir );
470         if ( !info->home_dir )
471                 return NULL;
472
473         info->home_drive =
474                 talloc_unistr2_to_ascii( mem_ctx, id21->uni_dir_drive );
475         if ( !info->home_drive )
476                 return NULL;
477
478         info->logon_script =
479                 talloc_unistr2_to_ascii( mem_ctx, id21->uni_logon_script );
480         if ( !info->logon_script )
481                 return NULL;
482
483         info->profile_path =
484                 talloc_unistr2_to_ascii( mem_ctx, id21->uni_profile_path );
485         if ( !info->profile_path )
486                 return NULL;
487
488         info->description =
489                 talloc_unistr2_to_ascii( mem_ctx, id21->uni_acct_desc );
490         if ( !info->description )
491                 return NULL;
492
493         info->workstations =
494                 talloc_unistr2_to_ascii( mem_ctx, id21->uni_workstations );
495         if ( !info->workstations )
496                 return NULL;
497
498         info->dial =
499                 talloc_unistr2_to_ascii( mem_ctx, id21->uni_munged_dial );
500         if ( !info->dial )
501                 return NULL;
502
503         info->rid = id21->user_rid;
504         info->group_rid = id21->group_rid;
505         info->acb_mask = id21->acb_info;
506         info->bad_passwd_count = id21->bad_password_count;
507         info->logon_count = id21->logon_count;
508
509         memcpy( info->nt_password, id21->nt_pwd, 8 );
510         memcpy( info->lm_password, id21->lm_pwd, 8 );
511
512         info->logon_hours =
513                 ( LOGON_HRS * ) talloc_memdup( mem_ctx, &( id21->logon_hrs ),
514                                                sizeof( LOGON_HRS ) );
515         if ( !info->logon_hours )
516                 return NULL;
517
518         info->pass_must_change = ( id21->passmustchange ) ? True : False;
519
520         return info;
521 }
522
523 CacGroupInfo *cac_MakeGroupInfo( TALLOC_CTX * mem_ctx, GROUP_INFO_CTR * ctr )
524 {
525         CacGroupInfo *info = NULL;
526
527         if ( !mem_ctx || !ctr || ctr->switch_value1 != 1 )
528                 return NULL;
529
530         info = talloc( mem_ctx, CacGroupInfo );
531         if ( !info )
532                 return NULL;
533
534         info->name =
535                 talloc_unistr2_to_ascii( mem_ctx,
536                                          ctr->group.info1.uni_acct_name );
537         if ( !info->name )
538                 return NULL;
539
540         info->description =
541                 talloc_unistr2_to_ascii( mem_ctx,
542                                          ctr->group.info1.uni_acct_desc );
543         if ( !info->description )
544                 return NULL;
545
546         info->num_members = ctr->group.info1.num_members;
547
548         return info;
549 }
550
551 GROUP_INFO_CTR *cac_MakeGroupInfoCtr( TALLOC_CTX * mem_ctx,
552                                       CacGroupInfo * info )
553 {
554         GROUP_INFO_CTR *ctr = NULL;
555
556         if ( !mem_ctx || !info )
557                 return NULL;
558
559         ctr = talloc( mem_ctx, GROUP_INFO_CTR );
560         if ( !ctr )
561                 return NULL;
562
563         ctr->switch_value1 = 1;
564
565         init_samr_group_info1( &( ctr->group.info1 ), info->name,
566                                info->description, info->num_members );
567
568         return ctr;
569 }
570
571 CacAliasInfo *cac_MakeAliasInfo( TALLOC_CTX * mem_ctx, ALIAS_INFO_CTR ctr )
572 {
573         CacGroupInfo *info = NULL;
574
575         if ( !mem_ctx || ctr.level != 1 )
576                 return NULL;
577
578         info = talloc( mem_ctx, CacAliasInfo );
579         if ( !info )
580                 return NULL;
581
582         info->name =
583                 talloc_unistr2_to_ascii( mem_ctx,
584                                          *( ctr.alias.info1.name.string ) );
585         if ( !info->name )
586                 return NULL;
587
588         info->description =
589                 talloc_unistr2_to_ascii( mem_ctx,
590                                          *( ctr.alias.info1.description.
591                                             string ) );
592         if ( !info->name )
593                 return NULL;
594
595         info->num_members = ctr.alias.info1.num_member;
596
597         return info;
598 }
599
600 ALIAS_INFO_CTR *cac_MakeAliasInfoCtr( TALLOC_CTX * mem_ctx,
601                                       CacAliasInfo * info )
602 {
603         ALIAS_INFO_CTR *ctr = NULL;
604
605         if ( !mem_ctx || !info )
606                 return NULL;
607
608         ctr = talloc( mem_ctx, ALIAS_INFO_CTR );
609         if ( !ctr )
610                 return NULL;
611
612         ctr->level = 1;
613
614         init_samr_alias_info1( &( ctr->alias.info1 ), info->name,
615                                info->num_members, info->description );
616
617         return ctr;
618 }
619
620 CacDomainInfo *cac_MakeDomainInfo( TALLOC_CTX * mem_ctx,
621                                    SAM_UNK_INFO_1 * info1,
622                                    SAM_UNK_INFO_2 * info2,
623                                    SAM_UNK_INFO_12 * info12 )
624 {
625         CacDomainInfo *info = NULL;
626
627         if ( !mem_ctx || !info1 || !info2 || !info12 )
628                 return NULL;
629
630         info = talloc( mem_ctx, CacDomainInfo );
631         if ( !info )
632                 return NULL;
633
634         info->min_pass_length = info1->min_length_password;
635         info->pass_history = info1->password_history;
636
637         cac_InitCacTime( &( info->expire ), info1->expire );
638         cac_InitCacTime( &( info->min_pass_age ), info1->min_passwordage );
639
640         info->server_role = info2->server_role;
641         info->num_users = info2->num_domain_usrs;
642         info->num_domain_groups = info2->num_domain_grps;
643         info->num_local_groups = info2->num_local_grps;
644
645         /*if these have been ZERO'd out we need to know. uni_str_len will be 0 */
646         if ( info2->uni_comment.uni_str_len == 0 ) {
647                 info->comment = talloc_strdup( mem_ctx, "\0" );
648         } else {
649                 info->comment =
650                         talloc_unistr2_to_ascii( mem_ctx,
651                                                  info2->uni_comment );
652         }
653
654         if ( info2->uni_domain.uni_str_len == 0 ) {
655                 info->domain_name = talloc_strdup( mem_ctx, "\0" );
656         } else {
657                 info->domain_name =
658                         talloc_unistr2_to_ascii( mem_ctx, info2->uni_domain );
659         }
660
661         if ( info2->uni_server.uni_str_len == 0 ) {
662                 info->server_name = talloc_strdup( mem_ctx, "\0" );
663         } else {
664                 info->server_name =
665                         talloc_unistr2_to_ascii( mem_ctx, info2->uni_server );
666         }
667
668
669         cac_InitCacTime( &( info->lockout_duration ), info12->duration );
670         cac_InitCacTime( &( info->lockout_reset ), info12->reset_count );
671         info->num_bad_attempts = info12->bad_attempt_lockout;
672
673         return info;
674 }
675
676 char *cac_unistr_ascii( TALLOC_CTX * mem_ctx, UNISTR src )
677 {
678         char *buf;
679         uint32 len;
680
681         if ( !mem_ctx || !src.buffer )
682                 return NULL;
683
684         len = unistrlen( src.buffer ) + 1;
685
686         buf = TALLOC_ZERO_ARRAY( mem_ctx, char, len );
687         if ( !buf )
688                 return NULL;
689
690         rpcstr_pull( buf, src.buffer, len, -1, STR_TERMINATE );
691
692         return buf;
693 }
694
695 CacService *cac_MakeServiceArray( TALLOC_CTX * mem_ctx,
696                                   ENUM_SERVICES_STATUS * svc,
697                                   uint32 num_services )
698 {
699         int i;
700         CacService *services = NULL;
701
702         if ( !mem_ctx || !svc )
703                 return NULL;
704
705         services = TALLOC_ZERO_ARRAY( mem_ctx, CacService, num_services );
706         if ( !services )
707                 return NULL;
708
709         for ( i = 0; i < num_services; i++ ) {
710                 services[i].service_name =
711                         cac_unistr_ascii( mem_ctx, svc[i].servicename );
712                 services[i].display_name =
713                         cac_unistr_ascii( mem_ctx, svc[i].displayname );
714
715                 if ( !services[i].service_name || !services[i].display_name )
716                         return NULL;
717
718                 services[i].status = svc[i].status;
719         }
720
721         return services;
722 }
723
724 int cac_InitCacServiceConfig( TALLOC_CTX * mem_ctx, SERVICE_CONFIG * src,
725                               CacServiceConfig * dest )
726 {
727         if ( !src || !dest )
728                 return CAC_FAILURE;
729
730         dest->exe_path =
731                 talloc_unistr2_to_ascii( mem_ctx, *src->executablepath );
732         if ( !dest->exe_path )
733                 return CAC_FAILURE;
734
735         dest->load_order_group =
736                 talloc_unistr2_to_ascii( mem_ctx, *src->loadordergroup );
737         if ( !dest->load_order_group )
738                 return CAC_FAILURE;
739
740         dest->dependencies =
741                 talloc_unistr2_to_ascii( mem_ctx, *src->dependencies );
742         if ( !dest->dependencies )
743                 return CAC_FAILURE;
744
745         dest->start_name =
746                 talloc_unistr2_to_ascii( mem_ctx, *src->startname );
747         if ( !dest->start_name )
748                 return CAC_FAILURE;
749
750         dest->display_name =
751                 talloc_unistr2_to_ascii( mem_ctx, *src->displayname );
752         if ( !dest->display_name )
753                 return CAC_FAILURE;
754
755         dest->type = src->service_type;
756         dest->start_type = src->start_type;
757         dest->error_control = src->error_control;
758         dest->tag_id = src->tag_id;
759
760         return CAC_SUCCESS;
761 }