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