7c75d41352890288a851f69872b0924da80739ce
[ira/wip.git] / source3 / services / services_db.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Service Control API Implementation
4  * 
5  *  Copyright (C) Marcin Krzysztof Porwit         2005.
6  *  Largely Rewritten by:
7  *  Copyright (C) Gerald (Jerry) Carter           2005.
8  *  
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *  
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *  
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include "includes.h"
25
26 struct rcinit_file_information {
27         char *description;
28 };
29
30 struct service_display_info {
31         const char *servicename;
32         const char *daemon;
33         const char *dispname;
34         const char *description;
35 };
36
37 struct service_display_info builtin_svcs[] = {  
38   { "Spooler",        "smbd", "Print Spooler",
39         "Internal service for spooling files to print devices" },
40   { "NETLOGON",       "smbd", "Net Logon",
41         "File service providing access to policy and profile data" },
42   { "RemoteRegistry", "smbd", "Remote Registry Service",
43         "Internal service providing remote access to the Samba registry" },
44   { "WINS",           "nmbd", "Windows Internet Name Service (WINS)",
45         "Internal service providing a NetBIOS point-to-point name server" },
46   { NULL, NULL, NULL, NULL }
47 };
48
49 struct service_display_info common_unix_svcs[] = {  
50   { "cups",          NULL, "Common Unix Printing System", NULL },
51   { "postfix",       NULL, "Internet Mail Service", NULL },
52   { "sendmail",      NULL, "Internet Mail Service", NULL },
53   { "portmap",       NULL, "TCP Port to RPC PortMapper", NULL },
54   { "xinetd",        NULL, "Internet Meta-Daemon", NULL },
55   { "inet",          NULL, "Internet Meta-Daemon", NULL },
56   { "xntpd",         NULL, "Network Time Service", NULL },
57   { "ntpd",          NULL, "Network Time Service", NULL },
58   { "lpd",           NULL, "BSD Print Spooler", NULL },
59   { "nfsserver",     NULL, "Network File Service", NULL },
60   { "cron",          NULL, "Scheduling Service", NULL },
61   { "at",            NULL, "Scheduling Service", NULL },
62   { "nscd",          NULL, "Name Service Cache Daemon", NULL },
63   { "slapd",         NULL, "LDAP Directory Service", NULL },
64   { "ldap",          NULL, "LDAP DIrectory Service", NULL },
65   { "ypbind",        NULL, "NIS Directory Service", NULL },
66   { "courier-imap",  NULL, "IMAP4 Mail Service", NULL },
67   { "courier-pop3",  NULL, "POP3 Mail Service", NULL },
68   { "named",         NULL, "Domain Name Service", NULL },
69   { "bind",          NULL, "Domain Name Service", NULL },
70   { "httpd",         NULL, "HTTP Server", NULL },
71   { "apache",        NULL, "HTTP Server", NULL },
72   { "autofs",        NULL, "Automounter", NULL },
73   { "squid",         NULL, "Web Cache Proxy ", NULL },
74   { NULL, NULL, NULL, NULL }
75 };
76
77
78 /********************************************************************
79 ********************************************************************/
80
81 static SEC_DESC* construct_service_sd( TALLOC_CTX *ctx )
82 {
83         SEC_ACE ace[4]; 
84         SEC_ACCESS mask;
85         size_t i = 0;
86         SEC_DESC *sd;
87         SEC_ACL *acl;
88         size_t sd_size;
89         
90         /* basic access for Everyone */
91         
92         init_sec_access(&mask, SERVICE_READ_ACCESS );
93         init_sec_ace(&ace[i++], &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
94                 
95         init_sec_access(&mask,SERVICE_EXECUTE_ACCESS );
96         init_sec_ace(&ace[i++], &global_sid_Builtin_Power_Users, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
97         
98         init_sec_access(&mask,SERVICE_ALL_ACCESS );
99         init_sec_ace(&ace[i++], &global_sid_Builtin_Server_Operators, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
100         init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators, SEC_ACE_TYPE_ACCESS_ALLOWED, mask, 0);
101         
102         /* create the security descriptor */
103         
104         if ( !(acl = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace)) )
105                 return NULL;
106
107         if ( !(sd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL, acl, &sd_size)) )
108                 return NULL;
109
110         return sd;
111 }
112
113 /********************************************************************
114  This is where we do the dirty work of filling in things like the
115  Display name, Description, etc...
116 ********************************************************************/
117
118 static char *get_common_service_dispname( const char *servicename )
119 {
120         static fstring dispname;
121         int i;
122         
123         for ( i=0; common_unix_svcs[i].servicename; i++ ) {
124                 if ( strequal( servicename, common_unix_svcs[i].servicename ) ) {
125                         fstr_sprintf( dispname, "%s (%s)", 
126                                 common_unix_svcs[i].dispname,
127                                 common_unix_svcs[i].servicename );
128                                 
129                         return dispname;
130                 }
131         } 
132         
133         fstrcpy( dispname, servicename );
134         
135         return dispname;
136 }
137
138 /********************************************************************
139 ********************************************************************/
140
141 static char* cleanup_string( const char *string )
142 {
143         static pstring clean;
144         char *begin, *end;
145
146         pstrcpy( clean, string );
147         begin = clean;
148         
149         /* trim any beginning whilespace */
150         
151         while ( isspace(*begin) )
152                 begin++;
153
154         if ( !begin )
155                 return NULL;
156                         
157         /* trim any trailing whitespace or carriage returns.
158            Start at the end and move backwards */
159                         
160         end = begin + strlen(begin) - 1;
161                         
162         while ( isspace(*end) || *end=='\n' || *end=='\r' ) {
163                 *end = '\0';
164                 end--;
165         }
166
167         return begin;
168 }
169
170 /********************************************************************
171 ********************************************************************/
172
173 static BOOL read_init_file( const char *servicename, struct rcinit_file_information **service_info )
174 {
175         struct rcinit_file_information *info;
176         pstring filepath, str;
177         XFILE *f;
178         char *p, *s;
179                 
180         if ( !(info = TALLOC_ZERO_P( NULL, struct rcinit_file_information ) ) )
181                 return False;
182         
183         /* attempt the file open */
184                 
185         pstr_sprintf( filepath, "%s/%s/%s", dyn_LIBDIR, SVCCTL_SCRIPT_DIR, servicename );
186         if ( !(f = x_fopen( filepath, O_RDONLY, 0 )) ) {
187                 DEBUG(0,("read_init_file: failed to open [%s]\n", filepath));
188                 TALLOC_FREE(info);
189                 return False;
190         }
191         
192         while ( (s = x_fgets( str, sizeof(str)-1, f )) != NULL ) {
193                 /* ignore everything that is not a full line 
194                    comment starting with a '#' */
195                    
196                 if ( str[0] != '#' )
197                         continue;
198                 
199                 /* Look for a line like '^#.*Description:' */
200                 
201                 if ( (p = strstr( str, "Description:" )) != NULL ) {
202                         char *desc;
203
204                         p += strlen( "Description:" ) + 1;
205                         if ( !p ) 
206                                 break;
207                                 
208                         if ( (desc = cleanup_string(p)) != NULL )
209                                 info->description = talloc_strdup( info, desc );
210                 }
211         }
212         
213         x_fclose( f );
214         
215         if ( !info->description )
216                 info->description = talloc_strdup( info, "External Unix Service" );
217         
218         *service_info = info;
219         
220         return True;
221 }
222
223 /********************************************************************
224  This is where we do the dirty work of filling in things like the
225  Display name, Description, etc...
226 ********************************************************************/
227
228 static void fill_service_values( const char *name, REGVAL_CTR *values )
229 {
230         UNISTR2 data, dname, ipath, description;
231         uint32 dword;
232         pstring pstr;
233         int i;
234         
235         /* These values are hardcoded in all QueryServiceConfig() replies.
236            I'm just storing them here for cosmetic purposes */
237         
238         dword = SVCCTL_AUTO_START;
239         regval_ctr_addvalue( values, "Start", REG_DWORD, (char*)&dword, sizeof(uint32));
240         
241         dword = SVCCTL_WIN32_OWN_PROC;
242         regval_ctr_addvalue( values, "Type", REG_DWORD, (char*)&dword, sizeof(uint32));
243
244         dword = SVCCTL_SVC_ERROR_NORMAL;
245         regval_ctr_addvalue( values, "ErrorControl", REG_DWORD, (char*)&dword, sizeof(uint32));
246         
247         /* everything runs as LocalSystem */
248         
249         init_unistr2( &data, "LocalSystem", UNI_STR_TERMINATE );
250         regval_ctr_addvalue( values, "ObjectName", REG_SZ, (char*)data.buffer, data.uni_str_len*2);
251         
252         /* special considerations for internal services and the DisplayName value */
253         
254         for ( i=0; builtin_svcs[i].servicename; i++ ) {
255                 if ( strequal( name, builtin_svcs[i].servicename ) ) {
256                         pstr_sprintf( pstr, "%s/%s/%s",dyn_LIBDIR, SVCCTL_SCRIPT_DIR, builtin_svcs[i].daemon );
257                         init_unistr2( &ipath, pstr, UNI_STR_TERMINATE );
258                         init_unistr2( &description, builtin_svcs[i].description, UNI_STR_TERMINATE );
259                         init_unistr2( &dname, builtin_svcs[i].dispname, UNI_STR_TERMINATE );
260                         break;
261                 }
262         } 
263         
264         /* default to an external service if we haven't found a match */
265         
266         if ( builtin_svcs[i].servicename == NULL ) {
267                 struct rcinit_file_information *init_info = NULL;
268
269                 pstr_sprintf( pstr, "%s/%s/%s",dyn_LIBDIR, SVCCTL_SCRIPT_DIR, name );
270                 init_unistr2( &ipath, pstr, UNI_STR_TERMINATE );
271                 
272                 /* lookup common unix display names */
273                 init_unistr2( &dname, get_common_service_dispname( name ), UNI_STR_TERMINATE );
274
275                 /* get info from init file itself */            
276                 if ( read_init_file( name, &init_info ) ) {
277                         init_unistr2( &description, init_info->description, UNI_STR_TERMINATE );
278                         TALLOC_FREE( init_info );
279                 }
280                 else {
281                         init_unistr2( &description, "External Unix Service", UNI_STR_TERMINATE );
282                 }
283         }
284         
285         /* add the new values */
286         
287         regval_ctr_addvalue( values, "DisplayName", REG_SZ, (char*)dname.buffer, dname.uni_str_len*2);
288         regval_ctr_addvalue( values, "ImagePath", REG_SZ, (char*)ipath.buffer, ipath.uni_str_len*2);
289         regval_ctr_addvalue( values, "Description", REG_SZ, (char*)description.buffer, description.uni_str_len*2);
290         
291         return;
292 }
293
294 /********************************************************************
295 ********************************************************************/
296
297 static void add_new_svc_name( REGISTRY_KEY *key_parent, REGSUBKEY_CTR *subkeys, 
298                               const char *name )
299 {
300         REGISTRY_KEY *key_service, *key_secdesc;
301         WERROR wresult;
302         pstring path;
303         REGVAL_CTR *values;
304         REGSUBKEY_CTR *svc_subkeys;
305         SEC_DESC *sd;
306         prs_struct ps;
307
308         /* add to the list and create the subkey path */
309
310         regsubkey_ctr_addkey( subkeys, name );
311         store_reg_keys( key_parent, subkeys );
312
313         /* open the new service key */
314
315         pstr_sprintf( path, "%s\\%s", KEY_SERVICES, name );
316         wresult = regkey_open_internal( &key_service, path, get_root_nt_token(), 
317                 REG_KEY_ALL );
318         if ( !W_ERROR_IS_OK(wresult) ) {
319                 DEBUG(0,("add_new_svc_name: key lookup failed! [%s] (%s)\n", 
320                         path, dos_errstr(wresult)));
321                 return;
322         }
323         
324         /* add the 'Security' key */
325
326         if ( !(svc_subkeys = TALLOC_ZERO_P( key_service, REGSUBKEY_CTR )) ) {
327                 DEBUG(0,("add_new_svc_name: talloc() failed!\n"));
328                 return;
329         }
330         
331         fetch_reg_keys( key_service, svc_subkeys );
332         regsubkey_ctr_addkey( svc_subkeys, "Security" );
333         store_reg_keys( key_service, svc_subkeys );
334
335         /* now for the service values */
336         
337         if ( !(values = TALLOC_ZERO_P( key_service, REGVAL_CTR )) ) {
338                 DEBUG(0,("add_new_svc_name: talloc() failed!\n"));
339                 return;
340         }
341
342         fill_service_values( name, values );
343         store_reg_values( key_service, values );
344
345         /* cleanup the service key*/
346
347         TALLOC_FREE( key_service );
348
349         /* now add the security descriptor */
350
351         pstr_sprintf( path, "%s\\%s\\%s", KEY_SERVICES, name, "Security" );
352         wresult = regkey_open_internal( &key_secdesc, path, get_root_nt_token(), 
353                 REG_KEY_ALL );
354         if ( !W_ERROR_IS_OK(wresult) ) {
355                 DEBUG(0,("add_new_svc_name: key lookup failed! [%s] (%s)\n", 
356                         path, dos_errstr(wresult)));
357                 return;
358         }
359
360         if ( !(values = TALLOC_ZERO_P( key_secdesc, REGVAL_CTR )) ) {
361                 DEBUG(0,("add_new_svc_name: talloc() failed!\n"));
362                 return;
363         }
364
365         if ( !(sd = construct_service_sd(key_secdesc)) ) {
366                 DEBUG(0,("add_new_svc_name: Failed to create default sec_desc!\n"));
367                 TALLOC_FREE( key_secdesc );
368                 return;
369         }
370         
371         /* stream the printer security descriptor */
372         
373         prs_init( &ps, RPC_MAX_PDU_FRAG_LEN, key_secdesc, MARSHALL);
374         
375         if ( sec_io_desc("sec_desc", &sd, &ps, 0 ) ) {
376                 uint32 offset = prs_offset( &ps );
377                 regval_ctr_addvalue( values, "Security", REG_BINARY, prs_data_p(&ps), offset );
378                 store_reg_values( key_secdesc, values );
379         }
380         
381         /* finally cleanup the Security key */
382         
383         prs_mem_free( &ps );
384         TALLOC_FREE( key_secdesc );
385
386         return;
387 }
388
389 /********************************************************************
390 ********************************************************************/
391
392 void svcctl_init_keys( void )
393 {
394         const char **service_list = lp_svcctl_list();
395         int i;
396         REGSUBKEY_CTR *subkeys;
397         REGISTRY_KEY *key = NULL;
398         WERROR wresult;
399         BOOL new_services = False;
400         
401         /* bad mojo here if the lookup failed.  Should not happen */
402         
403         wresult = regkey_open_internal( &key, KEY_SERVICES, get_root_nt_token(), 
404                 REG_KEY_ALL );
405
406         if ( !W_ERROR_IS_OK(wresult) ) {
407                 DEBUG(0,("init_services_keys: key lookup failed! (%s)\n", 
408                         dos_errstr(wresult)));
409                 return;
410         }
411         
412         /* lookup the available subkeys */      
413         
414         if ( !(subkeys = TALLOC_ZERO_P( key, REGSUBKEY_CTR )) ) {
415                 DEBUG(0,("init_services_keys: talloc() failed!\n"));
416                 return;
417         }
418         
419         fetch_reg_keys( key, subkeys );
420         
421         /* the builting services exist */
422         
423         for ( i=0; builtin_svcs[i].servicename; i++ )
424                 add_new_svc_name( key, subkeys, builtin_svcs[i].servicename );
425                 
426         for ( i=0; service_list[i]; i++ ) {
427         
428                 /* only add new services */
429                 if ( regsubkey_ctr_key_exists( subkeys, service_list[i] ) )
430                         continue;
431
432                 /* Add the new service key and initialize the appropriate values */
433
434                 add_new_svc_name( key, subkeys, service_list[i] );
435
436                 new_services = True;
437         }
438
439         TALLOC_FREE( key );
440
441         /* initialize the control hooks */
442
443         init_service_op_table();
444
445         return;
446 }
447
448 /********************************************************************
449  This is where we do the dirty work of filling in things like the
450  Display name, Description, etc...Always return a default secdesc 
451  in case of any failure.
452 ********************************************************************/
453
454 SEC_DESC* svcctl_get_secdesc( TALLOC_CTX *ctx, const char *name, NT_USER_TOKEN *token )
455 {
456         REGISTRY_KEY *key;
457         prs_struct ps;
458         REGVAL_CTR *values;
459         REGISTRY_VALUE *val;
460         SEC_DESC *sd = NULL;
461         SEC_DESC *ret_sd = NULL;
462         pstring path;
463         WERROR wresult;
464         
465         /* now add the security descriptor */
466
467         pstr_sprintf( path, "%s\\%s\\%s", KEY_SERVICES, name, "Security" );
468         wresult = regkey_open_internal( &key, path, token, REG_KEY_ALL );
469         if ( !W_ERROR_IS_OK(wresult) ) {
470                 DEBUG(0,("svcctl_get_secdesc: key lookup failed! [%s] (%s)\n", 
471                         path, dos_errstr(wresult)));
472                 return NULL;
473         }
474
475         if ( !(values = TALLOC_ZERO_P( key, REGVAL_CTR )) ) {
476                 DEBUG(0,("add_new_svc_name: talloc() failed!\n"));
477                 TALLOC_FREE( key );
478                 return NULL;
479         }
480
481         fetch_reg_values( key, values );
482         
483         if ( !(val = regval_ctr_getvalue( values, "Security" )) ) {
484                 DEBUG(6,("svcctl_get_secdesc: constructing default secdesc for service [%s]\n", 
485                         name));
486                 TALLOC_FREE( key );
487                 return construct_service_sd( ctx );
488         }
489         
490
491         /* stream the printer security descriptor */
492         
493         prs_init( &ps, 0, key, UNMARSHALL);
494         prs_give_memory( &ps, regval_data_p(val), regval_size(val), False );
495         
496         if ( !sec_io_desc("sec_desc", &sd, &ps, 0 ) ) {
497                 TALLOC_FREE( key );
498                 return construct_service_sd( ctx );
499         }
500         
501         ret_sd = dup_sec_desc( ctx, sd );
502         
503         /* finally cleanup the Security key */
504         
505         prs_mem_free( &ps );
506         TALLOC_FREE( key );
507
508         return ret_sd;
509 }
510
511 /********************************************************************
512 ********************************************************************/
513
514 char* svcctl_lookup_dispname( const char *name, NT_USER_TOKEN *token )
515 {
516         static fstring display_name;
517         REGISTRY_KEY *key;
518         REGVAL_CTR *values;
519         REGISTRY_VALUE *val;
520         pstring path;
521         WERROR wresult;
522         
523         /* now add the security descriptor */
524
525         pstr_sprintf( path, "%s\\%s", KEY_SERVICES, name );
526         wresult = regkey_open_internal( &key, path, token, REG_KEY_READ );
527         if ( !W_ERROR_IS_OK(wresult) ) {
528                 DEBUG(0,("svcctl_lookup_dispname: key lookup failed! [%s] (%s)\n", 
529                         path, dos_errstr(wresult)));
530                 goto fail;
531         }
532
533         if ( !(values = TALLOC_ZERO_P( key, REGVAL_CTR )) ) {
534                 DEBUG(0,("svcctl_lookup_dispname: talloc() failed!\n"));
535                 TALLOC_FREE( key );
536                 goto fail;
537         }
538
539         fetch_reg_values( key, values );
540         
541         if ( !(val = regval_ctr_getvalue( values, "DisplayName" )) )
542                 goto fail;
543
544         rpcstr_pull( display_name, regval_data_p(val), sizeof(display_name), regval_size(val), 0 );
545
546         TALLOC_FREE( key );
547         
548         return display_name;
549
550 fail:
551         /* default to returning the service name */
552         fstrcpy( display_name, name );
553         return display_name;
554 }
555
556 /********************************************************************
557 ********************************************************************/
558
559 char* svcctl_lookup_description( const char *name, NT_USER_TOKEN *token )
560 {
561         static fstring description;
562         REGISTRY_KEY *key;
563         REGVAL_CTR *values;
564         REGISTRY_VALUE *val;
565         pstring path;
566         WERROR wresult;
567         
568         /* now add the security descriptor */
569
570         pstr_sprintf( path, "%s\\%s", KEY_SERVICES, name );
571         wresult = regkey_open_internal( &key, path, token, REG_KEY_READ );
572         if ( !W_ERROR_IS_OK(wresult) ) {
573                 DEBUG(0,("svcctl_lookup_dispname: key lookup failed! [%s] (%s)\n", 
574                         path, dos_errstr(wresult)));
575                 return NULL;
576         }
577
578         if ( !(values = TALLOC_ZERO_P( key, REGVAL_CTR )) ) {
579                 DEBUG(0,("svcctl_lookup_dispname: talloc() failed!\n"));
580                 TALLOC_FREE( key );
581                 return NULL;
582         }
583
584         fetch_reg_values( key, values );
585         
586         if ( !(val = regval_ctr_getvalue( values, "Description" )) )
587                 fstrcpy( description, "Unix Service");
588         else
589                 rpcstr_pull( description, regval_data_p(val), sizeof(description), regval_size(val), 0 );
590
591         TALLOC_FREE( key );
592         
593         return description;
594 }
595
596
597 /********************************************************************
598 ********************************************************************/
599
600 REGVAL_CTR* svcctl_fetch_regvalues( const char *name, NT_USER_TOKEN *token )
601 {
602         REGISTRY_KEY *key;
603         REGVAL_CTR *values;
604         pstring path;
605         WERROR wresult;
606         
607         /* now add the security descriptor */
608
609         pstr_sprintf( path, "%s\\%s", KEY_SERVICES, name );
610         wresult = regkey_open_internal( &key, path, token, REG_KEY_READ );
611         if ( !W_ERROR_IS_OK(wresult) ) {
612                 DEBUG(0,("svcctl_fetch_regvalues: key lookup failed! [%s] (%s)\n", 
613                         path, dos_errstr(wresult)));
614                 return NULL;
615         }
616
617         if ( !(values = TALLOC_ZERO_P( NULL, REGVAL_CTR )) ) {
618                 DEBUG(0,("svcctl_fetch_regvalues: talloc() failed!\n"));
619                 TALLOC_FREE( key );
620                 return NULL;
621         }
622         
623         fetch_reg_values( key, values );
624
625         TALLOC_FREE( key );
626         
627         return values;
628 }
629