r11137: Compile with only 2 warnings (I'm still working on that code) on a gcc4
[samba.git] / source / 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;
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 ( (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                 regkey_close_internal( key_service );
329                 return;
330         }
331         
332         fetch_reg_keys( key_service, svc_subkeys );
333         regsubkey_ctr_addkey( svc_subkeys, "Security" );
334         store_reg_keys( key_service, svc_subkeys );
335
336         /* now for the service values */
337         
338         if ( !(values = TALLOC_ZERO_P( key_service, REGVAL_CTR )) ) {
339                 DEBUG(0,("add_new_svc_name: talloc() failed!\n"));
340                 regkey_close_internal( key_service );
341                 return;
342         }
343
344         fill_service_values( name, values );
345         store_reg_values( key_service, values );
346
347         /* cleanup the service key*/
348
349         regkey_close_internal( key_service );
350
351         /* now add the security descriptor */
352
353         pstr_sprintf( path, "%s\\%s\\%s", KEY_SERVICES, name, "Security" );
354         wresult = regkey_open_internal( &key_secdesc, path, get_root_nt_token(), 
355                 REG_KEY_ALL );
356         if ( !W_ERROR_IS_OK(wresult) ) {
357                 DEBUG(0,("add_new_svc_name: key lookup failed! [%s] (%s)\n", 
358                         path, dos_errstr(wresult)));
359                 regkey_close_internal( key_secdesc );
360                 return;
361         }
362
363         if ( !(values = TALLOC_ZERO_P( key_secdesc, REGVAL_CTR )) ) {
364                 DEBUG(0,("add_new_svc_name: talloc() failed!\n"));
365                 regkey_close_internal( key_secdesc );
366                 return;
367         }
368
369         if ( !(sd = construct_service_sd(key_secdesc)) ) {
370                 DEBUG(0,("add_new_svc_name: Failed to create default sec_desc!\n"));
371                 regkey_close_internal( key_secdesc );
372                 return;
373         }
374         
375         /* stream the printer security descriptor */
376         
377         prs_init( &ps, RPC_MAX_PDU_FRAG_LEN, key_secdesc, MARSHALL);
378         
379         if ( sec_io_desc("sec_desc", &sd, &ps, 0 ) ) {
380                 uint32 offset = prs_offset( &ps );
381                 regval_ctr_addvalue( values, "Security", REG_BINARY, prs_data_p(&ps), offset );
382                 store_reg_values( key_secdesc, values );
383         }
384         
385         /* finally cleanup the Security key */
386         
387         prs_mem_free( &ps );
388         regkey_close_internal( key_secdesc );
389
390         return;
391 }
392
393 /********************************************************************
394 ********************************************************************/
395
396 void svcctl_init_keys( void )
397 {
398         const char **service_list = lp_svcctl_list();
399         int i;
400         REGSUBKEY_CTR *subkeys;
401         REGISTRY_KEY *key = NULL;
402         WERROR wresult;
403         
404         /* bad mojo here if the lookup failed.  Should not happen */
405         
406         wresult = regkey_open_internal( &key, KEY_SERVICES, get_root_nt_token(), 
407                 REG_KEY_ALL );
408
409         if ( !W_ERROR_IS_OK(wresult) ) {
410                 DEBUG(0,("init_services_keys: key lookup failed! (%s)\n", 
411                         dos_errstr(wresult)));
412                 return;
413         }
414         
415         /* lookup the available subkeys */      
416         
417         if ( !(subkeys = TALLOC_ZERO_P( key, REGSUBKEY_CTR )) ) {
418                 DEBUG(0,("init_services_keys: talloc() failed!\n"));
419                 regkey_close_internal( key );
420                 return;
421         }
422         
423         fetch_reg_keys( key, subkeys );
424         
425         /* the builting services exist */
426         
427         for ( i=0; builtin_svcs[i].servicename; i++ )
428                 add_new_svc_name( key, subkeys, builtin_svcs[i].servicename );
429                 
430         for ( i=0; service_list[i]; i++ ) {
431         
432                 /* only add new services */
433                 if ( regsubkey_ctr_key_exists( subkeys, service_list[i] ) )
434                         continue;
435
436                 /* Add the new service key and initialize the appropriate values */
437
438                 add_new_svc_name( key, subkeys, service_list[i] );
439         }
440
441         regkey_close_internal( key );
442
443         /* initialize the control hooks */
444
445         init_service_op_table();
446
447         return;
448 }
449
450 /********************************************************************
451  This is where we do the dirty work of filling in things like the
452  Display name, Description, etc...Always return a default secdesc 
453  in case of any failure.
454 ********************************************************************/
455
456 SEC_DESC* svcctl_get_secdesc( TALLOC_CTX *ctx, const char *name, NT_USER_TOKEN *token )
457 {
458         REGISTRY_KEY *key;
459         prs_struct ps;
460         REGVAL_CTR *values;
461         REGISTRY_VALUE *val;
462         SEC_DESC *sd = NULL;
463         SEC_DESC *ret_sd = NULL;
464         pstring path;
465         WERROR wresult;
466         
467         /* now add the security descriptor */
468
469         pstr_sprintf( path, "%s\\%s\\%s", KEY_SERVICES, name, "Security" );
470         wresult = regkey_open_internal( &key, path, token, REG_KEY_ALL );
471         if ( !W_ERROR_IS_OK(wresult) ) {
472                 DEBUG(0,("svcctl_get_secdesc: key lookup failed! [%s] (%s)\n", 
473                         path, dos_errstr(wresult)));
474                 return NULL;
475         }
476
477         if ( !(values = TALLOC_ZERO_P( key, REGVAL_CTR )) ) {
478                 DEBUG(0,("add_new_svc_name: talloc() failed!\n"));
479                 regkey_close_internal( key );
480                 return NULL;
481         }
482
483         fetch_reg_values( key, values );
484         
485         if ( !(val = regval_ctr_getvalue( values, "Security" )) ) {
486                 DEBUG(6,("svcctl_get_secdesc: constructing default secdesc for service [%s]\n", 
487                         name));
488                 regkey_close_internal( key );
489                 return construct_service_sd( ctx );
490         }
491         
492
493         /* stream the printer security descriptor */
494         
495         prs_init( &ps, 0, key, UNMARSHALL);
496         prs_give_memory( &ps, (char *)regval_data_p(val), regval_size(val), False );
497         
498         if ( !sec_io_desc("sec_desc", &sd, &ps, 0 ) ) {
499                 regkey_close_internal( key );
500                 return construct_service_sd( ctx );
501         }
502         
503         ret_sd = dup_sec_desc( ctx, sd );
504         
505         /* finally cleanup the Security key */
506         
507         prs_mem_free( &ps );
508         regkey_close_internal( key );
509
510         return ret_sd;
511 }
512
513 /********************************************************************
514 ********************************************************************/
515
516 char* svcctl_lookup_dispname( const char *name, NT_USER_TOKEN *token )
517 {
518         static fstring display_name;
519         REGISTRY_KEY *key;
520         REGVAL_CTR *values;
521         REGISTRY_VALUE *val;
522         pstring path;
523         WERROR wresult;
524         
525         /* now add the security descriptor */
526
527         pstr_sprintf( path, "%s\\%s", KEY_SERVICES, name );
528         wresult = regkey_open_internal( &key, path, token, REG_KEY_READ );
529         if ( !W_ERROR_IS_OK(wresult) ) {
530                 DEBUG(0,("svcctl_lookup_dispname: key lookup failed! [%s] (%s)\n", 
531                         path, dos_errstr(wresult)));
532                 goto fail;
533         }
534
535         if ( !(values = TALLOC_ZERO_P( key, REGVAL_CTR )) ) {
536                 DEBUG(0,("svcctl_lookup_dispname: talloc() failed!\n"));
537                 regkey_close_internal( key );
538                 goto fail;
539         }
540
541         fetch_reg_values( key, values );
542         
543         if ( !(val = regval_ctr_getvalue( values, "DisplayName" )) )
544                 goto fail;
545
546         rpcstr_pull( display_name, regval_data_p(val), sizeof(display_name), regval_size(val), 0 );
547
548         regkey_close_internal( key );
549         
550         return display_name;
551
552 fail:
553         /* default to returning the service name */
554         regkey_close_internal( key );
555         fstrcpy( display_name, name );
556         return display_name;
557 }
558
559 /********************************************************************
560 ********************************************************************/
561
562 char* svcctl_lookup_description( const char *name, NT_USER_TOKEN *token )
563 {
564         static fstring description;
565         REGISTRY_KEY *key;
566         REGVAL_CTR *values;
567         REGISTRY_VALUE *val;
568         pstring path;
569         WERROR wresult;
570         
571         /* now add the security descriptor */
572
573         pstr_sprintf( path, "%s\\%s", KEY_SERVICES, name );
574         wresult = regkey_open_internal( &key, path, token, REG_KEY_READ );
575         if ( !W_ERROR_IS_OK(wresult) ) {
576                 DEBUG(0,("svcctl_lookup_dispname: key lookup failed! [%s] (%s)\n", 
577                         path, dos_errstr(wresult)));
578                 return NULL;
579         }
580
581         if ( !(values = TALLOC_ZERO_P( key, REGVAL_CTR )) ) {
582                 DEBUG(0,("svcctl_lookup_dispname: talloc() failed!\n"));
583                 regkey_close_internal( key );
584                 return NULL;
585         }
586
587         fetch_reg_values( key, values );
588         
589         if ( !(val = regval_ctr_getvalue( values, "Description" )) )
590                 fstrcpy( description, "Unix Service");
591         else
592                 rpcstr_pull( description, regval_data_p(val), sizeof(description), regval_size(val), 0 );
593
594         regkey_close_internal( key );
595         
596         return description;
597 }
598
599
600 /********************************************************************
601 ********************************************************************/
602
603 REGVAL_CTR* svcctl_fetch_regvalues( const char *name, NT_USER_TOKEN *token )
604 {
605         REGISTRY_KEY *key;
606         REGVAL_CTR *values;
607         pstring path;
608         WERROR wresult;
609         
610         /* now add the security descriptor */
611
612         pstr_sprintf( path, "%s\\%s", KEY_SERVICES, name );
613         wresult = regkey_open_internal( &key, path, token, REG_KEY_READ );
614         if ( !W_ERROR_IS_OK(wresult) ) {
615                 DEBUG(0,("svcctl_fetch_regvalues: key lookup failed! [%s] (%s)\n", 
616                         path, dos_errstr(wresult)));
617                 return NULL;
618         }
619
620         if ( !(values = TALLOC_ZERO_P( NULL, REGVAL_CTR )) ) {
621                 DEBUG(0,("svcctl_fetch_regvalues: talloc() failed!\n"));
622                 regkey_close_internal( key );
623                 return NULL;
624         }
625         
626         fetch_reg_values( key, values );
627
628         regkey_close_internal( key );
629         
630         return values;
631 }
632