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