📄 cms_srv.cc
字号:
/********************************************************************* Description: cms_srv.cc* Server to read and write to a local CMS buffer for remote processes.** Derived from a work by Fred Proctor & Will Shackleford** Author:* License: LGPL Version 2* System: Linux* * Copyright (c) 2004 All rights reserved.** Last change: * $Revision: 1.6 $* $Author: paul_c $* $Date: 2005/06/13 14:38:49 $********************************************************************/#ifdef __cplusplusextern "C" {#endif#include <stdio.h> /* sscanf(),NULL, FILE, fopen(), fgets() */#include <string.h> /* strchr(), memcpy() */#include <stdlib.h> /* malloc(), free(), exit() */#include <ctype.h> // isgraph()#include <math.h> /* fmod() */#include <sys/types.h>#include <unistd.h> /* getpid() */#include <sys/wait.h> /* waitpid() */#include <signal.h> /* sigvec(), struct sigvec, SIGINT, kill() */#ifdef __cplusplus}#endif#include "cms.hh" /* class CMS */#include "rem_msg.hh" /* struct REMOTE_READ_REQUEST, */ /* struct REMOTE_WRITE_REQUEST, */#include "cms_srv.hh" /* class CMS_SERVER */#include "cms_cfg.hh" /* cms_config() */#include "rcs_print.hh" /* rcs_print_error() */#ifndef NO_DCE_RPC#define NO_DCE_RPC#endif#include "tcp_srv.hh" /* CMS_SERVER_TCP_PORT */#include "timer.hh" // etime()#include "cmsdiag.hh"int cms_server_count = 0;int cms_server_task_priority = 100;int cms_server_task_stack_size = 32768;void wait_for_servers(int count_to_waitfor){ do { esleep(0.1); } while (cms_server_count < count_to_waitfor);}class CMS_USER_INFO { public: CMS_USER_INFO(); private: char passwd[16]; char epasswd[16]; char name[16]; char passwd_file_line[256]; char key1[8]; char key2[8]; int has_passwd; int user_number; int allow_read; int allow_write; friend class CMS_SERVER;};CMS_USER_INFO::CMS_USER_INFO(){ memset(passwd, 0, 16); memset(epasswd, 0, 16); memset(name, 0, 16); memset(passwd_file_line, 0, 256); memset(key1, 0, 8); memset(key2, 0, 8); has_passwd = 0; user_number = 0; allow_read = 0; allow_write = 0;}CMS_USER_CONNECT_STRUCT::CMS_USER_CONNECT_STRUCT(){ user_info = NULL; fd = -1;}/* CMS_SERVER Global Variables */LinkedList *cms_server_list = NULL;CMS_SERVER_LOCAL_PORT::CMS_SERVER_LOCAL_PORT(CMS * _cms){ local_channel_reused = 1; cms = _cms; orig_info = NULL; if (NULL != cms) { buffer_number = cms->buffer_number; } else { buffer_number = 0; } list_id = 0;};CMS_SERVER_LOCAL_PORT::~CMS_SERVER_LOCAL_PORT(){ if (NULL != orig_info) { delete orig_info; orig_info = NULL; }}/* local_port function for reads */REMOTE_READ_REPLY *CMS_SERVER_LOCAL_PORT::reader(REMOTE_READ_REQUEST * _req){ return (NULL);}REMOTE_READ_REPLY *CMS_SERVER_LOCAL_PORT::blocking_read(REMOTE_READ_REQUEST * _req){ return (NULL);}/* local_port function for writes */REMOTE_WRITE_REPLY *CMS_SERVER_LOCAL_PORT::writer(REMOTE_WRITE_REQUEST * _req){ return (NULL);}REMOTE_SET_DIAG_INFO_REPLY *CMS_SERVER_LOCAL_PORT::set_diag_info(REMOTE_SET_DIAG_INFO_REQUEST * _req){ return (NULL);}REMOTE_GET_DIAG_INFO_REPLY *CMS_SERVER_LOCAL_PORT::get_diag_info(REMOTE_GET_DIAG_INFO_REQUEST * _req){ get_diag_info_reply.cdi = cms->get_diagnostics_info(); get_diag_info_reply.status = cms->status; return (&get_diag_info_reply);}REMOTE_GET_MSG_COUNT_REPLY *CMS_SERVER_LOCAL_PORT::get_msg_count(REMOTE_GET_DIAG_INFO_REQUEST * _req){ return (NULL);}void CMS_SERVER_LOCAL_PORT::reset_diag_info(){}CMS_SERVER_REMOTE_PORT::CMS_SERVER_REMOTE_PORT(CMS_SERVER * _cms_server_parent){ current_clients = 0; max_clients = 0; port_registered = 0; cms_server_parent = _cms_server_parent; connected_users = NULL; confirm_write = 0; min_compatible_version = 0.0; current_user_info = NULL; running = 0; max_total_subdivisions = _cms_server_parent->max_total_subdivisions;}CMS_SERVER_REMOTE_PORT::~CMS_SERVER_REMOTE_PORT(){ if (NULL != connected_users) { CMS_USER_CONNECT_STRUCT *connected_user_struct = (CMS_USER_CONNECT_STRUCT *) connected_users->get_head(); while (NULL != connected_user_struct) { delete connected_user_struct; connected_user_struct = NULL; connected_users->delete_current_node(); connected_user_struct = (CMS_USER_CONNECT_STRUCT *) connected_users->get_next(); } delete connected_users; } current_connected_user_struct = NULL;}void CMS_SERVER_REMOTE_PORT::add_connected_user(int _fd){ current_connected_user_struct = NULL; rcs_print_debug(PRINT_SOCKET_CONNECT, "Adding connected user %d\n", _fd); CMS_USER_CONNECT_STRUCT *connected_user_struct = new CMS_USER_CONNECT_STRUCT(); if (NULL == connected_user_struct) { return; } connected_user_struct->fd = _fd; if (NULL == connected_users) { connected_users = new LinkedList(); } if (NULL == connected_users) { return; } connected_users->store_at_tail(connected_user_struct, sizeof(connected_user_struct), 0); current_connected_user_struct = connected_user_struct; // delete connected_user_struct;}CMS_USER_INFO *CMS_SERVER_REMOTE_PORT::get_connected_user(int _fd){ current_connected_user_struct = NULL; if (NULL == connected_users) { connected_users = new LinkedList(); } if (NULL == connected_users) { return NULL; } CMS_USER_CONNECT_STRUCT *connected_user_struct = (CMS_USER_CONNECT_STRUCT *) connected_users->get_head(); while (NULL != connected_user_struct) { if (connected_user_struct->fd == _fd) { current_connected_user_struct = connected_user_struct; return connected_user_struct->user_info; } connected_user_struct = (CMS_USER_CONNECT_STRUCT *) connected_users->get_next(); } add_connected_user(_fd); return NULL;}void CMS_SERVER_REMOTE_PORT::register_port(){ return;}void CMS_SERVER_REMOTE_PORT::unregister_port(){ return;}int CMS_SERVER_REMOTE_PORT::accept_local_port_cms(CMS * _cms){ if (NULL != _cms) { if (min_compatible_version < 1e-6 || (min_compatible_version > _cms->min_compatible_version && _cms->min_compatible_version > 1e-6)) { min_compatible_version = _cms->min_compatible_version; } } if (_cms->total_subdivisions > max_total_subdivisions) { max_total_subdivisions = _cms->total_subdivisions; } return 1;}CMS_SERVER *CMS_SERVER_REMOTE_PORT::find_server(long _pid, long _tid /* =0 */ ){ CMS_SERVER *cms_server; if (NULL == cms_server_list) { return NULL; } cms_server = (CMS_SERVER *) cms_server_list->get_head(); while (NULL != cms_server) { if (cms_server->server_pid == _pid && cms_server->server_tid == _tid) { break; } cms_server = (CMS_SERVER *) cms_server_list->get_next(); } return (cms_server);}void CMS_SERVER_REMOTE_PORT::print_servers(){ CMS_SERVER *cms_server; if (NULL == cms_server_list) { rcs_print("cms_server_list is NULL.\n"); return; } cms_server = (CMS_SERVER *) cms_server_list->get_head(); rcs_print("Server Tasks for this remote port.\n"); while (NULL != cms_server) { rcs_print(" \t(%d (0x%X), %d (0x%X))\n", cms_server->server_pid, cms_server->server_pid, cms_server->server_tid, cms_server->server_tid); cms_server = (CMS_SERVER *) cms_server_list->get_next(); }}/*! \todo Another #if 0 */#if 0void CMS_SERVER::read_passwd_file(){ using_passwd_file = 1; int user_name_length; int passwd_length; if (NULL == known_users) { known_users = new LinkedList(); } srand((int) ((2 ^ 32) * etime())); char buf[256]; INET_FILE *ifp = NULL; rcs_print("Reading passwd file %s.\n", passwd_file); ifp = inet_file_open(passwd_file, "r"); if (NULL == ifp) { rcs_print_error("Can not open passwd file %s.\n", passwd_file); return; } CMS_USER_INFO *user_info = NULL; while (!inet_file_eof(ifp)) { memset(buf, 0, 256); inet_file_gets(buf, 256, ifp); user_name_length = strcspn(buf, "\n\r \t:"); if (user_name_length > 16) { rcs_print_error("CMS_SERVER: user name is too long.\n"); continue; } if (user_name_length < 2) { continue; } user_info = new CMS_USER_INFO(); if (NULL == user_info) { break; } strcpy(user_info->passwd_file_line, buf); memcpy(user_info->name, buf, user_name_length); passwd_length = strcspn(buf + user_name_length + 1, "\n\r \t:"); if (passwd_length > 16) { rcs_print_error("CMS_SERVER: password is too long.\n"); continue; } if (passwd_length > 16) { rcs_print_error("CMS_SERVER: password is too long.\n"); } if (passwd_length > 2) { memcpy(user_info->passwd, buf + user_name_length + 1, passwd_length); memcpy(user_info->key1, buf + user_name_length + 1, 2); user_info->has_passwd = 1; } else { user_info->has_passwd = 0; } gen_random_key(user_info->key2, 2); strcpy(user_info->epasswd, crypt(user_info->passwd, user_info->key2)); user_info->allow_read = (NULL != strstr(buf, "read=true")); user_info->allow_write = (NULL != strstr(buf, "write=true")); user_info->user_number = known_users->store_at_tail(user_info, sizeof(user_info), 0); rcs_print("Storing info for user (%s).\n", user_info->name); // delete user_info; if (!strcmp(user_info->name, "guest")) { guest_can_read = user_info->allow_read; guest_can_write = user_info->allow_write; } user_info = NULL; }}#endifvoid CMS_SERVER::gen_random_key(char key[], int len){ for (int i = 0; i < len; i++) { while (!isgraph(key[i]) || !key[i]) { key[i] = (char) ((rand() % 128)); } }}CMS_USER_INFO *CMS_SERVER::find_user(const char *name){ if (NULL == known_users) { return NULL; } CMS_USER_INFO *user_info = (CMS_USER_INFO *) known_users->get_head(); while (NULL != user_info) { rcs_print("CMS_SERVER::find_user: strcmp(%s,%s)\n", name, user_info->name); if (!strcmp(name, user_info->name)) { return user_info; } user_info = (CMS_USER_INFO *) known_users->get_next(); } rcs_print_error("CMS_SERVER: Can't find entry for user %s.\n", name); return NULL;}/*! \todo Another #if 0 */#if 0int CMS_SERVER::get_user_keys(const char *name, char *key1, char *key2){ if (NULL == known_users) { gen_random_key(key1, 2); gen_random_key(key2, 2); return -1; } CMS_USER_INFO *user_info = find_user(name); if (NULL == user_info) { gen_random_key(key1, 2); gen_random_key(key2, 2); return -1; } strcpy(key1, user_info->key1); if (fabs(etime() - time_of_last_key_request) > 30.0) { memset(user_info->key2, 0, 8); memset(user_info->epasswd, 0, 16); gen_random_key(user_info->key2, 2); strcpy(user_info->epasswd, crypt(user_info->passwd, user_info->key2)); } strcpy(key2, user_info->key2); time_of_last_key_request = etime(); return 0;}#endifCMS_USER_INFO *CMS_SERVER::get_user_info(const char *name, const char *epasswd){ if (NULL == known_users) { return NULL; } CMS_USER_INFO *user_info = find_user(name); if (NULL == user_info) { return NULL; } if (!strcmp(user_info->epasswd, epasswd) || !user_info->has_passwd) { return user_info; } rcs_print_error("CMS_SERVER: %s gave the wrong passwd.\n", name); rcs_print_error("CMS_SERVER: user_info->passwd = %s\n", user_info->passwd); rcs_print_error("CMS_SERVER: user_info->epasswd = %s\n", user_info->epasswd); rcs_print_error("CMS_SERVER: epasswd = %s\n", epasswd); return NULL;}void CMS_SERVER::add_local_port(CMS_SERVER_LOCAL_PORT * _local_port){ if (NULL == _local_port) { rcs_print_error("CMS_SERVER: Attempt to add NULL local port.\n"); return; } if (NULL == _local_port->cms) { rcs_print_error ("CMS_SERVER: Attempt to add local port with NULL cms object.\n"); return; } if (NULL == cms_local_ports) { rcs_print_error ("CMS_SERVER: Attempt to add local port when local ports list is NULL.\n"); return; } if (NULL == remote_port) { switch (_local_port->cms->remote_port_type) { case CMS_TCP_REMOTE_PORT_TYPE: remote_port = new CMS_SERVER_REMOTE_TCP_PORT(this); break;/*! \todo Another #if 0 */#if 0 case CMS_STCP_REMOTE_PORT_TYPE: remote_port = new CMS_SERVER_REMOTE_STCP_PORT(this); break; case CMS_TTY_REMOTE_PORT_TYPE: remote_port = new CMS_SERVER_REMOTE_TTY_PORT(this); break; case CMS_UDP_REMOTE_PORT_TYPE: remote_port = new CMS_SERVER_REMOTE_UDP_PORT(this); break;#endif default: rcs_print_error("CMS_SERVER: Invalid remote port type. (%d)\n", _local_port->cms->remote_port_type); return; } } if (NULL == remote_port) { rcs_print_error("CMS_SERVER: couldn't create remote port object.\n"); return; } if (!accept_local_port_cms(_local_port->cms)) { rcs_print_error ("CMS_SERVER: Attempt to add local port failed because the port was of an incompatible type.\n"); } char *passwd_eq = strstr(_local_port->cms->BufferLine, "passwd="); if (NULL != passwd_eq) { if (!using_passwd_file) { memset(passwd_file, 0, 256); for (int i = 0; i < 256 && passwd_eq[i + 7]; i++) { if (passwd_eq[i + 7] == ' ' || passwd_eq[i + 7] == '\t' || passwd_eq[i + 7] == '\n' || passwd_eq[i + 7] == '\r') { break; } passwd_file[i] = passwd_eq[i + 7]; }/*! \todo Another #if 0 */#if 0 if (strlen(passwd_file) > 0) { read_passwd_file(); }#endif } } _local_port->list_id = cms_local_ports->store_at_tail(_local_port, sizeof(CMS_SERVER_LOCAL_PORT), 0); if (-1 == _local_port->list_id) { rcs_print_error ("CMS_SERVER: Can not store local port on linked list.\n"); }}int CMS_SERVER::accept_local_port_cms(CMS * _cms){ if (NULL == remote_port || NULL == _cms) { return (0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -