s3:services_db: does not need legacy registry code any more.
[Samba/gbeck.git] / source3 / services / services_db.c
blob6d361c74648da77fc3a38882a2f0bb07b8497d69
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 3 of the License, or
12 * (at your option) any later version.
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.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "services/services.h"
25 #include "registry.h"
26 #include "registry/reg_api_util.h"
28 struct rcinit_file_information {
29 char *description;
32 struct service_display_info {
33 const char *servicename;
34 const char *daemon;
35 const char *dispname;
36 const char *description;
39 struct service_display_info builtin_svcs[] = {
40 { "Spooler", "smbd", "Print Spooler", "Internal service for spooling files to print devices" },
41 { "NETLOGON", "smbd", "Net Logon", "File service providing access to policy and profile data (not remotely manageable)" },
42 { "RemoteRegistry", "smbd", "Remote Registry Service", "Internal service providing remote access to "
43 "the Samba registry" },
44 { "WINS", "nmbd", "Windows Internet Name Service (WINS)", "Internal service providing a "
45 "NetBIOS point-to-point name server (not remotely manageable)" },
46 { NULL, NULL, NULL, NULL }
49 struct service_display_info common_unix_svcs[] = {
50 { "cups", NULL, "Common Unix Printing System","Provides unified printing support for all operating systems" },
51 { "postfix", NULL, "Internet Mail Service", "Provides support for sending and receiving electonic mail" },
52 { "sendmail", NULL, "Internet Mail Service", "Provides support for sending and receiving electonic mail" },
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", "Provides s highly scalable and flexible web server "
72 "capable of implementing various protocols incluing "
73 "but not limited to HTTP" },
74 { "autofs", NULL, "Automounter", NULL },
75 { "squid", NULL, "Web Cache Proxy ", NULL },
76 { "perfcountd", NULL, "Performance Monitoring Daemon", NULL },
77 { "pgsql", NULL, "PgSQL Database Server", "Provides service for SQL database from Postgresql.org" },
78 { "arpwatch", NULL, "ARP Tables watcher", "Provides service for monitoring ARP tables for changes" },
79 { "dhcpd", NULL, "DHCP Server", "Provides service for dynamic host configuration and IP assignment" },
80 { "nwserv", NULL, "NetWare Server Emulator", "Provides service for emulating Novell NetWare 3.12 server" },
81 { "proftpd", NULL, "Professional FTP Server", "Provides high configurable service for FTP connection and "
82 "file transferring" },
83 { "ssh2", NULL, "SSH Secure Shell", "Provides service for secure connection for remote administration" },
84 { "sshd", NULL, "SSH Secure Shell", "Provides service for secure connection for remote administration" },
85 { NULL, NULL, NULL, NULL }
88 static WERROR svcctl_set_secdesc_internal(struct registry_key *key,
89 struct security_descriptor *sec_desc);
91 /********************************************************************
92 ********************************************************************/
94 static struct security_descriptor* construct_service_sd( TALLOC_CTX *ctx )
96 struct security_ace ace[4];
97 size_t i = 0;
98 struct security_descriptor *sd = NULL;
99 struct security_acl *theacl = NULL;
100 size_t sd_size;
102 /* basic access for Everyone */
104 init_sec_ace(&ace[i++], &global_sid_World,
105 SEC_ACE_TYPE_ACCESS_ALLOWED, SERVICE_READ_ACCESS, 0);
107 init_sec_ace(&ace[i++], &global_sid_Builtin_Power_Users,
108 SEC_ACE_TYPE_ACCESS_ALLOWED, SERVICE_EXECUTE_ACCESS, 0);
110 init_sec_ace(&ace[i++], &global_sid_Builtin_Server_Operators,
111 SEC_ACE_TYPE_ACCESS_ALLOWED, SERVICE_ALL_ACCESS, 0);
112 init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
113 SEC_ACE_TYPE_ACCESS_ALLOWED, SERVICE_ALL_ACCESS, 0);
115 /* create the security descriptor */
117 theacl = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace);
118 if (theacl == NULL) {
119 return NULL;
122 sd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
123 SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
124 theacl, &sd_size);
125 if (sd == NULL) {
126 return NULL;
129 return sd;
132 /********************************************************************
133 This is where we do the dirty work of filling in things like the
134 Display name, Description, etc...
135 ********************************************************************/
137 static char *get_common_service_dispname( const char *servicename )
139 int i;
141 for ( i=0; common_unix_svcs[i].servicename; i++ ) {
142 if (strequal(servicename, common_unix_svcs[i].servicename)) {
143 char *dispname;
144 if (asprintf(&dispname,
145 "%s (%s)",
146 common_unix_svcs[i].dispname,
147 common_unix_svcs[i].servicename) < 0) {
148 return NULL;
150 return dispname;
154 return SMB_STRDUP(servicename );
157 /********************************************************************
158 ********************************************************************/
160 static char *cleanup_string( const char *string )
162 char *clean = NULL;
163 char *begin, *end;
164 TALLOC_CTX *ctx = talloc_tos();
166 clean = talloc_strdup(ctx, string);
167 if (!clean) {
168 return NULL;
170 begin = clean;
172 /* trim any beginning whilespace */
174 while (isspace(*begin)) {
175 begin++;
178 if (*begin == '\0') {
179 return NULL;
182 /* trim any trailing whitespace or carriage returns.
183 Start at the end and move backwards */
185 end = begin + strlen(begin) - 1;
187 while ( isspace(*end) || *end=='\n' || *end=='\r' ) {
188 *end = '\0';
189 end--;
192 return begin;
195 /********************************************************************
196 ********************************************************************/
198 static bool read_init_file( const char *servicename, struct rcinit_file_information **service_info )
200 struct rcinit_file_information *info = NULL;
201 char *filepath = NULL;
202 char str[1024];
203 XFILE *f = NULL;
204 char *p = NULL;
206 info = TALLOC_ZERO_P( NULL, struct rcinit_file_information );
207 if (info == NULL) {
208 return False;
211 /* attempt the file open */
213 filepath = talloc_asprintf(info, "%s/%s/%s", get_dyn_MODULESDIR(),
214 SVCCTL_SCRIPT_DIR, servicename);
215 if (!filepath) {
216 TALLOC_FREE(info);
217 return false;
219 f = x_fopen( filepath, O_RDONLY, 0 );
220 if (f == NULL) {
221 DEBUG(0,("read_init_file: failed to open [%s]\n", filepath));
222 TALLOC_FREE(info);
223 return false;
226 while ( (x_fgets( str, sizeof(str)-1, f )) != NULL ) {
227 /* ignore everything that is not a full line
228 comment starting with a '#' */
230 if ( str[0] != '#' )
231 continue;
233 /* Look for a line like '^#.*Description:' */
235 p = strstr( str, "Description:" );
236 if (p != NULL) {
237 char *desc;
239 p += strlen( "Description:" ) + 1;
240 if ( !p )
241 break;
243 desc = cleanup_string(p);
244 if (desc != NULL)
245 info->description = talloc_strdup( info, desc );
249 x_fclose( f );
251 if ( !info->description )
252 info->description = talloc_strdup( info, "External Unix Service" );
254 *service_info = info;
255 TALLOC_FREE(filepath);
257 return True;
260 /********************************************************************
261 This is where we do the dirty work of filling in things like the
262 Display name, Description, etc...
263 ********************************************************************/
265 static WERROR svcctl_setvalue(struct registry_key *key,
266 const char *name,
267 struct registry_value *value)
269 WERROR wresult;
271 wresult = reg_setvalue(key, name, value);
272 if (!W_ERROR_IS_OK(wresult)) {
273 DEBUG(0, ("reg_setvalue failed for %s in key %s: %s\n",
274 name, key->key->name, win_errstr(wresult)));
277 return wresult;
280 static WERROR svcctl_setvalue_dword(struct registry_key *key,
281 const char *name,
282 uint32_t dword)
284 struct registry_value value;
286 value.type = REG_DWORD;
287 value.data.length = sizeof(uint32_t);
288 value.data.data = (uint8_t *)&dword;
290 return svcctl_setvalue(key, name, &value);
293 static WERROR svcctl_setvalue_sz(struct registry_key *key,
294 const char *name,
295 const char *sz)
297 struct registry_value value;
298 WERROR wresult;
299 TALLOC_CTX *mem_ctx = talloc_stackframe();
301 if (!push_reg_sz(mem_ctx, &value.data, sz)) {
302 DEBUG(0, ("push_reg_sz failed\n"));
303 wresult = WERR_NOMEM;
304 goto done;
306 value.type = REG_SZ;
308 wresult = svcctl_setvalue(key, name, &value);
309 done:
310 talloc_free(mem_ctx);
311 return wresult;
314 static void fill_service_values(struct registry_key *key)
316 char *dname, *ipath, *description;
317 int i;
318 WERROR wresult;
319 TALLOC_CTX *mem_ctx = talloc_stackframe();
320 char *name = NULL;
322 name = strrchr(key->key->name, '\\');
323 if (name == NULL) {
324 name = key->key->name;
325 } else {
326 name++;
329 /* These values are hardcoded in all QueryServiceConfig() replies.
330 I'm just storing them here for cosmetic purposes */
332 wresult = svcctl_setvalue_dword(key, "Start", SVCCTL_AUTO_START);
333 if (!W_ERROR_IS_OK(wresult)) {
334 goto done;
337 wresult = svcctl_setvalue_dword(key, "Type", SERVICE_TYPE_WIN32_OWN_PROCESS);
338 if (!W_ERROR_IS_OK(wresult)) {
339 goto done;
342 wresult = svcctl_setvalue_dword(key, "ErrorControl", SVCCTL_SVC_ERROR_NORMAL);
343 if (!W_ERROR_IS_OK(wresult)) {
344 goto done;
347 /* everything runs as LocalSystem */
349 wresult = svcctl_setvalue_sz(key, "ObjectName", "LocalSystem");
350 if (!W_ERROR_IS_OK(wresult)) {
351 goto done;
354 /* special considerations for internal services and the DisplayName value */
356 for ( i=0; builtin_svcs[i].servicename; i++ ) {
357 if ( strequal( name, builtin_svcs[i].servicename ) ) {
358 ipath = talloc_asprintf(mem_ctx, "%s/%s/%s",
359 get_dyn_MODULESDIR(),
360 SVCCTL_SCRIPT_DIR,
361 builtin_svcs[i].daemon);
362 description = talloc_strdup(mem_ctx, builtin_svcs[i].description);
363 dname = talloc_strdup(mem_ctx, builtin_svcs[i].dispname);
364 break;
368 /* default to an external service if we haven't found a match */
370 if ( builtin_svcs[i].servicename == NULL ) {
371 char *dispname = NULL;
372 struct rcinit_file_information *init_info = NULL;
374 ipath = talloc_asprintf(mem_ctx, "%s/%s/%s",
375 get_dyn_MODULESDIR(), SVCCTL_SCRIPT_DIR,
376 name);
378 /* lookup common unix display names */
379 dispname = get_common_service_dispname(name);
380 dname = talloc_strdup(mem_ctx, dispname ? dispname : "");
381 SAFE_FREE(dispname);
383 /* get info from init file itself */
384 if ( read_init_file( name, &init_info ) ) {
385 description = talloc_strdup(mem_ctx, init_info->description);
386 TALLOC_FREE( init_info );
388 else {
389 description = talloc_strdup(mem_ctx, "External Unix Service");
393 /* add the new values */
395 wresult = svcctl_setvalue_sz(key, "DisplayName", dname);
396 if (!W_ERROR_IS_OK(wresult)) {
397 goto done;
400 wresult = svcctl_setvalue_sz(key, "ImagePath", ipath);
401 if (!W_ERROR_IS_OK(wresult)) {
402 goto done;
405 wresult = svcctl_setvalue_sz(key, "Description", description);
407 done:
408 talloc_free(mem_ctx);
409 return;
412 /********************************************************************
413 ********************************************************************/
415 static void add_new_svc_name(struct registry_key *key_parent,
416 const char *name)
418 struct registry_key *key_service = NULL, *key_secdesc = NULL;
419 WERROR wresult;
420 struct security_descriptor *sd = NULL;
421 TALLOC_CTX *mem_ctx = talloc_stackframe();
422 enum winreg_CreateAction action = REG_ACTION_NONE;
424 wresult = reg_createkey(mem_ctx, key_parent, name, REG_KEY_ALL,
425 &key_service, &action);
427 if (!W_ERROR_IS_OK(wresult)) {
428 DEBUG(0, ("add_new_svc_name: reg_createkey failed for %s\\%s: "
429 "%s\n", key_parent->key->name, name,
430 win_errstr(wresult)));
431 goto done;
434 /* now for the service values */
436 fill_service_values(key_service);
438 /* now add the security descriptor */
440 sd = construct_service_sd(key_secdesc);
441 if (sd == NULL) {
442 DEBUG(0, ("add_new_svc_name: Failed to create default "
443 "sec_desc!\n"));
444 goto done;
447 wresult = svcctl_set_secdesc_internal(key_service, sd);
449 done:
450 talloc_free(mem_ctx);
451 return;
454 /********************************************************************
455 ********************************************************************/
457 void svcctl_init_keys( void )
459 const char **service_list = lp_svcctl_list();
460 int i;
461 struct registry_key *key = NULL;
462 struct registry_key *subkey = NULL;
463 WERROR wresult;
464 TALLOC_CTX *mem_ctx = talloc_stackframe();
466 /* bad mojo here if the lookup failed. Should not happen */
468 wresult = reg_open_path(mem_ctx, KEY_SERVICES, REG_KEY_ALL, get_root_nt_token(), &key);
470 if ( !W_ERROR_IS_OK(wresult) ) {
471 DEBUG(0,("svcctl_init_keys: key lookup failed! (%s)\n",
472 win_errstr(wresult)));
473 goto done;
476 /* the builtin services exist */
478 for ( i=0; builtin_svcs[i].servicename; i++ )
479 add_new_svc_name(key, builtin_svcs[i].servicename);
481 for ( i=0; service_list && service_list[i]; i++ ) {
483 /* only add new services */
485 wresult = reg_openkey(mem_ctx, key, service_list[i], REG_KEY_ALL, &subkey);
486 if (W_ERROR_IS_OK(wresult)) {
487 continue;
490 /* Add the new service key and initialize the appropriate values */
492 add_new_svc_name(key, service_list[i]);
495 /* initialize the control hooks */
497 init_service_op_table();
499 done:
500 talloc_free(mem_ctx);
501 return;
504 /********************************************************************
505 This is where we do the dirty work of filling in things like the
506 Display name, Description, etc...Always return a default secdesc
507 in case of any failure.
508 ********************************************************************/
510 struct security_descriptor *svcctl_get_secdesc( TALLOC_CTX *ctx, const char *name, struct security_token *token )
512 struct registry_key *key = NULL;
513 struct registry_value *value;
514 struct security_descriptor *ret_sd = NULL;
515 char *path= NULL;
516 WERROR wresult;
517 NTSTATUS status;
518 TALLOC_CTX *mem_ctx = talloc_stackframe();
520 path = talloc_asprintf(mem_ctx, "%s\\%s\\%s", KEY_SERVICES, name,
521 "Security");
522 if (path == NULL) {
523 goto done;
526 wresult = reg_open_path(mem_ctx, path, REG_KEY_ALL, token, &key);
527 if ( !W_ERROR_IS_OK(wresult) ) {
528 DEBUG(0,("svcctl_get_secdesc: key lookup failed! [%s] (%s)\n",
529 path, win_errstr(wresult)));
530 goto done;
533 wresult = reg_queryvalue(mem_ctx, key, "Security", &value);
534 if (W_ERROR_EQUAL(wresult, WERR_BADFILE)) {
535 goto fallback_to_default_sd;
536 } else if (!W_ERROR_IS_OK(wresult)) {
537 DEBUG(0, ("svcctl_get_secdesc: error getting value 'Security': "
538 "%s\n", win_errstr(wresult)));
539 goto done;
542 status = unmarshall_sec_desc(ctx, value->data.data,
543 value->data.length, &ret_sd);
545 if (NT_STATUS_IS_OK(status)) {
546 goto done;
549 fallback_to_default_sd:
550 DEBUG(6, ("svcctl_get_secdesc: constructing default secdesc for "
551 "service [%s]\n", name));
552 ret_sd = construct_service_sd(ctx);
554 done:
555 talloc_free(mem_ctx);
556 return ret_sd;
559 /********************************************************************
560 Wrapper to make storing a Service sd easier
561 ********************************************************************/
563 static WERROR svcctl_set_secdesc_internal(struct registry_key *key,
564 struct security_descriptor *sec_desc)
566 struct registry_key *key_security = NULL;
567 WERROR wresult;
568 struct registry_value value;
569 NTSTATUS status;
570 enum winreg_CreateAction action = REG_ACTION_NONE;
571 TALLOC_CTX *mem_ctx = talloc_stackframe();
573 wresult = reg_createkey(mem_ctx, key, "Security", REG_KEY_ALL, &key_security, &action);
574 if (!W_ERROR_IS_OK(wresult)) {
575 DEBUG(0, ("svcctl_set_secdesc: reg_createkey failed: "
576 "[%s\\Security] (%s)\n", key->key->name,
577 win_errstr(wresult)));
578 goto done;
581 status = marshall_sec_desc(mem_ctx, sec_desc, &value.data.data,
582 &value.data.length);
583 if (!NT_STATUS_IS_OK(status)) {
584 DEBUG(0, ("svcctl_set_secdesc: marshall_sec_desc() failed: %s\n",
585 nt_errstr(status)));
586 wresult = ntstatus_to_werror(status);
587 goto done;
590 value.type = REG_BINARY;
592 wresult = reg_setvalue(key_security, "Security", &value);
593 if (!W_ERROR_IS_OK(wresult)) {
594 DEBUG(0, ("svcctl_set_secdesc: reg_setvalue failed: %s\n",
595 win_errstr(wresult)));
598 done:
599 talloc_free(mem_ctx);
600 return wresult;
603 bool svcctl_set_secdesc(const char *name, struct security_descriptor *sec_desc,
604 struct security_token *token)
606 struct registry_key *key = NULL;
607 WERROR wresult;
608 char *path = NULL;
609 bool ret = false;
610 TALLOC_CTX *mem_ctx = talloc_stackframe();
612 path = talloc_asprintf(mem_ctx, "%s\\%s", KEY_SERVICES, name);
613 if (path == NULL) {
614 goto done;
617 wresult = reg_open_path(mem_ctx, path, REG_KEY_ALL, token, &key);
618 if ( !W_ERROR_IS_OK(wresult) ) {
619 DEBUG(0, ("svcctl_set_secdesc: key lookup failed! [%s] (%s)\n",
620 path, win_errstr(wresult)));
621 goto done;
624 wresult = svcctl_set_secdesc_internal(key, sec_desc);
626 ret = W_ERROR_IS_OK(wresult);
628 done:
629 talloc_free(mem_ctx);
630 return ret;
633 const char *svcctl_get_string_value(TALLOC_CTX *ctx, const char *key_name,
634 const char *value_name,
635 struct security_token *token)
637 const char *result = NULL;
638 struct registry_key *key = NULL;
639 struct registry_value *value = NULL;
640 char *path = NULL;
641 WERROR wresult;
642 TALLOC_CTX *mem_ctx = talloc_stackframe();
644 path = talloc_asprintf(mem_ctx, "%s\\%s", KEY_SERVICES, key_name);
645 if (path == NULL) {
646 goto done;
649 wresult = reg_open_path(mem_ctx, path, REG_KEY_READ, token, &key);
650 if (!W_ERROR_IS_OK(wresult)) {
651 DEBUG(0, ("svcctl_get_string_value: key lookup failed! "
652 "[%s] (%s)\n", path, win_errstr(wresult)));
653 goto done;
656 wresult = reg_queryvalue(mem_ctx, key, value_name, &value);
657 if (!W_ERROR_IS_OK(wresult)) {
658 DEBUG(0, ("svcctl_get_string_value: error getting value "
659 "'%s': %s\n", value_name, win_errstr(wresult)));
660 goto done;
663 if (value->type != REG_SZ) {
664 goto done;
667 pull_reg_sz(ctx, &value->data, &result);
669 goto done;
671 done:
672 talloc_free(mem_ctx);
673 return result;
676 /********************************************************************
677 ********************************************************************/
679 const char *svcctl_lookup_dispname(TALLOC_CTX *ctx, const char *name, struct security_token *token )
681 const char *display_name = NULL;
683 display_name = svcctl_get_string_value(ctx, name, "DisplayName", token);
685 if (display_name == NULL) {
686 display_name = talloc_strdup(ctx, name);
689 return display_name;
692 /********************************************************************
693 ********************************************************************/
695 const char *svcctl_lookup_description(TALLOC_CTX *ctx, const char *name, struct security_token *token )
697 const char *description = NULL;
699 description = svcctl_get_string_value(ctx, name, "Description", token);
701 if (description == NULL) {
702 description = talloc_strdup(ctx, "Unix Service");
705 return description;