Merge from HEAD - make Samba compile with -Wwrite-strings without additional
[tprouty/samba.git] / source / 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                                 const 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                 result = NT_STATUS_NO_SUCH_FILE;
135         }
136         else {
137                 /* 
138                  * This would previously return NT_STATUS_TOO_MANY_SECRETS
139                  * that doesn't sound quite right to me  --jerry
140                  */
141                 
142                 if ( !create_policy_hnd( p, hnd, free_regkey_info, regkey ) )
143                         result = NT_STATUS_OBJECT_NAME_NOT_FOUND; 
144         }
145         
146         /* clean up */
147
148         regsubkey_ctr_destroy( &subkeys );
149         
150         if ( ! NT_STATUS_IS_OK(result) )
151                 SAFE_FREE( regkey );
152         else
153                 DLIST_ADD( regkeys_list, regkey );
154
155         
156         DEBUG(7,("open_registry_key: exit\n"));
157
158         return result;
159 }
160
161 /*******************************************************************
162  Function for open a new registry handle and creating a handle 
163  Note that P should be valid & hnd should already have space
164  *******************************************************************/
165
166 static BOOL close_registry_key(pipes_struct *p, POLICY_HND *hnd)
167 {
168         REGISTRY_KEY *regkey = find_regkey_index_by_hnd(p, hnd);
169         
170         if ( !regkey ) {
171                 DEBUG(2,("close_registry_key: Invalid handle (%s:%u:%u)\n", OUR_HANDLE(hnd)));
172                 return False;
173         }
174         
175         close_policy_hnd(p, hnd);
176         
177         return True;
178 }
179
180 /********************************************************************
181  retrieve information about the subkeys
182  *******************************************************************/
183  
184 static BOOL get_subkey_information( REGISTRY_KEY *key, uint32 *maxnum, uint32 *maxlen )
185 {
186         int             num_subkeys, i;
187         uint32          max_len;
188         REGSUBKEY_CTR   subkeys;
189         uint32          len;
190         
191         if ( !key )
192                 return False;
193
194         ZERO_STRUCTP( &subkeys );
195         
196         regsubkey_ctr_init( &subkeys ); 
197            
198         if ( fetch_reg_keys( key, &subkeys ) == -1 )
199                 return False;
200
201         /* find the longest string */
202         
203         max_len = 0;
204         num_subkeys = regsubkey_ctr_numkeys( &subkeys );
205         
206         for ( i=0; i<num_subkeys; i++ ) {
207                 len = strlen( regsubkey_ctr_specific_key(&subkeys, i) );
208                 max_len = MAX(max_len, len);
209         }
210
211         *maxnum = num_subkeys;
212         *maxlen = max_len*2;
213         
214         regsubkey_ctr_destroy( &subkeys );
215         
216         return True;
217 }
218
219 /********************************************************************
220  retrieve information about the values.  We don't store values 
221  here.  The registry tdb is intended to be a frontend to oether 
222  Samba tdb's (such as ntdrivers.tdb).
223  *******************************************************************/
224  
225 static BOOL get_value_information( REGISTRY_KEY *key, uint32 *maxnum, 
226                                     uint32 *maxlen, uint32 *maxsize )
227 {
228         REGVAL_CTR      values;
229         REGISTRY_VALUE  *val;
230         uint32          sizemax, lenmax;
231         int             i, num_values;
232         
233         if ( !key )
234                 return False;
235
236
237         ZERO_STRUCTP( &values );
238         
239         regval_ctr_init( &values );
240         
241         if ( fetch_reg_values( key, &values ) == -1 )
242                 return False;
243         
244         lenmax = sizemax = 0;
245         num_values = regval_ctr_numvals( &values );
246         
247         val = regval_ctr_specific_value( &values, 0 );
248         
249         for ( i=0; i<num_values && val; i++ ) 
250         {
251                 lenmax  = MAX(lenmax,  strlen(val->valuename)+1 );
252                 sizemax = MAX(sizemax, val->size );
253                 
254                 val = regval_ctr_specific_value( &values, i );
255         }
256
257         *maxnum   = num_values;
258         *maxlen   = lenmax;
259         *maxsize  = sizemax;
260         
261         regval_ctr_destroy( &values );
262         
263         return True;
264 }
265
266
267 /********************************************************************
268  reg_close
269  ********************************************************************/
270
271 NTSTATUS _reg_close(pipes_struct *p, REG_Q_CLOSE *q_u, REG_R_CLOSE *r_u)
272 {
273         /* set up the REG unknown_1 response */
274         ZERO_STRUCT(r_u->pol);
275
276         /* close the policy handle */
277         if (!close_registry_key(p, &q_u->pol))
278                 return NT_STATUS_OBJECT_NAME_INVALID;
279
280         return NT_STATUS_OK;
281 }
282
283 /*******************************************************************
284  ********************************************************************/
285
286 NTSTATUS _reg_open_hklm(pipes_struct *p, REG_Q_OPEN_HKLM *q_u, REG_R_OPEN_HKLM *r_u)
287 {
288         return open_registry_key( p, &r_u->pol, NULL, KEY_HKLM, 0x0 );
289 }
290
291 /*******************************************************************
292  ********************************************************************/
293
294 NTSTATUS _reg_open_hkcr(pipes_struct *p, REG_Q_OPEN_HKCR *q_u, REG_R_OPEN_HKCR *r_u)
295 {
296         return open_registry_key( p, &r_u->pol, NULL, KEY_HKCR, 0x0 );
297 }
298
299 /*******************************************************************
300  ********************************************************************/
301
302 NTSTATUS _reg_open_hku(pipes_struct *p, REG_Q_OPEN_HKU *q_u, REG_R_OPEN_HKU *r_u)
303 {
304         return open_registry_key( p, &r_u->pol, NULL, KEY_HKU, 0x0 );
305 }
306
307 /*******************************************************************
308  reg_reply_open_entry
309  ********************************************************************/
310
311 NTSTATUS _reg_open_entry(pipes_struct *p, REG_Q_OPEN_ENTRY *q_u, REG_R_OPEN_ENTRY *r_u)
312 {
313         POLICY_HND pol;
314         fstring name;
315         REGISTRY_KEY *key = find_regkey_index_by_hnd(p, &q_u->pol);
316         NTSTATUS result;
317
318         DEBUG(5,("reg_open_entry: Enter\n"));
319
320         if ( !key )
321                 return NT_STATUS_INVALID_HANDLE;
322
323         rpcstr_pull(name,q_u->uni_name.buffer,sizeof(name),q_u->uni_name.uni_str_len*2,0);
324         
325         DEBUG(5,("reg_open_entry: Enter\n"));
326                    
327         result = open_registry_key( p, &pol, key, name, 0x0 );
328         
329         init_reg_r_open_entry( r_u, &pol, result );
330
331         DEBUG(5,("reg_open_entry: Exit\n"));
332
333         return r_u->status;
334 }
335
336 /*******************************************************************
337  reg_reply_info
338  ********************************************************************/
339
340 NTSTATUS _reg_info(pipes_struct *p, REG_Q_INFO *q_u, REG_R_INFO *r_u)
341 {
342         NTSTATUS                status = NT_STATUS_NO_SUCH_FILE;
343         fstring                 name;
344         const char              *value_ascii = "";
345         fstring                 value;
346         int                     value_length;
347         REGISTRY_KEY            *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
348         REGISTRY_VALUE          *val = NULL;
349         REGISTRY_VALUE          emptyval;
350         REGVAL_CTR              regvals;
351         int                     i;
352
353         DEBUG(5,("_reg_info: Enter\n"));
354
355         if ( !regkey )
356                 return NT_STATUS_INVALID_HANDLE;
357                 
358         DEBUG(7,("_reg_info: policy key name = [%s]\n", regkey->name));
359         
360         rpcstr_pull(name, q_u->uni_type.buffer, sizeof(name), q_u->uni_type.uni_str_len*2, 0);
361
362         DEBUG(5,("reg_info: looking up value: [%s]\n", name));
363
364         ZERO_STRUCTP( &regvals );
365         
366         regval_ctr_init( &regvals );
367
368         /* couple of hard coded registry values */
369         
370         if ( strequal(name, "RefusePasswordChange") ) {
371                 ZERO_STRUCTP( &emptyval );
372                 val = &emptyval;
373         
374                 goto out;
375         }
376
377         if ( strequal(name, REGSTR_PRODUCTTYPE) ) {
378                 /* This makes the server look like a member server to clients */
379                 /* which tells clients that we have our own local user and    */
380                 /* group databases and helps with ACL support.                */
381                 
382                 switch (lp_server_role()) {
383                         case ROLE_DOMAIN_PDC:
384                         case ROLE_DOMAIN_BDC:
385                                 value_ascii = REG_PT_LANMANNT;
386                                 break;
387                         case ROLE_STANDALONE:
388                                 value_ascii = REG_PT_SERVERNT;
389                                 break;
390                         case ROLE_DOMAIN_MEMBER:
391                                 value_ascii = REG_PT_WINNT;
392                                 break;
393                 }
394                 value_length = push_ucs2(value, value, value_ascii,
395                                          sizeof(value),
396                                          STR_TERMINATE|STR_NOALIGN);
397                 regval_ctr_addvalue(&regvals, REGSTR_PRODUCTTYPE, REG_SZ,
398                                     value, value_length);
399                 
400                 val = dup_registry_value( regval_ctr_specific_value( &regvals, 0 ) );
401                 
402                 status = NT_STATUS_OK;
403                 
404                 goto out;
405         }
406
407         /* else fall back to actually looking up the value */
408         
409         for ( i=0; fetch_reg_values_specific(regkey, &val, i); i++ ) 
410         {
411                 DEBUG(10,("_reg_info: Testing value [%s]\n", val->valuename));
412                 if ( StrCaseCmp( val->valuename, name ) == 0 ) {
413                         DEBUG(10,("_reg_info: Found match for value [%s]\n", name));
414                         status = NT_STATUS_OK;
415                         break;
416                 }
417                 
418                 free_registry_value( val );
419         }
420
421   
422 out:
423         new_init_reg_r_info(q_u->ptr_buf, r_u, val, status);
424         
425         regval_ctr_destroy( &regvals );
426         free_registry_value( val );
427
428         DEBUG(5,("_reg_info: Exit\n"));
429
430         return status;
431 }
432
433
434 /*****************************************************************************
435  Implementation of REG_QUERY_KEY
436  ****************************************************************************/
437  
438 NTSTATUS _reg_query_key(pipes_struct *p, REG_Q_QUERY_KEY *q_u, REG_R_QUERY_KEY *r_u)
439 {
440         NTSTATUS        status = NT_STATUS_OK;
441         REGISTRY_KEY    *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
442         
443         DEBUG(5,("_reg_query_key: Enter\n"));
444         
445         if ( !regkey )
446                 return NT_STATUS_INVALID_HANDLE;        
447         
448         if ( !get_subkey_information( regkey, &r_u->num_subkeys, &r_u->max_subkeylen ) )
449                 return NT_STATUS_ACCESS_DENIED;
450                 
451         if ( !get_value_information( regkey, &r_u->num_values, &r_u->max_valnamelen, &r_u->max_valbufsize ) )
452                 return NT_STATUS_ACCESS_DENIED; 
453
454                 
455         r_u->sec_desc = 0x00000078;     /* size for key's sec_desc */
456         
457         /* Win9x set this to 0x0 since it does not keep timestamps.
458            Doing the same here for simplicity   --jerry */
459            
460         ZERO_STRUCT(r_u->mod_time);     
461
462         DEBUG(5,("_reg_query_key: Exit\n"));
463         
464         return status;
465 }
466
467
468 /*****************************************************************************
469  Implementation of REG_UNKNOWN_1A
470  ****************************************************************************/
471  
472 NTSTATUS _reg_unknown_1a(pipes_struct *p, REG_Q_UNKNOWN_1A *q_u, REG_R_UNKNOWN_1A *r_u)
473 {
474         NTSTATUS        status = NT_STATUS_OK;
475         REGISTRY_KEY    *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
476         
477         DEBUG(5,("_reg_unknown_1a: Enter\n"));
478         
479         if ( !regkey )
480                 return NT_STATUS_INVALID_HANDLE;        
481         
482         r_u->unknown = 0x00000005;      /* seems to be consistent...no idea what it means */
483         
484         DEBUG(5,("_reg_unknown_1a: Exit\n"));
485         
486         return status;
487 }
488
489
490 /*****************************************************************************
491  Implementation of REG_ENUM_KEY
492  ****************************************************************************/
493  
494 NTSTATUS _reg_enum_key(pipes_struct *p, REG_Q_ENUM_KEY *q_u, REG_R_ENUM_KEY *r_u)
495 {
496         NTSTATUS        status = NT_STATUS_OK;
497         REGISTRY_KEY    *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
498         char            *subkey = NULL;
499         
500         
501         DEBUG(5,("_reg_enum_key: Enter\n"));
502         
503         if ( !regkey )
504                 return NT_STATUS_INVALID_HANDLE;        
505
506         DEBUG(8,("_reg_enum_key: enumerating key [%s]\n", regkey->name));
507         
508         if ( !fetch_reg_keys_specific( regkey, &subkey, q_u->key_index ) )
509         {
510                 status = NT_STATUS_NO_MORE_ENTRIES;
511                 goto done;
512         }
513         
514         DEBUG(10,("_reg_enum_key: retrieved subkey named [%s]\n", subkey));
515         
516         /* subkey has the string name now */
517         
518         init_reg_r_enum_key( r_u, subkey, q_u->unknown_1, q_u->unknown_2 );
519         
520         DEBUG(5,("_reg_enum_key: Exit\n"));
521         
522 done:   
523         SAFE_FREE( subkey );
524         return status;
525 }
526
527 /*****************************************************************************
528  Implementation of REG_ENUM_VALUE
529  ****************************************************************************/
530  
531 NTSTATUS _reg_enum_value(pipes_struct *p, REG_Q_ENUM_VALUE *q_u, REG_R_ENUM_VALUE *r_u)
532 {
533         NTSTATUS        status = NT_STATUS_OK;
534         REGISTRY_KEY    *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
535         REGISTRY_VALUE  *val;
536         
537         
538         DEBUG(5,("_reg_enum_value: Enter\n"));
539         
540         if ( !regkey )
541                 return NT_STATUS_INVALID_HANDLE;        
542
543         DEBUG(8,("_reg_enum_key: enumerating values for key [%s]\n", regkey->name));
544
545         if ( !fetch_reg_values_specific( regkey, &val, q_u->val_index ) )
546         {
547                 status = NT_STATUS_NO_MORE_ENTRIES;
548                 goto done;
549         }
550         
551         DEBUG(10,("_reg_enum_value: retrieved value named  [%s]\n", val->valuename));
552         
553         /* subkey has the string name now */
554         
555         init_reg_r_enum_val( r_u, val );
556
557
558         DEBUG(5,("_reg_enum_value: Exit\n"));
559         
560 done:   
561         free_registry_value( val );
562         
563         return status;
564 }
565
566
567 /*******************************************************************
568  reg_shutdwon
569  ********************************************************************/
570
571 #define SHUTDOWN_R_STRING "-r"
572 #define SHUTDOWN_F_STRING "-f"
573
574
575 NTSTATUS _reg_shutdown(pipes_struct *p, REG_Q_SHUTDOWN *q_u, REG_R_SHUTDOWN *r_u)
576 {
577         NTSTATUS status = NT_STATUS_OK;
578         pstring shutdown_script;
579         UNISTR2 unimsg = q_u->uni_msg;
580         pstring message;
581         pstring chkmsg;
582         fstring timeout;
583         fstring r;
584         fstring f;
585         
586         /* message */
587         rpcstr_pull (message, unimsg.buffer, sizeof(message), unimsg.uni_str_len*2,0);
588                 /* security check */
589         alpha_strcpy (chkmsg, message, NULL, sizeof(message));
590         /* timeout */
591         snprintf(timeout, sizeof(timeout), "%d", q_u->timeout);
592         /* reboot */
593         snprintf(r, sizeof(r), (q_u->flags & REG_REBOOT_ON_SHUTDOWN)?SHUTDOWN_R_STRING:"");
594         /* force */
595         snprintf(f, sizeof(f), (q_u->flags & REG_FORCE_SHUTDOWN)?SHUTDOWN_F_STRING:"");
596
597         pstrcpy(shutdown_script, lp_shutdown_script());
598
599         if(*shutdown_script) {
600                 int shutdown_ret;
601                 all_string_sub(shutdown_script, "%m", chkmsg, sizeof(shutdown_script));
602                 all_string_sub(shutdown_script, "%t", timeout, sizeof(shutdown_script));
603                 all_string_sub(shutdown_script, "%r", r, sizeof(shutdown_script));
604                 all_string_sub(shutdown_script, "%f", f, sizeof(shutdown_script));
605                 shutdown_ret = smbrun(shutdown_script,NULL);
606                 DEBUG(3,("_reg_shutdown: Running the command `%s' gave %d\n",shutdown_script,shutdown_ret));
607         }
608
609         return status;
610 }
611
612 /*******************************************************************
613  reg_abort_shutdwon
614  ********************************************************************/
615
616 NTSTATUS _reg_abort_shutdown(pipes_struct *p, REG_Q_ABORT_SHUTDOWN *q_u, REG_R_ABORT_SHUTDOWN *r_u)
617 {
618         NTSTATUS status = NT_STATUS_OK;
619         pstring abort_shutdown_script;
620
621         pstrcpy(abort_shutdown_script, lp_abort_shutdown_script());
622
623         if(*abort_shutdown_script) {
624                 int abort_shutdown_ret;
625                 abort_shutdown_ret = smbrun(abort_shutdown_script,NULL);
626                 DEBUG(3,("_reg_abort_shutdown: Running the command `%s' gave %d\n",abort_shutdown_script,abort_shutdown_ret));
627         }
628
629         return status;
630 }
631
632 /*******************************************************************
633  REG_SAVE_KEY (0x14)
634  ********************************************************************/
635
636 NTSTATUS _reg_save_key(pipes_struct *p, REG_Q_SAVE_KEY  *q_u, REG_R_SAVE_KEY *r_u)
637 {
638         REGISTRY_KEY    *regkey = find_regkey_index_by_hnd( p, &q_u->pol );
639         
640         DEBUG(5,("_reg_save_key: Enter\n"));
641         
642         /* 
643          * basically this is a no op function which just gverifies 
644          * that the client gave us a valid registry key handle 
645          */
646          
647         if ( !regkey )
648                 return NT_STATUS_INVALID_HANDLE;        
649
650         DEBUG(8,("_reg_save_key: berifying backup of key [%s]\n", regkey->name));
651         
652
653         return NT_STATUS_OK;
654 }
655
656