00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <string.h>
00036 #ifndef _WIN32
00037 #include <sys/types.h>
00038 #include <sys/socket.h>
00039 #include <netinet/in.h>
00040 #include <arpa/inet.h>
00041 #include <unistd.h>
00042 #include <netdb.h>
00043 #include "config.h"
00044 #else
00045 #include "winconfig.h"
00046 #endif
00047 #include <mxml.h>
00048 #include "include/libmc.h"
00049 #include "include/agent.h"
00050 #include "include/mc_platform.h"
00051 #include "include/message.h"
00052 #include "include/mtp_http.h"
00053 #include "include/xml_compose.h"
00054 #include "include/xml_helper.h"
00055 #include "include/xml_parser.h"
00056
00057 #include "security/asm_node.h"
00058
00059 #define SOCKET_INPUT_SIZE 4096
00060
00061 message_p
00062 message_New(void)
00063 {
00064 message_p message;
00065 message = (message_p)malloc(sizeof(message_t));
00066 CHECK_NULL(message, exit(0););
00067 message->addr = NULL;
00068 message->connect_id = 0;
00069 message->message_id = 0;
00070 message->isHTTP = 0;
00071 message->message_type = 0;
00072 message->http_type = 0;
00073 message->xml_root = NULL;
00074 message->xml_payload = NULL;
00075 message->message_body = NULL;
00076 message->update_name = NULL;
00077 message->update_num = 0;
00078 message->from_address = NULL;
00079 message->to_address = NULL;
00080 message->target = NULL;
00081 message->agent_xml_flag = 0;
00082 return message;
00083 }
00084
00085 message_p
00086 message_Copy(message_p src)
00087 {
00088 fprintf(stderr, "FIXME: message_Copy() is not implemented yet. %s:%d\n",
00089 __FILE__, __LINE__);
00090 return NULL;
00091 }
00092
00093 int
00094 message_InitializeFromAgent(
00095 mc_platform_p mc_platform,
00096 message_p message,
00097 agent_p agent)
00098 {
00099 struct hostent* host;
00100
00101 char* buf;
00102 char* destination_host;
00103 char* destination_port_str;
00104 #ifndef _WIN32
00105 char* save_ptr;
00106 #endif
00107 int destination_port;
00108
00109 message->message_id = rand();
00110 message->message_type = MOBILE_AGENT;
00111
00112 message->xml_root = agent_xml_compose(agent);
00113
00114
00115 CHECK_NULL(message->xml_root, exit(0););
00116 message->message_body = mxmlSaveAllocString(
00117 message->xml_root,
00118 MXML_NO_CALLBACK );
00119
00120 message->update_name = NULL;
00121
00122 message->from_address =
00123 (char*)malloc(sizeof(char) * (strlen(mc_platform->hostname) + 10));
00124 sprintf(
00125 message->from_address,
00126 "%s:%d",
00127 mc_platform->hostname,
00128 mc_platform->port );
00129 if (
00130 agent->datastate->task_progress >=
00131 agent->datastate->number_of_tasks
00132 )
00133 {
00134 message->to_address =
00135 (char*)malloc
00136 (
00137 sizeof(char) *
00138 (
00139 strlen(agent->home) + 1
00140 )
00141 );
00142 CHECK_NULL(message->to_address, exit(0););
00143 strcpy
00144 (
00145 message->to_address,
00146 agent->home
00147 );
00148 } else {
00149 message->to_address =
00150 (char*) malloc
00151 (
00152 sizeof(char) *
00153 (
00154 strlen
00155 (
00156 agent->datastate->tasks[ agent->datastate->task_progress ]
00157 ->server_name
00158 )
00159 +1
00160 )
00161 );
00162 CHECK_NULL( message->to_address, mc_platform->err = MC_ERR_MEMORY; return MC_ERR_MEMORY;);
00163 strcpy(
00164 message->to_address,
00165 agent->datastate->tasks[ agent->datastate->task_progress ]->server_name
00166 );
00167 }
00168 message->agent_xml_flag = 0;
00169 message->target = strdup("ams");
00170
00171 buf = (char*)malloc
00172 (
00173 sizeof(char) *
00174 (strlen(message->to_address)+1)
00175 );
00176 CHECK_NULL(buf, exit(0););
00177 strcpy(buf, message->to_address);
00178 destination_host = strtok_r(buf, ":", &save_ptr);
00179 destination_port_str = strtok_r(NULL, ":", &save_ptr);
00180 destination_port = atoi(destination_port_str);
00181 message->addr = (struct sockaddr_in*)malloc(sizeof(struct sockaddr_in));
00182 if ((host = gethostbyname(destination_host))) {
00183 memcpy(&(message->addr->sin_addr), host->h_addr, host->h_length);
00184 message->addr->sin_port = htons(destination_port);
00185 } else {
00186 WARN("Host not found.");
00187 }
00188 free(buf);
00189 return MC_SUCCESS;
00190 }
00191
00192 int
00193 message_InitializeFromConnection(
00194 mc_platform_p mc_platform,
00195 message_p message,
00196 connection_p connection)
00197 {
00198 int i = 1;
00199 int n;
00200 char *message_string;
00201 char *buffer;
00202
00203 message->addr = (struct sockaddr_in*)malloc(sizeof(struct sockaddr_in));
00204 CHECK_NULL(message->addr, exit(0););
00205 *(message->addr) = connection->addr;
00206
00207 message->connect_id = connection->connect_id;
00208
00209 message->message_id = rand();
00210
00211 message->to_address = NULL;
00212 message->from_address = NULL;
00213 message->target = NULL;
00214
00215 buffer = (char*) malloc(sizeof(char) * (SOCKET_INPUT_SIZE + 1));
00216 CHECK_NULL(buffer, exit(0););
00217 message_string = (char*) malloc(sizeof(char) * (SOCKET_INPUT_SIZE + 1));
00218 CHECK_NULL(message_string, exit(0););
00219 message_string[0] = '\0';
00220 buffer[0] = '\0';
00221
00222
00223 while(1) {
00224 #ifndef _WIN32
00225 n = recvfrom(connection->clientfd,
00226 (void *) buffer,
00227 (size_t) sizeof(char)*SOCKET_INPUT_SIZE,
00228 0,
00229 (struct sockaddr *) 0,
00230 (socklen_t *) 0);
00231 #else
00232 n = recvfrom(connection->clientfd,
00233 (void *) buffer,
00234 (size_t) sizeof(char)*SOCKET_INPUT_SIZE,
00235 0,
00236 (struct sockaddr *) 0,
00237 0);
00238 #endif
00239 if (n < 0) {
00240 free(buffer);
00241 return MC_ERR_CONNECT;
00242 }
00243 else if (n == 0) {
00244 free(buffer);
00245 break;
00246 } else {
00247 buffer[n] = '\0';
00248 i++;
00249 strcat(message_string, buffer);
00250 message_string = realloc
00251 (
00252 message_string,
00253 sizeof(char) * (SOCKET_INPUT_SIZE+1) * i
00254 );
00255 CHECK_NULL(message_string, exit(0););
00256 buffer[0] = '\0';
00257 }
00258 }
00259 message->message_body = (char*)malloc
00260 (
00261 sizeof(char) *
00262 (strlen(message_string) + 1)
00263 );
00264 CHECK_NULL(message->message_body, exit(0););
00265 strcpy(message->message_body, message_string);
00266 free(message_string);
00267 message->xml_root = mxmlLoadString
00268 (
00269 NULL,
00270 message->message_body,
00271 MXML_NO_CALLBACK
00272 );
00273 if (message_xml_parse(message)) {
00274 fprintf(stderr, "Error parsing message at %s:%d.\n",
00275 __FILE__, __LINE__);
00276 message_Destroy(message);
00277 return MC_ERR_PARSE;
00278 }
00279 return MC_SUCCESS;
00280 }
00281
00282 int http_to_hostport(const char* http_str, char** host, int* port, char** target)
00283 {
00284
00285
00286
00287
00288 char* tmp;
00289 if(strncmp(http_str, "http://", 7)) {
00290 return MC_ERR_PARSE;
00291 }
00292 http_str += 7;
00293 tmp = strchr(http_str, (int)':');
00294 if (tmp == NULL) return MC_ERR_PARSE;
00295
00296
00297 *host = (char*)malloc(sizeof(char) *
00298 (tmp - http_str + 1) );
00299 strncpy(*host, http_str, tmp - http_str);
00300 (*host)[tmp-http_str] = '\0';
00301
00302
00303 tmp++;
00304 sscanf(tmp, "%d", port);
00305
00306
00307 tmp = strchr(tmp, (int)'/');
00308 tmp++;
00309 *target = (char*)malloc(sizeof(char) *
00310 (strlen(tmp)+1) );
00311 strcpy(*target, tmp);
00312
00313 return 0;
00314 }
00315
00316 int
00317 message_InitializeFromString(
00318 mc_platform_p mc_platform,
00319 message_p message,
00320 const char* string,
00321 const char* destination_host,
00322 int destination_port,
00323 const char* target)
00324 {
00325 char* destination;
00326 struct hostent* host;
00327
00328 message->connect_id = 0;
00329 message->message_id = rand();
00330
00331 message->message_type = MOBILE_AGENT;
00332
00333 message->xml_root = NULL;
00334
00335 message->message_body =
00336 (char*)malloc( sizeof(char) * (strlen(string)+1));
00337 CHECK_NULL(message->message_body,
00338 mc_platform->err = MC_ERR_MEMORY;
00339 return MC_ERR_MEMORY; );
00340 strcpy(message->message_body, string);
00341
00342 message->update_name = NULL;
00343
00344 destination = malloc(sizeof(char)*(strlen(destination_host) + 10));
00345 CHECK_NULL(destination,
00346 mc_platform->err = MC_ERR_MEMORY;
00347 return MC_ERR_MEMORY; );
00348 sprintf(destination, "%s:%d",
00349 destination_host,
00350 destination_port
00351 );
00352
00353 message->to_address = destination;
00354 message->from_address = (char*)malloc(
00355 sizeof(char) * (strlen(mc_platform->hostname)+10));
00356 sprintf(message->from_address,
00357 "%s:%d",
00358 mc_platform->hostname,
00359 mc_platform->port );
00360 message->target = (char*)malloc(sizeof(char) *
00361 (strlen(target)+1));
00362 strcpy(message->target, target);
00363
00364
00365 message->addr = (struct sockaddr_in*)malloc(sizeof(struct sockaddr_in));
00366 if (destination_host != NULL && strlen(destination_host)!= 0) {
00367 if((host = gethostbyname(destination_host)))
00368 {
00369 memcpy(&(message->addr->sin_addr), host->h_addr, host->h_length);
00370 message->addr->sin_port = htons(destination_port);
00371 } else {
00372 fprintf(stderr, "Warning: Host not found: %s:%d %s:%d",
00373 destination_host, destination_port, __FILE__, __LINE__ );
00374 }
00375 }
00376 message->xml_root = mxmlLoadString
00377 (
00378 NULL,
00379 message->message_body,
00380 MXML_NO_CALLBACK
00381 );
00382 if (message_xml_parse(message)) {
00383 fprintf(stderr, "Error parsing message at %s:%d.\n",
00384 __FILE__, __LINE__);
00385 message_Destroy(message);
00386 return MC_ERR_PARSE;
00387 }
00388
00389 return MC_SUCCESS;
00390 }
00391
00392 int
00393 message_Destroy(message_p message)
00394 {
00395 if (message == NULL) {
00396 return MC_SUCCESS;
00397 }
00398
00399
00400 if(message->xml_root != NULL && message->agent_xml_flag == 0) {
00401 mxmlDelete(message->xml_root);
00402 }
00403
00404 if(message->addr) {
00405 free(message->addr);
00406 message->addr = NULL;
00407 }
00408 if(message->message_body != NULL) {
00409 free(message->message_body);
00410 message->message_body = NULL;
00411 }
00412 if(message->update_name != NULL) {
00413 free(message->update_name);
00414 }
00415 if(message->from_address != NULL) {
00416 free(message->from_address);
00417 }
00418 if(message->to_address != NULL) {
00419 free(message->to_address);
00420 }
00421 if(message->target != NULL) {
00422 free(message->target);
00423 }
00424
00425 free(message);
00426 message = NULL;
00427 return MC_SUCCESS;
00428 }
00429
00430
00431 int
00432 auth_rece_send_msg(int sockfd, char *hostname, char *message, char *privkey, char *known_host_filename){
00433
00434 int ret=-1;
00435 unsigned char passphrase[35], aes_key[35];
00436 int nonce, mode;
00437 char privatekey[1215];
00438 char peer_pubkey[300], ch;
00439 char plaintext[135];
00440 FILE *fd, *tempfd;
00441 char buffer[10];
00442 struct stat stbuf;
00443
00444
00445 char *infile;
00446 char *outfile;
00447 char *tempfile;
00448 memset(passphrase , '\0', 35);
00449 memset(privatekey , '\0', 1215);
00450 memset(plaintext , '\0', 135);
00451 memset(aes_key, '\0', 35);
00452
00453
00454
00455
00456 #ifndef _WIN32
00457 int fd1;
00458 infile = (char *)malloc(sizeof(char)*20);
00459 strcpy(infile, "msgXXXXXX");
00460 fd1 = mkstemp(infile);
00461 if (fd == -1) {
00462 fprintf(stderr, "Could not create temporary file:%s. %s:%d\n",
00463 infile, __FILE__, __LINE__ );
00464 exit(EXIT_FAILURE);
00465 }
00466 close(fd1);
00467
00468 outfile = (char *)malloc(sizeof(char)*20);
00469 strcpy(outfile, "en-msgXXXXXX");
00470 fd1 = mkstemp(outfile);
00471 if (fd1 == -1) {
00472 fprintf(stderr, "Could not create temporary file:%s. %s:%d\n",
00473 outfile, __FILE__, __LINE__ );
00474 exit(EXIT_FAILURE);
00475 }
00476 close(fd1);
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487 #else
00488 infile = _tempnam(".", "msg");
00489 outfile = _tempnam(".", "en-msg");
00490 #endif
00491
00492
00493
00494
00495
00496
00497 strcpy(privatekey, privkey);
00498
00499
00500
00501
00502 if (read_known_host_file(peer_pubkey, hostname, known_host_filename) == -1 ){
00503 printf("Client: %s 's Public key not found in know host file\n", hostname);
00504 if( remove(infile) ) printf("message.c : remove error 4");
00505 if( remove(outfile) ) printf("message.c : remove error 5");
00506 #ifndef _WIN32
00507
00508 #else
00509
00510 #endif
00511 }else{
00512 if ((ret=initiate_migration_process(sockfd, &nonce, peer_pubkey, privatekey, aes_key)) != 1){
00513 if (ret == -1)
00514 printf("Client: Connected peer is not authenticated \n");
00515 if (ret == -2)
00516 printf("Client: Unable to get authentication from oither peer \n");
00517 }else{
00518
00519
00520
00521
00522 if( (fd = fopen(infile,"w")) == NULL){
00523 printf("Unable to write message in a file \n");
00524 if( remove(infile) ) printf("message.c : remove error 1");
00525 }else{
00526
00527 fwrite (message , 1 , strlen(message) , fd );
00528 fclose(fd);
00529
00530 mode = 0;
00531 if (aes_en_de(mode, infile, outfile, aes_key, &nonce, 0) != 1){
00532 printf("Client: AES Encryption Failed \n");
00533 if( remove(infile) ) printf("message.c : remove error 2");
00534 if( remove(outfile) ) printf("message.c : remove error 3");
00535 }else{
00536 if( remove(infile) ) printf("message.c : remove error 4");
00537 if (send_AES_en_MA(sockfd, &nonce, outfile, peer_pubkey) != 1 ){
00538 printf("Client: Error while sending mobile agent \n");
00539 if( remove(outfile) ) printf("message.c : remove error 5");
00540 }else{
00541 if( remove(outfile) ) printf("message.c : remove error 6");
00542
00543 ret = 2;
00544 }
00545 }
00546 }
00547 }
00548 }
00549
00550
00551
00552
00553 return ret;
00554 }
00555
00556 int message_Send(mc_platform_t* mc_platform, message_p message, char *privatekey)
00557 {
00558 char *buffer;
00559 mtp_http_t* mtp_http;
00560 int n, ret;
00561 #ifndef _WIN32
00562 int skt;
00563 struct sockaddr_in sktin;
00564 #else
00565 SOCKET skt;
00566 SOCKADDR_IN sktin;
00567 #endif
00568 struct hostent *host;
00569 char *buf;
00570 char *hostname;
00571 #ifndef _WIN32
00572 char *saveptr;
00573 #endif
00574 int port;
00575
00576
00577 if (
00578 mtp_http_ComposeMessage(
00579 message
00580 )
00581 )
00582 {
00583 return MC_ERR;
00584 }
00585
00586
00587 buf = (char*)malloc(sizeof(char)*(strlen(message->to_address)+1));
00588 strcpy(buf, message->to_address);
00589 hostname = strtok_r(buf, ":", &saveptr);
00590 sscanf( strtok_r(NULL, ":", &saveptr), "%d", &port );
00591
00592 if((skt = socket(PF_INET, SOCK_STREAM, 0)) < 0)
00593 {
00594 fprintf(stderr, "Error - can't create socket\n");
00595 return -1;
00596 }
00597
00598 memset(&sktin, 0, sizeof(sktin));
00599 sktin.sin_family = PF_INET;
00600 sktin.sin_port = htons(port);
00601
00602 if((host = gethostbyname(hostname)))
00603 {
00604 memcpy(&sktin.sin_addr, host->h_addr, host->h_length);
00605 }
00606 else if((sktin.sin_addr.s_addr = inet_addr(hostname)) < 0)
00607 {
00608 fprintf(stderr, "Error - can't get host entry for %s\n", hostname);
00609 free(buf);
00610 return -1;
00611 }
00612
00613 if(connect(skt, (struct sockaddr *) &sktin, sizeof(sktin)) < 0) {
00614 fprintf(stderr, "Error - can't connect to %s:%d\n",
00615 hostname,
00616 port
00617 );
00618 free(buf);
00619 return MC_ERR_CONNECT;
00620 }
00621
00622 #ifdef NEW_SECURITY
00623
00624 ret = auth_rece_send_msg(skt, hostname, message->message_body, privatekey, mc_platform->agency->known_host_filename);
00625 if( ret == 1 ){
00626 printf("Successfull Authenticate and but send of MA is failed \n");
00627 #ifndef _WIN32
00628 close(skt);
00629 #else
00630 closesocket(skt);
00631 #endif
00632 free(buf);
00633 return MC_ERR_SEND;
00634 }else if(ret != 2){
00635 #ifndef _WIN32
00636 close(skt);
00637 #else
00638 closesocket(skt);
00639 #endif
00640 free(buf);
00641 return MC_ERR_SEND;
00642 }else if(ret == 2)
00643
00644 #else
00645
00646
00647 if(send(skt, message->message_body, strlen(message->message_body), 0) < 0) {
00648 perror(send);
00649 printf("cannot write to socket %s:%d\n");
00650 }
00651 else
00652 #endif
00653 {
00654
00655 buffer = (char*) malloc(sizeof(char) * (SOCKET_INPUT_SIZE + 1));
00656 CHECK_NULL(buffer, exit(0););
00657 mtp_http = mtp_http_New();
00658 #ifndef _WIN32
00659 n = recvfrom(skt,
00660 (void *) buffer,
00661 (size_t) sizeof(char)*SOCKET_INPUT_SIZE,
00662 0,
00663 (struct sockaddr *) 0,
00664 (socklen_t *) 0);
00665 #else
00666 n = recvfrom(skt,
00667 (void *) buffer,
00668 (size_t) sizeof(char)*SOCKET_INPUT_SIZE,
00669 0,
00670 (struct sockaddr *) 0,
00671 0);
00672 #endif
00673 if( mtp_http_Parse(mtp_http, buffer) ) {
00674 fprintf(stderr, "http parsing error: Response expected. %s:%d\n",
00675 __FILE__, __LINE__);
00676 fprintf(stderr, "Received message was:\n%s\n", buffer);
00677 }
00678 if (mtp_http->response_code != 200) {
00679 fprintf(stderr, "Warning: remote http server responded: %d %s\n",
00680 mtp_http->response_code, mtp_http->response_string );
00681 }
00682
00683 mtp_http_Destroy(mtp_http);
00684 }
00685
00686 #ifndef _WIN32
00687 close(skt);
00688 #else
00689 closesocket(skt);
00690 #endif
00691 free(buf);
00692 free(buffer);
00693 return 0;
00694 }
00695
00696