2 * Unix SMB/CIFS implementation.
3 * Service Control API Implementation
5 * Copyright (C) Marcin Krzysztof Porwit 2005.
6 * Largely Rewritten by:
7 * Copyright (C) Gerald (Jerry) Carter 2005.
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/>.
24 #include "services/services.h"
26 #include "registry/reg_api_util.h"
28 struct rcinit_file_information
{
32 struct service_display_info
{
33 const char *servicename
;
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];
98 struct security_descriptor
*sd
= NULL
;
99 struct security_acl
*theacl
= NULL
;
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
) {
122 sd
= make_sec_desc(ctx
, SECURITY_DESCRIPTOR_REVISION_1
,
123 SEC_DESC_SELF_RELATIVE
, NULL
, NULL
, NULL
,
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
)
141 for ( i
=0; common_unix_svcs
[i
].servicename
; i
++ ) {
142 if (strequal(servicename
, common_unix_svcs
[i
].servicename
)) {
144 if (asprintf(&dispname
,
146 common_unix_svcs
[i
].dispname
,
147 common_unix_svcs
[i
].servicename
) < 0) {
154 return SMB_STRDUP(servicename
);
157 /********************************************************************
158 ********************************************************************/
160 static char *cleanup_string( const char *string
)
164 TALLOC_CTX
*ctx
= talloc_tos();
166 clean
= talloc_strdup(ctx
, string
);
172 /* trim any beginning whilespace */
174 while (isspace(*begin
)) {
178 if (*begin
== '\0') {
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' ) {
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
;
206 info
= TALLOC_ZERO_P( NULL
, struct rcinit_file_information
);
211 /* attempt the file open */
213 filepath
= talloc_asprintf(info
, "%s/%s/%s", get_dyn_MODULESDIR(),
214 SVCCTL_SCRIPT_DIR
, servicename
);
219 f
= x_fopen( filepath
, O_RDONLY
, 0 );
221 DEBUG(0,("read_init_file: failed to open [%s]\n", filepath
));
226 while ( (x_fgets( str
, sizeof(str
)-1, f
)) != NULL
) {
227 /* ignore everything that is not a full line
228 comment starting with a '#' */
233 /* Look for a line like '^#.*Description:' */
235 p
= strstr( str
, "Description:" );
239 p
+= strlen( "Description:" ) + 1;
243 desc
= cleanup_string(p
);
245 info
->description
= talloc_strdup( info
, desc
);
251 if ( !info
->description
)
252 info
->description
= talloc_strdup( info
, "External Unix Service" );
254 *service_info
= info
;
255 TALLOC_FREE(filepath
);
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
,
267 struct registry_value
*value
)
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
)));
280 static WERROR
svcctl_setvalue_dword(struct registry_key
*key
,
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
,
297 struct registry_value value
;
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
;
308 wresult
= svcctl_setvalue(key
, name
, &value
);
310 talloc_free(mem_ctx
);
314 static void fill_service_values(struct registry_key
*key
)
316 char *dname
, *ipath
, *description
;
319 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
322 name
= strrchr(key
->key
->name
, '\\');
324 name
= key
->key
->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
)) {
337 wresult
= svcctl_setvalue_dword(key
, "Type", SERVICE_TYPE_WIN32_OWN_PROCESS
);
338 if (!W_ERROR_IS_OK(wresult
)) {
342 wresult
= svcctl_setvalue_dword(key
, "ErrorControl", SVCCTL_SVC_ERROR_NORMAL
);
343 if (!W_ERROR_IS_OK(wresult
)) {
347 /* everything runs as LocalSystem */
349 wresult
= svcctl_setvalue_sz(key
, "ObjectName", "LocalSystem");
350 if (!W_ERROR_IS_OK(wresult
)) {
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(),
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
);
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
,
378 /* lookup common unix display names */
379 dispname
= get_common_service_dispname(name
);
380 dname
= talloc_strdup(mem_ctx
, dispname
? 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
);
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
)) {
400 wresult
= svcctl_setvalue_sz(key
, "ImagePath", ipath
);
401 if (!W_ERROR_IS_OK(wresult
)) {
405 wresult
= svcctl_setvalue_sz(key
, "Description", description
);
408 talloc_free(mem_ctx
);
412 /********************************************************************
413 ********************************************************************/
415 static void add_new_svc_name(struct registry_key
*key_parent
,
418 struct registry_key
*key_service
= NULL
, *key_secdesc
= NULL
;
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
)));
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
);
442 DEBUG(0, ("add_new_svc_name: Failed to create default "
447 wresult
= svcctl_set_secdesc_internal(key_service
, sd
);
450 talloc_free(mem_ctx
);
454 /********************************************************************
455 ********************************************************************/
457 void svcctl_init_keys( void )
459 const char **service_list
= lp_svcctl_list();
461 struct registry_key
*key
= NULL
;
462 struct registry_key
*subkey
= NULL
;
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
)));
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
)) {
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();
500 talloc_free(mem_ctx
);
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
;
518 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
520 path
= talloc_asprintf(mem_ctx
, "%s\\%s\\%s", KEY_SERVICES
, name
,
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
)));
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
)));
542 status
= unmarshall_sec_desc(ctx
, value
->data
.data
,
543 value
->data
.length
, &ret_sd
);
545 if (NT_STATUS_IS_OK(status
)) {
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
);
555 talloc_free(mem_ctx
);
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
;
568 struct registry_value value
;
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
)));
581 status
= marshall_sec_desc(mem_ctx
, sec_desc
, &value
.data
.data
,
583 if (!NT_STATUS_IS_OK(status
)) {
584 DEBUG(0, ("svcctl_set_secdesc: marshall_sec_desc() failed: %s\n",
586 wresult
= ntstatus_to_werror(status
);
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
)));
599 talloc_free(mem_ctx
);
603 bool svcctl_set_secdesc(const char *name
, struct security_descriptor
*sec_desc
,
604 struct security_token
*token
)
606 struct registry_key
*key
= NULL
;
610 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
612 path
= talloc_asprintf(mem_ctx
, "%s\\%s", KEY_SERVICES
, name
);
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
)));
624 wresult
= svcctl_set_secdesc_internal(key
, sec_desc
);
626 ret
= W_ERROR_IS_OK(wresult
);
629 talloc_free(mem_ctx
);
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
;
642 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
644 path
= talloc_asprintf(mem_ctx
, "%s\\%s", KEY_SERVICES
, key_name
);
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
)));
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
)));
663 if (value
->type
!= REG_SZ
) {
667 pull_reg_sz(ctx
, &value
->data
, &result
);
672 talloc_free(mem_ctx
);
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
);
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");