|
Lines 69-74
Link Here
|
| 69 |
#include <sys/wait.h> |
69 |
#include <sys/wait.h> |
| 70 |
#include <openssl/ssl.h> |
70 |
#include <openssl/ssl.h> |
| 71 |
#include <tptp/TPTPConfig.h> |
71 |
#include <tptp/TPTPConfig.h> |
|
|
72 |
#include "SSLSupport.h" |
| 72 |
#endif |
73 |
#endif |
| 73 |
|
74 |
|
| 74 |
#include "SocketListener.h" |
75 |
#include "SocketListener.h" |
|
Lines 83-93
Link Here
|
| 83 |
#define DEFAULT_PORT_NUM 10002 |
84 |
#define DEFAULT_PORT_NUM 10002 |
| 84 |
#define PROTO_VERSION 1 |
85 |
#define PROTO_VERSION 1 |
| 85 |
|
86 |
|
| 86 |
#define CONFIGURATION_HOME "TPTP_AC_HOME" |
|
|
| 87 |
|
| 88 |
#define CERTF "cert.pem" |
| 89 |
#define KEYF "key.pem" |
| 90 |
|
| 91 |
/** thread status */ |
87 |
/** thread status */ |
| 92 |
enum ThreadStatus { IDLE, RUNNING } ; |
88 |
enum ThreadStatus { IDLE, RUNNING } ; |
| 93 |
|
89 |
|
|
Lines 119-128
Link Here
|
| 119 |
|
115 |
|
| 120 |
BOOL vrfusrpwd(tptp_string *userid, tptp_string *password); |
116 |
BOOL vrfusrpwd(tptp_string *userid, tptp_string *password); |
| 121 |
|
117 |
|
| 122 |
static int sslinit = 1; |
|
|
| 123 |
static char* certFile = NULL; |
| 124 |
static char* keyFile = NULL; |
| 125 |
|
| 126 |
/** |
118 |
/** |
| 127 |
********************************************************* |
119 |
********************************************************* |
| 128 |
* |
120 |
* |
|
Lines 179-252
Link Here
|
| 179 |
return ( rc ) ; |
171 |
return ( rc ) ; |
| 180 |
} |
172 |
} |
| 181 |
|
173 |
|
| 182 |
#ifndef _WIN32 |
|
|
| 183 |
int setSSL(request_block_ptr_t pBlk) { |
| 184 |
SSL_METHOD *meth; |
| 185 |
SSL_CTX* ctx; |
| 186 |
SSL* ssl; |
| 187 |
int err; |
| 188 |
|
| 189 |
if (sslinit) { |
| 190 |
SSL_load_error_strings(); |
| 191 |
SSL_library_init(); |
| 192 |
sslinit = 0; |
| 193 |
} |
| 194 |
|
| 195 |
meth = SSLv23_server_method(); |
| 196 |
|
| 197 |
ctx = SSL_CTX_new (meth); |
| 198 |
if (!ctx) { |
| 199 |
TPTP_LOG_DEBUG_MSG(pBlk->pServerData, "SSL: context error"); |
| 200 |
return -1; |
| 201 |
} |
| 202 |
|
| 203 |
if (certFile == NULL) { |
| 204 |
TPTP_LOG_DEBUG_MSG(pBlk->pServerData, "SSL: no certificate file found"); |
| 205 |
return -1; |
| 206 |
} |
| 207 |
|
| 208 |
if (SSL_CTX_use_certificate_file(ctx, certFile, SSL_FILETYPE_PEM) <= 0) { |
| 209 |
TPTP_LOG_DEBUG_MSG1(pBlk->pServerData, "SSL: illegal certificate file %s", certFile); |
| 210 |
return -1; |
| 211 |
} |
| 212 |
|
| 213 |
if (keyFile == NULL) { |
| 214 |
TPTP_LOG_DEBUG_MSG(pBlk->pServerData, "SSL: no key file found"); |
| 215 |
return -1; |
| 216 |
} |
| 217 |
|
| 218 |
if (SSL_CTX_use_PrivateKey_file(ctx, keyFile, SSL_FILETYPE_PEM) <= 0) { |
| 219 |
TPTP_LOG_DEBUG_MSG1(pBlk->pServerData, "SSL: illegal key file %s", keyFile); |
| 220 |
return -1; |
| 221 |
} |
| 222 |
|
| 223 |
if (!SSL_CTX_check_private_key(ctx)) { |
| 224 |
TPTP_LOG_DEBUG_MSG2(pBlk->pServerData, "SSL: Private key %s does not match the certificate public key %s", |
| 225 |
keyFile, certFile); |
| 226 |
return -1; |
| 227 |
} |
| 228 |
|
| 229 |
ssl = SSL_new (ctx); |
| 230 |
if (ssl < 0) { |
| 231 |
TPTP_LOG_DEBUG_MSG(pBlk->pServerData, "SSL.new error"); |
| 232 |
return -1; |
| 233 |
} |
| 234 |
|
| 235 |
SSL_set_fd (ssl, pBlk->clientSock); |
| 236 |
err = SSL_accept (ssl); |
| 237 |
if (err < 0) { |
| 238 |
TPTP_LOG_DEBUG_MSG1(pBlk->pServerData, "SSL: ssl_accept error %d", SSL_get_error(ssl, err)); |
| 239 |
return -1; |
| 240 |
} |
| 241 |
|
| 242 |
pBlk->sslCtx = ctx; |
| 243 |
pBlk->ssl = ssl; |
| 244 |
pBlk->secured = TRUE; |
| 245 |
|
| 246 |
return 0; |
| 247 |
} |
| 248 |
#endif |
| 249 |
|
| 250 |
/** |
174 |
/** |
| 251 |
********************************************************* |
175 |
********************************************************* |
| 252 |
* |
176 |
* |
|
Lines 694-701
Link Here
|
| 694 |
|
618 |
|
| 695 |
int recvData (request_block_ptr_t pRdb, char *buffer, int length, int *bytesRead) { |
619 |
int recvData (request_block_ptr_t pRdb, char *buffer, int length, int *bytesRead) { |
| 696 |
int result; |
620 |
int result; |
|
|
621 |
|
| 697 |
if (pRdb->secured) { |
622 |
if (pRdb->secured) { |
| 698 |
result = SSL_read (pRdb->ssl, buffer, length); |
623 |
result = sslRead(pRdb->ssl, buffer, length); |
| 699 |
*bytesRead = result; |
624 |
*bytesRead = result; |
| 700 |
} |
625 |
} |
| 701 |
else { |
626 |
else { |
|
Lines 908-970
Link Here
|
| 908 |
TPTP_LOG_DEBUG_MSG1(pParam, "Socket server is running at port number of %d.", pParam->port) ; |
833 |
TPTP_LOG_DEBUG_MSG1(pParam, "Socket server is running at port number of %d.", pParam->port) ; |
| 909 |
serveRequest(serverSock, pParam) ; |
834 |
serveRequest(serverSock, pParam) ; |
| 910 |
} |
835 |
} |
| 911 |
|
|
|
| 912 |
return ( 0 ); |
836 |
return ( 0 ); |
| 913 |
} |
837 |
} |
| 914 |
|
838 |
|
| 915 |
int getACHome (char* buf, int len) { |
|
|
| 916 |
char* acHome; |
| 917 |
|
| 918 |
acHome = getenv(CONFIGURATION_HOME); |
| 919 |
if (acHome != NULL) { |
| 920 |
strncpy(buf, acHome, len); |
| 921 |
return 0; |
| 922 |
} |
| 923 |
|
| 924 |
#ifdef _WIN32 |
| 925 |
acHome = _getcwd(buf, 1024); |
| 926 |
if (acHome == NULL) return -1; |
| 927 |
|
| 928 |
strcat(buf, "\\.."); |
| 929 |
#else |
| 930 |
acHome = getcwd(buf, 1024); |
| 931 |
if (acHome == NULL) return -1; |
| 932 |
|
| 933 |
strcat(buf, "/.."); |
| 934 |
#endif |
| 935 |
|
| 936 |
return 0; |
| 937 |
} |
| 938 |
|
| 939 |
int initSSL() { |
| 940 |
char buf[1024]; |
| 941 |
|
| 942 |
if (getACHome(buf, 1024) < 0) { |
| 943 |
certFile = (char*) malloc(strlen(CERTF) + 1); |
| 944 |
strcpy(certFile, CERTF); |
| 945 |
|
| 946 |
keyFile = (char*) malloc(strlen(KEYF) + 1); |
| 947 |
strcpy(keyFile, CERTF); |
| 948 |
} |
| 949 |
else { |
| 950 |
#ifdef _WIN32 |
| 951 |
strcat(buf, "\\security\\"); |
| 952 |
#else |
| 953 |
strcat(buf, "/security/"); |
| 954 |
#endif |
| 955 |
|
| 956 |
certFile = (char*) malloc(strlen(buf) + strlen(CERTF) + 1); |
| 957 |
strcpy(certFile, buf); |
| 958 |
strcat(certFile, CERTF); |
| 959 |
|
| 960 |
keyFile = (char*) malloc(strlen(buf) + strlen(KEYF) + 1); |
| 961 |
strcpy(keyFile, buf); |
| 962 |
strcat(keyFile, KEYF); |
| 963 |
} |
| 964 |
|
| 965 |
return 0; |
| 966 |
} |
| 967 |
|
| 968 |
/** |
839 |
/** |
| 969 |
********************************************************* |
840 |
********************************************************* |
| 970 |
* |
841 |
* |
|
Lines 975-1028
Link Here
|
| 975 |
* 0 - Success |
846 |
* 0 - Success |
| 976 |
* nonzero - Error. |
847 |
* nonzero - Error. |
| 977 |
*********************************************************/ |
848 |
*********************************************************/ |
| 978 |
tptp_int32 createSocketListener(tptp_object* cmo, transport_layer_data_t * pTransportData, tptp_object* tlo) |
849 |
tptp_int32 createSocketListener(tptp_object* cmo, transport_layer_data_t * pTransportData, tptp_object* tlo) { |
| 979 |
{ |
850 |
server_block_t* pServerData; |
| 980 |
int rc = 0 ; |
851 |
SocketConfigInfo socketInfo; |
| 981 |
|
852 |
int rc; |
| 982 |
initSSL(); |
|
|
| 983 |
|
853 |
|
| 984 |
rc = initForSocketCalls() ; |
854 |
rc = initForSocketCalls(); |
| 985 |
|
855 |
if (rc != 0) { |
| 986 |
if (rc == 0) |
856 |
if (pTransportData->logEventEntry) { |
| 987 |
{ |
857 |
pTransportData->logEventEntry(cmo, "Socket TL", pTransportData->transportID, __FILE__, __LINE__, TPTP_FATAL, "Unable to initialize socket library."); |
| 988 |
server_block_t* pServerData; |
|
|
| 989 |
SocketConfigInfo socketInfo; |
| 990 |
|
| 991 |
/* prepare the globally available server data block */ |
| 992 |
pServerData = (server_block_ptr_t) malloc(sizeof(server_block_t)) ; |
| 993 |
pServerData->cmo = cmo; |
| 994 |
pServerData->threadStatus = 0 ; |
| 995 |
pServerData->agentControllerDataBlk = *pTransportData ; |
| 996 |
|
| 997 |
/* allocate connection table */ |
| 998 |
pServerData->connectionTable = tableCreate(); |
| 999 |
|
| 1000 |
rc = getSocketConfigInfo(pTransportData->configurationData, &socketInfo); |
| 1001 |
if (rc != -1) |
| 1002 |
{ |
| 1003 |
pServerData->port = socketInfo.portNumber; |
| 1004 |
pServerData->securityEnabled = socketInfo.securityEnabled; |
| 1005 |
} |
| 1006 |
else |
| 1007 |
{ |
| 1008 |
pServerData->port = DEFAULT_PORT_NUM; |
| 1009 |
pServerData->securityEnabled = 0; |
| 1010 |
} |
858 |
} |
| 1011 |
|
859 |
|
| 1012 |
tlo->data = pServerData; |
860 |
return rc; |
| 1013 |
tlo->objectID = SOCKET_LISTENER_OBJECT_ID; |
|
|
| 1014 |
|
| 1015 |
TPTP_LOG_DEBUG_MSG(pServerData, "createTransportListener (socket)") ; |
| 1016 |
} |
861 |
} |
| 1017 |
else |
862 |
|
| 1018 |
{ |
863 |
/* prepare the globally available server data block */ |
| 1019 |
if ( pTransportData->logEventEntry ) |
864 |
pServerData = (server_block_ptr_t) malloc(sizeof(server_block_t)) ; |
| 1020 |
{ |
865 |
pServerData->cmo = cmo; |
| 1021 |
pTransportData->logEventEntry( cmo, "Socket TL", pTransportData->transportID, __FILE__, __LINE__, TPTP_FATAL, "Unable to initialize socket library." ); |
866 |
pServerData->threadStatus = 0 ; |
| 1022 |
} |
867 |
pServerData->agentControllerDataBlk = *pTransportData ; |
|
|
868 |
|
| 869 |
/* allocate connection table */ |
| 870 |
pServerData->connectionTable = tableCreate(); |
| 871 |
|
| 872 |
rc = getSocketConfigInfo(pTransportData->configurationData, &socketInfo); |
| 873 |
if (rc != -1) { |
| 874 |
pServerData->port = socketInfo.portNumber; |
| 875 |
#ifdef _WIN32 |
| 876 |
pServerData->securityEnabled = 0; |
| 877 |
#else |
| 878 |
pServerData->securityEnabled = socketInfo.securityEnabled; |
| 879 |
#endif |
| 880 |
} |
| 881 |
else { |
| 882 |
pServerData->port = DEFAULT_PORT_NUM; |
| 883 |
pServerData->securityEnabled = 0; |
| 1023 |
} |
884 |
} |
| 1024 |
|
885 |
|
| 1025 |
return ( rc ) ; |
886 |
tlo->data = pServerData; |
|
|
887 |
tlo->objectID = SOCKET_LISTENER_OBJECT_ID; |
| 888 |
|
| 889 |
TPTP_LOG_DEBUG_MSG(pServerData, "createTransportListener (socket)") ; |
| 890 |
|
| 891 |
return 0; |
| 1026 |
} |
892 |
} |
| 1027 |
|
893 |
|
| 1028 |
/** |
894 |
/** |
|
Lines 1090-1103
Link Here
|
| 1090 |
* 0 - Success |
956 |
* 0 - Success |
| 1091 |
* nonzero - Error. |
957 |
* nonzero - Error. |
| 1092 |
*********************************************************/ |
958 |
*********************************************************/ |
| 1093 |
tptp_int32 startSocketListener(server_block_t* pServerData) |
959 |
tptp_int32 startSocketListener(server_block_t* pServerData) { |
| 1094 |
{ |
|
|
| 1095 |
int rc = 0 ; |
960 |
int rc = 0 ; |
| 1096 |
TID threadId; |
961 |
TID threadId; |
| 1097 |
HANDLE threadHandle ; |
962 |
HANDLE threadHandle ; |
| 1098 |
|
963 |
|
| 1099 |
TPTP_LOG_DEBUG_MSG(pServerData, "startTransportListener (socket)") ; |
964 |
TPTP_LOG_DEBUG_MSG(pServerData, "startTransportListener (socket)") ; |
| 1100 |
|
965 |
|
|
|
966 |
#ifndef _WIN32 |
| 967 |
if (pServerData->securityEnabled && initSSL(pServerData)) { |
| 968 |
return -1; |
| 969 |
} |
| 970 |
#endif |
| 971 |
|
| 1101 |
/* create new thread to listen for incoming connection requests */ |
972 |
/* create new thread to listen for incoming connection requests */ |
| 1102 |
rc = tptpStartThread(doListening, |
973 |
rc = tptpStartThread(doListening, |
| 1103 |
(LPVOID) pServerData, &threadId, &threadHandle) ; |
974 |
(LPVOID) pServerData, &threadId, &threadHandle) ; |
|
Lines 1108-1123
Link Here
|
| 1108 |
tptp_int32 closeConnection(request_block_ptr_t pBlock) { |
979 |
tptp_int32 closeConnection(request_block_ptr_t pBlock) { |
| 1109 |
if (pBlock == NULL) return -1; |
980 |
if (pBlock == NULL) return -1; |
| 1110 |
|
981 |
|
| 1111 |
#ifndef _WIN32 |
982 |
#ifndef _WIN32 |
| 1112 |
if (pBlock->ssl != NULL) { |
983 |
sslFree(pBlock); |
| 1113 |
SSL_free(pBlock->ssl); |
|
|
| 1114 |
pBlock->ssl = NULL; |
| 1115 |
} |
| 1116 |
|
| 1117 |
if (pBlock->sslCtx != NULL) { |
| 1118 |
SSL_CTX_free(pBlock->sslCtx); |
| 1119 |
pBlock->sslCtx = NULL; |
| 1120 |
} |
| 1121 |
#endif |
984 |
#endif |
| 1122 |
|
985 |
|
| 1123 |
pBlock->secured = FALSE; |
986 |
pBlock->secured = FALSE; |
|
Lines 1173-1179
Link Here
|
| 1173 |
|
1036 |
|
| 1174 |
tptp_int32 writeData(request_block_ptr_t pBlock, char* buffer, int length) { |
1037 |
tptp_int32 writeData(request_block_ptr_t pBlock, char* buffer, int length) { |
| 1175 |
if (pBlock->secured) { |
1038 |
if (pBlock->secured) { |
| 1176 |
return SSL_write(pBlock->ssl, buffer, length); |
1039 |
return sslWrite(pBlock->ssl, buffer, length); |
| 1177 |
} |
1040 |
} |
| 1178 |
else { |
1041 |
else { |
| 1179 |
return writeToSocket(pBlock->clientSock, buffer, length); |
1042 |
return writeToSocket(pBlock->clientSock, buffer, length); |