1/* $OpenLDAP$ */
2/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 *
4 * Copyright 1998-2011 The OpenLDAP Foundation.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
9 * Public License.
10 *
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
14 */
15
16#include "portable.h"
17#include <stdio.h>
18#include <ac/string.h>
19#include "slap.h"
20#include "lutil.h"
21
22#ifdef HAVE_NT_SERVICE_MANAGER
23
24/* in main.c */
25void WINAPI ServiceMain( DWORD argc, LPTSTR *argv );
26
27/* in ntservice.c */
28int main( int argc, LPTSTR *argv )
29{
30	int		length;
31	char	filename[MAX_PATH], *fname_start;
32
33	/*
34	 * Because the service was registered as SERVICE_WIN32_OWN_PROCESS,
35	 * the lpServiceName element of the SERVICE_TABLE_ENTRY will be
36	 * ignored.
37	 */
38
39	SERVICE_TABLE_ENTRY		DispatchTable[] = {
40		{	"",	(LPSERVICE_MAIN_FUNCTION) ServiceMain	},
41		{	NULL,			NULL	}
42	};
43
44	/*
45	 * set the service's current directory to the installation directory
46	 * for the service. this way we don't have to write absolute paths
47	 * in the configuration files
48	 */
49	GetModuleFileName( NULL, filename, sizeof( filename ) );
50	fname_start = strrchr( filename, *LDAP_DIRSEP );
51
52	if ( argc > 1 ) {
53		if ( _stricmp( "install", argv[1] ) == 0 )
54		{
55			char *svcName = SERVICE_NAME;
56			char *displayName = "OpenLDAP Directory Service";
57			BOOL auto_start = FALSE;
58
59			if ( (argc > 2) && (argv[2] != NULL) )
60				svcName = argv[2];
61
62			if ( argc > 3 && argv[3])
63				displayName = argv[3];
64
65			if ( argc > 4 && stricmp(argv[4], "auto") == 0)
66				auto_start = TRUE;
67
68			strcat(filename, " service");
69			if ( !lutil_srv_install(svcName, displayName, filename, auto_start) )
70			{
71				fputs( "service failed installation ...\n", stderr  );
72				return EXIT_FAILURE;
73			}
74			fputs( "service has been installed ...\n", stderr  );
75			return EXIT_SUCCESS;
76		}
77
78		if ( _stricmp( "remove", argv[1] ) == 0 )
79		{
80			char *svcName = SERVICE_NAME;
81			if ( (argc > 2) && (argv[2] != NULL) )
82				svcName = argv[2];
83			if ( !lutil_srv_remove(svcName, filename) )
84			{
85				fputs( "failed to remove the service ...\n", stderr  );
86				return EXIT_FAILURE;
87			}
88			fputs( "service has been removed ...\n", stderr );
89			return EXIT_SUCCESS;
90		}
91		if ( _stricmp( "service", argv[1] ) == 0 )
92		{
93			is_NT_Service = 1;
94			*fname_start = '\0';
95			SetCurrentDirectory( filename );
96		}
97	}
98
99	if (is_NT_Service)
100	{
101		StartServiceCtrlDispatcher(DispatchTable);
102	} else
103	{
104		ServiceMain( argc, argv );
105	}
106
107	return EXIT_SUCCESS;
108}
109
110#endif
111