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