s3:services_db: change svcctl_get_secdesc() to use reg_api functions
[amitay/samba.git] / source3 / services / services_db.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Service Control API Implementation
4  * 
5  *  Copyright (C) Marcin Krzysztof Porwit         2005.
6  *  Largely Rewritten by:
7  *  Copyright (C) Gerald (Jerry) Carter           2005.
8  *  
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 3 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, see <http://www.gnu.org/licenses/>.
21  */
22
23 #include "includes.h"
24 #include "services/services.h"
25 #include "registry.h"
26 #include "registry/reg_util_legacy.h"
27 #include "registry/reg_dispatcher.h"
28 #include "registry/reg_objects.h"
29 #include "registry/reg_api_util.h"
30
31 struct rcinit_file_information {
32         char *description;
33 };
34
35 struct service_display_info {
36         const char *servicename;
37         const char *daemon;
38         const char *dispname;
39         const char *description;
40 };
41
42 struct service_display_info builtin_svcs[] = {
43   { "Spooler",        "smbd",   "Print Spooler", "Internal service for spooling files to print devices" },
44   { "NETLOGON",       "smbd",   "Net Logon", "File service providing access to policy and profile data (not remotely manageable)" },
45   { "RemoteRegistry", "smbd",   "Remote Registry Service", "Internal service providing remote access to "
46                                 "the Samba registry" },
47   { "WINS",           "nmbd",   "Windows Internet Name Service (WINS)", "Internal service providing a "
48                                 "NetBIOS point-to-point name server (not remotely manageable)" },
49   { NULL, NULL, NULL, NULL }
50 };
51
52 struct service_display_info common_unix_svcs[] = {
53   { "cups",          NULL, "Common Unix Printing System","Provides unified printing support for all operating systems" },
54   { "postfix",       NULL, "Internet Mail Service",     "Provides support for sending and receiving electonic mail" },
55   { "sendmail",      NULL, "Internet Mail Service",     "Provides support for sending and receiving electonic mail" },
56   { "portmap",       NULL, "TCP Port to RPC PortMapper",NULL },
57   { "xinetd",        NULL, "Internet Meta-Daemon",      NULL },
58   { "inet",          NULL, "Internet Meta-Daemon",      NULL },
59   { "xntpd",         NULL, "Network Time Service",      NULL },
60   { "ntpd",          NULL, "Network Time Service",      NULL },
61   { "lpd",           NULL, "BSD Print Spooler",         NULL },
62   { "nfsserver",     NULL, "Network File Service",      NULL },
63   { "cron",          NULL, "Scheduling Service",        NULL },
64   { "at",            NULL, "Scheduling Service",        NULL },
65   { "nscd",          NULL, "Name Service Cache Daemon", NULL },
66   { "slapd",         NULL, "LDAP Directory Service",    NULL },
67   { "ldap",          NULL, "LDAP DIrectory Service",    NULL },
68   { "ypbind",        NULL, "NIS Directory Service",     NULL },
69   { "courier-imap",  NULL, "IMAP4 Mail Service",        NULL },
70   { "courier-pop3",  NULL, "POP3 Mail Service",         NULL },
71   { "named",         NULL, "Domain Name Service",       NULL },
72   { "bind",          NULL, "Domain Name Service",       NULL },
73   { "httpd",         NULL, "HTTP Server",               NULL },
74   { "apache",        NULL, "HTTP Server",               "Provides s highly scalable and flexible web server "
75                                                         "capable of implementing various protocols incluing "
76                                                         "but not limited to HTTP" },
77   { "autofs",        NULL, "Automounter",               NULL },
78   { "squid",         NULL, "Web Cache Proxy ",          NULL },
79   { "perfcountd",    NULL, "Performance Monitoring Daemon", NULL },
80   { "pgsql",         NULL, "PgSQL Database Server",     "Provides service for SQL database from Postgresql.org" },
81   { "arpwatch",      NULL, "ARP Tables watcher",        "Provides service for monitoring ARP tables for changes" },
82   { "dhcpd",         NULL, "DHCP Server",               "Provides service for dynamic host configuration and IP assignment" },
83   { "nwserv",        NULL, "NetWare Server Emulator",   "Provides service for emulating Novell NetWare 3.12 server" },
84   { "proftpd",       NULL, "Professional FTP Server",   "Provides high configurable service for FTP connection and "
85                                                         "file transferring" },
86   { "ssh2",          NULL, "SSH Secure Shell",          "Provides service for secure connection for remote administration" },
87   { "sshd",          NULL, "SSH Secure Shell",          "Provides service for secure connection for remote administration" },
88   { NULL, NULL, NULL, NULL }
89 };
90
91
92 /********************************************************************
93 ********************************************************************/
94
95 static struct security_descriptor* construct_service_sd( TALLOC_CTX *ctx )
96 {
97         struct security_ace ace[4];
98         size_t i = 0;
99         struct security_descriptor *sd = NULL;
100         struct security_acl *theacl = NULL;
101         size_t sd_size;
102
103         /* basic access for Everyone */
104
105         init_sec_ace(&ace[i++], &global_sid_World,
106                 SEC_ACE_TYPE_ACCESS_ALLOWED, SERVICE_READ_ACCESS, 0);
107
108         init_sec_ace(&ace[i++], &global_sid_Builtin_Power_Users,
109                         SEC_ACE_TYPE_ACCESS_ALLOWED, SERVICE_EXECUTE_ACCESS, 0);
110
111         init_sec_ace(&ace[i++], &global_sid_Builtin_Server_Operators,
112                 SEC_ACE_TYPE_ACCESS_ALLOWED, SERVICE_ALL_ACCESS, 0);
113         init_sec_ace(&ace[i++], &global_sid_Builtin_Administrators,
114                 SEC_ACE_TYPE_ACCESS_ALLOWED, SERVICE_ALL_ACCESS, 0);
115
116         /* create the security descriptor */
117
118         theacl = make_sec_acl(ctx, NT4_ACL_REVISION, i, ace);
119         if (theacl == NULL) {
120                 return NULL;
121         }
122
123         sd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
124                            SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
125                            theacl, &sd_size);
126         if (sd == NULL) {
127                 return NULL;
128         }
129
130         return sd;
131 }
132
133 /********************************************************************
134  This is where we do the dirty work of filling in things like the
135  Display name, Description, etc...
136 ********************************************************************/
137
138 static char *get_common_service_dispname( const char *servicename )
139 {
140         int i;
141
142         for ( i=0; common_unix_svcs[i].servicename; i++ ) {
143                 if (strequal(servicename, common_unix_svcs[i].servicename)) {
144                         char *dispname;
145                         if (asprintf(&dispname,
146                                 "%s (%s)",
147                                 common_unix_svcs[i].dispname,
148                                 common_unix_svcs[i].servicename) < 0) {
149                                 return NULL;
150                         }
151                         return dispname;
152                 }
153         }
154
155         return SMB_STRDUP(servicename );
156 }
157
158 /********************************************************************
159 ********************************************************************/
160
161 static char *cleanup_string( const char *string )
162 {
163         char *clean = NULL;
164         char *begin, *end;
165         TALLOC_CTX *ctx = talloc_tos();
166
167         clean = talloc_strdup(ctx, string);
168         if (!clean) {
169                 return NULL;
170         }
171         begin = clean;
172
173         /* trim any beginning whilespace */
174
175         while (isspace(*begin)) {
176                 begin++;
177         }
178
179         if (*begin == '\0') {
180                 return NULL;
181         }
182
183         /* trim any trailing whitespace or carriage returns.
184            Start at the end and move backwards */
185
186         end = begin + strlen(begin) - 1;
187
188         while ( isspace(*end) || *end=='\n' || *end=='\r' ) {
189                 *end = '\0';
190                 end--;
191         }
192
193         return begin;
194 }
195
196 /********************************************************************
197 ********************************************************************/
198
199 static bool read_init_file( const char *servicename, struct rcinit_file_information **service_info )
200 {
201         struct rcinit_file_information *info = NULL;
202         char *filepath = NULL;
203         char str[1024];
204         XFILE *f = NULL;
205         char *p = NULL;
206
207         info = TALLOC_ZERO_P( NULL, struct rcinit_file_information );
208         if (info == NULL) {
209                 return False;
210         }
211
212         /* attempt the file open */
213
214         filepath = talloc_asprintf(info, "%s/%s/%s", get_dyn_MODULESDIR(),
215                                 SVCCTL_SCRIPT_DIR, servicename);
216         if (!filepath) {
217                 TALLOC_FREE(info);
218                 return false;
219         }
220         f = x_fopen( filepath, O_RDONLY, 0 );
221         if (f == NULL) {
222                 DEBUG(0,("read_init_file: failed to open [%s]\n", filepath));
223                 TALLOC_FREE(info);
224                 return false;
225         }
226
227         while ( (x_fgets( str, sizeof(str)-1, f )) != NULL ) {
228                 /* ignore everything that is not a full line
229                    comment starting with a '#' */
230
231                 if ( str[0] != '#' )
232                         continue;
233
234                 /* Look for a line like '^#.*Description:' */
235
236                 p = strstr( str, "Description:" );
237                 if (p != NULL) {
238                         char *desc;
239
240                         p += strlen( "Description:" ) + 1;
241                         if ( !p )
242                                 break;
243
244                         desc = cleanup_string(p);
245                         if (desc != NULL)
246                                 info->description = talloc_strdup( info, desc );
247                 }
248         }
249
250         x_fclose( f );
251
252         if ( !info->description )
253                 info->description = talloc_strdup( info, "External Unix Service" );
254
255         *service_info = info;
256         TALLOC_FREE(filepath);
257
258         return True;
259 }
260
261 /********************************************************************
262  This is where we do the dirty work of filling in things like the
263  Display name, Description, etc...
264 ********************************************************************/
265
266 static void fill_service_values(const char *name, struct regval_ctr *values)
267 {
268         char *dname, *ipath, *description;
269         uint32 dword;
270         int i;
271
272         /* These values are hardcoded in all QueryServiceConfig() replies.
273            I'm just storing them here for cosmetic purposes */
274
275         dword = SVCCTL_AUTO_START;
276         regval_ctr_addvalue( values, "Start", REG_DWORD, (uint8 *)&dword, sizeof(uint32));
277
278         dword = SERVICE_TYPE_WIN32_OWN_PROCESS;
279         regval_ctr_addvalue( values, "Type", REG_DWORD, (uint8 *)&dword, sizeof(uint32));
280
281         dword = SVCCTL_SVC_ERROR_NORMAL;
282         regval_ctr_addvalue( values, "ErrorControl", REG_DWORD, (uint8 *)&dword, sizeof(uint32));
283
284         /* everything runs as LocalSystem */
285
286         regval_ctr_addvalue_sz(values, "ObjectName", "LocalSystem");
287
288         /* special considerations for internal services and the DisplayName value */
289
290         for ( i=0; builtin_svcs[i].servicename; i++ ) {
291                 if ( strequal( name, builtin_svcs[i].servicename ) ) {
292                         ipath = talloc_asprintf(talloc_tos(), "%s/%s/%s",
293                                         get_dyn_MODULESDIR(), SVCCTL_SCRIPT_DIR,
294                                         builtin_svcs[i].daemon);
295                         description = talloc_strdup(talloc_tos(), builtin_svcs[i].description);
296                         dname = talloc_strdup(talloc_tos(), builtin_svcs[i].dispname);
297                         break;
298                 }
299         }
300
301         /* default to an external service if we haven't found a match */
302
303         if ( builtin_svcs[i].servicename == NULL ) {
304                 char *dispname = NULL;
305                 struct rcinit_file_information *init_info = NULL;
306
307                 ipath = talloc_asprintf(talloc_tos(), "%s/%s/%s",
308                                         get_dyn_MODULESDIR(), SVCCTL_SCRIPT_DIR,
309                                         name);
310
311                 /* lookup common unix display names */
312                 dispname = get_common_service_dispname(name);
313                 dname = talloc_strdup(talloc_tos(), dispname ? dispname : "");
314                 SAFE_FREE(dispname);
315
316                 /* get info from init file itself */
317                 if ( read_init_file( name, &init_info ) ) {
318                         description = talloc_strdup(talloc_tos(), init_info->description);
319                         TALLOC_FREE( init_info );
320                 }
321                 else {
322                         description = talloc_strdup(talloc_tos(), "External Unix Service");
323                 }
324         }
325
326         /* add the new values */
327
328         regval_ctr_addvalue_sz(values, "DisplayName", dname);
329         regval_ctr_addvalue_sz(values, "ImagePath", ipath);
330         regval_ctr_addvalue_sz(values, "Description", description);
331
332         TALLOC_FREE(dname);
333         TALLOC_FREE(ipath);
334         TALLOC_FREE(description);
335
336         return;
337 }
338
339 /********************************************************************
340 ********************************************************************/
341
342 static void add_new_svc_name(struct registry_key_handle *key_parent,
343                              struct regsubkey_ctr *subkeys,
344                              const char *name )
345 {
346         struct registry_key_handle *key_service = NULL, *key_secdesc = NULL;
347         WERROR wresult;
348         char *path = NULL;
349         struct regval_ctr *values = NULL;
350         struct regsubkey_ctr *svc_subkeys = NULL;
351         struct security_descriptor *sd = NULL;
352         DATA_BLOB sd_blob;
353         NTSTATUS status;
354
355         /* add to the list and create the subkey path */
356
357         regsubkey_ctr_addkey( subkeys, name );
358         store_reg_keys( key_parent, subkeys );
359
360         /* open the new service key */
361
362         if (asprintf(&path, "%s\\%s", KEY_SERVICES, name) < 0) {
363                 return;
364         }
365         wresult = regkey_open_internal( NULL, &key_service, path,
366                                         get_root_nt_token(), REG_KEY_ALL );
367         if ( !W_ERROR_IS_OK(wresult) ) {
368                 DEBUG(0,("add_new_svc_name: key lookup failed! [%s] (%s)\n",
369                         path, win_errstr(wresult)));
370                 SAFE_FREE(path);
371                 return;
372         }
373         SAFE_FREE(path);
374
375         /* add the 'Security' key */
376
377         wresult = regsubkey_ctr_init(key_service, &svc_subkeys);
378         if (!W_ERROR_IS_OK(wresult)) {
379                 DEBUG(0,("add_new_svc_name: talloc() failed!\n"));
380                 TALLOC_FREE( key_service );
381                 return;
382         }
383
384         fetch_reg_keys( key_service, svc_subkeys );
385         regsubkey_ctr_addkey( svc_subkeys, "Security" );
386         store_reg_keys( key_service, svc_subkeys );
387
388         /* now for the service values */
389
390         wresult = regval_ctr_init(key_service, &values);
391         if (!W_ERROR_IS_OK(wresult)) {
392                 DEBUG(0,("add_new_svc_name: talloc() failed!\n"));
393                 TALLOC_FREE( key_service );
394                 return;
395         }
396
397         fill_service_values( name, values );
398         store_reg_values( key_service, values );
399
400         /* cleanup the service key*/
401
402         TALLOC_FREE( key_service );
403
404         /* now add the security descriptor */
405
406         if (asprintf(&path, "%s\\%s\\%s", KEY_SERVICES, name, "Security") < 0) {
407                 return;
408         }
409         wresult = regkey_open_internal( NULL, &key_secdesc, path,
410                                         get_root_nt_token(), REG_KEY_ALL );
411         if ( !W_ERROR_IS_OK(wresult) ) {
412                 DEBUG(0,("add_new_svc_name: key lookup failed! [%s] (%s)\n",
413                         path, win_errstr(wresult)));
414                 TALLOC_FREE( key_secdesc );
415                 SAFE_FREE(path);
416                 return;
417         }
418         SAFE_FREE(path);
419
420         wresult = regval_ctr_init(key_secdesc, &values);
421         if (!W_ERROR_IS_OK(wresult)) {
422                 DEBUG(0,("add_new_svc_name: talloc() failed!\n"));
423                 TALLOC_FREE( key_secdesc );
424                 return;
425         }
426
427         if ( !(sd = construct_service_sd(key_secdesc)) ) {
428                 DEBUG(0,("add_new_svc_name: Failed to create default sec_desc!\n"));
429                 TALLOC_FREE( key_secdesc );
430                 return;
431         }
432
433         status = marshall_sec_desc(key_secdesc, sd, &sd_blob.data,
434                                    &sd_blob.length);
435         if (!NT_STATUS_IS_OK(status)) {
436                 DEBUG(0, ("marshall_sec_desc failed: %s\n",
437                           nt_errstr(status)));
438                 TALLOC_FREE(key_secdesc);
439                 return;
440         }
441
442         regval_ctr_addvalue(values, "Security", REG_BINARY,
443                             sd_blob.data, sd_blob.length);
444         store_reg_values( key_secdesc, values );
445
446         TALLOC_FREE( key_secdesc );
447
448         return;
449 }
450
451 /********************************************************************
452 ********************************************************************/
453
454 void svcctl_init_keys( void )
455 {
456         const char **service_list = lp_svcctl_list();
457         int i;
458         struct regsubkey_ctr *subkeys = NULL;
459         struct registry_key_handle *key = NULL;
460         WERROR wresult;
461
462         /* bad mojo here if the lookup failed.  Should not happen */
463
464         wresult = regkey_open_internal( NULL, &key, KEY_SERVICES,
465                                         get_root_nt_token(), REG_KEY_ALL );
466
467         if ( !W_ERROR_IS_OK(wresult) ) {
468                 DEBUG(0,("svcctl_init_keys: key lookup failed! (%s)\n",
469                         win_errstr(wresult)));
470                 return;
471         }
472
473         /* lookup the available subkeys */
474
475         wresult = regsubkey_ctr_init(key, &subkeys);
476         if (!W_ERROR_IS_OK(wresult)) {
477                 DEBUG(0,("svcctl_init_keys: talloc() failed!\n"));
478                 TALLOC_FREE( key );
479                 return;
480         }
481
482         fetch_reg_keys( key, subkeys );
483
484         /* the builtin services exist */
485
486         for ( i=0; builtin_svcs[i].servicename; i++ )
487                 add_new_svc_name( key, subkeys, builtin_svcs[i].servicename );
488
489         for ( i=0; service_list && service_list[i]; i++ ) {
490
491                 /* only add new services */
492                 if ( regsubkey_ctr_key_exists( subkeys, service_list[i] ) )
493                         continue;
494
495                 /* Add the new service key and initialize the appropriate values */
496
497                 add_new_svc_name( key, subkeys, service_list[i] );
498         }
499
500         TALLOC_FREE( key );
501
502         /* initialize the control hooks */
503
504         init_service_op_table();
505
506         return;
507 }
508
509 /********************************************************************
510  This is where we do the dirty work of filling in things like the
511  Display name, Description, etc...Always return a default secdesc
512  in case of any failure.
513 ********************************************************************/
514
515 struct security_descriptor *svcctl_get_secdesc( TALLOC_CTX *ctx, const char *name, struct security_token *token )
516 {
517         struct registry_key *key = NULL;
518         struct registry_value *value;
519         struct security_descriptor *ret_sd = NULL;
520         char *path= NULL;
521         WERROR wresult;
522         NTSTATUS status;
523         TALLOC_CTX *mem_ctx = talloc_stackframe();
524
525         path = talloc_asprintf(mem_ctx, "%s\\%s\\%s", KEY_SERVICES, name,
526                                "Security");
527         if (path == NULL) {
528                 goto done;
529         }
530
531         wresult = reg_open_path(mem_ctx, path, REG_KEY_ALL, token, &key);
532         if ( !W_ERROR_IS_OK(wresult) ) {
533                 DEBUG(0,("svcctl_get_secdesc: key lookup failed! [%s] (%s)\n",
534                         path, win_errstr(wresult)));
535                 goto done;
536         }
537
538         wresult = reg_queryvalue(mem_ctx, key, "Security", &value);
539         if (W_ERROR_EQUAL(wresult, WERR_BADFILE)) {
540                 goto fallback_to_default_sd;
541         } else if (!W_ERROR_IS_OK(wresult)) {
542                 DEBUG(0, ("svcctl_get_secdesc: error getting value 'Security': "
543                           "%s\n", win_errstr(wresult)));
544                 goto done;
545         }
546
547         status = unmarshall_sec_desc(ctx, value->data.data,
548                                      value->data.length, &ret_sd);
549
550         if (NT_STATUS_IS_OK(status)) {
551                 goto done;
552         }
553
554 fallback_to_default_sd:
555         DEBUG(6, ("svcctl_get_secdesc: constructing default secdesc for "
556                   "service [%s]\n", name));
557         ret_sd = construct_service_sd(ctx);
558
559 done:
560         talloc_free(mem_ctx);
561         return ret_sd;
562 }
563
564 /********************************************************************
565  Wrapper to make storing a Service sd easier
566 ********************************************************************/
567
568 bool svcctl_set_secdesc(const char *name, struct security_descriptor *sec_desc,
569                         struct security_token *token)
570 {
571         struct registry_key *key = NULL;
572         WERROR wresult;
573         char *path = NULL;
574         struct registry_value value;
575         NTSTATUS status;
576         bool ret = false;
577         TALLOC_CTX *mem_ctx = talloc_stackframe();
578
579         /* now add the security descriptor */
580
581         path = talloc_asprintf(mem_ctx, "%s\\%s\\%s", KEY_SERVICES, name,
582                                "Security");
583         if (path == NULL) {
584                 goto done;
585         }
586
587         wresult = reg_open_path(mem_ctx, path, REG_KEY_ALL, token, &key);
588
589         if ( !W_ERROR_IS_OK(wresult) ) {
590                 DEBUG(0, ("svcctl_set_secdesc: key lookup failed! [%s] (%s)\n",
591                           path, win_errstr(wresult)));
592                 goto done;
593         }
594
595         /* stream the printer security descriptor */
596
597         status = marshall_sec_desc(mem_ctx, sec_desc, &value.data.data,
598                                    &value.data.length);
599         if (!NT_STATUS_IS_OK(status)) {
600                 DEBUG(0,("svcctl_set_secdesc: ndr_push_struct_blob() failed!\n"));
601                 goto done;
602         }
603
604         value.type = REG_BINARY;
605
606         wresult = reg_setvalue(key, "Security", &value);
607         if (!W_ERROR_IS_OK(wresult)) {
608                 DEBUG(0, ("svcctl_set_secdesc: reg_setvalue failed: %s\n",
609                           win_errstr(wresult)));
610                 goto done;
611         }
612
613         ret = true;
614
615 done:
616         talloc_free(mem_ctx);
617         return ret;
618 }
619
620 /********************************************************************
621 ********************************************************************/
622
623 const char *svcctl_lookup_dispname(TALLOC_CTX *ctx, const char *name, struct security_token *token )
624 {
625         const char *display_name = NULL;
626         struct registry_key_handle *key = NULL;
627         struct regval_ctr *values = NULL;
628         struct regval_blob *val = NULL;
629         char *path = NULL;
630         WERROR wresult;
631         DATA_BLOB blob;
632
633         /* now add the security descriptor */
634
635         if (asprintf(&path, "%s\\%s", KEY_SERVICES, name) < 0) {
636                 return NULL;
637         }
638         wresult = regkey_open_internal( NULL, &key, path, token,
639                                         REG_KEY_READ );
640         if ( !W_ERROR_IS_OK(wresult) ) {
641                 DEBUG(0,("svcctl_lookup_dispname: key lookup failed! [%s] (%s)\n", 
642                         path, win_errstr(wresult)));
643                 SAFE_FREE(path);
644                 goto fail;
645         }
646         SAFE_FREE(path);
647
648         wresult = regval_ctr_init(key, &values);
649         if (!W_ERROR_IS_OK(wresult)) {
650                 DEBUG(0,("svcctl_lookup_dispname: talloc() failed!\n"));
651                 TALLOC_FREE( key );
652                 goto fail;
653         }
654
655         fetch_reg_values( key, values );
656
657         if ( !(val = regval_ctr_getvalue( values, "DisplayName" )) )
658                 goto fail;
659
660         blob = data_blob_const(regval_data_p(val), regval_size(val));
661         pull_reg_sz(ctx, &blob, &display_name);
662
663         TALLOC_FREE( key );
664
665         return display_name;
666
667 fail:
668         /* default to returning the service name */
669         TALLOC_FREE( key );
670         return talloc_strdup(ctx, name);
671 }
672
673 /********************************************************************
674 ********************************************************************/
675
676 const char *svcctl_lookup_description(TALLOC_CTX *ctx, const char *name, struct security_token *token )
677 {
678         const char *description = NULL;
679         struct registry_key_handle *key = NULL;
680         struct regval_ctr *values = NULL;
681         struct regval_blob *val = NULL;
682         char *path = NULL;
683         WERROR wresult;
684         DATA_BLOB blob;
685
686         /* now add the security descriptor */
687
688         if (asprintf(&path, "%s\\%s", KEY_SERVICES, name) < 0) {
689                 return NULL;
690         }
691         wresult = regkey_open_internal( NULL, &key, path, token,
692                                         REG_KEY_READ );
693         if ( !W_ERROR_IS_OK(wresult) ) {
694                 DEBUG(0,("svcctl_lookup_description: key lookup failed! [%s] (%s)\n", 
695                         path, win_errstr(wresult)));
696                 SAFE_FREE(path);
697                 return NULL;
698         }
699         SAFE_FREE(path);
700
701         wresult = regval_ctr_init(key, &values);
702         if (!W_ERROR_IS_OK(wresult)) {
703                 DEBUG(0,("svcctl_lookup_description: talloc() failed!\n"));
704                 TALLOC_FREE( key );
705                 return NULL;
706         }
707
708         fetch_reg_values( key, values );
709
710         if ( !(val = regval_ctr_getvalue( values, "Description" )) ) {
711                 TALLOC_FREE( key );
712                 return "Unix Service";
713         }
714
715         blob = data_blob_const(regval_data_p(val), regval_size(val));
716         pull_reg_sz(ctx, &blob, &description);
717
718         TALLOC_FREE(key);
719
720         return description;
721 }
722
723
724 /********************************************************************
725 ********************************************************************/
726
727 struct regval_ctr *svcctl_fetch_regvalues(const char *name, struct security_token *token)
728 {
729         struct registry_key_handle *key = NULL;
730         struct regval_ctr *values = NULL;
731         char *path = NULL;
732         WERROR wresult;
733
734         /* now add the security descriptor */
735
736         if (asprintf(&path, "%s\\%s", KEY_SERVICES, name) < 0) {
737                 return NULL;
738         }
739         wresult = regkey_open_internal( NULL, &key, path, token,
740                                         REG_KEY_READ );
741         if ( !W_ERROR_IS_OK(wresult) ) {
742                 DEBUG(0,("svcctl_fetch_regvalues: key lookup failed! [%s] (%s)\n",
743                         path, win_errstr(wresult)));
744                 SAFE_FREE(path);
745                 return NULL;
746         }
747         SAFE_FREE(path);
748
749         wresult = regval_ctr_init(NULL, &values);
750         if (!W_ERROR_IS_OK(wresult)) {
751                 DEBUG(0,("svcctl_fetch_regvalues: talloc() failed!\n"));
752                 TALLOC_FREE( key );
753                 return NULL;
754         }
755         fetch_reg_values( key, values );
756
757         TALLOC_FREE( key );
758         return values;
759 }