This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[vlendec/samba-autobuild/.git] / source3 / rpc_server / srv_reg_nt.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Andrew Tridgell               1992-1997.
5  *  Copyright (C) Luke Kenneth Casson Leighton  1996-1997.
6  *  Copyright (C) Paul Ashton                        1997.
7  *  Copyright (C) Jeremy Allison                     2001.
8  *  Copyright (C) Gerald Carter                      2002.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *  
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *  
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 /* Implementation of registry functions. */
26
27 #include "includes.h"
28
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_RPC_SRV
31
32 #define REGSTR_PRODUCTTYPE              "ProductType"
33 #define REG_PT_WINNT                    "WinNT"
34 #define REG_PT_LANMANNT                 "LanmanNT"
35 #define REG_PT_SERVERNT                 "ServerNT"
36
37 #define OUR_HANDLE(hnd) (((hnd)==NULL)?"NULL":(IVAL((hnd)->data5,4)==(uint32)sys_getpid()?"OURS":"OTHER")), \
38 ((unsigned int)IVAL((hnd)->data5,4)),((unsigned int)sys_getpid())
39
40
41 static REGISTRY_KEY *regkeys_list;
42
43
44 /******************************************************************
45  free() function for REGISTRY_KEY
46  *****************************************************************/
47  
48 static void free_regkey_info(void *ptr)
49 {
50         REGISTRY_KEY *info = (REGISTRY_KEY*)ptr;
51         
52         DLIST_REMOVE(regkeys_list, info);
53
54         SAFE_FREE(info);
55 }
56
57 /******************************************************************
58  Find a registry key handle and return a REGISTRY_KEY
59  *****************************************************************/
60
61 static REGISTRY_KEY *find_regkey_index_by_hnd(pipes_struct *p, POLICY_HND *hnd)
62 {
63         REGISTRY_KEY *regkey = NULL;
64
65         if(!find_policy_by_hnd(p,hnd,(void **)&regkey)) {
66                 DEBUG(2,("find_regkey_index_by_hnd: Registry Key not found: "));
67                 return NULL;
68         }
69
70         return regkey;
71 }
72
73
74 /*******************************************************************
75  Function for open a new registry handle and creating a handle 
76  Note that P should be valid & hnd should already have space
77  
78  When we open a key, we store the full path to the key as 
79  HK[LM|U]\<key>\<key>\...
80  *******************************************************************/
81  
82 static NTSTATUS open_registry_key(pipes_struct *p, POLICY_HND *hnd, REGISTRY_KEY *parent,
83                                 char *subkeyname, uint32 access_granted  )
84 {
85         REGISTRY_KEY    *regkey = NULL;
86         NTSTATUS        result = NT_STATUS_OK;
87         REGSUBKEY_CTR   subkeys;
88         
89         DEBUG(7,("open_registry_key: name = [%s][%s]\n", 
90                 parent ? parent->name : "NULL", subkeyname));
91
92         if ((regkey=(REGISTRY_KEY*)malloc(sizeof(REGISTRY_KEY))) == NULL)
93                 return NT_STATUS_NO_MEMORY;
94                 
95         ZERO_STRUCTP( regkey );
96         
97         /* 
98          * very crazy, but regedit.exe on Win2k will attempt to call 
99          * REG_OPEN_ENTRY with a keyname of "".  We should return a new 
100          * (second) handle here on the key->name.  regedt32.exe does 
101          * not do this stupidity.   --jerry
102          */
103         
104         if (!subkeyname || !*subkeyname ) {
105                 pstrcpy( regkey->name, parent->name );  
106         }
107         else {
108                 pstrcpy( regkey->name, "" );
109                 if ( parent ) {
110                         pstrcat( regkey->name, parent->name );
111                         pstrcat( regkey->name, "\\" );
112                 }
113                 pstrcat( regkey->name, subkeyname );
114         }
115         
116         /* Look up the table of registry I/O operations */
117
118         if ( !(regkey->hook = reghook_cache_find( regkey->name )) ) {
119                 DEBUG(0,("open_registry_key: Failed to assigned a REGISTRY_HOOK to [%s]\n",
120                         regkey->name ));
121                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
122         }
123         
124         /* check if the path really exists; failed is indicated by -1 */
125         /* if the subkey count failed, bail out */
126
127         ZERO_STRUCTP( &subkeys );
128         
129         regsubkey_ctr_init( &subkeys );
130         
131         if ( fetch_reg_keys( regkey, &subkeys ) == -1 )  {
132         
133                 /* don't really know what to return here */
134                 
135                 result = NT_STATUS_NO_SUCH_FILE;
136         }
137         else {
138                 /* 
139                  * This would previously return NT_STATUS_TOO_MANY_SECRETS
140                  * that doesn't sound quite right to me  --jerry
141                  */
142                 
143                 if ( !create_policy_hnd( p, hnd, free_regkey_info, regkey ) )
144                         result = NT_STATUS_OBJECT_NAME_NOT_FOUND; 
145         }
146         
147         /* clean up */
148
149         regsubkey_ctr_destroy( &subkeys );
150         
151         if ( ! NT_STATUS_IS_OK(result) )
152                 SAFE_FREE( regkey );
153         else
154                 DLIST_ADD( regkeys_list, regkey );
155
156         
157         DEBUG(7,("open_registry_key: exit\n"));
158
159         return result;
160 }
161
162 /*******************************************************************
163  Function for open a new registry handle and creating a handle 
164  Note that P should be valid & hnd should already have space
165  *******************************************************************/
166
167 static BOOL close_registry_key(pipes_struct *p, POLICY_HND *hnd)
168 {
169         REGISTRY_KEY *regkey = find_regkey_index_by_hnd(p, hnd);
170         
171         if ( !regkey ) {
172                 DEBUG(2,("close_registry_key: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(hnd)));
173                 return False;
174         }
175         
176         close_policy_hnd(p, hnd);
177         
178         return True;
179 }
180
181 /********************************************************************
182  retrieve information about the subkeys
183  *******************************************************************/
184  
185 static BOOL get_subkey_information( REGISTRY_KEY *key, uint32 *maxnum, uint32 *maxlen )
186 {
187         int             num_subkeys, i;
188         uint32          max_len;
189         REGSUBKEY_CTR   subkeys;
190         uint32          len;
191         
192         if ( !key )
193                 return False;
194
195         ZERO_STRUCTP( &subkeys );
196         
197         regsubkey_ctr_init( &subkeys ); 
198            
199         if ( fetch_reg_keys( key, &subkeys ) == -1 )
200                 return False;
201
202         /* find the longest string */
203         
204         max_len = 0;
205         num_subkeys = regsubkey_ctr_numkeys( &subkeys );
206         
207         for ( i=0; i<num_subkeys; i++ ) {
208                 len = strlen( regsubkey_ctr_specific_key(&subkeys, i) );
209                 max_len = MAX(max_len, len);
210         }
211
212         *maxnum = num_subkeys;
213         *maxlen = max_len*2;
214         
215         regsubkey_ctr_destroy( &subkeys );
216         
217         return True;
218 }
219
220 /********************************************************************
221  retrieve information about the values.  We don't store values 
222  here.  The registry tdb is intended to be a frontend to oether 
223  Samba tdb's (such as ntdrivers.tdb).
224  *******************************************************************/
225  
226 static BOOL get_value_information( REGISTRY_KEY *key, uint32 *maxnum, 
227                                     uint32 *maxlen, uint32 *maxsize )
228 {
229         REGVAL_CTR      values;
230         REGISTRY_VALUE  *val;
231         uint32          sizemax, lenmax;
232         int             i, num_values;
233         
234         if ( !key )
235                 return False;
236
237
238         ZERO_STRUCTP( &values );
239         
240         regval_ctr_init( &values );
241         
242         if ( fetch_reg_values( key, &values ) == -1 )
243                 return False;
244         
245         lenmax = sizemax = 0;
246         num_values = regval_ctr_numvals( &values );
247         
248         val = regval_ctr_specific_value( &values, 0 );
249         
250         for ( i=0; i<num_values && val; i++ ) 
251         {
252                 lenmax  = MAX(lenmax,  strlen(val->valuename)+1 );
253                 sizemax = MAX(sizemax, val->size );
254                 
255                 val = regval_ctr_specific_value( &values, i );
256         }
257
258         *maxnum   = num_values;
259         *maxlen   = lenmax;
260         *maxsize  = sizemax;
261         
262         regval_ctr_destroy( &values );
263         
264         return True;
265 }
266
267
268 /********************************************************************
269  reg_close
270  ********************************************************************/
271
272 NTSTATUS _reg_close(pipes_struct *p, REG_Q_CLOSE *q_u, REG_R_CLOSE *r_u)
273 {
274         /* set up the REG unknown_1 response */
275         ZERO_STRUCT(r_u->pol);
276
277         /* close the policy handle */
278         if (!close_registry_key(p, &q_u->pol))
279                 return NT_STATUS_OBJECT_NAME_INVALID;
280
281         return NT_STATUS_OK;
282 }
283
284 /*******************************************************************
285  ********************************************************************/
286
287 NTSTATUS _reg_open_hklm(pipes_struct *p, REG_Q_OPEN_HKLM *q_u, REG_R_OPEN_HKLM *r_u)
288 {
289         return open_registry_key( p, &r_u->pol, NULL, KEY_HKLM, 0x0 );
290 }
291
292 /*******************************************************************
293  ********************************************************************/
294
295 NTSTATUS _reg_open_hkcr(pipes_struct *p, REG_Q_OPEN_HKCR *q_u, REG_R_OPEN_HKCR *r_u)
296 {
297         return open_registry_key( p, &r_u->pol, NULL, KEY_HKCR, 0x0 );
298 }
299
300 /*******************************************************************
301  ********************************************************************/
302
303 NTSTATUS _reg_open_hku(pipes_struct *p, REG_Q_OPEN_HKU *q_u, REG_R_OPEN_HKU *r_u)
304 {
305         return open_registry_key( p, &r_u->pol, NULL, KEY_HKU, 0x0 );
306 }
307
308 /*******************************************************************
309  reg_reply_open_entry
310  ********************************************************************/
311
312 NTSTATUS _reg_open_entry(pipes_struct *p, REG_Q_OPEN_ENTRY *q_u, REG_R_OPEN_ENTRY *r_u)
313 {
314         POLICY_HND pol;
315         fstring name;
316         REGISTRY_KEY *key = find_regkey_index_by_hnd(p, &q_u->pol);
317         NTSTATUS result;
318
319         DEBUG(5,("reg_open_entry: Enter\n"));
320
321         if ( !key )
322                 return NT_STATUS_INVALID_HANDLE;
323
324         rpcstr_pull(name,q_u->uni_name.buffer,sizeof(name),q_u->uni_name.uni_str_len*2,0);
325         
326         DEBUG(5,("reg_open_entry: Enter\n"));
327                    
328         result = open_registry_key( p, &pol, key, name, 0x0 );
329         
330         init_reg_r_open_entry( r_u, &pol, result );
331
332         DEBUG(5,("reg_open_entry: Exit\n"));
333
334         return r_u->status;
335 }
336
337 /*******************************************************************
338  reg_reply_info
339  ********************************************************************/
340
341 NTSTATUS _reg_info(pipes_struct *p, REG_Q_INFO *q_u, REG_R_INFO *r_u)
342 {
343         NTSTATUS                status = NT_STATUS_NO_SUCH_FILE;
344         fstring                 name;
345         char                    *value_ascii = "";
346         fstring                 value;
347         int                     value_length;
348         REGISTRY_KEY            *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
349         REGISTRY_VALUE          *val = NULL;
350         REGISTRY_VALUE          emptyval;
351         REGVAL_CTR              regvals;
352         int                     i;
353
354         DEBUG(5,("_reg_info: Enter\n"));
355
356         if ( !regkey )
357                 return NT_STATUS_INVALID_HANDLE;
358                 
359         DEBUG(7,("_reg_info: policy key name = [%s]\n", regkey->name));
360         
361         rpcstr_pull(name, q_u->uni_type.buffer, sizeof(name), q_u->uni_type.uni_str_len*2, 0);
362
363         DEBUG(5,("reg_info: looking up value: [%s]\n", name));
364
365         ZERO_STRUCTP( &regvals );
366         
367         regval_ctr_init( &regvals );
368
369         /* couple of hard coded registry values */
370         
371         if ( strequal(name, "RefusePasswordChange") ) {
372                 ZERO_STRUCTP( &emptyval );
373                 val = &emptyval;
374         
375                 goto out;
376         }
377
378         if ( strequal(name, REGSTR_PRODUCTTYPE) ) {
379                 /* This makes the server look like a member server to clients */
380                 /* which tells clients that we have our own local user and    */
381                 /* group databases and helps with ACL support.                */
382                 
383                 switch (lp_server_role()) {
384                         case ROLE_DOMAIN_PDC:
385                         case ROLE_DOMAIN_BDC:
386                                 value_ascii = REG_PT_LANMANNT;
387                                 break;
388                         case ROLE_STANDALONE:
389                                 value_ascii = REG_PT_SERVERNT;
390                                 break;
391                         case ROLE_DOMAIN_MEMBER:
392                                 value_ascii = REG_PT_WINNT;
393                                 break;
394                 }
395                 value_length = push_ucs2(value, value, value_ascii,
396                                          sizeof(value),
397                                          STR_TERMINATE|STR_NOALIGN);
398                 regval_ctr_addvalue(&regvals, REGSTR_PRODUCTTYPE, REG_SZ,
399                                     value, value_length);
400                 
401                 val = dup_registry_value( regval_ctr_specific_value( &regvals, 0 ) );
402                 
403                 status = NT_STATUS_OK;
404                 
405                 goto out;
406         }
407
408         /* else fall back to actually looking up the value */
409         
410         for ( i=0; fetch_reg_values_specific(regkey, &val, i); i++ ) 
411         {
412                 DEBUG(10,("_reg_info: Testing value [%s]\n", val->valuename));
413                 if ( StrCaseCmp( val->valuename, name ) == 0 ) {
414                         DEBUG(10,("_reg_info: Found match for value [%s]\n", name));
415                         status = NT_STATUS_OK;
416                         break;
417                 }
418                 
419                 free_registry_value( val );
420         }
421
422   
423 out:
424         new_init_reg_r_info(q_u->ptr_buf, r_u, val, status);
425         
426         regval_ctr_destroy( &regvals );
427         free_registry_value( val );
428
429         DEBUG(5,("_reg_info: Exit\n"));
430
431         return status;
432 }
433
434
435 /*****************************************************************************
436  Implementation of REG_QUERY_KEY
437  ****************************************************************************/
438  
439 NTSTATUS _reg_query_key(pipes_struct *p, REG_Q_QUERY_KEY *q_u, REG_R_QUERY_KEY *r_u)
440 {
441         NTSTATUS        status = NT_STATUS_OK;
442         REGISTRY_KEY    *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
443         
444         DEBUG(5,("_reg_query_key: Enter\n"));
445         
446         if ( !regkey )
447                 return NT_STATUS_INVALID_HANDLE;        
448         
449         if ( !get_subkey_information( regkey, &r_u->num_subkeys, &r_u->max_subkeylen ) )
450                 return NT_STATUS_ACCESS_DENIED;
451                 
452         if ( !get_value_information( regkey, &r_u->num_values, &r_u->max_valnamelen, &r_u->max_valbufsize ) )
453                 return NT_STATUS_ACCESS_DENIED; 
454
455                 
456         r_u->sec_desc = 0x00000078;     /* size for key's sec_desc */
457         
458         /* Win9x set this to 0x0 since it does not keep timestamps.
459            Doing the same here for simplicity   --jerry */
460            
461         ZERO_STRUCT(r_u->mod_time);     
462
463         DEBUG(5,("_reg_query_key: Exit\n"));
464         
465         return status;
466 }
467
468
469 /*****************************************************************************
470  Implementation of REG_UNKNOWN_1A
471  ****************************************************************************/
472  
473 NTSTATUS _reg_unknown_1a(pipes_struct *p, REG_Q_UNKNOWN_1A *q_u, REG_R_UNKNOWN_1A *r_u)
474 {
475         NTSTATUS        status = NT_STATUS_OK;
476         REGISTRY_KEY    *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
477         
478         DEBUG(5,("_reg_unknown_1a: Enter\n"));
479         
480         if ( !regkey )
481                 return NT_STATUS_INVALID_HANDLE;        
482         
483         r_u->unknown = 0x00000005;      /* seems to be consistent...no idea what it means */
484         
485         DEBUG(5,("_reg_unknown_1a: Exit\n"));
486         
487         return status;
488 }
489
490
491 /*****************************************************************************
492  Implementation of REG_ENUM_KEY
493  ****************************************************************************/
494  
495 NTSTATUS _reg_enum_key(pipes_struct *p, REG_Q_ENUM_KEY *q_u, REG_R_ENUM_KEY *r_u)
496 {
497         NTSTATUS        status = NT_STATUS_OK;
498         REGISTRY_KEY    *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
499         char            *subkey = NULL;
500         
501         
502         DEBUG(5,("_reg_enum_key: Enter\n"));
503         
504         if ( !regkey )
505                 return NT_STATUS_INVALID_HANDLE;        
506
507         DEBUG(8,("_reg_enum_key: enumerating key [%s]\n", regkey->name));
508         
509         if ( !fetch_reg_keys_specific( regkey, &subkey, q_u->key_index ) )
510         {
511                 status = NT_STATUS_NO_MORE_ENTRIES;
512                 goto done;
513         }
514         
515         DEBUG(10,("_reg_enum_key: retrieved subkey named [%s]\n", subkey));
516         
517         /* subkey has the string name now */
518         
519         init_reg_r_enum_key( r_u, subkey, q_u->unknown_1, q_u->unknown_2 );
520         
521         DEBUG(5,("_reg_enum_key: Exit\n"));
522         
523 done:   
524         SAFE_FREE( subkey );
525         return status;
526 }
527
528 /*****************************************************************************
529  Implementation of REG_ENUM_VALUE
530  ****************************************************************************/
531  
532 NTSTATUS _reg_enum_value(pipes_struct *p, REG_Q_ENUM_VALUE *q_u, REG_R_ENUM_VALUE *r_u)
533 {
534         NTSTATUS        status = NT_STATUS_OK;
535         REGISTRY_KEY    *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
536         REGISTRY_VALUE  *val;
537         
538         
539         DEBUG(5,("_reg_enum_value: Enter\n"));
540         
541         if ( !regkey )
542                 return NT_STATUS_INVALID_HANDLE;        
543
544         DEBUG(8,("_reg_enum_key: enumerating values for key [%s]\n", regkey->name));
545
546         if ( !fetch_reg_values_specific( regkey, &val, q_u->val_index ) )
547         {
548                 status = NT_STATUS_NO_MORE_ENTRIES;
549                 goto done;
550         }
551         
552         DEBUG(10,("_reg_enum_value: retrieved value named  [%s]\n", val->valuename));
553         
554         /* subkey has the string name now */
555         
556         init_reg_r_enum_val( r_u, val );
557
558
559         DEBUG(5,("_reg_enum_value: Exit\n"));
560         
561 done:   
562         free_registry_value( val );
563         
564         return status;
565 }
566
567
568 /*******************************************************************
569  reg_shutdwon
570  ********************************************************************/
571
572 #define SHUTDOWN_R_STRING "-r"
573 #define SHUTDOWN_F_STRING "-f"
574
575
576 NTSTATUS _reg_shutdown(pipes_struct *p, REG_Q_SHUTDOWN *q_u, REG_R_SHUTDOWN *r_u)
577 {
578         NTSTATUS status = NT_STATUS_OK;
579         pstring shutdown_script;
580         UNISTR2 unimsg = q_u->uni_msg;
581         pstring message;
582         pstring chkmsg;
583         fstring timeout;
584         fstring r;
585         fstring f;
586         
587         /* message */
588         rpcstr_pull (message, unimsg.buffer, sizeof(message), unimsg.uni_str_len*2,0);
589                 /* security check */
590         alpha_strcpy (chkmsg, message, NULL, sizeof(message));
591         /* timeout */
592         snprintf(timeout, sizeof(timeout), "%d", q_u->timeout);
593         /* reboot */
594         snprintf(r, sizeof(r), (q_u->flags & REG_REBOOT_ON_SHUTDOWN)?SHUTDOWN_R_STRING:"");
595         /* force */
596         snprintf(f, sizeof(f), (q_u->flags & REG_FORCE_SHUTDOWN)?SHUTDOWN_F_STRING:"");
597
598         pstrcpy(shutdown_script, lp_shutdown_script());
599
600         if(*shutdown_script) {
601                 int shutdown_ret;
602                 all_string_sub(shutdown_script, "%m", chkmsg, sizeof(shutdown_script));
603                 all_string_sub(shutdown_script, "%t", timeout, sizeof(shutdown_script));
604                 all_string_sub(shutdown_script, "%r", r, sizeof(shutdown_script));
605                 all_string_sub(shutdown_script, "%f", f, sizeof(shutdown_script));
606                 shutdown_ret = smbrun(shutdown_script,NULL);
607                 DEBUG(3,("_reg_shutdown: Running the command `%s' gave %d\n",shutdown_script,shutdown_ret));
608         }
609
610         return status;
611 }
612
613 /*******************************************************************
614  reg_abort_shutdwon
615  ********************************************************************/
616
617 NTSTATUS _reg_abort_shutdown(pipes_struct *p, REG_Q_ABORT_SHUTDOWN *q_u, REG_R_ABORT_SHUTDOWN *r_u)
618 {
619         NTSTATUS status = NT_STATUS_OK;
620         pstring abort_shutdown_script;
621
622         pstrcpy(abort_shutdown_script, lp_abort_shutdown_script());
623
624         if(*abort_shutdown_script) {
625                 int abort_shutdown_ret;
626                 abort_shutdown_ret = smbrun(abort_shutdown_script,NULL);
627                 DEBUG(3,("_reg_abort_shutdown: Running the command `%s' gave %d\n",abort_shutdown_script,abort_shutdown_ret));
628         }
629
630         return status;
631 }
632
633 /*******************************************************************
634  REG_SAVE_KEY (0x14)
635  ********************************************************************/
636
637 NTSTATUS _reg_save_key(pipes_struct *p, REG_Q_SAVE_KEY  *q_u, REG_R_SAVE_KEY *r_u)
638 {
639         REGISTRY_KEY    *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
640         
641         DEBUG(5,("_reg_save_key: Enter\n"));
642         
643         /* 
644          * basically this is a no op function which just gverifies 
645          * that the client gave us a valid registry key handle 
646          */
647          
648         if ( !regkey )
649                 return NT_STATUS_INVALID_HANDLE;        
650
651         DEBUG(8,("_reg_save_key: berifying backup of key [%s]\n", regkey->name));
652         
653
654         return NT_STATUS_OK;
655 }
656
657