s3:services_db: use regval_ctr_init() instead of direct allocation
[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
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         wresult = regval_ctr_init(key_service, &values);
380         if (!W_ERROR_IS_OK(wresult)) {
381                 DEBUG(0,("add_new_svc_name: talloc() failed!\n"));
382                 TALLOC_FREE( key_service );
383                 return;
384         }
385
386         fill_service_values( name, values );
387         store_reg_values( key_service, values );
388
389         /* cleanup the service key*/
390
391         TALLOC_FREE( key_service );
392
393         /* now add the security descriptor */
394
395         if (asprintf(&path, "%s\\%s\\%s", KEY_SERVICES, name, "Security") < 0) {
396                 return;
397         }
398         wresult = regkey_open_internal( NULL, &key_secdesc, path,
399                                         get_root_nt_token(), REG_KEY_ALL );
400         if ( !W_ERROR_IS_OK(wresult) ) {
401                 DEBUG(0,("add_new_svc_name: key lookup failed! [%s] (%s)\n",
402                         path, win_errstr(wresult)));
403                 TALLOC_FREE( key_secdesc );
404                 SAFE_FREE(path);
405                 return;
406         }
407         SAFE_FREE(path);
408
409         wresult = regval_ctr_init(key_secdesc, &values);
410         if (!W_ERROR_IS_OK(wresult)) {
411                 DEBUG(0,("add_new_svc_name: talloc() failed!\n"));
412                 TALLOC_FREE( key_secdesc );
413                 return;
414         }
415
416         if ( !(sd = construct_service_sd(key_secdesc)) ) {
417                 DEBUG(0,("add_new_svc_name: Failed to create default sec_desc!\n"));
418                 TALLOC_FREE( key_secdesc );
419                 return;
420         }
421
422         status = marshall_sec_desc(key_secdesc, sd, &sd_blob.data,
423                                    &sd_blob.length);
424         if (!NT_STATUS_IS_OK(status)) {
425                 DEBUG(0, ("marshall_sec_desc failed: %s\n",
426                           nt_errstr(status)));
427                 TALLOC_FREE(key_secdesc);
428                 return;
429         }
430
431         regval_ctr_addvalue(values, "Security", REG_BINARY,
432                             sd_blob.data, sd_blob.length);
433         store_reg_values( key_secdesc, values );
434
435         TALLOC_FREE( key_secdesc );
436
437         return;
438 }
439
440 /********************************************************************
441 ********************************************************************/
442
443 void svcctl_init_keys( void )
444 {
445         const char **service_list = lp_svcctl_list();
446         int i;
447         struct regsubkey_ctr *subkeys = NULL;
448         struct registry_key_handle *key = NULL;
449         WERROR wresult;
450
451         /* bad mojo here if the lookup failed.  Should not happen */
452
453         wresult = regkey_open_internal( NULL, &key, KEY_SERVICES,
454                                         get_root_nt_token(), REG_KEY_ALL );
455
456         if ( !W_ERROR_IS_OK(wresult) ) {
457                 DEBUG(0,("svcctl_init_keys: key lookup failed! (%s)\n",
458                         win_errstr(wresult)));
459                 return;
460         }
461
462         /* lookup the available subkeys */
463
464         wresult = regsubkey_ctr_init(key, &subkeys);
465         if (!W_ERROR_IS_OK(wresult)) {
466                 DEBUG(0,("svcctl_init_keys: talloc() failed!\n"));
467                 TALLOC_FREE( key );
468                 return;
469         }
470
471         fetch_reg_keys( key, subkeys );
472
473         /* the builtin services exist */
474
475         for ( i=0; builtin_svcs[i].servicename; i++ )
476                 add_new_svc_name( key, subkeys, builtin_svcs[i].servicename );
477
478         for ( i=0; service_list && service_list[i]; i++ ) {
479
480                 /* only add new services */
481                 if ( regsubkey_ctr_key_exists( subkeys, service_list[i] ) )
482                         continue;
483
484                 /* Add the new service key and initialize the appropriate values */
485
486                 add_new_svc_name( key, subkeys, service_list[i] );
487         }
488
489         TALLOC_FREE( key );
490
491         /* initialize the control hooks */
492
493         init_service_op_table();
494
495         return;
496 }
497
498 /********************************************************************
499  This is where we do the dirty work of filling in things like the
500  Display name, Description, etc...Always return a default secdesc
501  in case of any failure.
502 ********************************************************************/
503
504 struct security_descriptor *svcctl_get_secdesc( TALLOC_CTX *ctx, const char *name, NT_USER_TOKEN *token )
505 {
506         struct registry_key_handle *key = NULL;
507         struct regval_ctr *values = NULL;
508         struct regval_blob *val = NULL;
509         struct security_descriptor *ret_sd = NULL;
510         char *path= NULL;
511         WERROR wresult;
512         NTSTATUS status;
513
514         /* now add the security descriptor */
515
516         if (asprintf(&path, "%s\\%s\\%s", KEY_SERVICES, name, "Security") < 0) {
517                 return NULL;
518         }
519         wresult = regkey_open_internal( NULL, &key, path, token,
520                                         REG_KEY_ALL );
521         if ( !W_ERROR_IS_OK(wresult) ) {
522                 DEBUG(0,("svcctl_get_secdesc: key lookup failed! [%s] (%s)\n",
523                         path, win_errstr(wresult)));
524                 goto done;
525         }
526
527         wresult = regval_ctr_init(key, &values);
528         if (!W_ERROR_IS_OK(wresult)) {
529                 DEBUG(0,("svcctl_get_secdesc: talloc() failed!\n"));
530                 goto done;
531         }
532
533         if (fetch_reg_values( key, values ) == -1) {
534                 DEBUG(0, ("Error getting registry values\n"));
535                 goto done;
536         }
537
538         if ( !(val = regval_ctr_getvalue( values, "Security" )) ) {
539                 goto fallback_to_default_sd;
540         }
541
542         /* stream the service security descriptor */
543
544         status = unmarshall_sec_desc(ctx, regval_data_p(val),
545                                      regval_size(val), &ret_sd);
546
547         if (NT_STATUS_IS_OK(status)) {
548                 goto done;
549         }
550
551 fallback_to_default_sd:
552         DEBUG(6, ("svcctl_get_secdesc: constructing default secdesc for "
553                   "service [%s]\n", name));
554         ret_sd = construct_service_sd(ctx);
555
556 done:
557         SAFE_FREE(path);
558         TALLOC_FREE(key);
559         return ret_sd;
560 }
561
562 /********************************************************************
563  Wrapper to make storing a Service sd easier
564 ********************************************************************/
565
566 bool svcctl_set_secdesc( TALLOC_CTX *ctx, const char *name, struct security_descriptor *sec_desc, NT_USER_TOKEN *token )
567 {
568         struct registry_key_handle *key = NULL;
569         WERROR wresult;
570         char *path = NULL;
571         struct regval_ctr *values = NULL;
572         DATA_BLOB blob;
573         NTSTATUS status;
574         bool ret = False;
575
576         /* now add the security descriptor */
577
578         if (asprintf(&path, "%s\\%s\\%s", KEY_SERVICES, name, "Security") < 0) {
579                 return false;
580         }
581         wresult = regkey_open_internal( NULL, &key, path, token,
582                                         REG_KEY_ALL );
583         if ( !W_ERROR_IS_OK(wresult) ) {
584                 DEBUG(0,("svcctl_get_secdesc: key lookup failed! [%s] (%s)\n",
585                         path, win_errstr(wresult)));
586                 SAFE_FREE(path);
587                 return False;
588         }
589         SAFE_FREE(path);
590
591         wresult = regval_ctr_init(key, &values);
592         if (!W_ERROR_IS_OK(wresult)) {
593                 DEBUG(0,("svcctl_set_secdesc: talloc() failed!\n"));
594                 TALLOC_FREE( key );
595                 return False;
596         }
597
598         /* stream the printer security descriptor */
599
600         status = marshall_sec_desc(ctx, sec_desc, &blob.data, &blob.length);
601         if (!NT_STATUS_IS_OK(status)) {
602                 DEBUG(0,("svcctl_set_secdesc: ndr_push_struct_blob() failed!\n"));
603                 TALLOC_FREE( key );
604                 return False;
605         }
606
607         regval_ctr_addvalue( values, "Security", REG_BINARY, blob.data, blob.length);
608         ret = store_reg_values( key, values );
609
610         /* cleanup */
611
612         TALLOC_FREE( key);
613
614         return ret;
615 }
616
617 /********************************************************************
618 ********************************************************************/
619
620 const char *svcctl_lookup_dispname(TALLOC_CTX *ctx, const char *name, NT_USER_TOKEN *token )
621 {
622         const char *display_name = NULL;
623         struct registry_key_handle *key = NULL;
624         struct regval_ctr *values = NULL;
625         struct regval_blob *val = NULL;
626         char *path = NULL;
627         WERROR wresult;
628         DATA_BLOB blob;
629
630         /* now add the security descriptor */
631
632         if (asprintf(&path, "%s\\%s", KEY_SERVICES, name) < 0) {
633                 return NULL;
634         }
635         wresult = regkey_open_internal( NULL, &key, path, token,
636                                         REG_KEY_READ );
637         if ( !W_ERROR_IS_OK(wresult) ) {
638                 DEBUG(0,("svcctl_lookup_dispname: key lookup failed! [%s] (%s)\n", 
639                         path, win_errstr(wresult)));
640                 SAFE_FREE(path);
641                 goto fail;
642         }
643         SAFE_FREE(path);
644
645         wresult = regval_ctr_init(key, &values);
646         if (!W_ERROR_IS_OK(wresult)) {
647                 DEBUG(0,("svcctl_lookup_dispname: talloc() failed!\n"));
648                 TALLOC_FREE( key );
649                 goto fail;
650         }
651
652         fetch_reg_values( key, values );
653
654         if ( !(val = regval_ctr_getvalue( values, "DisplayName" )) )
655                 goto fail;
656
657         blob = data_blob_const(regval_data_p(val), regval_size(val));
658         pull_reg_sz(ctx, &blob, &display_name);
659
660         TALLOC_FREE( key );
661
662         return display_name;
663
664 fail:
665         /* default to returning the service name */
666         TALLOC_FREE( key );
667         return talloc_strdup(ctx, name);
668 }
669
670 /********************************************************************
671 ********************************************************************/
672
673 const char *svcctl_lookup_description(TALLOC_CTX *ctx, const char *name, NT_USER_TOKEN *token )
674 {
675         const char *description = NULL;
676         struct registry_key_handle *key = NULL;
677         struct regval_ctr *values = NULL;
678         struct regval_blob *val = NULL;
679         char *path = NULL;
680         WERROR wresult;
681         DATA_BLOB blob;
682
683         /* now add the security descriptor */
684
685         if (asprintf(&path, "%s\\%s", KEY_SERVICES, name) < 0) {
686                 return NULL;
687         }
688         wresult = regkey_open_internal( NULL, &key, path, token,
689                                         REG_KEY_READ );
690         if ( !W_ERROR_IS_OK(wresult) ) {
691                 DEBUG(0,("svcctl_lookup_description: key lookup failed! [%s] (%s)\n", 
692                         path, win_errstr(wresult)));
693                 SAFE_FREE(path);
694                 return NULL;
695         }
696         SAFE_FREE(path);
697
698         wresult = regval_ctr_init(key, &values);
699         if (!W_ERROR_IS_OK(wresult)) {
700                 DEBUG(0,("svcctl_lookup_description: talloc() failed!\n"));
701                 TALLOC_FREE( key );
702                 return NULL;
703         }
704
705         fetch_reg_values( key, values );
706
707         if ( !(val = regval_ctr_getvalue( values, "Description" )) ) {
708                 TALLOC_FREE( key );
709                 return "Unix Service";
710         }
711
712         blob = data_blob_const(regval_data_p(val), regval_size(val));
713         pull_reg_sz(ctx, &blob, &description);
714
715         TALLOC_FREE(key);
716
717         return description;
718 }
719
720
721 /********************************************************************
722 ********************************************************************/
723
724 struct regval_ctr *svcctl_fetch_regvalues(const char *name, NT_USER_TOKEN *token)
725 {
726         struct registry_key_handle *key = NULL;
727         struct regval_ctr *values = NULL;
728         char *path = NULL;
729         WERROR wresult;
730
731         /* now add the security descriptor */
732
733         if (asprintf(&path, "%s\\%s", KEY_SERVICES, name) < 0) {
734                 return NULL;
735         }
736         wresult = regkey_open_internal( NULL, &key, path, token,
737                                         REG_KEY_READ );
738         if ( !W_ERROR_IS_OK(wresult) ) {
739                 DEBUG(0,("svcctl_fetch_regvalues: key lookup failed! [%s] (%s)\n",
740                         path, win_errstr(wresult)));
741                 SAFE_FREE(path);
742                 return NULL;
743         }
744         SAFE_FREE(path);
745
746         wresult = regval_ctr_init(NULL, &values);
747         if (!W_ERROR_IS_OK(wresult)) {
748                 DEBUG(0,("svcctl_fetch_regvalues: talloc() failed!\n"));
749                 TALLOC_FREE( key );
750                 return NULL;
751         }
752         fetch_reg_values( key, values );
753
754         TALLOC_FREE( key );
755         return values;
756 }