int env_check_password_file(const char *filename, const char *passwd) { char encryptedPasswd[16]; char key[24]; char passwd_hash[20]; char passwd_hash_text[40]; int fd; int passwd_bytes; void *des; void *sha1; /* create password hash from password */ passwd_bytes = g_strlen(passwd); sha1 = ssl_sha1_info_create(); ssl_sha1_transform(sha1, "xrdp_vnc", 8); ssl_sha1_transform(sha1, passwd, passwd_bytes); ssl_sha1_transform(sha1, passwd, passwd_bytes); ssl_sha1_complete(sha1, passwd_hash); ssl_sha1_info_delete(sha1); g_snprintf(passwd_hash_text, 39, "%2.2x%2.2x%2.2x%2.2x", (tui8)passwd_hash[0], (tui8)passwd_hash[1], (tui8)passwd_hash[2], (tui8)passwd_hash[3]); passwd_hash_text[39] = 0; passwd = passwd_hash_text; /* create file from password */ g_memset(encryptedPasswd, 0, sizeof(encryptedPasswd)); g_strncpy(encryptedPasswd, passwd, 8); g_memset(key, 0, sizeof(key)); g_mirror_memcpy(key, g_fixedkey, 8); des = ssl_des3_encrypt_info_create(key, 0); ssl_des3_encrypt(des, 8, encryptedPasswd, encryptedPasswd); ssl_des3_info_delete(des); fd = g_file_open_ex(filename, 0, 1, 1, 1); if (fd == -1) { log_message(LOG_LEVEL_WARNING, "Cannot write VNC password hash to file %s: %s", filename, g_get_strerror()); return 1; } g_file_write(fd, encryptedPasswd, 8); g_file_close(fd); return 0; }
void APP_CC logd_main_loop() { int client = 0; char buffer[1024]; int count; log_fd = open(LOGGING_FILE, O_WRONLY | O_CREAT | O_APPEND | O_SYNC, S_IRUSR | S_IWUSR); g_mkdir("/var/spool/xrdp"); g_chmod_hex("/var/spool/xrdp", 0775); if ( g_directory_exist("/var/spool/xrdp") == 0) { g_writeln("Unable to create the logging socket"); g_exit(1); } g_writeln("xrdp logging spool application\n"); log_socket = g_create_unix_socket(LOGGING_SOCKET); g_chmod_hex(LOGGING_SOCKET, 0xFFFF); if ( log_socket == 1) { g_writeln("Unable to create logging socket"); return ; } if (log_fd < 0){ g_writeln("Unable to create logging instance\n"); } running = 1; while(running) { client = g_wait_connection(log_socket); count = g_tcp_recv(client, (char*)buffer, 1024, 0); while(count > 0) { g_file_write(log_fd, buffer, count); count = g_tcp_recv(client, (char*)buffer, 1024, 0); } count = 0; g_tcp_close(client); } }
int DEFAULT_CC env_check_password_file(char* filename, char* password) { char encryptedPasswd[16]; int fd; g_memset(encryptedPasswd, 0, 16); g_strncpy(encryptedPasswd, password, 8); rfbDesKey(g_fixedkey, 0); rfbDes((unsigned char*)encryptedPasswd, (unsigned char*)encryptedPasswd); fd = g_file_open(filename); if (fd == -1) { log_message(&(g_cfg->log), LOG_LEVEL_WARNING, "can't read vnc password file - %s", filename); return 1; } g_file_write(fd, encryptedPasswd, 8); g_file_close(fd); return 0; }
int DEFAULT_CC main(int argc, char **argv) { int test; int host_be; #if defined(_WIN32) WSADATA w; SC_HANDLE sc_man; SC_HANDLE sc_ser; int run_as_service; SERVICE_TABLE_ENTRY te[2]; #else int pid; int fd; int no_daemon; char text[256]; char pid_file[256]; #endif g_init(); ssl_init(); /* check compiled endian with actual endian */ test = 1; host_be = !((int)(*(unsigned char *)(&test))); #if defined(B_ENDIAN) if (!host_be) #endif #if defined(L_ENDIAN) if (host_be) #endif { g_writeln("endian wrong, edit arch.h"); return 0; } /* check long, int and void* sizes */ if (sizeof(int) != 4) { g_writeln("unusable int size, must be 4"); return 0; } if (sizeof(long) != sizeof(void *)) { g_writeln("long size must match void* size"); return 0; } if (sizeof(long) != 4 && sizeof(long) != 8) { g_writeln("unusable long size, must be 4 or 8"); return 0; } if (sizeof(tui64) != 8) { g_writeln("unusable tui64 size, must be 8"); return 0; } #if defined(_WIN32) run_as_service = 1; if (argc == 2) { if (g_strncasecmp(argv[1], "-help", 255) == 0 || g_strncasecmp(argv[1], "--help", 255) == 0 || g_strncasecmp(argv[1], "-h", 255) == 0) { g_writeln(""); g_writeln("xrdp: A Remote Desktop Protocol server."); g_writeln("Copyright (C) Jay Sorg 2004-2011"); g_writeln("See http://xrdp.sourceforge.net for more information."); g_writeln(""); g_writeln("Usage: xrdp [options]"); g_writeln(" -h: show help"); g_writeln(" -install: install service"); g_writeln(" -remove: remove service"); g_writeln(""); g_exit(0); } else if (g_strncasecmp(argv[1], "-install", 255) == 0 || g_strncasecmp(argv[1], "--install", 255) == 0 || g_strncasecmp(argv[1], "-i", 255) == 0) { /* open service manager */ sc_man = OpenSCManager(0, 0, GENERIC_WRITE); if (sc_man == 0) { g_writeln("error OpenSCManager, do you have rights?"); g_exit(0); } /* check if service is allready installed */ sc_ser = OpenService(sc_man, "xrdp", SERVICE_ALL_ACCESS); if (sc_ser == 0) { /* install service */ CreateService(sc_man, "xrdp", "xrdp", SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, "c:\\temp\\xrdp\\xrdp.exe", 0, 0, 0, 0, 0); } else { g_writeln("error service is allready installed"); CloseServiceHandle(sc_ser); CloseServiceHandle(sc_man); g_exit(0); } CloseServiceHandle(sc_man); g_exit(0); } else if (g_strncasecmp(argv[1], "-remove", 255) == 0 || g_strncasecmp(argv[1], "--remove", 255) == 0 || g_strncasecmp(argv[1], "-r", 255) == 0) { /* open service manager */ sc_man = OpenSCManager(0, 0, GENERIC_WRITE); if (sc_man == 0) { g_writeln("error OpenSCManager, do you have rights?"); g_exit(0); } /* check if service is allready installed */ sc_ser = OpenService(sc_man, "xrdp", SERVICE_ALL_ACCESS); if (sc_ser == 0) { g_writeln("error service is not installed"); CloseServiceHandle(sc_man); g_exit(0); } DeleteService(sc_ser); CloseServiceHandle(sc_man); g_exit(0); } else { g_writeln("Unknown Parameter"); g_writeln("xrdp -h for help"); g_writeln(""); g_exit(0); } } else if (argc > 1) { g_writeln("Unknown Parameter"); g_writeln("xrdp -h for help"); g_writeln(""); g_exit(0); } if (run_as_service) { g_memset(&te, 0, sizeof(te)); te[0].lpServiceName = "xrdp"; te[0].lpServiceProc = MyServiceMain; StartServiceCtrlDispatcher(&te); g_exit(0); } WSAStartup(2, &w); #else /* _WIN32 */ g_snprintf(pid_file, 255, "%s/xrdp.pid", XRDP_PID_PATH); no_daemon = 0; if (argc == 2) { if ((g_strncasecmp(argv[1], "-kill", 255) == 0) || (g_strncasecmp(argv[1], "--kill", 255) == 0) || (g_strncasecmp(argv[1], "-k", 255) == 0)) { g_writeln("stopping xrdp"); /* read the xrdp.pid file */ fd = -1; if (g_file_exist(pid_file)) /* xrdp.pid */ { fd = g_file_open(pid_file); /* xrdp.pid */ } if (fd == -1) { g_writeln("problem opening to xrdp.pid"); g_writeln("maybe its not running"); } else { g_memset(text, 0, 32); g_file_read(fd, text, 31); pid = g_atoi(text); g_writeln("stopping process id %d", pid); if (pid > 0) { g_sigterm(pid); } g_file_close(fd); } g_exit(0); } else if (g_strncasecmp(argv[1], "-nodaemon", 255) == 0 || g_strncasecmp(argv[1], "--nodaemon", 255) == 0 || g_strncasecmp(argv[1], "-nd", 255) == 0 || g_strncasecmp(argv[1], "--nd", 255) == 0 || g_strncasecmp(argv[1], "-ns", 255) == 0 || g_strncasecmp(argv[1], "--ns", 255) == 0) { no_daemon = 1; } else if (g_strncasecmp(argv[1], "-help", 255) == 0 || g_strncasecmp(argv[1], "--help", 255) == 0 || g_strncasecmp(argv[1], "-h", 255) == 0) { g_writeln(""); g_writeln("xrdp: A Remote Desktop Protocol server."); g_writeln("Copyright (C) Jay Sorg 2004-2011"); g_writeln("See http://xrdp.sourceforge.net for more information."); g_writeln(""); g_writeln("Usage: xrdp [options]"); g_writeln(" -h: show help"); g_writeln(" -nodaemon: don't fork into background"); g_writeln(" -kill: shut down xrdp"); g_writeln(""); g_exit(0); } else if ((g_strncasecmp(argv[1], "-v", 255) == 0) || (g_strncasecmp(argv[1], "--version", 255) == 0)) { g_writeln(""); g_writeln("xrdp: A Remote Desktop Protocol server."); g_writeln("Copyright (C) Jay Sorg 2004-2011"); g_writeln("See http://xrdp.sourceforge.net for more information."); g_writeln("Version %s", PACKAGE_VERSION); g_writeln(""); g_exit(0); } else { g_writeln("Unknown Parameter"); g_writeln("xrdp -h for help"); g_writeln(""); g_exit(0); } } else if (argc > 1) { g_writeln("Unknown Parameter"); g_writeln("xrdp -h for help"); g_writeln(""); g_exit(0); } if (g_file_exist(pid_file)) /* xrdp.pid */ { g_writeln("It looks like xrdp is allready running,"); g_writeln("if not delete the xrdp.pid file and try again"); g_exit(0); } if (!no_daemon) { /* make sure we can write to pid file */ fd = g_file_open(pid_file); /* xrdp.pid */ if (fd == -1) { g_writeln("running in daemon mode with no access to pid files, quitting"); g_exit(0); } if (g_file_write(fd, "0", 1) == -1) { g_writeln("running in daemon mode with no access to pid files, quitting"); g_exit(0); } g_file_close(fd); g_file_delete(pid_file); } if (!no_daemon) { /* start of daemonizing code */ pid = g_fork(); if (pid == -1) { g_writeln("problem forking"); g_exit(1); } if (0 != pid) { g_writeln("process %d started ok", pid); /* exit, this is the main process */ g_exit(0); } g_sleep(1000); g_file_close(0); g_file_close(1); g_file_close(2); g_file_open("/dev/null"); g_file_open("/dev/null"); g_file_open("/dev/null"); /* end of daemonizing code */ } if (!no_daemon) { /* write the pid to file */ pid = g_getpid(); fd = g_file_open(pid_file); /* xrdp.pid */ if (fd == -1) { g_writeln("trying to write process id to xrdp.pid"); g_writeln("problem opening xrdp.pid"); g_writeln("maybe no rights"); } else { g_sprintf(text, "%d", pid); g_file_write(fd, text, g_strlen(text)); g_file_close(fd); } } #endif g_threadid = tc_get_threadid(); g_listen = xrdp_listen_create(); g_signal_user_interrupt(xrdp_shutdown); /* SIGINT */ g_signal_kill(xrdp_shutdown); /* SIGKILL */ g_signal_pipe(pipe_sig); /* SIGPIPE */ g_signal_terminate(xrdp_shutdown); /* SIGTERM */ g_sync_mutex = tc_mutex_create(); g_sync1_mutex = tc_mutex_create(); pid = g_getpid(); g_snprintf(text, 255, "xrdp_%8.8x_main_term", pid); g_term_event = g_create_wait_obj(text); g_snprintf(text, 255, "xrdp_%8.8x_main_sync", pid); g_sync_event = g_create_wait_obj(text); if (g_term_event == 0) { g_writeln("error creating g_term_event"); } xrdp_listen_main_loop(g_listen); xrdp_listen_delete(g_listen); tc_mutex_delete(g_sync_mutex); tc_mutex_delete(g_sync1_mutex); g_delete_wait_obj(g_term_event); g_delete_wait_obj(g_sync_event); #if defined(_WIN32) /* I don't think it ever gets here */ /* when running in win32 app mode, control c exits right away */ WSACleanup(); #else /* delete the xrdp.pid file */ g_file_delete(pid_file); #endif return 0; }
int DEFAULT_CC main(int argc, char **argv) { int fd; enum logReturns log_error; int error; int daemon = 1; int pid; char pid_s[32]; char text[256]; char pid_file[256]; char cfg_file[256]; g_init("xrdp-sesman"); g_snprintf(pid_file, 255, "%s/xrdp-sesman.pid", XRDP_PID_PATH); if (1 == argc) { /* no options on command line. normal startup */ g_printf("starting sesman...\n"); daemon = 1; } else if ((2 == argc) && ((0 == g_strcasecmp(argv[1], "--nodaemon")) || (0 == g_strcasecmp(argv[1], "-nodaemon")) || (0 == g_strcasecmp(argv[1], "-n")) || (0 == g_strcasecmp(argv[1], "-ns")))) { /* starts sesman not daemonized */ g_printf("starting sesman in foreground...\n"); daemon = 0; } else if ((2 == argc) && ((0 == g_strcasecmp(argv[1], "--help")) || (0 == g_strcasecmp(argv[1], "-help")) || (0 == g_strcasecmp(argv[1], "-h")))) { /* help screen */ g_printf("sesman - xrdp session manager\n\n"); g_printf("usage: sesman [command]\n\n"); g_printf("command can be one of the following:\n"); g_printf("-n, -ns, --nodaemon starts sesman in foreground\n"); g_printf("-k, --kill kills running sesman\n"); g_printf("-h, --help shows this help\n"); g_printf("if no command is specified, sesman is started in background"); g_deinit(); g_exit(0); } else if ((2 == argc) && ((0 == g_strcasecmp(argv[1], "--kill")) || (0 == g_strcasecmp(argv[1], "-kill")) || (0 == g_strcasecmp(argv[1], "-k")))) { /* killing running sesman */ /* check if sesman is running */ if (!g_file_exist(pid_file)) { g_printf("sesman is not running (pid file not found - %s)\n", pid_file); g_deinit(); g_exit(1); } fd = g_file_open(pid_file); if (-1 == fd) { g_printf("error opening pid file[%s]: %s\n", pid_file, g_get_strerror()); return 1; } g_memset(pid_s, 0, sizeof(pid_s)); error = g_file_read(fd, pid_s, 31); if (-1 == error) { g_printf("error reading pid file: %s\n", g_get_strerror()); g_file_close(fd); g_deinit(); g_exit(error); } g_file_close(fd); pid = g_atoi(pid_s); error = g_sigterm(pid); if (0 != error) { g_printf("error killing sesman: %s\n", g_get_strerror()); } else { g_file_delete(pid_file); } g_deinit(); g_exit(error); } else { /* there's something strange on the command line */ g_printf("sesman - xrdp session manager\n\n"); g_printf("error: invalid command line\n"); g_printf("usage: sesman [ --nodaemon | --kill | --help ]\n"); g_deinit(); g_exit(1); } if (g_file_exist(pid_file)) { g_printf("sesman is already running.\n"); g_printf("if it's not running, try removing "); g_printf("%s", pid_file); g_printf("\n"); g_deinit(); g_exit(1); } /* reading config */ g_cfg = g_new0(struct config_sesman, 1); if (0 == g_cfg) { g_printf("error creating config: quitting.\n"); g_deinit(); g_exit(1); } //g_cfg->log.fd = -1; /* don't use logging before reading its config */ if (0 != config_read(g_cfg)) { g_printf("error reading config: %s\nquitting.\n", g_get_strerror()); g_deinit(); g_exit(1); } g_snprintf(cfg_file, 255, "%s/sesman.ini", XRDP_CFG_PATH); /* starting logging subsystem */ log_error = log_start(cfg_file, "xrdp-sesman"); if (log_error != LOG_STARTUP_OK) { switch (log_error) { case LOG_ERROR_MALLOC: g_writeln("error on malloc. cannot start logging. quitting."); break; case LOG_ERROR_FILE_OPEN: g_writeln("error opening log file [%s]. quitting.", getLogFile(text, 255)); break; default: g_writeln("error"); break; } g_deinit(); g_exit(1); } /* libscp initialization */ scp_init(); if (daemon) { /* start of daemonizing code */ g_pid = g_fork(); if (0 != g_pid) { g_deinit(); g_exit(0); } g_file_close(0); g_file_close(1); g_file_close(2); if (g_file_open("/dev/null") < 0) { } if (g_file_open("/dev/null") < 0) { } if (g_file_open("/dev/null") < 0) { } } /* signal handling */ g_pid = g_getpid(); /* old style signal handling is now managed synchronously by a * separate thread. uncomment this block if you need old style * signal handling and comment out thread_sighandler_start() * going back to old style for the time being * problem with the sigaddset functions in sig.c - jts */ #if 1 g_signal_hang_up(sig_sesman_reload_cfg); /* SIGHUP */ g_signal_user_interrupt(sig_sesman_shutdown); /* SIGINT */ g_signal_terminate(sig_sesman_shutdown); /* SIGTERM */ g_signal_child_stop(sig_sesman_session_end); /* SIGCHLD */ #endif #if 0 thread_sighandler_start(); #endif if (daemon) { /* writing pid file */ fd = g_file_open(pid_file); if (-1 == fd) { log_message(LOG_LEVEL_ERROR, "error opening pid file[%s]: %s", pid_file, g_get_strerror()); log_end(); g_deinit(); g_exit(1); } g_sprintf(pid_s, "%d", g_pid); g_file_write(fd, pid_s, g_strlen(pid_s)); g_file_close(fd); } /* start program main loop */ log_message(LOG_LEVEL_INFO, "starting xrdp-sesman with pid %d", g_pid); /* make sure the /tmp/.X11-unix directory exist */ if (!g_directory_exist("/tmp/.X11-unix")) { if (!g_create_dir("/tmp/.X11-unix")) { log_message(LOG_LEVEL_ERROR, "sesman.c: error creating dir /tmp/.X11-unix"); } g_chmod_hex("/tmp/.X11-unix", 0x1777); } g_snprintf(text, 255, "xrdp_sesman_%8.8x_main_term", g_pid); g_term_event = g_create_wait_obj(text); sesman_main_loop(); /* clean up PID file on exit */ if (daemon) { g_file_delete(pid_file); } g_delete_wait_obj(g_term_event); if (!daemon) { log_end(); } g_deinit(); return 0; }
int main(int argc, char **argv) { int fd; int pid; int no_daemon; char text[256]; char pid_file[256]; char cfg_file[256]; enum logReturns error; xrdpStartupParams* startup_params; g_init("xrdp"); g_snprintf(cfg_file, 255, "%s/xrdp.ini", XRDP_CFG_PATH); /* starting logging subsystem */ error = log_start(cfg_file, "XRDP"); if (error != LOG_STARTUP_OK) { switch (error) { case LOG_ERROR_MALLOC: g_writeln("error on malloc. cannot start logging. quitting."); break; case LOG_ERROR_FILE_OPEN: g_writeln("error opening log file [%s]. quitting.", getLogFile(text, 255)); break; default: g_writeln("log_start error"); break; } g_deinit(); g_exit(1); } startup_params = (xrdpStartupParams *) g_malloc(sizeof(xrdpStartupParams), 1); if (xrdp_process_params(argc, argv, startup_params) != 0) { g_writeln("Unknown Parameter"); g_writeln("xrdp -h for help"); g_writeln(""); g_deinit(); g_exit(0); } g_snprintf(pid_file, 255, "%s/xrdp-ng.pid", XRDP_PID_PATH); no_daemon = 0; if (startup_params->kill) { g_writeln("stopping xrdp"); /* read the xrdp.pid file */ fd = -1; if (g_file_exist(pid_file)) /* xrdp-ng.pid */ { fd = g_file_open(pid_file); /* xrdp-ng.pid */ } if (fd == -1) { g_writeln("problem opening to xrdp-ng.pid [%s]", pid_file); g_writeln("maybe its not running"); } else { g_memset(text, 0, 32); g_file_read(fd, (unsigned char*) text, 31); pid = g_atoi(text); g_writeln("stopping process id %d", pid); if (pid > 0) { g_sigterm(pid); } g_file_close(fd); } g_deinit(); g_exit(0); } if (startup_params->no_daemon) { no_daemon = 1; } if (startup_params->help) { g_writeln(""); g_writeln("xrdp: A Remote Desktop Protocol server."); g_writeln("Copyright (C) Jay Sorg 2004-2011"); g_writeln("See http://xrdp.sourceforge.net for more information."); g_writeln(""); g_writeln("Usage: xrdp [options]"); g_writeln(" --help: show help"); g_writeln(" --nodaemon: don't fork into background"); g_writeln(" --kill: shut down xrdp"); g_writeln(" --port: tcp listen port"); g_writeln(" --fork: fork on new connection"); g_writeln(""); g_deinit(); g_exit(0); } if (startup_params->version) { g_writeln(""); g_writeln("xrdp: A Remote Desktop Protocol server."); g_writeln("Copyright (C) Jay Sorg 2004-2011"); g_writeln("See http://xrdp.sourceforge.net for more information."); g_writeln("Version %s", XRDP_NG_VERSION_FULL); g_writeln(""); g_deinit(); g_exit(0); } if (g_file_exist(pid_file)) /* xrdp-ng.pid */ { g_writeln("It looks like xrdp is already running,"); g_writeln("if not delete the xrdp-ng.pid file and try again"); g_deinit(); g_exit(0); } if (!no_daemon) { /* make sure containing directory exists */ g_create_path(pid_file); /* make sure we can write to pid file */ fd = g_file_open(pid_file); /* xrdp-ng.pid */ if (fd == -1) { g_writeln("running in daemon mode with no access to pid files, quitting"); g_deinit(); g_exit(0); } if (g_file_write(fd, (unsigned char*) "0", 1) == -1) { g_writeln("running in daemon mode with no access to pid files, quitting"); g_deinit(); g_exit(0); } g_file_close(fd); g_file_delete(pid_file); } if (!no_daemon) { /* start of daemonizing code */ pid = g_fork(); if (pid == -1) { g_writeln("problem forking"); g_deinit(); g_exit(1); } if (0 != pid) { g_writeln("process %d started ok", pid); /* exit, this is the main process */ g_deinit(); g_exit(0); } g_sleep(1000); /* write the pid to file */ pid = g_getpid(); fd = g_file_open(pid_file); /* xrdp-ng.pid */ if (fd == -1) { g_writeln("trying to write process id to xrdp-ng.pid"); g_writeln("problem opening xrdp-ng.pid"); g_writeln("maybe no rights"); } else { g_sprintf(text, "%d", pid); g_file_write(fd, (unsigned char*) text, g_strlen(text)); g_file_close(fd); } g_sleep(1000); g_file_close(0); g_file_close(1); g_file_close(2); g_file_open("/dev/null"); g_file_open("/dev/null"); g_file_open("/dev/null"); /* end of daemonizing code */ } g_threadid = tc_get_threadid(); g_listen = xrdp_listen_create(); g_signal_user_interrupt(xrdp_shutdown); /* SIGINT */ g_signal_kill(xrdp_shutdown); /* SIGKILL */ g_signal_pipe(pipe_sig); /* SIGPIPE */ g_signal_terminate(xrdp_shutdown); /* SIGTERM */ g_signal_child_stop(xrdp_child); /* SIGCHLD */ g_sync_mutex = tc_mutex_create(); g_sync1_mutex = tc_mutex_create(); pid = g_getpid(); g_TermEvent = CreateEvent(NULL, TRUE, FALSE, NULL); g_SyncEvent = CreateEvent(NULL, TRUE, FALSE, NULL); xrdp_listen_main_loop(g_listen); xrdp_listen_delete(g_listen); tc_mutex_delete(g_sync_mutex); tc_mutex_delete(g_sync1_mutex); CloseHandle(g_TermEvent); CloseHandle(g_SyncEvent); /* only main process should delete pid file */ if ((!no_daemon) && (pid == g_getpid())) { /* delete the xrdp-ng.pid file */ g_file_delete(pid_file); } free(startup_params); g_deinit(); return 0; }
enum logReturns DEFAULT_CC log_message(const enum logLevels lvl, const char *msg, ...) { char buff[LOG_BUFFER_SIZE + 31]; /* 19 (datetime) 4 (space+cr+lf+\0) */ va_list ap; int len = 0; enum logReturns rv = LOG_STARTUP_OK; int writereply = 0; time_t now_t; struct tm *now; if (g_staticLogConfig == NULL) { g_writeln("The log reference is NULL - log not initialized properly"); return LOG_ERROR_NO_CFG; } if (0 > g_staticLogConfig->fd && g_staticLogConfig->enable_syslog == 0) { return LOG_ERROR_FILE_NOT_OPEN; } now_t = time(&now_t); now = localtime(&now_t); snprintf(buff, 21, "[%.4d%.2d%.2d-%.2d:%.2d:%.2d] ", now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); internal_log_lvl2str(lvl, buff + 20); va_start(ap, msg); len = vsnprintf(buff + 28, LOG_BUFFER_SIZE, msg, ap); va_end(ap); /* checking for truncated messages */ if (len > LOG_BUFFER_SIZE) { log_message(LOG_LEVEL_WARNING, "next message will be truncated"); } /* forcing the end of message string */ #ifdef _WIN32 buff[len + 28] = '\r'; buff[len + 29] = '\n'; buff[len + 30] = '\0'; #else #ifdef _MACOS buff[len + 28] = '\r'; buff[len + 29] = '\0'; #else buff[len + 28] = '\n'; buff[len + 29] = '\0'; #endif #endif if (g_staticLogConfig->enable_syslog && (lvl <= g_staticLogConfig->syslog_level)) { /* log to syslog*/ /* %s fix compiler warning 'not a string literal' */ syslog(internal_log_xrdp2syslog(lvl), "(%d)(%ld)%s", g_getpid(), tc_get_threadid(), buff + 20); } if (lvl <= g_staticLogConfig->log_level) { /* log to console */ g_printf("%s", buff); /* log to application logfile */ #ifdef LOG_ENABLE_THREAD pthread_mutex_lock(&(g_staticLogConfig->log_lock)); #endif if (g_staticLogConfig->fd > 0) { writereply = g_file_write(g_staticLogConfig->fd, buff, g_strlen(buff)); if (writereply <= 0) { rv = LOG_ERROR_NULL_FILE; } } #ifdef LOG_ENABLE_THREAD pthread_mutex_unlock(&(g_staticLogConfig->log_lock)); #endif } return rv; }
int DEFAULT_CC log_message(struct log_config* l_cfg, const unsigned int lvl, const char* msg, ...) { char buff[LOG_BUFFER_SIZE + 31]; /* 19 (datetime) 4 (space+cr+lf+\0) */ va_list ap; int len = 0; int rv; time_t now_t; struct tm* now; rv = 0; if (0 == l_cfg) { return LOG_ERROR_NO_CFG; } if (0 > l_cfg->fd) { return LOG_ERROR_FILE_NOT_OPEN; } now_t = time(&now_t); now = localtime(&now_t); snprintf(buff, 21, "[%.4d%.2d%.2d-%.2d:%.2d:%.2d] ", (now->tm_year) + 1900, (now->tm_mon) + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); log_lvl2str(lvl, buff + 20); va_start(ap, msg); len = vsnprintf(buff + 28, LOG_BUFFER_SIZE, msg, ap); va_end(ap); /* checking for truncated messages */ if (len > LOG_BUFFER_SIZE) { log_message(l_cfg, LOG_LEVEL_WARNING, "next message will be truncated"); } /* forcing the end of message string */ #ifdef _WIN32 buff[len + 28] = '\r'; buff[len + 29] = '\n'; buff[len + 30] = '\0'; #else #ifdef _MACOS buff[len + 28] = '\r'; buff[len + 29] = '\0'; #else buff[len + 28] = '\n'; buff[len + 29] = '\0'; #endif #endif if (l_cfg->enable_syslog && (lvl <= l_cfg->log_level)) { /* log to syslog */ syslog(log_xrdp2syslog(lvl), buff + 20); } if (lvl <= l_cfg->log_level) { /* log to console */ g_printf((char*)buff); /* log to application logfile */ #ifdef LOG_ENABLE_THREAD pthread_mutex_lock(&(l_cfg->log_lock)); #endif rv = g_file_write(l_cfg->fd, (char*)buff, g_strlen((char*)buff)); #ifdef LOG_ENABLE_THREAD pthread_mutex_unlock(&(l_cfg->log_lock)); #endif } return rv; }
int DEFAULT_CC main(int argc, char** argv) { int fd; int error; int daemon = 1; int pid; char pid_s[8]; if (1 == argc) { /* no options on command line. normal startup */ g_printf("starting sesman...\n"); daemon = 1; } else if ((2 == argc) && ((0 == g_strcasecmp(argv[1], "--nodaemon")) || (0 == g_strcasecmp(argv[1], "-n")) ) ) { /* starts sesman not daemonized */ g_printf("starting sesman in foregroud...\n"); daemon = 0; } else if ((2 == argc) && ((0 == g_strcasecmp(argv[1], "--help")) || (0 == g_strcasecmp(argv[1], "-h")))) { /* help screen */ g_printf("sesman - xrdp session manager\n\n"); g_printf("usage: sesman [command]\n\n"); g_printf("command can be one of the following:\n"); g_printf("-n, --nodaemon starts sesman in foregroun\n"); g_printf("-k, --kill kills running sesman\n"); g_printf("-h, --help shows this help\n"); g_printf("if no command is specified, sesman is started in background"); g_exit(0); } else if ((2 == argc) && ((0 == g_strcasecmp(argv[1], "--kill")) || (0 == g_strcasecmp(argv[1], "-k")))) { /* killing running sesman */ /* check if sesman is running */ if (!g_file_exist(SESMAN_PID_FILE)) { g_printf("sesman is not running (pid file not found - %s)\n", SESMAN_PID_FILE); g_exit(1); } fd = g_file_open(SESMAN_PID_FILE); if (-1 == fd) { g_printf("error opening pid file: %s\n", g_get_strerror()); return 1; } error = g_file_read(fd, pid_s, 7); if (-1 == error) { g_printf("error reading pid file: %s\n", g_get_strerror()); g_file_close(fd); g_exit(error); } g_file_close(fd); pid = g_atoi(pid_s); error = g_sigterm(pid); if (0 != error) { g_printf("error killing sesman: %s\n", g_get_strerror()); } else { g_file_delete(SESMAN_PID_FILE); } g_exit(error); } else { /* there's something strange on the command line */ g_printf("sesman - xrdp session manager\n\n"); g_printf("error: invalid command line\n"); g_printf("usage: sesman [ --nodaemon | --kill | --help ]\n"); g_exit(1); } if (g_file_exist(SESMAN_PID_FILE)) { g_printf("sesman is already running.\n"); g_printf("if it's not running, try removing "); g_printf(SESMAN_PID_FILE); g_printf("\n"); g_exit(1); } /* reading config */ g_cfg = g_malloc(sizeof(struct config_sesman), 1); if (0 == g_cfg) { g_printf("error creating config: quitting.\n"); g_exit(1); } g_cfg->log.fd = -1; /* don't use logging before reading its config */ if (0 != config_read(g_cfg)) { g_printf("error reading config: %s\nquitting.\n", g_get_strerror()); g_exit(1); } /* starting logging subsystem */ error = log_start(&(g_cfg->log)); if (error != LOG_STARTUP_OK) { switch (error) { case LOG_ERROR_MALLOC: g_printf("error on malloc. cannot start logging. quitting.\n"); break; case LOG_ERROR_FILE_OPEN: g_printf("error opening log file. quitting.\n"); break; } g_exit(1); } /* libscp initialization */ scp_init(&(g_cfg->log)); if (daemon) { /* start of daemonizing code */ g_pid = g_fork(); if (0 != g_pid) { g_exit(0); } g_file_close(0); g_file_close(1); g_file_close(2); g_file_open("/dev/null"); g_file_open("/dev/null"); g_file_open("/dev/null"); } /* initializing locks */ lock_init(); /* signal handling */ g_pid = g_getpid(); /* old style signal handling is now managed synchronously by a * separate thread. uncomment this block if you need old style * signal handling and comment out thread_sighandler_start() */ /* g_signal(1, sig_sesman_reload_cfg); / * SIGHUP * / g_signal(2, sig_sesman_shutdown); / * SIGINT * / g_signal(9, sig_sesman_shutdown); / * SIGKILL * / g_signal(15, sig_sesman_shutdown); / * SIGTERM * / g_signal_child_stop(cterm); / * SIGCHLD * / */ thread_sighandler_start(); /* writing pid file */ fd = g_file_open(SESMAN_PID_FILE); if (-1 == fd) { log_message(&(g_cfg->log), LOG_LEVEL_ERROR, "error opening pid file: %s", g_get_strerror()); log_end(&(g_cfg->log)); g_exit(1); } g_sprintf(pid_s, "%d", g_pid); g_file_write(fd, pid_s, g_strlen(pid_s) + 1); g_file_close(fd); /* start program main loop */ log_message(&(g_cfg->log), LOG_LEVEL_ALWAYS, "starting sesman with pid %d", g_pid); sesman_main_loop(); if (!daemon) { log_end(&(g_cfg->log)); } return 0; }