view common/util.h @ 841:216a65d7fc4b

(issue66) Implement is_system_install and use it This has completly different implementations for linux and Windows. The commit also moves some code into util.c for better reuse.
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 29 Jul 2014 18:12:57 +0200
parents 4ad764bfb39c
children 427e2e18b8c8
line wrap: on
line source
/* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU GPL (v>=2)
 * and comes with ABSOLUTELY NO WARRANTY!
 * See LICENSE.txt for details.
 */
#ifndef COMMON_UTIL_H
#define COMMON_UTIL_H
/* @file util.h
 * @brief The usual useful stuff that fit nowhere else
 */
#include <stdbool.h>

#ifdef WIN32
#include <windows.h>
#include <psapi.h>
#endif

#ifdef __cplusplus
extern "C" {
#endif

#ifndef WIN32
/**@def Some value to use as equivalent as MAX_PATH on windows */
#define MAX_PATH_LINUX 4000
#endif

/**@brief Check if the current process is running with elevated privileges.
 *
 * Elevates the current process token to check if it is marked as elevated.
 * Uses TokenElevation on windows and checks effective UID on Linux.
 *
 * @returns true if the current process is elevated.*/
bool is_elevated();

/**@brief Check if the Software is installed system wide
 *
 * On Windows this checks if a registry key under HKLM exists for
 * trustbridge and that the installation path mentioned there matches
 * the current module path.
 *
 * On linux this looks for the installation configuration in /etc
 * and checks if the current process is inside the installation prefix.
 *
 * The checked path is limited to MAX_PATH on Windows and @MAX_PATH_LINUX on
 * Linux.
 */
bool is_system_install();

/**@brief Check if the user is in the administrators group.
 *
 * The function checks if the account that startet this process
 * belongs to a user that is a member of the Administrators group.
 *
 * @returns True if the user is in the admin group. False otherwise or on error.
 */
bool is_admin();

/**@brief Get the directory in which the current process resides in
 *
 * Look up the directory in which the current process is placed.
 * If the path is longer then MAX_PATH NULL is returned.
 *
 * Returns a utf-8 encoded string that has to be freed by the caller
 * on linux the path is returned as is including the last /.
 *
 * @returns The directory of the current process
 */
char * get_install_dir();

#ifdef WIN32
/**@brief Get a copy of the processes owner sid
 *
 * Copy the SID of the owner of the process hProcess.
 *
 * The returned sid structure has to be freed with free by the caller
 *
 * @param[in] hProcess A handle to the process whose user should be obtained.
 * The process must have the PROCESS_QUERY_INFORMATION access permission.
 *
 * @returns A copy of the process owners sid or NULL on error.
 */
PSID get_process_owner(HANDLE hProcess);

/**@brief Read (and expand if necessary) a registry string.
 *
 * Reads a registry string and calls ExpandEnvironmentString
 * if necessary on it. Returns a newly allocated string array
 * with the expanded registry value converted to UTF-8
 *
 * Caller has to free return value with free.
 *
 * @param [in] root the root key (e.g. HKEY_LOCAL_MACHINE)
 * @param [in] key the key
 * @param [in] name the name of the value to read.
 *
 * @returns the expanded, null terminated utf-8 string of the value.
 *          or NULL on error.
 */
char * read_registry_string (const HKEY root, const wchar_t *key,
                             const wchar_t *name);
#endif

#ifdef __cplusplus
}
#endif

#endif // COMMON_UTIL_H

http://wald.intevation.org/projects/trustbridge/