r23801: The FSF has moved around a lot. This fixes their Mass Ave address.
[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 3 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, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #include "libmsrpc.h"
23 #include "libmsrpc_internal.h"
24
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 );
28
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 )
31 {
32         SMBCSRV *srv = NULL;
33         struct rpc_pipe_client *pipe_hnd = NULL;
34
35         if ( !hnd ) {
36                 return NULL;
37         }
38
39         if ( hnd->_internal.pipes[pi_idx] == False ) {
40                 hnd->status = NT_STATUS_INVALID_HANDLE;
41                 return NULL;
42         }
43
44         srv = cac_GetServer( hnd );
45         if ( !srv ) {
46                 hnd->status = NT_STATUS_INVALID_CONNECTION;
47                 return NULL;
48         }
49
50         pipe_hnd = srv->cli->pipe_list;
51
52         while ( pipe_hnd != NULL && pipe_hnd->pipe_idx != pi_idx ) {
53                 pipe_hnd = pipe_hnd->next;
54         }
55
56         return pipe_hnd;
57 }
58
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 )
61 {
62
63         if ( !path )
64                 return CAC_FAILURE;
65
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;
90         } else {
91                 return CAC_FAILURE;
92         }
93
94         return CAC_SUCCESS;
95 }
96
97
98
99 RPC_DATA_BLOB *cac_MakeRpcDataBlob( TALLOC_CTX * mem_ctx, uint32 data_type,
100                                     REG_VALUE_DATA data )
101 {
102         RPC_DATA_BLOB *blob = NULL;
103         int i;
104         uint32 size = 0;
105         uint8 *multi = NULL;
106         uint32 multi_idx = 0;
107
108         blob = talloc( mem_ctx, RPC_DATA_BLOB );
109
110         if ( !blob ) {
111                 errno = ENOMEM;
112                 return NULL;
113         }
114
115         switch ( data_type ) {
116         case REG_SZ:
117                 init_rpc_blob_str( blob, data.reg_sz,
118                                    strlen( data.reg_sz ) + 1 );
119                 break;
120
121         case REG_EXPAND_SZ:
122                 init_rpc_blob_str( blob, data.reg_expand_sz,
123                                    strlen( data.reg_sz ) + 1 );
124                 break;
125
126         case REG_BINARY:
127                 init_rpc_blob_bytes( blob, data.reg_binary.data,
128                                      data.reg_binary.data_length );
129                 break;
130
131         case REG_DWORD:
132                 init_rpc_blob_uint32( blob, data.reg_dword );
133                 break;
134
135         case REG_DWORD_BIG_ENDIAN:
136                 init_rpc_blob_uint32( blob, data.reg_dword_be );
137                 break;
138
139         case REG_MULTI_SZ:
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;
143                 }
144
145          /**need a whole bunch of unicode strings in a row (seperated by null characters), with an extra null-character on the end*/
146
147                 multi = TALLOC_ZERO_ARRAY( mem_ctx, uint8, ( size + 1 ) * 2 );  /*size +1 for the extra null character */
148                 if ( !multi ) {
149                         errno = ENOMEM;
150                         break;
151                 }
152
153                 /*do it using rpcstr_push() */
154                 multi_idx = 0;
155                 for ( i = 0; i < data.reg_multi_sz.num_strings; i++ ) {
156                         size_t len =
157                                 strlen( data.reg_multi_sz.strings[i] ) + 1;
158
159                         rpcstr_push( ( multi + multi_idx ),
160                                      data.reg_multi_sz.strings[i], len * 2,
161                                      STR_TERMINATE );
162
163                         /* x2 becuase it is a uint8 buffer */
164                         multi_idx += len * 2;
165                 }
166
167                 /*now initialize the buffer as binary data */
168                 init_rpc_blob_bytes( blob, multi, ( size + 1 ) * 2 );
169
170                 break;
171
172         default:
173                 TALLOC_FREE( blob );
174                 blob = NULL;
175                 return NULL;
176         }
177
178         if ( !( blob->buffer ) ) {
179                 TALLOC_FREE( blob );
180                 return NULL;
181         }
182
183         return blob;
184 }
185
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 )
188 {
189         char *buf;
190
191         int i = 0;
192
193         uint32 str_len = 0;
194
195         /*don't allocate more space than we need */
196         while ( ( str_len ) < num_bytes / 2 && src[str_len] != 0x0000 )
197                 str_len++;
198
199         /*need room for a '\0' */
200         str_len++;
201
202         buf = TALLOC_ARRAY( mem_ctx, char, str_len );
203
204         if ( !buf ) {
205                 return NULL;
206         }
207
208         for ( i = 0; i < num_bytes / 2; i++ ) {
209                 buf[i] = ( ( char * ) src )[2 * i];
210         }
211
212         buf[str_len - 1] = '\0';
213
214         return buf;
215 }
216
217 REG_VALUE_DATA *cac_MakeRegValueData( TALLOC_CTX * mem_ctx, uint32 data_type,
218                                       REGVAL_BUFFER buf )
219 {
220         REG_VALUE_DATA *data;
221
222         uint32 i;
223
224         /*all of the following used for MULTI_SZ data */
225         uint32 size = 0;
226         uint32 len = 0;
227         uint32 multi_idx = 0;
228         uint32 num_strings = 0;
229         char **strings = NULL;
230
231         data = talloc( mem_ctx, REG_VALUE_DATA );
232         if ( !data ) {
233                 errno = ENOMEM;
234                 return NULL;
235         }
236
237         switch ( data_type ) {
238         case REG_SZ:
239                 data->reg_sz =
240                         cac_unistr_to_str( mem_ctx, buf.buffer, buf.buf_len );
241                 if ( !data->reg_sz ) {
242                         TALLOC_FREE( data );
243                         errno = ENOMEM;
244                         data = NULL;
245                 }
246
247                 break;
248
249         case REG_EXPAND_SZ:
250                 data->reg_expand_sz =
251                         cac_unistr_to_str( mem_ctx, buf.buffer, buf.buf_len );
252
253                 if ( !data->reg_expand_sz ) {
254                         TALLOC_FREE( data );
255                         errno = ENOMEM;
256                         data = NULL;
257                 }
258
259                 break;
260
261         case REG_BINARY:
262                 size = buf.buf_len;
263
264                 data->reg_binary.data_length = size;
265
266                 if (size) {
267                         data->reg_binary.data =
268                                 ( uint8 * ) TALLOC_MEMDUP( mem_ctx, buf.buffer, size );
269                         if ( !data->reg_binary.data ) {
270                                 TALLOC_FREE( data );
271                                 errno = ENOMEM;
272                                 data = NULL;
273                         }
274                 } else {
275                         data->reg_binary.data = NULL;
276                 }
277                 break;
278
279         case REG_DWORD:
280                 data->reg_dword = *( ( uint32 * ) buf.buffer );
281                 break;
282
283         case REG_DWORD_BIG_ENDIAN:
284                 data->reg_dword_be = *( ( uint32 * ) buf.buffer );
285                 break;
286
287         case REG_MULTI_SZ:
288                 size = buf.buf_len;
289
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 )
293                                 num_strings++;
294
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 )
298                                 break;
299                 }
300
301                 if (num_strings) {
302                         strings = TALLOC_ARRAY( mem_ctx, char *, num_strings );
303
304                         if ( !strings ) {
305                                 errno = ENOMEM;
306                                 TALLOC_FREE( data );
307                                 break;
308                         }
309                 } else {
310                         strings = NULL;
311                 }
312
313                 if ( num_strings == 0 ) /*then our work here is done */
314                         break;
315
316                 for ( i = 0; i < num_strings; i++ ) {
317                         /*find out how many characters are in this string */
318                         len = 0;
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 )
322                                 len++;
323
324                         /*stay aware of the \0\0 */
325                         len++;
326
327                         strings[i] = TALLOC_ZERO_ARRAY( mem_ctx, char, len );
328
329                         /*pull out the unicode string */
330                         rpcstr_pull( strings[i], ( buf.buffer + multi_idx ),
331                                      len, -1, STR_TERMINATE );
332
333                         /*keep track of where we are in the bigger array */
334                         multi_idx += len;
335                 }
336
337                 data->reg_multi_sz.num_strings = num_strings;
338                 data->reg_multi_sz.strings = strings;
339
340                 break;
341
342         default:
343                 TALLOC_FREE( data );
344                 data = NULL;
345         }
346
347         return data;
348 }
349
350 SAM_USERINFO_CTR *cac_MakeUserInfoCtr( TALLOC_CTX * mem_ctx,
351                                        CacUserInfo * info )
352 {
353         SAM_USERINFO_CTR *ctr = NULL;
354
355         /*the flags we are 'setting'- include/passdb.h */
356         uint32 flags =
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;
361
362         NTTIME logon_time;
363         NTTIME logoff_time;
364         NTTIME kickoff_time;
365         NTTIME pass_last_set_time;
366         NTTIME pass_can_change_time;
367         NTTIME pass_must_change_time;
368
369         UNISTR2 user_name;
370         UNISTR2 full_name;
371         UNISTR2 home_dir;
372         UNISTR2 dir_drive;
373         UNISTR2 log_scr;
374         UNISTR2 prof_path;
375         UNISTR2 desc;
376         UNISTR2 wkstas;
377         UNISTR2 mung_dial;
378         UNISTR2 unk;
379
380         ctr = talloc( mem_ctx, SAM_USERINFO_CTR );
381         if ( !ctr )
382                 return NULL;
383
384         ZERO_STRUCTP( ctr->info.id23 );
385
386         ctr->info.id21 = talloc( mem_ctx, SAM_USER_INFO_21 );
387         if ( !ctr->info.id21 )
388                 return NULL;
389
390         ctr->switch_value = 21;
391
392         ZERO_STRUCTP( ctr->info.id21 );
393
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 );
401
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 );
413
414         /*manually set passmustchange */
415         ctr->info.id21->passmustchange =
416                 ( info->pass_must_change ) ? 0x01 : 0x00;
417
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 */
419                                info->logon_hours,
420                                info->bad_passwd_count, info->logon_count );
421
422         return ctr;
423
424 }
425
426 char *talloc_unistr2_to_ascii( TALLOC_CTX * mem_ctx, UNISTR2 str )
427 {
428         char *buf = NULL;
429
430         if ( !mem_ctx )
431                 return NULL;
432
433         buf = TALLOC_ARRAY( mem_ctx, char, ( str.uni_str_len + 1 ) );
434         if ( !buf )
435                 return NULL;
436
437         unistr2_to_ascii( buf, &str, str.uni_str_len + 1 );
438
439         return buf;
440 }
441
442 CacUserInfo *cac_MakeUserInfo( TALLOC_CTX * mem_ctx, SAM_USERINFO_CTR * ctr )
443 {
444         CacUserInfo *info = NULL;
445         SAM_USER_INFO_21 *id21 = NULL;
446
447         if ( !ctr || ctr->switch_value != 21 )
448                 return NULL;
449
450         info = talloc( mem_ctx, CacUserInfo );
451         if ( !info )
452                 return NULL;
453
454         id21 = ctr->info.id21;
455
456         ZERO_STRUCTP( info );
457
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 );
467
468         info->username =
469                 talloc_unistr2_to_ascii( mem_ctx, id21->uni_user_name );
470         if ( !info->username )
471                 return NULL;
472
473         info->full_name =
474                 talloc_unistr2_to_ascii( mem_ctx, id21->uni_full_name );
475         if ( !info->full_name )
476                 return NULL;
477
478         info->home_dir =
479                 talloc_unistr2_to_ascii( mem_ctx, id21->uni_home_dir );
480         if ( !info->home_dir )
481                 return NULL;
482
483         info->home_drive =
484                 talloc_unistr2_to_ascii( mem_ctx, id21->uni_dir_drive );
485         if ( !info->home_drive )
486                 return NULL;
487
488         info->logon_script =
489                 talloc_unistr2_to_ascii( mem_ctx, id21->uni_logon_script );
490         if ( !info->logon_script )
491                 return NULL;
492
493         info->profile_path =
494                 talloc_unistr2_to_ascii( mem_ctx, id21->uni_profile_path );
495         if ( !info->profile_path )
496                 return NULL;
497
498         info->description =
499                 talloc_unistr2_to_ascii( mem_ctx, id21->uni_acct_desc );
500         if ( !info->description )
501                 return NULL;
502
503         info->workstations =
504                 talloc_unistr2_to_ascii( mem_ctx, id21->uni_workstations );
505         if ( !info->workstations )
506                 return NULL;
507
508         info->dial =
509                 talloc_unistr2_to_ascii( mem_ctx, id21->uni_munged_dial );
510         if ( !info->dial )
511                 return NULL;
512
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;
518
519         memcpy( info->nt_password, id21->nt_pwd, 8 );
520         memcpy( info->lm_password, id21->lm_pwd, 8 );
521
522         info->logon_hours =
523                 ( LOGON_HRS * ) TALLOC_MEMDUP( mem_ctx, &( id21->logon_hrs ),
524                                                sizeof( LOGON_HRS ) );
525         if ( !info->logon_hours )
526                 return NULL;
527
528         info->pass_must_change = ( id21->passmustchange ) ? True : False;
529
530         return info;
531 }
532
533 CacGroupInfo *cac_MakeGroupInfo( TALLOC_CTX * mem_ctx, GROUP_INFO_CTR * ctr )
534 {
535         CacGroupInfo *info = NULL;
536
537         if ( !mem_ctx || !ctr || ctr->switch_value1 != 1 )
538                 return NULL;
539
540         info = talloc( mem_ctx, CacGroupInfo );
541         if ( !info )
542                 return NULL;
543
544         info->name =
545                 talloc_unistr2_to_ascii( mem_ctx,
546                                          ctr->group.info1.uni_acct_name );
547         if ( !info->name )
548                 return NULL;
549
550         info->description =
551                 talloc_unistr2_to_ascii( mem_ctx,
552                                          ctr->group.info1.uni_acct_desc );
553         if ( !info->description )
554                 return NULL;
555
556         info->num_members = ctr->group.info1.num_members;
557
558         return info;
559 }
560
561 GROUP_INFO_CTR *cac_MakeGroupInfoCtr( TALLOC_CTX * mem_ctx,
562                                       CacGroupInfo * info )
563 {
564         GROUP_INFO_CTR *ctr = NULL;
565
566         if ( !mem_ctx || !info )
567                 return NULL;
568
569         ctr = talloc( mem_ctx, GROUP_INFO_CTR );
570         if ( !ctr )
571                 return NULL;
572
573         ctr->switch_value1 = 1;
574
575         init_samr_group_info1( &( ctr->group.info1 ), info->name,
576                                info->description, info->num_members );
577
578         return ctr;
579 }
580
581 CacAliasInfo *cac_MakeAliasInfo( TALLOC_CTX * mem_ctx, ALIAS_INFO_CTR ctr )
582 {
583         CacGroupInfo *info = NULL;
584
585         if ( !mem_ctx || ctr.level != 1 )
586                 return NULL;
587
588         info = talloc( mem_ctx, CacAliasInfo );
589         if ( !info )
590                 return NULL;
591
592         info->name =
593                 talloc_unistr2_to_ascii( mem_ctx,
594                                          *( ctr.alias.info1.name.string ) );
595         if ( !info->name )
596                 return NULL;
597
598         info->description =
599                 talloc_unistr2_to_ascii( mem_ctx,
600                                          *( ctr.alias.info1.description.
601                                             string ) );
602         if ( !info->name )
603                 return NULL;
604
605         info->num_members = ctr.alias.info1.num_member;
606
607         return info;
608 }
609
610 ALIAS_INFO_CTR *cac_MakeAliasInfoCtr( TALLOC_CTX * mem_ctx,
611                                       CacAliasInfo * info )
612 {
613         ALIAS_INFO_CTR *ctr = NULL;
614
615         if ( !mem_ctx || !info )
616                 return NULL;
617
618         ctr = talloc( mem_ctx, ALIAS_INFO_CTR );
619         if ( !ctr )
620                 return NULL;
621
622         ctr->level = 1;
623
624         init_samr_alias_info1( &( ctr->alias.info1 ), info->name,
625                                info->num_members, info->description );
626
627         return ctr;
628 }
629
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 )
634 {
635         CacDomainInfo *info = NULL;
636
637         if ( !mem_ctx || !info1 || !info2 || !info12 )
638                 return NULL;
639
640         info = talloc( mem_ctx, CacDomainInfo );
641         if ( !info )
642                 return NULL;
643
644         info->min_pass_length = info1->min_length_password;
645         info->pass_history = info1->password_history;
646
647         cac_InitCacTime( &( info->expire ), info1->expire );
648         cac_InitCacTime( &( info->min_pass_age ), info1->min_passwordage );
649
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;
654
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" );
658         } else {
659                 info->comment =
660                         talloc_unistr2_to_ascii( mem_ctx,
661                                                  info2->uni_comment );
662         }
663
664         if ( info2->uni_domain.uni_str_len == 0 ) {
665                 info->domain_name = talloc_strdup( mem_ctx, "\0" );
666         } else {
667                 info->domain_name =
668                         talloc_unistr2_to_ascii( mem_ctx, info2->uni_domain );
669         }
670
671         if ( info2->uni_server.uni_str_len == 0 ) {
672                 info->server_name = talloc_strdup( mem_ctx, "\0" );
673         } else {
674                 info->server_name =
675                         talloc_unistr2_to_ascii( mem_ctx, info2->uni_server );
676         }
677
678
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;
682
683         return info;
684 }
685
686 char *cac_unistr_ascii( TALLOC_CTX * mem_ctx, UNISTR src )
687 {
688         char *buf;
689         uint32 len;
690
691         if ( !mem_ctx || !src.buffer )
692                 return NULL;
693
694         len = unistrlen( src.buffer ) + 1;
695
696         buf = TALLOC_ZERO_ARRAY( mem_ctx, char, len );
697         if ( !buf )
698                 return NULL;
699
700         rpcstr_pull( buf, src.buffer, len, -1, STR_TERMINATE );
701
702         return buf;
703 }
704
705 CacService *cac_MakeServiceArray( TALLOC_CTX * mem_ctx,
706                                   ENUM_SERVICES_STATUS * svc,
707                                   uint32 num_services )
708 {
709         int i;
710         CacService *services = NULL;
711
712         if ( !mem_ctx || !svc )
713                 return NULL;
714
715         if (num_services) {
716                 services = TALLOC_ZERO_ARRAY( mem_ctx, CacService, num_services );
717                 if ( !services )
718                         return NULL;
719         } else {
720                 services = NULL;
721         }
722
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 );
728
729                 if ( !services[i].service_name || !services[i].display_name )
730                         return NULL;
731
732                 services[i].status = svc[i].status;
733         }
734
735         return services;
736 }
737
738 int cac_InitCacServiceConfig( TALLOC_CTX * mem_ctx, SERVICE_CONFIG * src,
739                               CacServiceConfig * dest )
740 {
741         if ( !src || !dest )
742                 return CAC_FAILURE;
743
744         dest->exe_path =
745                 talloc_unistr2_to_ascii( mem_ctx, *src->executablepath );
746         if ( !dest->exe_path )
747                 return CAC_FAILURE;
748
749         dest->load_order_group =
750                 talloc_unistr2_to_ascii( mem_ctx, *src->loadordergroup );
751         if ( !dest->load_order_group )
752                 return CAC_FAILURE;
753
754         dest->dependencies =
755                 talloc_unistr2_to_ascii( mem_ctx, *src->dependencies );
756         if ( !dest->dependencies )
757                 return CAC_FAILURE;
758
759         dest->start_name =
760                 talloc_unistr2_to_ascii( mem_ctx, *src->startname );
761         if ( !dest->start_name )
762                 return CAC_FAILURE;
763
764         dest->display_name =
765                 talloc_unistr2_to_ascii( mem_ctx, *src->displayname );
766         if ( !dest->display_name )
767                 return CAC_FAILURE;
768
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;
773
774         return CAC_SUCCESS;
775 }