/*
* Unix SMB/CIFS implementation.
* Service Control API Implementation
- * Copyright (C) Gerald Carter 2005.
+ *
* Copyright (C) Marcin Krzysztof Porwit 2005.
+ * Largely Rewritten by:
+ * Copyright (C) Gerald (Jerry) Carter 2005.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
+#include "services/services.h"
+#include "registry.h"
+#include "registry/reg_util_legacy.h"
+#include "registry/reg_dispatcher.h"
+#include "registry/reg_objects.h"
+
+struct rcinit_file_information {
+ char *description;
+};
-#if 0
+struct service_display_info {
+ const char *servicename;
+ const char *daemon;
+ const char *dispname;
+ const char *description;
+};
-/* backend database routines for services.tdb */
+struct service_display_info builtin_svcs[] = {
+ { "Spooler", "smbd", "Print Spooler", "Internal service for spooling files to print devices" },
+ { "NETLOGON", "smbd", "Net Logon", "File service providing access to policy and profile data (not remotely manageable)" },
+ { "RemoteRegistry", "smbd", "Remote Registry Service", "Internal service providing remote access to "
+ "the Samba registry" },
+ { "WINS", "nmbd", "Windows Internet Name Service (WINS)", "Internal service providing a "
+ "NetBIOS point-to-point name server (not remotely manageable)" },
+ { NULL, NULL, NULL, NULL }
+};
-#define SERVICEDB_VERSION_V1 1 /* Will there be more? */
-#define INTERNAL_SERVICES_LIST "NETLOGON Spooler"
+struct service_display_info common_unix_svcs[] = {
+ { "cups", NULL, "Common Unix Printing System","Provides unified printing support for all operating systems" },
+ { "postfix", NULL, "Internet Mail Service", "Provides support for sending and receiving electonic mail" },
+ { "sendmail", NULL, "Internet Mail Service", "Provides support for sending and receiving electonic mail" },
+ { "portmap", NULL, "TCP Port to RPC PortMapper",NULL },
+ { "xinetd", NULL, "Internet Meta-Daemon", NULL },
+ { "inet", NULL, "Internet Meta-Daemon", NULL },
+ { "xntpd", NULL, "Network Time Service", NULL },
+ { "ntpd", NULL, "Network Time Service", NULL },
+ { "lpd", NULL, "BSD Print Spooler", NULL },
+ { "nfsserver", NULL, "Network File Service", NULL },
+ { "cron", NULL, "Scheduling Service", NULL },
+ { "at", NULL, "Scheduling Service", NULL },
+ { "nscd", NULL, "Name Service Cache Daemon", NULL },
+ { "slapd", NULL, "LDAP Directory Service", NULL },
+ { "ldap", NULL, "LDAP DIrectory Service", NULL },
+ { "ypbind", NULL, "NIS Directory Service", NULL },
+ { "courier-imap", NULL, "IMAP4 Mail Service", NULL },
+ { "courier-pop3", NULL, "POP3 Mail Service", NULL },
+ { "named", NULL, "Domain Name Service", NULL },
+ { "bind", NULL, "Domain Name Service", NULL },
+ { "httpd", NULL, "HTTP Server", NULL },
+ { "apache", NULL, "HTTP Server", "Provides s highly scalable and flexible web server "
+ "capable of implementing various protocols incluing "
+ "but not limited to HTTP" },
+ { "autofs", NULL, "Automounter", NULL },
+ { "squid", NULL, "Web Cache Proxy ", NULL },
+ { "perfcountd", NULL, "Performance Monitoring Daemon", NULL },
+ { "pgsql", NULL, "PgSQL Database Server", "Provides service for SQL database from Postgresql.org" },
+ { "arpwatch", NULL, "ARP Tables watcher", "Provides service for monitoring ARP tables for changes" },
+ { "dhcpd", NULL, "DHCP Server", "Provides service for dynamic host configuration and IP assignment" },
+ { "nwserv", NULL, "NetWare Server Emulator", "Provides service for emulating Novell NetWare 3.12 server" },
+ { "proftpd", NULL, "Professional FTP Server", "Provides high configurable service for FTP connection and "
+ "file transferring" },
+ { "ssh2", NULL, "SSH Secure Shell", "Provides service for secure connection for remote administration" },
+ { "sshd", NULL, "SSH Secure Shell", "Provides service for secure connection for remote administration" },
+ { NULL, NULL, NULL, NULL }
+};
-/* */
-/* scripts will execute from the following libdir, if they are in the enable svcctl=<list of scripts> */
-/* these should likely be symbolic links. Note that information about them will be extracted from the files themselves */
-/* using the LSB standard keynames for various information */
-#define SCVCTL_DATABASE_VERSION_V1 1
-static TDB_CONTEXT *service_tdb; /* used for services tdb file */
+/********************************************************************
+********************************************************************/
-/* there are two types of services -- internal, and external.
- Internal services are "built-in" to samba -- there may be
- functions that exist to provide the control and enumeration
- functions. There certainly is information returned to be
- displayed in the typical management console.
+static struct security_descriptor* construct_service_sd( TALLOC_CTX *ctx )
+{
+ struct security_ace ace[4];
+ size_t i = 0;
+ struct security_descriptor *sd = NULL;
+ struct security_acl *theacl = NULL;
+ size_t sd_size;
- External services are those that can be specified in the smb.conf
- file -- and they conform to the LSB specification as to having
- particular keywords in the scripts. Note that these "scripts" are
- located in the lib directory, and are likely links to LSB-compliant
- init.d scripts, such as those that might come with Suse. Note
- that the spec is located http://www.linuxbase.org/spec/ */
+ /* basic access for Everyone */
+ init_sec_ace(&ace[i++], &global_sid_World,
+ SEC_ACE_TYPE_ACCESS_ALLOWED, SERVICE_READ_ACCESS, 0);
+ init_sec_ace(&ace[i++], &global_sid_Builtin_Power_Users,
+ SEC_ACE_TYPE_ACCESS_ALLOWED, SERVICE_EXECUTE_ACCESS, 0);
-/* Expand this to include what can and can't be done
- with a particular internal service. Expand as necessary
- to add other infromation like what can be controlled,
- etc. */
+ init_sec_ace(&ace[i++], &global_sid_Builtin_Server_Operators,
+ SEC_ACE_TYPE_ACCESS_ALLOWED, SERVICE_ALL_ACCESS, 0);
+ init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
+ SEC_ACE_TYPE_ACCESS_ALLOWED, SERVICE_ALL_ACCESS, 0);
-typedef struct Internal_service_struct
-{
- const char *filename; /* internal name "index" */
- const char *displayname;
- const char *description;
- const uint32 statustype;
- void *status_fn;
- void *control_fn;
-} Internal_service_description;
+ /* create the security descriptor */
+ theacl = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace);
+ if (theacl == NULL) {
+ return NULL;
+ }
-static const Internal_service_description ISD[] = {
- { "NETLOGON", "Net Logon", "Provides logon and authentication service to the network", 0x110, NULL, NULL},
- { "Spooler", "Spooler", "Printing Services", 0x0020, NULL, NULL},
- { NULL, NULL, NULL, 0, NULL, NULL}
-};
+ sd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
+ SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
+ theacl, &sd_size);
+ if (sd == NULL) {
+ return NULL;
+ }
+ return sd;
+}
/********************************************************************
- allocate an array of external services and return them. Null return
- is okay, make sure &added is also zero!
+ This is where we do the dirty work of filling in things like the
+ Display name, Description, etc...
********************************************************************/
-int num_external_services(void)
+static char *get_common_service_dispname( const char *servicename )
{
- int num_services;
- char **svc_list;
- pstring keystring, external_services_string;
- TDB_DATA key_data;
-
-
- if (!service_tdb) {
- DEBUG(8,("enum_external_services: service database is not open!!!\n"));
- num_services = 0;
- } else {
- pstrcpy(keystring,"EXTERNAL_SERVICES");
- key_data = tdb_fetch_bystring(service_tdb, keystring);
-
- if ((key_data.dptr != NULL) && (key_data.dsize != 0)) {
- strncpy(external_services_string,key_data.dptr,key_data.dsize);
- external_services_string[key_data.dsize] = 0;
- DEBUG(8,("enum_external_services: services list is %s, size is %d\n",external_services_string,key_data.dsize));
+ int i;
+
+ for ( i=0; common_unix_svcs[i].servicename; i++ ) {
+ if (strequal(servicename, common_unix_svcs[i].servicename)) {
+ char *dispname;
+ if (asprintf(&dispname,
+ "%s (%s)",
+ common_unix_svcs[i].dispname,
+ common_unix_svcs[i].servicename) < 0) {
+ return NULL;
+ }
+ return dispname;
}
- }
- svc_list = str_list_make(external_services_string,NULL);
-
- num_services = str_list_count( (const char **)svc_list);
+ }
- return num_services;
+ return SMB_STRDUP(servicename );
}
+/********************************************************************
+********************************************************************/
+
+static char *cleanup_string( const char *string )
+{
+ char *clean = NULL;
+ char *begin, *end;
+ TALLOC_CTX *ctx = talloc_tos();
+
+ clean = talloc_strdup(ctx, string);
+ if (!clean) {
+ return NULL;
+ }
+ begin = clean;
+
+ /* trim any beginning whilespace */
+
+ while (isspace(*begin)) {
+ begin++;
+ }
+
+ if (*begin == '\0') {
+ return NULL;
+ }
+
+ /* trim any trailing whitespace or carriage returns.
+ Start at the end and move backwards */
+ end = begin + strlen(begin) - 1;
+
+ while ( isspace(*end) || *end=='\n' || *end=='\r' ) {
+ *end = '\0';
+ end--;
+ }
+
+ return begin;
+}
/********************************************************************
- Gather information on the "external services". These are services
- listed in the smb.conf file, and found to exist through checks in
- this code. Note that added will be incremented on the basis of the
- number of services added. svc_ptr should have enough memory allocated
- to accommodate all of the services that exist.
-
- Typically num_external_services is used to "size" the amount of
- memory allocated, but does little/no work.
-
- enum_external_services() actually examines each of the specified
- external services, populates the memory structures, and returns.
-
- ** note that 'added' may end up with less than the number of services
- found in _num_external_services, such as the case when a service is
- called out, but the actual service doesn't exist or the file can't be
- read for the service information.
********************************************************************/
-WERROR enum_external_services(TALLOC_CTX *tcx,ENUM_SERVICES_STATUS **svc_ptr, int existing_services,int *added)
+static bool read_init_file( const char *servicename, struct rcinit_file_information **service_info )
{
- /* *svc_ptr must have pre-allocated memory */
- int num_services = 0;
- int i = 0;
- ENUM_SERVICES_STATUS *services=NULL;
- char **svc_list,**svcname;
- pstring command, keystring, external_services_string;
- int ret;
- int fd = -1;
- Service_info *si;
- TDB_DATA key_data;
-
- *added = num_services;
-
- if (!service_tdb) {
- DEBUG(8,("enum_external_services: service database is not open!!!\n"));
- } else {
- pstrcpy(keystring,"EXTERNAL_SERVICES");
- key_data = tdb_fetch_bystring(service_tdb, keystring);
- if ((key_data.dptr != NULL) && (key_data.dsize != 0)) {
- strncpy(external_services_string,key_data.dptr,key_data.dsize);
- external_services_string[key_data.dsize] = 0;
- DEBUG(8,("enum_external_services: services list is %s, size is %d\n",external_services_string,key_data.dsize));
- }
- }
- svc_list = str_list_make(external_services_string,NULL);
-
- num_services = str_list_count( (const char **)svc_list);
-
- if (0 == num_services) {
- DEBUG(8,("enum_external_services: there are no external services\n"));
- *added = num_services;
- return WERR_OK;
- }
- DEBUG(8,("enum_external_services: there are [%d] external services\n",num_services));
- si=TALLOC_ARRAY( tcx, Service_info, 1 );
- if (si == NULL) {
- DEBUG(8,("enum_external_services: Failed to alloc si\n"));
- return WERR_NOMEM;
- }
-
-#if 0
-/* *svc_ptr has the pointer to the array if there is one already. NULL if not. */
- if ((existing_services>0) && svc_ptr && *svc_ptr) { /* reallocate vs. allocate */
- DEBUG(8,("enum_external_services: REALLOCing %x to %d services\n", *svc_ptr, existing_services+num_services));
-
- services=TALLOC_REALLOC_ARRAY(tcx,*svc_ptr,ENUM_SERVICES_STATUS,existing_services+num_services);
- DEBUG(8,("enum_external_services: REALLOCed to %x services\n", services));
-
- if (!services) return WERR_NOMEM;
- *svc_ptr = services;
- } else {
- if ( !(services = TALLOC_ARRAY( tcx, ENUM_SERVICES_STATUS, num_services )) )
- return WERR_NOMEM;
- }
-#endif
-
- if (!svc_ptr || !(*svc_ptr))
- return WERR_NOMEM;
- services = *svc_ptr;
- if (existing_services > 0) {
- i+=existing_services;
- }
-
- svcname = svc_list;
- DEBUG(8,("enum_external_services: enumerating %d external services starting at index %d\n", num_services,existing_services));
-
- while (*svcname) {
- DEBUG(10,("enum_external_services: Reading information on service %s, index %d\n",*svcname,i));
- /* get_LSB_data(*svcname,si); */
- if (!get_service_info(service_tdb,*svcname, si)) {
- DEBUG(1,("enum_external_services: CAN'T FIND INFO FOR SERVICE %s in the services DB\n",*svcname));
- }
+ struct rcinit_file_information *info = NULL;
+ char *filepath = NULL;
+ char str[1024];
+ XFILE *f = NULL;
+ char *p = NULL;
- if ((si->filename == NULL) || (*si->filename == 0)) {
- init_unistr(&services[i].servicename, *svcname );
- } else {
- init_unistr( &services[i].servicename, si->filename );
- /* init_unistr( &services[i].servicename, si->servicename ); */
- }
+ if ( !(info = TALLOC_ZERO_P( NULL, struct rcinit_file_information ) ) )
+ return False;
- if ((si->provides == NULL) || (*si->provides == 0)) {
- init_unistr(&services[i].displayname, *svcname );
- } else {
- init_unistr( &services[i].displayname, si->provides );
- }
+ /* attempt the file open */
+
+ filepath = talloc_asprintf(info, "%s/%s/%s", get_dyn_MODULESDIR(),
+ SVCCTL_SCRIPT_DIR, servicename);
+ if (!filepath) {
+ TALLOC_FREE(info);
+ return false;
+ }
+ if (!(f = x_fopen( filepath, O_RDONLY, 0 ))) {
+ DEBUG(0,("read_init_file: failed to open [%s]\n", filepath));
+ TALLOC_FREE(info);
+ return false;
+ }
+
+ while ( (x_fgets( str, sizeof(str)-1, f )) != NULL ) {
+ /* ignore everything that is not a full line
+ comment starting with a '#' */
+
+ if ( str[0] != '#' )
+ continue;
+
+ /* Look for a line like '^#.*Description:' */
- /* TODO - we could keep the following info in the DB, too... */
-
- DEBUG(8,("enum_external_services: Service name [%s] displayname [%s]\n",
- si->filename, si->provides));
- services[i].status.type = SVCCTL_WIN32_OWN_PROC;
- services[i].status.win32_exit_code = 0x0;
- services[i].status.service_exit_code = 0x0;
- services[i].status.check_point = 0x0;
- services[i].status.wait_hint = 0x0;
-
- /* TODO - do callout here to get the status */
-
- memset(command, 0, sizeof(command));
- slprintf(command, sizeof(command)-1, "%s%s%s %s", dyn_LIBDIR, SVCCTL_SCRIPT_DIR, *svcname, "status");
-
- DEBUG(10, ("enum_external_services: status command is [%s]\n", command));
-
- /* TODO - wrap in privilege check */
-
- ret = smbrun(command, &fd);
- DEBUGADD(10, ("returned [%d]\n", ret));
- close(fd);
- if(ret != 0)
- DEBUG(10, ("enum_external_services: Command returned [%d]\n", ret));
- services[i].status.state = SVCCTL_STOPPED;
- if (ret == 0) {
- services[i].status.state = SVCCTL_RUNNING;
- services[i].status.controls_accepted = SVCCTL_CONTROL_SHUTDOWN | SVCCTL_CONTROL_STOP;
- } else {
- services[i].status.state = SVCCTL_STOPPED;
- services[i].status.controls_accepted = 0;
+ if ( (p = strstr( str, "Description:" )) != NULL ) {
+ char *desc;
+
+ p += strlen( "Description:" ) + 1;
+ if ( !p )
+ break;
+
+ if ( (desc = cleanup_string(p)) != NULL )
+ info->description = talloc_strdup( info, desc );
}
- svcname++;
- i++;
- }
+ }
- DEBUG(10,("enum_external_services: Read services %d\n",num_services));
- *added = num_services;
+ x_fclose( f );
- return WERR_OK;
+ if ( !info->description )
+ info->description = talloc_strdup( info, "External Unix Service" );
+
+ *service_info = info;
+ TALLOC_FREE(filepath);
+
+ return True;
}
/********************************************************************
+ This is where we do the dirty work of filling in things like the
+ Display name, Description, etc...
********************************************************************/
-int num_internal_services(void)
+static void fill_service_values(const char *name, struct regval_ctr *values)
{
- int num_services;
- char **svc_list;
- pstring keystring, internal_services_string;
- TDB_DATA key_data;
-
- if (!service_tdb) {
- DEBUG(8,("enum_internal_services: service database is not open!!!\n"));
- num_services = 0;
- } else {
- pstrcpy(keystring,"INTERNAL_SERVICES");
- key_data = tdb_fetch_bystring(service_tdb, keystring);
-
- if ((key_data.dptr != NULL) && (key_data.dsize != 0)) {
- strncpy(internal_services_string,key_data.dptr,key_data.dsize);
- internal_services_string[key_data.dsize] = 0;
- DEBUG(8,("enum_internal_services: services list is %s, size is %d\n",internal_services_string,key_data.dsize));
+ char *dname, *ipath, *description;
+ uint32 dword;
+ int i;
+
+ /* These values are hardcoded in all QueryServiceConfig() replies.
+ I'm just storing them here for cosmetic purposes */
+
+ dword = SVCCTL_AUTO_START;
+ regval_ctr_addvalue( values, "Start", REG_DWORD, (uint8 *)&dword, sizeof(uint32));
+
+ dword = SERVICE_TYPE_WIN32_OWN_PROCESS;
+ regval_ctr_addvalue( values, "Type", REG_DWORD, (uint8 *)&dword, sizeof(uint32));
+
+ dword = SVCCTL_SVC_ERROR_NORMAL;
+ regval_ctr_addvalue( values, "ErrorControl", REG_DWORD, (uint8 *)&dword, sizeof(uint32));
+
+ /* everything runs as LocalSystem */
+
+ regval_ctr_addvalue_sz(values, "ObjectName", "LocalSystem");
+
+ /* special considerations for internal services and the DisplayName value */
+
+ for ( i=0; builtin_svcs[i].servicename; i++ ) {
+ if ( strequal( name, builtin_svcs[i].servicename ) ) {
+ ipath = talloc_asprintf(talloc_tos(), "%s/%s/%s",
+ get_dyn_MODULESDIR(), SVCCTL_SCRIPT_DIR,
+ builtin_svcs[i].daemon);
+ description = talloc_strdup(talloc_tos(), builtin_svcs[i].description);
+ dname = talloc_strdup(talloc_tos(), builtin_svcs[i].dispname);
+ break;
+ }
+ }
+
+ /* default to an external service if we haven't found a match */
+
+ if ( builtin_svcs[i].servicename == NULL ) {
+ char *dispname = NULL;
+ struct rcinit_file_information *init_info = NULL;
+
+ ipath = talloc_asprintf(talloc_tos(), "%s/%s/%s",
+ get_dyn_MODULESDIR(), SVCCTL_SCRIPT_DIR,
+ name);
+
+ /* lookup common unix display names */
+ dispname = get_common_service_dispname(name);
+ dname = talloc_strdup(talloc_tos(), dispname ? dispname : "");
+ SAFE_FREE(dispname);
+
+ /* get info from init file itself */
+ if ( read_init_file( name, &init_info ) ) {
+ description = talloc_strdup(talloc_tos(), init_info->description);
+ TALLOC_FREE( init_info );
}
- }
- svc_list = str_list_make(internal_services_string,NULL);
-
- num_services = str_list_count( (const char **)svc_list);
+ else {
+ description = talloc_strdup(talloc_tos(), "External Unix Service");
+ }
+ }
+
+ /* add the new values */
- return num_services;
+ regval_ctr_addvalue_sz(values, "DisplayName", dname);
+ regval_ctr_addvalue_sz(values, "ImagePath", ipath);
+ regval_ctr_addvalue_sz(values, "Description", description);
+
+ TALLOC_FREE(dname);
+ TALLOC_FREE(ipath);
+ TALLOC_FREE(description);
+
+ return;
}
-#if 0
-/*********************************************************************
- given a service nice name, find the underlying service name
-*********************************************************************/
+/********************************************************************
+********************************************************************/
-static BOOL convert_service_displayname(TDB_CONTEXT *stdb,pstring service_nicename, pstring servicename,int szsvcname)
+static void add_new_svc_name(struct registry_key_handle *key_parent,
+ struct regsubkey_ctr *subkeys,
+ const char *name )
{
- pstring keystring;
- TDB_DATA key_data;
+ struct registry_key_handle *key_service = NULL, *key_secdesc = NULL;
+ WERROR wresult;
+ char *path = NULL;
+ struct regval_ctr *values = NULL;
+ struct regsubkey_ctr *svc_subkeys = NULL;
+ struct security_descriptor *sd = NULL;
+ DATA_BLOB sd_blob;
+ NTSTATUS status;
- if ((stdb == NULL) || (service_nicename==NULL) || (servicename == NULL))
- return False;
+ /* add to the list and create the subkey path */
+
+ regsubkey_ctr_addkey( subkeys, name );
+ store_reg_keys( key_parent, subkeys );
- pstr_sprintf(keystring,"SERVICE_NICENAME/%s", servicename);
+ /* open the new service key */
- DEBUG(5, ("convert_service_displayname: Looking for service name [%s], key [%s]\n",
- service_nicename, keystring));
+ if (asprintf(&path, "%s\\%s", KEY_SERVICES, name) < 0) {
+ return;
+ }
+ wresult = regkey_open_internal( NULL, &key_service, path,
+ get_root_nt_token(), REG_KEY_ALL );
+ if ( !W_ERROR_IS_OK(wresult) ) {
+ DEBUG(0,("add_new_svc_name: key lookup failed! [%s] (%s)\n",
+ path, win_errstr(wresult)));
+ SAFE_FREE(path);
+ return;
+ }
+ SAFE_FREE(path);
- key_data = tdb_fetch_bystring(stdb,keystring);
+ /* add the 'Security' key */
- if (key_data.dsize == 0) {
- DEBUG(5, ("convert_service_displayname: [%s] Not found, tried key [%s]\n",service_nicename,keystring));
- return False;
+ wresult = regsubkey_ctr_init(key_service, &svc_subkeys);
+ if (!W_ERROR_IS_OK(wresult)) {
+ DEBUG(0,("add_new_svc_name: talloc() failed!\n"));
+ TALLOC_FREE( key_service );
+ return;
}
- strncpy(servicename,key_data.dptr,szsvcname);
- servicename[(key_data.dsize > szsvcname ? szsvcname : key_data.dsize)] = 0;
- DEBUG(5, ("convert_service_displayname: Found service name [%s], name is [%s]\n",
- service_nicename,servicename));
+ fetch_reg_keys( key_service, svc_subkeys );
+ regsubkey_ctr_addkey( svc_subkeys, "Security" );
+ store_reg_keys( key_service, svc_subkeys );
- return True;
+ /* now for the service values */
+
+ wresult = regval_ctr_init(key_service, &values);
+ if (!W_ERROR_IS_OK(wresult)) {
+ DEBUG(0,("add_new_svc_name: talloc() failed!\n"));
+ TALLOC_FREE( key_service );
+ return;
+ }
+
+ fill_service_values( name, values );
+ store_reg_values( key_service, values );
+
+ /* cleanup the service key*/
+
+ TALLOC_FREE( key_service );
+
+ /* now add the security descriptor */
+
+ if (asprintf(&path, "%s\\%s\\%s", KEY_SERVICES, name, "Security") < 0) {
+ return;
+ }
+ wresult = regkey_open_internal( NULL, &key_secdesc, path,
+ get_root_nt_token(), REG_KEY_ALL );
+ if ( !W_ERROR_IS_OK(wresult) ) {
+ DEBUG(0,("add_new_svc_name: key lookup failed! [%s] (%s)\n",
+ path, win_errstr(wresult)));
+ TALLOC_FREE( key_secdesc );
+ SAFE_FREE(path);
+ return;
+ }
+ SAFE_FREE(path);
+
+ wresult = regval_ctr_init(key_secdesc, &values);
+ if (!W_ERROR_IS_OK(wresult)) {
+ DEBUG(0,("add_new_svc_name: talloc() failed!\n"));
+ TALLOC_FREE( key_secdesc );
+ return;
+ }
+
+ if ( !(sd = construct_service_sd(key_secdesc)) ) {
+ DEBUG(0,("add_new_svc_name: Failed to create default sec_desc!\n"));
+ TALLOC_FREE( key_secdesc );
+ return;
+ }
+
+ status = marshall_sec_desc(key_secdesc, sd, &sd_blob.data,
+ &sd_blob.length);
+ if (!NT_STATUS_IS_OK(status)) {
+ DEBUG(0, ("marshall_sec_desc failed: %s\n",
+ nt_errstr(status)));
+ TALLOC_FREE(key_secdesc);
+ return;
+ }
+
+ regval_ctr_addvalue(values, "Security", REG_BINARY,
+ sd_blob.data, sd_blob.length);
+ store_reg_values( key_secdesc, values );
+
+ TALLOC_FREE( key_secdesc );
+
+ return;
}
-#endif
-/*******************************************************************************
- Get the INTERNAL services information for the given service name.
-*******************************************************************************/
+/********************************************************************
+********************************************************************/
-static BOOL get_internal_service_data(const Internal_service_description *isd, Service_info *si)
+void svcctl_init_keys( void )
{
- ZERO_STRUCTP( si );
-#if 0
-
- pstrcpy( si->servicename, isd->displayname);
- pstrcpy( si->servicetype, "INTERNAL");
- pstrcpy( si->filename, isd->filename);
- pstrcpy( si->provides, isd->displayname);
- pstrcpy( si->description, isd->description);
- pstrcpy( si->shortdescription, isd->description);
-#endif
-
- return True;
+ const char **service_list = lp_svcctl_list();
+ int i;
+ struct regsubkey_ctr *subkeys = NULL;
+ struct registry_key_handle *key = NULL;
+ WERROR wresult;
+
+ /* bad mojo here if the lookup failed. Should not happen */
+
+ wresult = regkey_open_internal( NULL, &key, KEY_SERVICES,
+ get_root_nt_token(), REG_KEY_ALL );
+
+ if ( !W_ERROR_IS_OK(wresult) ) {
+ DEBUG(0,("svcctl_init_keys: key lookup failed! (%s)\n",
+ win_errstr(wresult)));
+ return;
+ }
+
+ /* lookup the available subkeys */
+
+ wresult = regsubkey_ctr_init(key, &subkeys);
+ if (!W_ERROR_IS_OK(wresult)) {
+ DEBUG(0,("svcctl_init_keys: talloc() failed!\n"));
+ TALLOC_FREE( key );
+ return;
+ }
+
+ fetch_reg_keys( key, subkeys );
+
+ /* the builtin services exist */
+
+ for ( i=0; builtin_svcs[i].servicename; i++ )
+ add_new_svc_name( key, subkeys, builtin_svcs[i].servicename );
+
+ for ( i=0; service_list && service_list[i]; i++ ) {
+
+ /* only add new services */
+ if ( regsubkey_ctr_key_exists( subkeys, service_list[i] ) )
+ continue;
+
+ /* Add the new service key and initialize the appropriate values */
+
+ add_new_svc_name( key, subkeys, service_list[i] );
+ }
+
+ TALLOC_FREE( key );
+
+ /* initialize the control hooks */
+
+ init_service_op_table();
+
+ return;
}
/********************************************************************
+ This is where we do the dirty work of filling in things like the
+ Display name, Description, etc...Always return a default secdesc
+ in case of any failure.
********************************************************************/
-BOOL get_service_info(TDB_CONTEXT *stdb,char *service_name, Service_info *si)
+struct security_descriptor *svcctl_get_secdesc( TALLOC_CTX *ctx, const char *name, struct security_token *token )
{
+ struct registry_key_handle *key = NULL;
+ struct regval_ctr *values = NULL;
+ struct regval_blob *val = NULL;
+ struct security_descriptor *ret_sd = NULL;
+ char *path= NULL;
+ WERROR wresult;
+ NTSTATUS status;
+
+ /* now add the security descriptor */
+
+ if (asprintf(&path, "%s\\%s\\%s", KEY_SERVICES, name, "Security") < 0) {
+ return NULL;
+ }
+ wresult = regkey_open_internal( NULL, &key, path, token,
+ REG_KEY_ALL );
+ if ( !W_ERROR_IS_OK(wresult) ) {
+ DEBUG(0,("svcctl_get_secdesc: key lookup failed! [%s] (%s)\n",
+ path, win_errstr(wresult)));
+ goto done;
+ }
- pstring keystring;
- TDB_DATA key_data;
+ wresult = regval_ctr_init(key, &values);
+ if (!W_ERROR_IS_OK(wresult)) {
+ DEBUG(0,("svcctl_get_secdesc: talloc() failed!\n"));
+ goto done;
+ }
- if ((stdb == NULL) || (si == NULL) || (service_name==NULL) || (*service_name == 0))
- return False;
+ if (fetch_reg_values( key, values ) == -1) {
+ DEBUG(0, ("Error getting registry values\n"));
+ goto done;
+ }
+
+ if ( !(val = regval_ctr_getvalue( values, "Security" )) ) {
+ goto fallback_to_default_sd;
+ }
- /* TODO - error handling -- what if the service isn't in the DB? */
-
- pstr_sprintf(keystring,"SERVICE/%s/TYPE", service_name);
- key_data = tdb_fetch_bystring(stdb,keystring);
- strncpy(si->servicetype,key_data.dptr,key_data.dsize);
- si->servicetype[key_data.dsize] = 0;
+ /* stream the service security descriptor */
- /* crude check to see if the service exists... */
- DEBUG(3,("Size of the TYPE field is %d\n",key_data.dsize));
- if (key_data.dsize == 0)
- return False;
+ status = unmarshall_sec_desc(ctx, regval_data_p(val),
+ regval_size(val), &ret_sd);
- pstr_sprintf(keystring,"SERVICE/%s/FILENAME", service_name);
- key_data = tdb_fetch_bystring(stdb,keystring);
- strncpy(si->filename,key_data.dptr,key_data.dsize);
- si->filename[key_data.dsize] = 0;
-
- pstr_sprintf(keystring,"SERVICE/%s/PROVIDES", service_name);
- key_data = tdb_fetch_bystring(stdb,keystring);
- strncpy(si->provides,key_data.dptr,key_data.dsize);
- si->provides[key_data.dsize] = 0;
- strncpy(si->servicename,key_data.dptr,key_data.dsize);
- si->servicename[key_data.dsize] = 0;
-
-
- pstr_sprintf(keystring,"SERVICE/%s/DEPENDENCIES", service_name);
- key_data = tdb_fetch_bystring(stdb,keystring);
- strncpy(si->dependencies,key_data.dptr,key_data.dsize);
- si->dependencies[key_data.dsize] = 0;
-
- pstr_sprintf(keystring,"SERVICE/%s/SHOULDSTART", service_name);
- key_data = tdb_fetch_bystring(stdb,keystring);
- strncpy(si->shouldstart,key_data.dptr,key_data.dsize);
- si->shouldstart[key_data.dsize] = 0;
-
- pstr_sprintf(keystring,"SERVICE/%s/SHOULD_STOP", service_name);
- key_data = tdb_fetch_bystring(stdb,keystring);
- strncpy(si->shouldstop,key_data.dptr,key_data.dsize);
- si->shouldstop[key_data.dsize] = 0;
-
- pstr_sprintf(keystring,"SERVICE/%s/REQUIREDSTART", service_name);
- key_data = tdb_fetch_bystring(stdb,keystring);
- strncpy(si->requiredstart,key_data.dptr,key_data.dsize);
- si->requiredstart[key_data.dsize] = 0;
-
- pstr_sprintf(keystring,"SERVICE/%s/REQUIREDSTOP", service_name);
- key_data = tdb_fetch_bystring(stdb,keystring);
- strncpy(si->requiredstop,key_data.dptr,key_data.dsize);
- si->requiredstop[key_data.dsize] = 0;
-
- pstr_sprintf(keystring,"SERVICE/%s/DESCRIPTION", service_name);
- key_data = tdb_fetch_bystring(stdb,keystring);
- strncpy(si->description,key_data.dptr,key_data.dsize);
- si->description[key_data.dsize] = 0;
-
- pstr_sprintf(keystring,"SERVICE/%s/SHORTDESC", service_name);
- key_data = tdb_fetch_bystring(stdb,keystring);
- strncpy(si->shortdescription,key_data.dptr,key_data.dsize);
- si->shortdescription[key_data.dsize] = 0;
+ if (NT_STATUS_IS_OK(status)) {
+ goto done;
+ }
- return True;
+fallback_to_default_sd:
+ DEBUG(6, ("svcctl_get_secdesc: constructing default secdesc for "
+ "service [%s]\n", name));
+ ret_sd = construct_service_sd(ctx);
+
+done:
+ SAFE_FREE(path);
+ TALLOC_FREE(key);
+ return ret_sd;
}
-/*********************************************************************
-*********************************************************************/
+/********************************************************************
+ Wrapper to make storing a Service sd easier
+********************************************************************/
-BOOL store_service_info(TDB_CONTEXT *stdb,char *service_name, Service_info *si)
+bool svcctl_set_secdesc( TALLOC_CTX *ctx, const char *name, struct security_descriptor *sec_desc, struct security_token *token )
{
- pstring keystring;
+ struct registry_key_handle *key = NULL;
+ WERROR wresult;
+ char *path = NULL;
+ struct regval_ctr *values = NULL;
+ DATA_BLOB blob;
+ NTSTATUS status;
+ bool ret = False;
+
+ /* now add the security descriptor */
+
+ if (asprintf(&path, "%s\\%s\\%s", KEY_SERVICES, name, "Security") < 0) {
+ return false;
+ }
+ wresult = regkey_open_internal( NULL, &key, path, token,
+ REG_KEY_ALL );
+ if ( !W_ERROR_IS_OK(wresult) ) {
+ DEBUG(0,("svcctl_get_secdesc: key lookup failed! [%s] (%s)\n",
+ path, win_errstr(wresult)));
+ SAFE_FREE(path);
+ return False;
+ }
+ SAFE_FREE(path);
- /* Note -- when we write to the tdb, we "index" on the filename
- field, not the nice name. when a service is "opened", it is
- opened by the nice (SERVICENAME) name, not the file name.
- So there needs to be a mapping from nice name back to the file name. */
+ wresult = regval_ctr_init(key, &values);
+ if (!W_ERROR_IS_OK(wresult)) {
+ DEBUG(0,("svcctl_set_secdesc: talloc() failed!\n"));
+ TALLOC_FREE( key );
+ return False;
+ }
+
+ /* stream the printer security descriptor */
- if ((stdb == NULL) || (si == NULL) || (service_name==NULL) || (*service_name == 0))
+ status = marshall_sec_desc(ctx, sec_desc, &blob.data, &blob.length);
+ if (!NT_STATUS_IS_OK(status)) {
+ DEBUG(0,("svcctl_set_secdesc: ndr_push_struct_blob() failed!\n"));
+ TALLOC_FREE( key );
return False;
+ }
+ regval_ctr_addvalue( values, "Security", REG_BINARY, blob.data, blob.length);
+ ret = store_reg_values( key, values );
- /* Store the nicename */
+ /* cleanup */
- pstr_sprintf(keystring,"SERVICE_NICENAME/%s", si->servicename);
- tdb_store_bystring(stdb,keystring,string_tdb_data(service_name),TDB_REPLACE);
+ TALLOC_FREE( key);
- pstr_sprintf(keystring,"SERVICE/%s/TYPE", service_name);
- tdb_store_bystring(stdb,keystring,string_tdb_data(si->servicetype),TDB_REPLACE);
+ return ret;
+}
- pstr_sprintf(keystring,"SERVICE/%s/FILENAME", service_name);
- tdb_store_bystring(stdb,keystring,string_tdb_data(si->filename),TDB_REPLACE);
+/********************************************************************
+********************************************************************/
- pstr_sprintf(keystring,"SERVICE/%s/PROVIDES", service_name);
- tdb_store_bystring(stdb,keystring,string_tdb_data(si->provides),TDB_REPLACE);
+const char *svcctl_lookup_dispname(TALLOC_CTX *ctx, const char *name, struct security_token *token )
+{
+ const char *display_name = NULL;
+ struct registry_key_handle *key = NULL;
+ struct regval_ctr *values = NULL;
+ struct regval_blob *val = NULL;
+ char *path = NULL;
+ WERROR wresult;
+ DATA_BLOB blob;
+
+ /* now add the security descriptor */
+
+ if (asprintf(&path, "%s\\%s", KEY_SERVICES, name) < 0) {
+ return NULL;
+ }
+ wresult = regkey_open_internal( NULL, &key, path, token,
+ REG_KEY_READ );
+ if ( !W_ERROR_IS_OK(wresult) ) {
+ DEBUG(0,("svcctl_lookup_dispname: key lookup failed! [%s] (%s)\n",
+ path, win_errstr(wresult)));
+ SAFE_FREE(path);
+ goto fail;
+ }
+ SAFE_FREE(path);
- pstr_sprintf(keystring,"SERVICE/%s/SERVICENAME", service_name);
- tdb_store_bystring(stdb,keystring,string_tdb_data(si->servicename),TDB_REPLACE);
+ wresult = regval_ctr_init(key, &values);
+ if (!W_ERROR_IS_OK(wresult)) {
+ DEBUG(0,("svcctl_lookup_dispname: talloc() failed!\n"));
+ TALLOC_FREE( key );
+ goto fail;
+ }
- pstr_sprintf(keystring,"SERVICE/%s/DEPENDENCIES", service_name);
- tdb_store_bystring(stdb,keystring,string_tdb_data(si->dependencies),TDB_REPLACE);
+ fetch_reg_values( key, values );
- pstr_sprintf(keystring,"SERVICE/%s/SHOULDSTART", service_name);
- tdb_store_bystring(stdb,keystring,string_tdb_data(si->shouldstart),TDB_REPLACE);
+ if ( !(val = regval_ctr_getvalue( values, "DisplayName" )) )
+ goto fail;
- pstr_sprintf(keystring,"SERVICE/%s/SHOULDSTOP", service_name);
- tdb_store_bystring(stdb,keystring,string_tdb_data(si->shouldstop),TDB_REPLACE);
+ blob = data_blob_const(regval_data_p(val), regval_size(val));
+ pull_reg_sz(ctx, &blob, &display_name);
- pstr_sprintf(keystring,"SERVICE/%s/REQUIREDSTART", service_name);
- tdb_store_bystring(stdb,keystring,string_tdb_data(si->requiredstart),TDB_REPLACE);
+ TALLOC_FREE( key );
- pstr_sprintf(keystring,"SERVICE/%s/REQUIREDSTOP", service_name);
- tdb_store_bystring(stdb,keystring,string_tdb_data(si->requiredstop),TDB_REPLACE);
+ return display_name;
- pstr_sprintf(keystring,"SERVICE/%s/DESCRIPTION", service_name);
- tdb_store_bystring(stdb,keystring,string_tdb_data(si->description),TDB_REPLACE);
+fail:
+ /* default to returning the service name */
+ TALLOC_FREE( key );
+ return talloc_strdup(ctx, name);
+}
- pstr_sprintf(keystring,"SERVICE/%s/SHORTDESC", service_name);
- if (si->shortdescription && *si->shortdescription)
- tdb_store_bystring(stdb,keystring,string_tdb_data(si->shortdescription),TDB_REPLACE);
- else
- tdb_store_bystring(stdb,keystring,string_tdb_data(si->description),TDB_REPLACE);
+/********************************************************************
+********************************************************************/
- return True;
+const char *svcctl_lookup_description(TALLOC_CTX *ctx, const char *name, struct security_token *token )
+{
+ const char *description = NULL;
+ struct registry_key_handle *key = NULL;
+ struct regval_ctr *values = NULL;
+ struct regval_blob *val = NULL;
+ char *path = NULL;
+ WERROR wresult;
+ DATA_BLOB blob;
+
+ /* now add the security descriptor */
+
+ if (asprintf(&path, "%s\\%s", KEY_SERVICES, name) < 0) {
+ return NULL;
+ }
+ wresult = regkey_open_internal( NULL, &key, path, token,
+ REG_KEY_READ );
+ if ( !W_ERROR_IS_OK(wresult) ) {
+ DEBUG(0,("svcctl_lookup_description: key lookup failed! [%s] (%s)\n",
+ path, win_errstr(wresult)));
+ SAFE_FREE(path);
+ return NULL;
+ }
+ SAFE_FREE(path);
+
+ wresult = regval_ctr_init(key, &values);
+ if (!W_ERROR_IS_OK(wresult)) {
+ DEBUG(0,("svcctl_lookup_description: talloc() failed!\n"));
+ TALLOC_FREE( key );
+ return NULL;
+ }
+
+ fetch_reg_values( key, values );
+
+ if ( !(val = regval_ctr_getvalue( values, "Description" )) ) {
+ TALLOC_FREE( key );
+ return "Unix Service";
+ }
+
+ blob = data_blob_const(regval_data_p(val), regval_size(val));
+ pull_reg_sz(ctx, &blob, &description);
+
+ TALLOC_FREE(key);
+
+ return description;
}
-/****************************************************************************
- Create/Open the service control manager tdb. This code a clone of init_group_mapping.
-****************************************************************************/
-BOOL init_svcctl_db(void)
+/********************************************************************
+********************************************************************/
+
+struct regval_ctr *svcctl_fetch_regvalues(const char *name, struct security_token *token)
{
- const char *vstring = "INFO/version";
- uint32 vers_id;
- char **svc_list;
- char **svcname;
- pstring keystring;
- pstring external_service_list;
- pstring internal_service_list;
- Service_info si;
- const Internal_service_description *isd_ptr;
- /* svc_list = str_list_make( "etc/init.d/skeleton etc/init.d/syslog", NULL ); */
- svc_list=(char **)lp_enable_svcctl();
-
- if (service_tdb)
- return True;
-
- pstrcpy(external_service_list,"");
-
- service_tdb = tdb_open_log(lock_path("services.tdb"), 0, TDB_DEFAULT, O_RDWR, 0600);
- if (!service_tdb) {
- DEBUG(0,("Failed to open service db\n"));
- service_tdb = tdb_open_log(lock_path("services.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
- if (!service_tdb) return False;
- DEBUG(0,("Created new services db\n"));
- }
-
- if ((-1 == tdb_fetch_uint32(service_tdb, vstring,&vers_id)) || (vers_id != SERVICEDB_VERSION_V1)) {
- /* wrong version of DB, or db was just created */
- tdb_traverse(service_tdb, tdb_traverse_delete_fn, NULL);
- tdb_store_uint32(service_tdb, vstring, SERVICEDB_VERSION_V1);
- }
- tdb_unlock_bystring(service_tdb, vstring);
-
- DEBUG(0,("Initializing services db\n"));
-
- svcname = svc_list;
-
- /* Get the EXTERNAL services as mentioned by line in smb.conf */
-
- while (*svcname) {
- DEBUG(10,("Reading information on service %s\n",*svcname));
- if (get_LSB_data(*svcname,&si));{
- /* write the information to the TDB */
- store_service_info(service_tdb,*svcname,&si);
- /* definitely not efficient to do it this way. */
- pstrcat(external_service_list,"\"");
- pstrcat(external_service_list,*svcname);
- pstrcat(external_service_list,"\" ");
- }
- svcname++;
- }
- pstrcpy(keystring,"EXTERNAL_SERVICES");
- DEBUG(8,("Storing external service list [%s]\n",external_service_list));
- tdb_store_bystring(service_tdb,keystring,string_tdb_data(external_service_list),TDB_REPLACE);
-
- /* Get the INTERNAL services */
-
- pstrcpy(internal_service_list,"");
- isd_ptr = ISD;
-
- while (isd_ptr && (isd_ptr->filename)) {
- DEBUG(10,("Reading information on service %s\n",isd_ptr->filename));
- if (get_internal_service_data(isd_ptr,&si)){
- /* write the information to the TDB */
- store_service_info(service_tdb,(char *)isd_ptr->filename,&si);
- /* definitely not efficient to do it this way. */
- pstrcat(internal_service_list,"\"");
- pstrcat(internal_service_list,isd_ptr->filename);
- pstrcat(internal_service_list,"\" ");
+ struct registry_key_handle *key = NULL;
+ struct regval_ctr *values = NULL;
+ char *path = NULL;
+ WERROR wresult;
- }
- isd_ptr++;
+ /* now add the security descriptor */
+
+ if (asprintf(&path, "%s\\%s", KEY_SERVICES, name) < 0) {
+ return NULL;
+ }
+ wresult = regkey_open_internal( NULL, &key, path, token,
+ REG_KEY_READ );
+ if ( !W_ERROR_IS_OK(wresult) ) {
+ DEBUG(0,("svcctl_fetch_regvalues: key lookup failed! [%s] (%s)\n",
+ path, win_errstr(wresult)));
+ SAFE_FREE(path);
+ return NULL;
}
- pstrcpy(keystring,"INTERNAL_SERVICES");
- DEBUG(8,("Storing internal service list [%s]\n",internal_service_list));
- tdb_store_bystring(service_tdb,keystring,string_tdb_data(internal_service_list),TDB_REPLACE);
+ SAFE_FREE(path);
- return True;
+ wresult = regval_ctr_init(NULL, &values);
+ if (!W_ERROR_IS_OK(wresult)) {
+ DEBUG(0,("svcctl_fetch_regvalues: talloc() failed!\n"));
+ TALLOC_FREE( key );
+ return NULL;
+ }
+ fetch_reg_values( key, values );
+
+ TALLOC_FREE( key );
+ return values;
}
-#endif