static void DEFAULT_CC session_start_sessvc(int xpid, int wmpid, long data) { struct list* sessvc_params; char wmpid_str[25]; char xpid_str[25]; char exe_path[262]; int i; /* new style waiting for clients */ g_sprintf(wmpid_str, "%d", wmpid); g_sprintf(xpid_str, "%d", xpid); log_message(&(g_cfg->log), LOG_LEVEL_INFO, "starting xrdp-sessvc - xpid=%s - wmpid=%s", xpid_str, wmpid_str); sessvc_params = list_create(); sessvc_params->auto_free = 1; /* building parameters */ g_snprintf(exe_path, 261, "%s/xrdp-sessvc", XRDP_SBIN_PATH); list_add_item(sessvc_params, (long)g_strdup(exe_path)); list_add_item(sessvc_params, (long)g_strdup(xpid_str)); list_add_item(sessvc_params, (long)g_strdup(wmpid_str)); list_add_item(sessvc_params, 0); /* mandatory */ /* executing sessvc */ g_execvp(exe_path, ((char**)sessvc_params->items)); /* should not get here */ log_message(&(g_cfg->log), LOG_LEVEL_ALWAYS, "error starting xrdp-sessvc - pid %d - xpid=%s - wmpid=%s", g_getpid(), xpid_str, wmpid_str); /* logging parameters */ /* no problem calling strerror for thread safety: other threads are blocked */ log_message(&(g_cfg->log), LOG_LEVEL_DEBUG, "errno: %d, description: %s", errno, g_get_strerror()); log_message(&(g_cfg->log), LOG_LEVEL_DEBUG, "execve parameter list:"); for (i = 0; i < (sessvc_params->count); i++) { log_message(&(g_cfg->log), LOG_LEVEL_DEBUG, " argv[%d] = %s", i, (char*)list_get_item(sessvc_params, i)); } list_delete(sessvc_params); /* keep the old waitpid if some error occurs during execlp */ g_waitpid(wmpid); g_sigterm(xpid); g_sigterm(wmpid); g_sleep(1000); auth_end(data); g_exit(0); }
int DEFAULT_CC xml_send_info(int client, xmlDocPtr doc) { xmlChar* xmlbuff; int buff_size, size; struct stream* s; xmlDocDumpFormatMemory(doc, &xmlbuff, &buff_size, 1); log_message(&(g_cfg->log), LOG_LEVEL_DEBUG_PLUS, "sesman[xml_send_info]: " "data send : %s\n",xmlbuff); make_stream(s); init_stream(s, buff_size + 6); out_uint32_be(s,buff_size); out_uint8p(s, xmlbuff, buff_size) size = s->p - s->data; if (g_tcp_can_send(client, 10)) { int sended = 0; int send = 0; while(sended < size) { send = (size-sended) > 2048 ? 2048 : size-sended; sended += g_tcp_send(client, s->data+sended, send, 0); if (sended < size) { if (g_get_errno() != 0) { log_message(&(g_cfg->log), LOG_LEVEL_DEBUG_PLUS, "sesman[xml_send_info]: " "Error while send %s\n",g_get_strerror()); goto end; } } } if (sended != size) { log_message(&(g_cfg->log), LOG_LEVEL_DEBUG_PLUS, "sesman[xml_send_info]: " "Error while sending data %i != %i\n",sended, size); } } else { log_message(&(g_cfg->log), LOG_LEVEL_DEBUG_PLUS, "sesman[xml_send_info]: " "Unable to send xml response: %s, cause: %s", xmlbuff, strerror(g_get_errno())); } end: free_stream(s); xmlFree(xmlbuff); return buff_size; }
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; }
/* called with the main thread */ static int APP_CC session_start_fork(int width, int height, int bpp, char *username, char *password, tbus data, tui8 type, char *domain, char *program, char *directory, char *client_ip) { int display = 0; int pid = 0; int wmpid = 0; int xpid = 0; int i = 0; char geometry[32]; char depth[32]; char screen[32]; char text[256]; char passwd_file[256]; char **pp1 = (char **)NULL; struct session_chain *temp = (struct session_chain *)NULL; struct list *xserver_params = (struct list *)NULL; time_t ltime; struct tm stime; char execvpparams[2048]; /* initialize (zero out) local variables: */ g_memset(<ime, 0, sizeof(time_t)); g_memset(&stime, 0, sizeof(struct tm)); g_memset(geometry, 0, sizeof(char) * 32); g_memset(depth, 0, sizeof(char) * 32); g_memset(screen, 0, sizeof(char) * 32); g_memset(text, 0, sizeof(char) * 256); g_memset(passwd_file, 0, sizeof(char) * 256); /* check to limit concurrent sessions */ if (g_session_count >= g_cfg->sess.max_sessions) { log_message(LOG_LEVEL_INFO, "max concurrent session limit " "exceeded. login for user %s denied", username); return 0; } temp = (struct session_chain *)g_malloc(sizeof(struct session_chain), 0); if (temp == 0) { log_message(LOG_LEVEL_ERROR, "cannot create new chain " "element - user %s", username); return 0; } temp->item = (struct session_item *)g_malloc(sizeof(struct session_item), 0); if (temp->item == 0) { g_free(temp); log_message(LOG_LEVEL_ERROR, "cannot create new session " "item - user %s", username); return 0; } display = session_get_aval_display_from_chain(); if (display == 0) { g_free(temp->item); g_free(temp); return 0; } pid = g_fork(); if (pid == -1) { } else if (pid == 0) /* child sesman */ { g_tcp_close(g_sck); g_tcp_close(g_thread_sck); auth_start_session(data, display); g_sprintf(geometry, "%dx%d", width, height); g_sprintf(depth, "%d", bpp); g_sprintf(screen, ":%d", display); wmpid = g_fork(); if (wmpid == -1) { } else if (wmpid == 0) /* child (child sesman) xserver */ { wait_for_xserver(display); env_set_user(username, 0, display); if (x_server_running(display)) { auth_set_env(data); if (directory != 0) { if (directory[0] != 0) { g_set_current_dir(directory); } } if (program != 0) { if (program[0] != 0) { g_execlp3(program, program, 0); log_message(LOG_LEVEL_ALWAYS, "error starting program %s for user %s - pid %d", program, username, g_getpid()); } } /* try to execute user window manager if enabled */ if (g_cfg->enable_user_wm) { g_sprintf(text, "%s/%s", g_getenv("HOME"), g_cfg->user_wm); if (g_file_exist(text)) { g_execlp3(text, g_cfg->user_wm, 0); log_message(LOG_LEVEL_ALWAYS, "error starting user " "wm for user %s - pid %d", username, g_getpid()); /* logging parameters */ log_message(LOG_LEVEL_DEBUG, "errno: %d, " "description: %s", errno, g_get_strerror()); log_message(LOG_LEVEL_DEBUG, "execlp3 parameter " "list:"); log_message(LOG_LEVEL_DEBUG, " argv[0] = %s", text); log_message(LOG_LEVEL_DEBUG, " argv[1] = %s", g_cfg->user_wm); } } /* if we're here something happened to g_execlp3 so we try running the default window manager */ g_sprintf(text, "%s/%s", XRDP_CFG_PATH, g_cfg->default_wm); g_execlp3(text, g_cfg->default_wm, 0); log_message( LOG_LEVEL_ALWAYS, "error starting default " "wm for user %s - pid %d", username, g_getpid()); /* logging parameters */ log_message( LOG_LEVEL_DEBUG, "errno: %d, description: " "%s", errno, g_get_strerror()); log_message(LOG_LEVEL_DEBUG, "execlp3 parameter list:"); log_message(LOG_LEVEL_DEBUG, " argv[0] = %s", text); log_message(LOG_LEVEL_DEBUG, " argv[1] = %s", g_cfg->default_wm); /* still a problem starting window manager just start xterm */ g_execlp3("xterm", "xterm", 0); /* should not get here */ log_message(LOG_LEVEL_ALWAYS, "error starting xterm " "for user %s - pid %d", username, g_getpid()); /* logging parameters */ log_message(LOG_LEVEL_DEBUG, "errno: %d, description: " "%s", errno, g_get_strerror()); } else { log_message(LOG_LEVEL_ERROR, "another Xserver might " "already be active on display %d - see log", display); } log_message(LOG_LEVEL_DEBUG, "aborting connection..."); g_exit(0); } else /* parent (child sesman) */ { xpid = g_fork(); if (xpid == -1) { } else if (xpid == 0) /* child */ { env_set_user(username, passwd_file, display); env_check_password_file(passwd_file, password); if (type == SESMAN_SESSION_TYPE_XVNC) { xserver_params = list_create(); xserver_params->auto_free = 1; /* these are the must have parameters */ list_add_item(xserver_params, (long)g_strdup("Xvnc")); list_add_item(xserver_params, (long)g_strdup(screen)); list_add_item(xserver_params, (long)g_strdup("-geometry")); list_add_item(xserver_params, (long)g_strdup(geometry)); list_add_item(xserver_params, (long)g_strdup("-depth")); list_add_item(xserver_params, (long)g_strdup(depth)); list_add_item(xserver_params, (long)g_strdup("-rfbauth")); list_add_item(xserver_params, (long)g_strdup(passwd_file)); /* additional parameters from sesman.ini file */ //config_read_xserver_params(SESMAN_SESSION_TYPE_XVNC, // xserver_params); list_append_list_strdup(g_cfg->vnc_params, xserver_params, 0); /* make sure it ends with a zero */ list_add_item(xserver_params, 0); pp1 = (char **)xserver_params->items; log_message(LOG_LEVEL_INFO, "Xvnc start:%s", dumpItemsToString(xserver_params, execvpparams, 2048)); g_execvp("Xvnc", pp1); } else if (type == SESMAN_SESSION_TYPE_XRDP) { xserver_params = list_create(); xserver_params->auto_free = 1; /* these are the must have parameters */ list_add_item(xserver_params, (long)g_strdup("X11rdp")); list_add_item(xserver_params, (long)g_strdup(screen)); list_add_item(xserver_params, (long)g_strdup("-geometry")); list_add_item(xserver_params, (long)g_strdup(geometry)); list_add_item(xserver_params, (long)g_strdup("-depth")); list_add_item(xserver_params, (long)g_strdup(depth)); /* additional parameters from sesman.ini file */ //config_read_xserver_params(SESMAN_SESSION_TYPE_XRDP, // xserver_params); list_append_list_strdup(g_cfg->rdp_params, xserver_params, 0); /* make sure it ends with a zero */ list_add_item(xserver_params, 0); pp1 = (char **)xserver_params->items; log_message(LOG_LEVEL_INFO, "X11rdp start:%s", dumpItemsToString(xserver_params, execvpparams, 2048)); g_execvp("X11rdp", pp1); } else { log_message(LOG_LEVEL_ALWAYS, "bad session type - " "user %s - pid %d", username, g_getpid()); g_exit(1); } /* should not get here */ log_message(LOG_LEVEL_ALWAYS, "error starting X server " "- user %s - pid %d", username, g_getpid()); /* logging parameters */ log_message(LOG_LEVEL_DEBUG, "errno: %d, description: " "%s", errno, g_get_strerror()); log_message(LOG_LEVEL_DEBUG, "execve parameter list size: " "%d", (xserver_params)->count); for (i = 0; i < (xserver_params->count); i++) { log_message(LOG_LEVEL_DEBUG, " argv[%d] = %s", i, (char *)list_get_item(xserver_params, i)); } list_delete(xserver_params); g_exit(1); } else /* parent (child sesman)*/ { wait_for_xserver(display); g_snprintf(text, 255, "%d", display); g_setenv("XRDP_SESSVC_DISPLAY", text, 1); g_snprintf(text, 255, ":%d.0", display); g_setenv("DISPLAY", text, 1); /* new style waiting for clients */ session_start_sessvc(xpid, wmpid, data, username, display); } } } else /* parent sesman process */ { temp->item->pid = pid; temp->item->display = display; temp->item->width = width; temp->item->height = height; temp->item->bpp = bpp; temp->item->data = data; g_strncpy(temp->item->client_ip, client_ip, 255); /* store client ip data */ g_strncpy(temp->item->name, username, 255); ltime = g_time1(); localtime_r(<ime, &stime); temp->item->connect_time.year = (tui16)(stime.tm_year + 1900); temp->item->connect_time.month = (tui8)stime.tm_mon; temp->item->connect_time.day = (tui8)stime.tm_mday; temp->item->connect_time.hour = (tui8)stime.tm_hour; temp->item->connect_time.minute = (tui8)stime.tm_min; zero_time(&(temp->item->disconnect_time)); zero_time(&(temp->item->idle_time)); temp->item->type = type; temp->item->status = SESMAN_SESSION_STATUS_ACTIVE; temp->next = g_sessions; g_sessions = temp; g_session_count++; } return display; }
/* its the responsibility of the caller to free passwd_file */ int env_set_user(const char *username, char **passwd_file, int display, const struct list *env_names, const struct list *env_values) { int error; int pw_uid; int pw_gid; int uid; int index; int len; char *name; char *value; char *pw_shell; char *pw_dir; char text[256]; char hostname[256]; pw_shell = 0; pw_dir = 0; error = g_getuser_info(username, &pw_gid, &pw_uid, &pw_shell, &pw_dir, 0); if (error == 0) { g_rm_temp_dir(); error = g_setgid(pw_gid); if (error == 0) { error = g_initgroups(username, pw_gid); } if (error == 0) { uid = pw_uid; error = g_setuid(uid); } g_mk_socket_path(0); if (error == 0) { g_clearenv(); g_setenv("SHELL", pw_shell, 1); g_setenv("PATH", "/sbin:/bin:/usr/bin:/usr/local/bin", 1); g_setenv("USER", username, 1); g_setenv("LOGNAME", username, 1); g_sprintf(text, "%d", uid); g_setenv("UID", text, 1); g_setenv("HOME", pw_dir, 1); g_set_current_dir(pw_dir); g_sprintf(text, ":%d.0", display); g_setenv("DISPLAY", text, 1); g_setenv("XRDP_SESSION", "1", 1); /* XRDP_SOCKET_PATH should be set even here, chansrv uses this */ g_setenv("XRDP_SOCKET_PATH", XRDP_SOCKET_PATH, 1); /* pulse sink socket */ g_snprintf(text, sizeof(text) - 1, CHANSRV_PORT_OUT_BASE_STR, display); g_setenv("XRDP_PULSE_SINK_SOCKET", text, 1); /* pulse source socket */ g_snprintf(text, sizeof(text) - 1, CHANSRV_PORT_IN_BASE_STR, display); g_setenv("XRDP_PULSE_SOURCE_SOCKET", text, 1); if ((env_names != 0) && (env_values != 0) && (env_names->count == env_values->count)) { for (index = 0; index < env_names->count; index++) { name = (char *) list_get_item(env_names, index), value = (char *) list_get_item(env_values, index), g_setenv(name, value, 1); } } g_gethostname(hostname, 255); hostname[255] = 0; if (passwd_file != 0) { if (0 == g_cfg->auth_file_path) { /* if no auth_file_path is set, then we go for $HOME/.vnc/sesman_passwd-USERNAME@HOSTNAME:DISPLAY */ if (!g_directory_exist(".vnc")) { if (g_mkdir(".vnc") < 0) { log_message(LOG_LEVEL_ERROR, "Error creating .vnc directory: %s", g_get_strerror()); } } len = g_snprintf(NULL, 0, "%s/.vnc/sesman_passwd-%s@%s:%d", pw_dir, username, hostname, display); *passwd_file = (char *) g_malloc(len + 1, 1); if (*passwd_file != NULL) { /* Try legacy names first, remove if found */ g_sprintf(*passwd_file, "%s/.vnc/sesman_%s_passwd:%d", pw_dir, username, display); if (g_file_exist(*passwd_file)) { log_message(LOG_LEVEL_WARNING, "Removing old " "password file %s", *passwd_file); g_file_delete(*passwd_file); } g_sprintf(*passwd_file, "%s/.vnc/sesman_%s_passwd", pw_dir, username); if (g_file_exist(*passwd_file)) { log_message(LOG_LEVEL_WARNING, "Removing insecure " "password file %s", *passwd_file); g_file_delete(*passwd_file); } g_sprintf(*passwd_file, "%s/.vnc/sesman_passwd-%s@%s:%d", pw_dir, username, hostname, display); } } else { /* we use auth_file_path as requested */ len = g_snprintf(NULL, 0, g_cfg->auth_file_path, username); *passwd_file = (char *) g_malloc(len + 1, 1); if (*passwd_file != NULL) { g_sprintf(*passwd_file, g_cfg->auth_file_path, username); } } if (*passwd_file != NULL) { LOG_DBG("pass file: %s", *passwd_file); } } g_free(pw_dir); g_free(pw_shell); } } else { log_message(LOG_LEVEL_ERROR, "error getting user info for user %s", username); } return error; }
int DEFAULT_CC main(int argc, char** argv) { int fd; int error; int daemon = 1; int pid; char pid_s[8]; char text[256]; if(g_is_root() != 0){ g_printf("Error, xrdp-sesman service must be start with root privilege\n"); return 0; } 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..."); daemon = 1; } else if ((2 == argc) && ((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 foregroud..."); 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, -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_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(pid_file)) { g_printf("sesman is not running (pid file not found - %s)\n", pid_file); 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; } 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(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(pid_file)) { g_printf("sesman is already running.\n"); g_printf("if it's not running, try removing "); g_printf(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 [%s]. quitting.\n", g_cfg->log.log_file); break; } g_exit(1); } /* libscp initialization */ scp_init(&(g_cfg->log)); if (daemon) { /* start of daemonizing code */ if (g_daemonize(pid_file) == 0) { g_writeln("problem daemonize"); g_exit(1); } } /* 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() * 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_kill(sig_sesman_shutdown); /* SIGKILL */ g_signal_terminate(sig_sesman_shutdown); /* SIGTERM */ // g_signal_child_stop(sig_sesman_session_end); /* SIGCHLD */ #endif #if 0 thread_sighandler_start(); #endif /* start program main loop */ log_message(&(g_cfg->log), LOG_LEVEL_INFO, "starting sesman with pid %d", g_pid); /* make sure the /tmp/.X11-unix directory exist */ if (!g_directory_exist("/tmp/.X11-unix")) { g_create_dir("/tmp/.X11-unix"); g_chmod_hex("/tmp/.X11-unix", 0x1777); } if (!g_directory_exist(XRDP_SOCKET_PATH)) { g_create_dir(XRDP_SOCKET_PATH); g_chmod_hex(XRDP_SOCKET_PATH, 0x1777); } g_snprintf(text, 255, "xrdp_sesman_%8.8x_main_term", g_pid); g_term_event = g_create_wait_obj(text); g_snprintf(text, 255, "xrdp_sesman_%8.8x_main_sync", g_pid); g_sync_event = g_create_wait_obj(text); scp_init_mutex(); tc_thread_create(admin_thread, 0); tc_thread_create(monit_thread, 0); sesman_main_loop(); scp_remove_mutex(); g_delete_wait_obj(g_term_event); g_delete_wait_obj(g_sync_event); if (!daemon) { log_end(&(g_cfg->log)); } return 0; }
/** * * @brief Starts sesman main loop * */ static void DEFAULT_CC sesman_main_loop(void) { int in_sck; int error; int robjs_count; int cont; tbus sck_obj; tbus robjs[8]; /*main program loop*/ log_message(&(g_cfg->log), LOG_LEVEL_INFO, "sesman[sesman_main_loop]: " "listening..."); g_sck = g_tcp_socket(); g_tcp_set_non_blocking(g_sck); error = scp_tcp_bind(g_sck, g_cfg->listen_address, g_cfg->listen_port); if (error == 0) { error = g_tcp_listen(g_sck); if (error == 0) { sck_obj = g_create_wait_obj_from_socket(g_sck, 0); cont = 1; while (cont) { /* build the wait obj list */ robjs_count = 0; robjs[robjs_count++] = sck_obj; robjs[robjs_count++] = g_term_event; robjs[robjs_count++] = g_sync_event; /* wait */ if (g_obj_wait(robjs, robjs_count, 0, 0, -1) != 0) { /* error, should not get here */ g_sleep(100); } if (g_is_wait_obj_set(g_term_event)) /* term */ { break; } if (g_is_wait_obj_set(g_sync_event)) /* sync */ { g_reset_wait_obj(g_sync_event); session_sync_start(); } if (g_is_wait_obj_set(sck_obj)) /* incomming connection */ { in_sck = g_tcp_accept(g_sck); if ((in_sck == -1) && g_tcp_last_error_would_block(g_sck)) { /* should not get here */ g_sleep(100); } else if (in_sck == -1) { /* error, should not get here */ break; } else { /* we've got a connection, so we pass it to scp code */ log_message(&(g_cfg->log), LOG_LEVEL_INFO, "sesman[sesman_main_loop]: " "new connection"); thread_scp_start(in_sck); /* todo, do we have to wait here ? */ } } } g_delete_wait_obj_from_socket(sck_obj); } else { log_message(&(g_cfg->log), LOG_LEVEL_ERROR, "sesman[sesman_main_loop]: " "listen error %d (%s)", g_get_errno(), g_get_strerror()); } } else { log_message(&(g_cfg->log), LOG_LEVEL_ERROR, "bind error on " "port '%s': %d (%s)", g_cfg->listen_port, g_get_errno(), g_get_strerror()); } g_tcp_close(g_sck); }
/** * * @brief Starts sesman main loop * */ static void DEFAULT_CC sesman_main_loop(void) { int in_sck; int error; int robjs_count; int cont; tbus sck_obj; tbus robjs[8]; g_sck = g_tcp_socket(); if (g_sck < 0) { log_message(LOG_LEVEL_ERROR, "error opening socket, g_tcp_socket() failed..."); return; } g_tcp_set_non_blocking(g_sck); error = scp_tcp_bind(g_sck, g_cfg->listen_address, g_cfg->listen_port); if (error == 0) { error = g_tcp_listen(g_sck); if (error == 0) { log_message(LOG_LEVEL_INFO, "listening to port %s on %s", g_cfg->listen_port, g_cfg->listen_address); sck_obj = g_create_wait_obj_from_socket(g_sck, 0); cont = 1; while (cont) { /* build the wait obj list */ robjs_count = 0; robjs[robjs_count++] = sck_obj; robjs[robjs_count++] = g_term_event; /* wait */ if (g_obj_wait(robjs, robjs_count, 0, 0, -1) != 0) { /* error, should not get here */ g_sleep(100); } if (g_is_wait_obj_set(g_term_event)) /* term */ { break; } if (g_is_wait_obj_set(sck_obj)) /* incoming connection */ { in_sck = g_tcp_accept(g_sck); if ((in_sck == -1) && g_tcp_last_error_would_block(g_sck)) { /* should not get here */ g_sleep(100); } else if (in_sck == -1) { /* error, should not get here */ break; } else { /* we've got a connection, so we pass it to scp code */ LOG_DBG("new connection"); scp_process_start((void*)(tintptr)in_sck); g_sck_close(in_sck); } } } g_delete_wait_obj_from_socket(sck_obj); } else { log_message(LOG_LEVEL_ERROR, "listen error %d (%s)", g_get_errno(), g_get_strerror()); } } else { log_message(LOG_LEVEL_ERROR, "bind error on " "port '%s': %d (%s)", g_cfg->listen_port, g_get_errno(), g_get_strerror()); } g_tcp_close(g_sck); }
/* its the responsibility of the caller to free passwd_file */ int DEFAULT_CC env_set_user(const char *username, char **passwd_file, int display, const struct list *env_names, const struct list *env_values) { int error; int pw_uid; int pw_gid; int uid; int index; int len; char *name; char *value; char *pw_shell; char *pw_dir; char text[256]; pw_shell = 0; pw_dir = 0; error = g_getuser_info(username, &pw_gid, &pw_uid, &pw_shell, &pw_dir, 0); if (error == 0) { g_rm_temp_dir(); error = g_setgid(pw_gid); if (error == 0) { error = g_initgroups(username, pw_gid); } if (error == 0) { uid = pw_uid; error = g_setuid(uid); } g_mk_temp_dir(0); if (error == 0) { g_clearenv(); g_setenv("SHELL", pw_shell, 1); g_setenv("PATH", "/sbin:/bin:/usr/bin:/usr/local/bin", 1); g_setenv("USER", username, 1); g_sprintf(text, "%d", uid); g_setenv("UID", text, 1); g_setenv("HOME", pw_dir, 1); g_set_current_dir(pw_dir); g_sprintf(text, ":%d.0", display); g_setenv("DISPLAY", text, 1); g_setenv("XRDP_SESSION", "1", 1); if ((env_names != 0) && (env_values != 0) && (env_names->count == env_values->count)) { for (index = 0; index < env_names->count; index++) { name = (char *) list_get_item(env_names, index), value = (char *) list_get_item(env_values, index), g_setenv(name, value, 1); } } if (passwd_file != 0) { if (0 == g_cfg->auth_file_path) { /* if no auth_file_path is set, then we go for $HOME/.vnc/sesman_username_passwd:DISPLAY */ if (!g_directory_exist(".vnc")) { if (g_mkdir(".vnc") < 0) { log_message(LOG_LEVEL_ERROR, "Error creating .vnc directory: %s", g_get_strerror()); } } len = g_snprintf(NULL, 0, "%s/.vnc/sesman_%s_passwd:%d", pw_dir, username, display); *passwd_file = (char *) g_malloc(len + 1, 1); if (*passwd_file != NULL) { /* Try legacy name first, remove if found */ g_sprintf(*passwd_file, "%s/.vnc/sesman_%s_passwd", pw_dir, username); if (g_file_exist(*passwd_file)) { log_message(LOG_LEVEL_WARNING, "Removing insecure " "password file %s", *passwd_file); g_file_delete(*passwd_file); } g_sprintf(*passwd_file, "%s/.vnc/sesman_%s_passwd:%d", pw_dir, username, display); } } else { /* we use auth_file_path as requested */ len = g_snprintf(NULL, 0, g_cfg->auth_file_path, username); *passwd_file = (char *) g_malloc(len + 1, 1); if (*passwd_file != NULL) { g_sprintf(*passwd_file, g_cfg->auth_file_path, username); } } if (*passwd_file != NULL) { LOG_DBG("pass file: %s", *passwd_file); } } g_free(pw_dir); g_free(pw_shell); } } else { log_message(LOG_LEVEL_ERROR, "error getting user info for user %s", username); } return error; }
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; }
int DEFAULT_CC main(int argc, char** argv) { int fd = 0; int error = 0; int daemon = 1; int pid = 0; char pid_s[8] = {0}; if(g_is_root() != 0) { g_printf("Error, xrdp-printerd service must be start with root privilege\n"); return 0; } g_snprintf(pid_file, 255, "%s/xrdp-printerd.pid", XRDP_PID_PATH); if (1 == argc) { /* no options on command line. normal startup */ g_printf("starting xrdp-printerd..."); daemon = 1; } else if ((2 == argc) && ((0 == g_strcasecmp(argv[1], "--nodaemon")) || (0 == g_strcasecmp(argv[1], "-n")) || (0 == g_strcasecmp(argv[1], "-ns")))) { /* starts xrdp-printerd not daemonized */ g_printf("starting xrdp-printerd in foregroud..."); daemon = 0; } else if ((2 == argc) && ((0 == g_strcasecmp(argv[1], "--help")) || (0 == g_strcasecmp(argv[1], "-h")))) { /* help screen */ g_printf("xrdp-printerd - xrdp printer manager\n\n"); g_printf("usage: xrdp-printerd [command]\n\n"); g_printf("command can be one of the following:\n"); g_printf("-n, -ns, --nodaemon starts xrdp-printerd in foreground\n"); g_printf("-k, --kill kills running xrdp-printerd\n"); g_printf("-h, --help shows this help\n"); g_printf("if no command is specified, xrdp-printerd 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 xrdp-printerd */ /* check if xrdp-printerd is running */ if (!g_file_exist(pid_file)) { g_printf("xrdp-printerd is not running (pid file not found - %s)\n", pid_file); 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; } 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 xrdp-printerd: %s\n", g_get_strerror()); } else { g_file_delete(pid_file); } g_exit(error); } else { /* there's something strange on the command line */ g_printf("xrdp-printerd - xrdp printer manager\n\n"); g_printf("error: invalid command line\n"); g_printf("usage: xrdp-printerd [ --nodaemon | --kill | --help ]\n"); g_exit(1); } if (g_file_exist(pid_file)) { g_printf("xrdp-printerd is already running.\n"); g_printf("if it's not running, try removing "); g_printf(pid_file); g_printf("\n"); g_exit(1); } if (printerd_init() != 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 [%s]. quitting.\n", l_config->log_file); break; } g_exit(1); } if (daemon) { /* start of daemonizing code */ if (g_daemonize(pid_file) == 0) { g_writeln("problem daemonize"); g_exit(1); } } /* signal handling */ g_pid = g_getpid(); g_signal_user_interrupt(sig_printerd_shutdown); /* SIGINT */ g_signal_kill(sig_printerd_shutdown); /* SIGKILL */ g_signal_terminate(sig_printerd_shutdown); /* SIGTERM */ g_signal_pipe(sig_printerd_pipe); /* SIGPIPE */ /* start program main loop */ log_message(l_config, LOG_LEVEL_INFO, "starting xrdp-printerd with pid %d", g_pid); if (!g_directory_exist(XRDP_SOCKET_PATH)) { g_create_dir(XRDP_SOCKET_PATH); g_chmod_hex(XRDP_SOCKET_PATH, 0x1777); } printerd_main_loop(); log_end(l_config); return 0; }