Bump header file dependencies in specfile
[shibboleth/cpp-sp.git] / shibd / shibd_win32.cpp
index 7a9a602..ae8ff46 100644 (file)
@@ -1,25 +1,25 @@
-/*
- *  Copyright 2001-2007 Internet2
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
+ *
+ * UCAID licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the
+ * License at
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
  */
 
 /*
  * shar_win32.cpp -- the SHAR "main" code on Win32
- *
- * Created By: Scott Cantor (cantor.2@osu.edu)
- *
- * $Id: shar_win32.cpp 2150 2007-02-02 04:06:15 +0000 (Fri, 02 Feb 2007) cantor $
  */
 
 #include "config_win32.h"
@@ -38,6 +38,7 @@ extern const char* shar_schemadir;
 extern const char* shar_config;
 extern const char* shar_prefix;
 extern bool shar_checkonly;
+extern bool shar_version;
 
 // internal variables
 SERVICE_STATUS          ssStatus;       // current status of the service
@@ -45,8 +46,10 @@ SERVICE_STATUS_HANDLE   sshStatusHandle;
 DWORD                   dwErr = 0;
 BOOL                    bConsole = FALSE;
 char                    szErr[256];
-LPCSTR                  lpszInstall = NULL;
-LPCSTR                  lpszRemove = NULL;
+LPCSTR                  lpszInstall = nullptr;
+LPCSTR                  lpszRemove = nullptr;
+LPCSTR                  lpszStdout = nullptr;
+LPCSTR                  lpszStderr = nullptr;
 
 // internal function prototypes
 VOID WINAPI service_ctrl(DWORD dwCtrlCode);
@@ -80,7 +83,7 @@ BOOL WINAPI BreakHandler(DWORD dwCtrlType)
 }
 
 
-int real_main(int);  // The revised two-phase main() in shar.cpp
+int real_main(int);  // The revised two-phase main() in shibd.cpp
 
 int main(int argc, char *argv[])
 {
@@ -97,6 +100,20 @@ int main(int argc, char *argv[])
             if (argc > ++i)
                 lpszRemove = argv[i++];
         }
+        else if (_stricmp("stdout", argv[i]+1) == 0)
+        {
+            if (argc > ++i) {
+                lpszStdout = argv[i++];
+                freopen(lpszStdout, "a+", stdout);
+            }
+        }
+        else if (_stricmp("stderr", argv[i]+1) == 0)
+        {
+            if (argc > ++i) {
+                lpszStderr = argv[i++];
+                freopen(lpszStderr, "a+", stderr);
+            }
+        }
         else if (_stricmp( "console", argv[i]+1) == 0)
         {
             i++;
@@ -106,7 +123,13 @@ int main(int argc, char *argv[])
         {
             i++;
             bConsole = TRUE;
-            shar_checkonly=true;
+            shar_checkonly = true;
+        }
+        else if (_stricmp( "version", argv[i]+1) == 0)
+        {
+            i++;
+            bConsole = TRUE;
+            shar_version = true;
         }
         else if (_stricmp( "config", argv[i]+1) == 0)
         {
@@ -135,7 +158,7 @@ int main(int argc, char *argv[])
         SetConsoleCtrlHandler(&BreakHandler,TRUE);
         if ((i=real_main(1))!=0)
         {
-            LogEvent(NULL, EVENTLOG_ERROR_TYPE, 2100, NULL, "shibd startup failed, check shibd.log for further details");
+            LogEvent(nullptr, EVENTLOG_ERROR_TYPE, 2100, nullptr, "shibd startup failed, check shibd.log for further details");
             return i;
         }
         return real_main(0);
@@ -161,6 +184,9 @@ int main(int argc, char *argv[])
         printf("%s -remove <name>    to remove the named service\n", argv[0]);
         printf("%s -console          to run as a console app for debugging\n", argv[0]);
         printf("%s -check            to run as a console app and check configuration\n", argv[0]);
+        printf("%s -version          to run as a console app and print the version\n", argv[0]);
+        printf("\t-stdout <path> to redirect stdout stream\n");
+        printf("\t-stderr <path> to redirect stderr stream\n");
         printf("\t-prefix <dir> to specify the installation directory\n");
         printf("\t-config <file> to specify the config file to use\n");
         printf("\t-catalogs <catalog1:catalog2> to specify schema catalogs\n");
@@ -169,11 +195,11 @@ int main(int argc, char *argv[])
     SERVICE_TABLE_ENTRY dispatchTable[] =
     {
         { "SHIBD", (LPSERVICE_MAIN_FUNCTION)service_main },
-        { NULL, NULL }
+        { nullptr, nullptr }
     };
 
     if (!StartServiceCtrlDispatcher(dispatchTable))
-        LogEvent(NULL, EVENTLOG_ERROR_TYPE, 2100, NULL, "StartServiceCtrlDispatcher failed.");
+        LogEvent(nullptr, EVENTLOG_ERROR_TYPE, 2100, nullptr, "StartServiceCtrlDispatcher failed.");
     return 0;
 }
 
@@ -188,11 +214,11 @@ VOID ServiceStart (DWORD dwArgc, LPSTR *lpszArgv)
 
     if (real_main(1)!=0)
     {
-        LogEvent(NULL, EVENTLOG_ERROR_TYPE, 2100, NULL, "shibd startup failed, check shibd.log for further details");
+        LogEvent(nullptr, EVENTLOG_ERROR_TYPE, 2100, nullptr, "shibd startup failed, check shibd.log for further details");
         return;
     }
 
-    LogEvent(NULL, EVENTLOG_INFORMATION_TYPE, 7700, NULL, "shibd started successfully.");
+    LogEvent(nullptr, EVENTLOG_INFORMATION_TYPE, 7700, nullptr, "shibd started successfully.");
 
     if (!ReportStatusToSCMgr(SERVICE_RUNNING, NO_ERROR, 0))
         return;
@@ -209,7 +235,7 @@ VOID ServiceStart (DWORD dwArgc, LPSTR *lpszArgv)
 VOID ServiceStop()
 {
     if (!bConsole)
-        LogEvent(NULL, EVENTLOG_INFORMATION_TYPE, 7701, NULL, "shibd stopping...");
+        LogEvent(nullptr, EVENTLOG_INFORMATION_TYPE, 7701, nullptr, "shibd stopping...");
     shibd_shutdown=true;
 }
 
@@ -336,7 +362,7 @@ BOOL ReportStatusToSCMgr(DWORD dwCurrentState,
         // Report the status of the service to the service control manager.
         //
         if (!(fResult = SetServiceStatus(sshStatusHandle, &ssStatus)))
-            LogEvent(NULL, EVENTLOG_ERROR_TYPE, 2100, NULL, "SetServiceStatus failed.");
+            LogEvent(nullptr, EVENTLOG_ERROR_TYPE, 2100, nullptr, "SetServiceStatus failed.");
     }
     return fResult;
 }
@@ -354,13 +380,13 @@ void CmdInstallService(LPCSTR name)
 
     char szPath[256];
 
-    if ( GetModuleFileName( NULL, szPath, 256 ) == 0 )
+    if ( GetModuleFileName( nullptr, szPath, 256 ) == 0 )
     {
         printf("Unable to install %s - %s\n", name, GetLastErrorText(szErr, 256));
         return;
     }
     
-    string dispName = string("Shibboleth ") + PACKAGE_VERSION + " Daemon (" + name + ")";
+    string dispName = string("Shibboleth 2 Daemon (") + name + ")";
     string realName = string("shibd_") + name;
     string cmd(szPath);
     if (shar_prefix)
@@ -369,10 +395,14 @@ void CmdInstallService(LPCSTR name)
         cmd = cmd + " -config " + shar_config;
     if (shar_schemadir)
         cmd = cmd + " -schemadir " + shar_schemadir;
+    if (lpszStdout)
+        cmd = cmd + " -stdout " + lpszStdout;
+    if (lpszStderr)
+        cmd = cmd + " -stderr " + lpszStderr;
 
     schSCManager = OpenSCManager(
-                        NULL,                   // machine (NULL == local)
-                        NULL,                   // database (NULL == default)
+                        nullptr,                   // machine (nullptr == local)
+                        nullptr,                   // database (nullptr == default)
                         SC_MANAGER_ALL_ACCESS   // access required
                         );
     
@@ -388,11 +418,11 @@ void CmdInstallService(LPCSTR name)
             SERVICE_AUTO_START,         // start type
             SERVICE_ERROR_NORMAL,       // error control type
             cmd.c_str(),                // service's command line
-            NULL,                       // no load ordering group
-            NULL,                       // no tag identifier
-            NULL,                       // dependencies
-            NULL,                       // LocalSystem account
-            NULL);                      // no password
+            nullptr,                       // no load ordering group
+            nullptr,                       // no tag identifier
+            nullptr,                       // dependencies
+            nullptr,                       // LocalSystem account
+            nullptr);                      // no password
 
         if ( schService )
         {
@@ -419,8 +449,8 @@ void CmdRemoveService(LPCSTR name)
     _snprintf(realName,sizeof(realName),"shibd_%s",name);
 
     schSCManager = OpenSCManager(
-                        NULL,                   // machine (NULL == local)
-                        NULL,                   // database (NULL == default)
+                        nullptr,                   // machine (nullptr == local)
+                        nullptr,                   // database (nullptr == default)
                         SC_MANAGER_ALL_ACCESS   // access required
                         );
     if ( schSCManager )
@@ -489,15 +519,15 @@ void CmdRemoveService(LPCSTR name)
 LPTSTR GetLastErrorText( LPSTR lpszBuf, DWORD dwSize )
 {
     DWORD dwRet;
-    LPSTR lpszTemp = NULL;
+    LPSTR lpszTemp = nullptr;
 
     dwRet = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_ARGUMENT_ARRAY,
-                           NULL,
+                           nullptr,
                            GetLastError(),
                            LANG_NEUTRAL,
                            (LPSTR)&lpszTemp,
                            0,
-                           NULL );
+                           nullptr );
 
     // supplied buffer is not long enough
     if ( !dwRet || ( (long)dwSize < (long)dwRet+14 ) )
@@ -521,9 +551,9 @@ BOOL LogEvent(
     PSID  lpUserSid,
     LPCSTR  message)
 {
-    LPCSTR  messages[] = {message, NULL};
+    LPCSTR  messages[] = {message, nullptr};
     
     HANDLE hElog = RegisterEventSource(lpUNCServerName, "Shibboleth Daemon");
-    BOOL res = ReportEvent(hElog, wType, 0, dwEventID, lpUserSid, 1, 0, messages, NULL);
+    BOOL res = ReportEvent(hElog, wType, 0, dwEventID, lpUserSid, 1, 0, messages, nullptr);
     return (DeregisterEventSource(hElog) && res);
 }