source: branches/prototype-v0/zoo-project/zoo-kernel/sshapi.c @ 886

Last change on this file since 886 was 886, checked in by djay, 6 years ago

Check for md5sum of any file in the cache to avoid sending the same file on HPC server twice. Add the multiple LiteralData? inputs support for HPC services. Remove condition for defining SCALE for every band in the outputed MapServer? mapfile.

  • Property svn:keywords set to Id
File size: 21.7 KB
Line 
1/*
2 *
3 * Author : Gérald FENOY
4 *
5 * Copyright 2017 GeoLabs SARL. All rights reserved.
6 *
7 * This work was supported by public funds received in the framework of GEOSUD,
8 * a project (ANR-10-EQPX-20) of the program "Investissements d'Avenir" managed
9 * by the French National Research Agency
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 */
29
30#include "sshapi.h"
31#include "service_internal.h"
32
33SSHCON *sessions[MAX_PARALLEL_SSH_CON];
34
35/**
36 * Wait until one or more file descriptor has been changed for the socket for a
37 * time defined by timeout
38 * @param socket_fd int defining the sockket file descriptor
39 * @param session an exeisting LIBSSH2_SESSION
40 */ 
41int waitsocket(int socket_fd, LIBSSH2_SESSION *session)
42{
43    struct timeval timeout;
44    int rc;
45    fd_set fd;
46    fd_set *writefd = NULL;
47    fd_set *readfd = NULL;
48    int dir;
49 
50    timeout.tv_sec = 10;
51    timeout.tv_usec = 0;
52 
53    FD_ZERO(&fd);
54 
55    FD_SET(socket_fd, &fd);
56 
57    /* now make sure we wait in the correct direction */ 
58    dir = libssh2_session_block_directions(session);
59
60 
61    if(dir & LIBSSH2_SESSION_BLOCK_INBOUND)
62        readfd = &fd;
63 
64    if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
65        writefd = &fd;
66 
67    rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout);
68 
69    return rc;
70}
71
72/**
73 * Connect to a remote host using SSH protocol
74 * @param conf maps The main configuration maps
75 * @return the libssh2 sessions pointer or NULL in case any failure occured.
76 */
77SSHCON *ssh_connect(maps* conf){
78  unsigned long hostaddr;
79  int i, use_pw = 1;
80  struct sockaddr_in sin;
81  const char *fingerprint;
82  SSHCON *result=(SSHCON*)malloc((2*sizeof(int))+sizeof(LIBSSH2_SESSION*)+sizeof(LIBSSH2_SFTP*));
83  result->sock_id=NULL;
84  result->index=NULL;
85  result->session=NULL;
86  result->sftp_session=NULL;
87  const char *user;
88  const char *password=NULL;
89  const char *public_key;
90  char *private_key;
91  int rc;
92  FILE *local;
93  char mem[1024 * 1000];
94  char error[1024];
95  int port=22;
96
97  map* hpc_config=getMapFromMaps(conf,"lenv","configId");
98  map* hpc_host=getMapFromMaps(conf,hpc_config->value,"ssh_host");
99  map* hpc_port=getMapFromMaps(conf,hpc_config->value,"ssh_port");
100  map* hpc_user=getMapFromMaps(conf,hpc_config->value,"ssh_user");
101  map* hpc_password=getMapFromMaps(conf,hpc_config->value,"ssh_password");
102  map* hpc_public_key=getMapFromMaps(conf,hpc_config->value,"ssh_key");
103 
104  char ip[100];
105  struct hostent *my_hostent;
106  struct in_addr **addrs;
107 
108  if (hpc_host != NULL) {
109    // Fetch ip address for the hostname
110    if ( (my_hostent = gethostbyname( hpc_host->value ) ) == NULL){
111      herror("gethostbyname");
112      setMapInMaps(conf,"lenv","message",_("Issue when invoking gethostbyname!"));
113      return NULL;
114    }
115 
116    addrs = (struct in_addr **) my_hostent->h_addr_list;
117 
118    for(i = 0; addrs[i] != NULL; i++) {
119      strcpy(ip , inet_ntoa(*addrs[i]) );
120      break;
121    }
122  }
123
124#ifdef WIN32
125  WSADATA wsadata;
126  int err;
127  err = WSAStartup(MAKEWORD(2,0), &wsadata);
128  if (err != 0) {
129    sprintf(error, "WSAStartup failed with error: %d\n", err);
130    setMapInMaps(conf,"lenv","message",error);
131    return NULL;
132  }
133#endif
134
135  if (hpc_host != NULL) {
136    hostaddr = inet_addr(ip);
137  } else {
138    setMapInMaps(conf,"lenv","message","No host parameter found in your main.cfg file!\n");
139    return NULL;
140  }
141
142  // Default port is 22
143  if(hpc_port!=NULL){
144    port=atoi(hpc_port->value);
145  }
146
147  // In case there is no HPC > user the it must failed
148  if (hpc_user != NULL) {
149    user = hpc_user->value;
150  }else{
151    setMapInMaps(conf,"lenv","message","No user parameter found in your main.cfg file!");
152    return NULL;
153  }
154
155  // TODO: in case password is available but there is also the public key
156  // defined, then we can consider this password as the pass phrase.
157  if (hpc_password != NULL) {
158    password = hpc_password->value;
159  }else{
160    use_pw=-1;
161    if (hpc_public_key != NULL) {
162      public_key = hpc_public_key->value;
163      private_key = strdup(hpc_public_key->value);
164      private_key[strlen(public_key)-4]=0;
165    }else{
166      setMapInMaps(conf,"lenv","message","No method found to authenticate!");
167      return NULL;
168    }
169  }
170
171  rc = libssh2_init (0);
172  if (rc != 0) {
173    sprintf (error, "libssh2 initialization failed (%d)\n", rc);
174    setMapInMaps(conf,"lenv","message",error);
175    return NULL;
176  }
177
178  result->sock_id = socket(AF_INET, SOCK_STREAM, 0);
179  sin.sin_family = AF_INET;
180  sin.sin_port = htons(port);
181  sin.sin_addr.s_addr = hostaddr;
182  if (connect(result->sock_id, (struct sockaddr*)(&sin),sizeof(struct sockaddr_in)) != 0) {
183    setMapInMaps(conf,"lenv","message","Failed to connect to remote host!");
184    return NULL;
185  }
186
187  result->session = libssh2_session_init();
188  result->sftp_session = NULL;
189  map* tmp=getMapFromMaps(conf,"lenv","nb_sessions");
190  if(tmp!=NULL){
191    char nb_sessions[10];
192    int nb_sess=atoi(tmp->value);
193    sprintf(nb_sessions,"%d",nb_sess+1);
194    setMapInMaps(conf,"lenv","nb_sessions",nb_sessions);
195    result->index=nb_sess+1;
196    sessions[nb_sess+1]=result;
197  }else{
198    setMapInMaps(conf,"lenv","nb_sessions","0");
199    sessions[0]=result;
200    result->index=0;
201  }
202
203  if (!result->session)
204    return NULL;
205
206  libssh2_session_set_blocking(result->session, 1);
207
208  while ((rc = libssh2_session_handshake(result->session, result->sock_id))
209         == LIBSSH2_ERROR_EAGAIN);
210
211  if (rc) {
212    sprintf(error, "Failure establishing SSH session: %d\n", rc);
213    setMapInMaps(conf,"lenv","message",error);
214    return NULL;
215  }
216
217  fingerprint = libssh2_hostkey_hash(result->session, LIBSSH2_HOSTKEY_HASH_SHA1);
218 
219  if (use_pw>0) {
220    while ((rc = libssh2_userauth_password(result->session, user, password)) ==
221           LIBSSH2_ERROR_EAGAIN);
222    if (rc) {
223      setMapInMaps(conf,"lenv","message","Authentication by password failed.");
224      ssh_close(conf);
225      return NULL;
226    }
227  } else {
228    while ((rc = libssh2_userauth_publickey_fromfile(result->session, user,
229                                                     public_key,
230                                                     private_key,
231                                                     password)) ==
232           LIBSSH2_ERROR_EAGAIN);
233    if (rc) {
234      setMapInMaps(conf,"lenv","message","Authentication by public key failed");
235      ssh_close(conf);
236      return NULL;
237    }
238    free(private_key);
239  }
240
241  return result;
242
243}
244
245/**
246 * Get the number of opened SSH connections
247 * @param conf maps pointer to the main configuration maps
248 * @return the number of opened SSH connections
249 */
250int ssh_get_cnt(maps* conf){
251  int result=0;
252  map* myMap=getMapFromMaps(conf,"lenv","nb_sessions");
253  if(myMap!=NULL){
254    result=atoi(myMap->value);
255  }
256  return result;
257}
258
259/**
260 * Verify if a file exists on the remote host
261 * @param conf maps pointer to the main configuration maps
262 * @param targetPath const char* defining the path for storing the file on the
263 * remote host
264 * @return true in case of success, false if failure occured
265 */
266size_t ssh_file_exists(maps* conf,const char* targetPath,int cnt){
267  size_t result=-1;
268  if(cnt>0)
269    cnt-=1;
270  int rc;
271  LIBSSH2_SFTP_ATTRIBUTES attrs;
272  LIBSSH2_SFTP_HANDLE *sftp_handle;
273  do{
274    sftp_handle =
275      libssh2_sftp_open(sessions[cnt]->sftp_session, targetPath, LIBSSH2_FXF_READ,
276                        LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
277                        LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
278    if (!sftp_handle) {
279      fprintf(stderr, "Unable to open file with SFTP: %d %ld\n",__LINE__,
280              libssh2_sftp_last_error(sessions[cnt]->sftp_session));
281      return 0;
282    }
283  }while(!sftp_handle);
284#ifdef SSH_DEBUG
285  fprintf(stderr, "libssh2_sftp_open() is done, get file information\n");
286#endif
287  do {
288  rc = libssh2_sftp_stat_ex(sessions[cnt]->sftp_session, targetPath, strlen(targetPath),
289                            LIBSSH2_SFTP_LSTAT, &attrs );
290  if (rc<0 &&
291      (libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN))
292    {
293      fprintf(stderr, "error trying to fstat_ex, returned %d\n", rc);
294      break;
295    }
296  else
297    {
298#ifdef SSH_DEBUG
299      fprintf(stderr, "Stat Data: RetCode=%d\n", rc);
300      fprintf(stderr, "Stat Data: Size=%llu\n", attrs.filesize);
301      fprintf(stderr, "Stat Data: Perm=%lx\n",  attrs.permissions);
302      fprintf(stderr, "Stat Data: mtime=%lu\n",  attrs.mtime);
303#endif
304      if(rc==0)
305        break;
306      result=attrs.filesize;
307    }
308  } while (true);
309  libssh2_sftp_close(sftp_handle);
310  //libssh2_sftp_shutdown(sessions[cnt]->sftp_session);
311
312  return result;
313}
314
315/**
316 * Upload a file over an opened SSH connection
317 * @param conf maps pointer to the main configuration maps
318 * @param localPath const char* defining the local path for accessing the file
319 * @param targetPath const char* defining the path for storing the file on the
320 * remote host
321 * @return true in case of success, false if failure occured
322 */
323bool ssh_copy(maps* conf,const char* localPath,const char* targetPath,int cnt){
324  char mem[1024 * 1000];
325  size_t nread;
326  size_t memuse=0;
327  time_t start;
328  long total = 0;
329  int duration;
330  int rc;
331  LIBSSH2_SFTP_HANDLE *sftp_handle;
332  //map* myMap=getMapFromMaps(conf,"lenv","cnt_session");
333  if(getMapFromMaps(conf,"lenv","cnt_session")!=NULL){
334    char tmp[10];
335    sprintf(tmp,"%d",cnt+1);
336    setMapInMaps(conf,"lenv","cnt_session",tmp);
337  }else
338    setMapInMaps(conf,"lenv","cnt_session","0");
339  FILE *local = fopen(localPath, "rb");
340  if (!local) {
341    fprintf(stderr, "Can't open local file %s\n", localPath);
342    return false;
343  }
344 
345  do {
346    sessions[cnt]->sftp_session = libssh2_sftp_init(sessions[cnt]->session);
347    if (!sessions[cnt]->sftp_session &&
348        (libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN)) {
349     
350      fprintf(stderr, "Unable to init SFTP session\n");
351      return false;
352    }
353    if(!sessions[cnt]->sftp_session)
354      zSleep(10);
355  } while (!sessions[cnt]->sftp_session);
356
357  do {
358    sftp_handle =
359      libssh2_sftp_open(sessions[cnt]->sftp_session, targetPath,
360                        LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
361                        LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
362                        LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
363
364    if (!sftp_handle &&
365        (libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN)) {
366     
367      fprintf(stderr, "Unable to open file with SFTP\n");
368      return false;
369    }
370    if(!sftp_handle)
371      zSleep(10);
372  } while (!sftp_handle);
373  start = time(NULL);
374 
375  do {
376    nread = fread(&mem[memuse], 1, sizeof(mem)-memuse, local);
377    if (nread <= 0) {
378      if (memuse > 0)
379        nread = 0;
380      else
381        break;
382    }
383    memuse += nread;
384    total += nread;
385   
386    while ((rc = libssh2_sftp_write(sftp_handle, mem, memuse)) ==
387           LIBSSH2_ERROR_EAGAIN) {
388      waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session);
389    }
390    if(rc < 0)
391      break;
392   
393    if(memuse - rc) {
394      memmove(&mem[0], &mem[rc], memuse - rc);
395      memuse -= rc;
396    }
397    else
398      memuse = 0;
399   
400  } while (rc > 0);
401 
402  duration = (int)(time(NULL)-start);
403  fclose(local);
404  libssh2_sftp_close_handle(sftp_handle);
405
406  libssh2_sftp_shutdown(sessions[cnt]->sftp_session);
407  return true;
408}
409
410/**
411 * Download a file over an opened SSH connection
412 * @param conf maps pointer to the main configuration maps
413 * @param localPath const char* defining the local path for storing the file
414 * @param targetPath const char* defining the path for accessing the file on the
415 * remote host
416 * @return 0 in case of success, -1 if failure occured
417 */
418int ssh_fetch(maps* conf,const char* localPath,const char* targetPath,int cnt){
419  size_t nread;
420  size_t memuse=0;
421  time_t start;
422  long total = 0;
423  int duration;
424  int rc;
425  LIBSSH2_SFTP_HANDLE *sftp_handle;
426  FILE *local = fopen(localPath, "wb");
427  if (!local) {
428    fprintf(stderr, "Can't open local file %s\n", localPath);
429    return -1;
430  }
431
432  do {
433    sessions[cnt]->sftp_session = libssh2_sftp_init(sessions[cnt]->session);
434    if (!sessions[cnt]->sftp_session &&
435        (libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN)) {
436      fprintf(stderr, "Unable to init SFTP session\n");
437      return -1;
438    }
439    if(!sessions[cnt]->sftp_session)
440      zSleep(1);
441  } while (!sessions[cnt]->sftp_session);
442  do {
443    sftp_handle = libssh2_sftp_open(sessions[cnt]->sftp_session, targetPath,   
444                                    LIBSSH2_FXF_READ, 0);
445    if (!sftp_handle) {
446      if (libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN) {
447        fprintf(stderr, " ** Unable to open file with SFTP\n");
448        return -1;
449      }
450      else {
451        waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session); 
452      }
453    }
454    if(!sftp_handle)
455      zSleep(1);
456  } while (!sftp_handle);
457 
458  int result=0;
459  do {
460    do {
461      char* mem=(char*)malloc(16*1024*1024);
462      rc = libssh2_sftp_read(sftp_handle, mem,16*1024*1024);
463      if(rc > 0) {
464        fwrite(mem, rc, 1, local);
465      }
466      free(mem);
467    } while (rc > 0);
468   
469    if(rc != LIBSSH2_ERROR_EAGAIN) {
470      result=-1;
471      break;
472    }
473   
474    struct timeval timeout;
475    fd_set fd;
476    timeout.tv_sec = 10;
477    timeout.tv_usec = 0;
478 
479    FD_ZERO(&fd);
480 
481    FD_SET(sessions[cnt]->sock_id, &fd);
482 
483    rc = select(sessions[cnt]->sock_id+1, &fd, &fd, NULL, &timeout);
484    if(rc <= 0) {
485      if(rc==0)
486        fprintf(stderr, "SFTP download timed out: %d\n", rc);
487      else
488        fprintf(stderr, "SFTP download error: %d\n", rc);
489      return -1;
490    }
491   
492  } while (1);
493  duration = (int)(time(NULL)-start);
494  fclose(local);
495  libssh2_sftp_close_handle(sftp_handle);
496  libssh2_sftp_shutdown(sessions[cnt]->sftp_session);
497  return 0;
498}
499
500/**
501 * Execute a command over an opened SSH connection
502 * @param conf maps pointer to the main configuration maps
503 * @param command const char pointer to the command to be executed
504 * @return bytecount resulting from the execution of the command
505 */
506int ssh_exec(maps* conf,const char* command,int cnt){
507  LIBSSH2_CHANNEL *channel;
508  int rc;
509  int bytecount = 0;
510  int exitcode;
511  char *exitsignal=(char *)"none";
512  while( (channel = libssh2_channel_open_session(sessions[cnt]->session)) == NULL &&
513         libssh2_session_last_error(sessions[cnt]->session,NULL,NULL,0) == LIBSSH2_ERROR_EAGAIN ) {
514    waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session);
515  }
516  if( channel == NULL ){
517    fprintf(stderr,"Error\n");
518    return -1;
519  }
520  while( (rc = libssh2_channel_exec(channel, command)) == LIBSSH2_ERROR_EAGAIN ) {
521    waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session);
522  }
523  if( rc != 0 ) {
524    fprintf(stderr,"Error\n");
525    return -1;
526  }
527
528  map* tmpPath=getMapFromMaps(conf,"main","tmpPath");
529  map* uuid=getMapFromMaps(conf,"lenv","usid");
530  char *logPath=(char*)malloc((strlen(tmpPath->value)+strlen(uuid->value)+11)*sizeof(char));
531  sprintf(logPath,"%s/exec_out_%s",tmpPath->value,uuid->value);
532 
533  FILE* logFile=fopen(logPath,"wb");
534  free(logPath);
535  while(true){
536    int rc;
537    do {
538      char buffer[0x4000];
539      rc = libssh2_channel_read( channel, buffer, sizeof(buffer) );
540     
541      if( rc > 0 ){
542        int i;
543        bytecount += rc;
544        buffer[rc]=0;
545       
546        fprintf(logFile,"%s",buffer);
547        fflush(logFile);
548      }
549    }
550    while( rc > 0 );
551   
552    if( rc == LIBSSH2_ERROR_EAGAIN ) {
553      waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session);
554    }
555    else
556      break;
557  }
558  fclose(logFile);
559  exitcode = 127;
560  while( (rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN )
561    waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session);
562 
563  if( rc == 0 ) {
564    exitcode = libssh2_channel_get_exit_status( channel );
565    libssh2_channel_get_exit_signal(channel, &exitsignal,
566                                    NULL, NULL, NULL, NULL, NULL);
567  }
568 
569  if (exitsignal)
570    fprintf(stderr, "\nGot signal: %s\n", exitsignal);
571  else
572    fprintf(stderr, "\nEXIT: %d bytecount: %d\n", exitcode, bytecount);
573 
574  libssh2_channel_free(channel);
575
576  return bytecount;
577}
578
579/**
580 * Close an opened SSH connection
581 * @param conf maps pointer to the main configuration maps
582 * @param con SSHCON pointer to the SSH connection
583 * @return true in case of success, false if failure occured
584 */
585bool ssh_close_session(maps* conf,SSHCON* con){
586  if(con==NULL)
587    return true;
588  while (libssh2_session_disconnect(con->session, "Normal Shutdown, Thank you for using the ZOO-Project sshapi")
589         == LIBSSH2_ERROR_EAGAIN);
590#ifdef WIN32
591  closesocket(con->sock_id);
592#else
593  close(con->sock_id);
594#endif
595  libssh2_session_free(con->session);
596  con=NULL;
597  return true;
598}
599
600/**
601 * Close all the opened SSH connections
602 * @param conf maps pointer to the main configuration maps
603 * @return true in case of success, false if failure occured
604 */
605bool ssh_close(maps* conf){
606  int i,nb_sessions;
607  map* tmp=getMapFromMaps(conf,"lenv","nb_sessions");
608  if(tmp!=NULL){
609    nb_sessions=atoi(tmp->value);
610    for(i=0;i<nb_sessions;i++)
611      ssh_close_session(conf,sessions[i]);
612  }
613  libssh2_exit();
614  return true;
615}
616
617bool addToUploadQueue(maps** conf,maps* input){
618  map* queueMap=getMapFromMaps(*conf,"uploadQueue","length");
619  if(queueMap==NULL){
620    maps* queueMaps=createMaps("uploadQueue");
621    queueMaps->content=createMap("length","0");
622    addMapsToMaps(conf,queueMaps);
623    freeMaps(&queueMaps);
624    free(queueMaps);
625    queueMap=getMapFromMaps(*conf,"uploadQueue","length");
626  }
627  maps* queueMaps=getMaps(*conf,"uploadQueue");
628  int queueIndex=atoi(queueMap->value);
629  if(input!=NULL){
630    if(getMap(input->content,"cache_file")!=NULL){
631      map* length=getMap(input->content,"length");
632      if(length==NULL){
633        addToMap(input->content,"length","1");
634        length=getMap(input->content,"length");
635      }
636      int len=atoi(length->value);
637      int i=0;
638      for(i=0;i<len;i++){
639       
640        map* tmp[2]={getMapArray(input->content,"localPath",i),
641                     getMapArray(input->content,"targetPath",i)};
642
643        setMapArray(queueMaps->content,"input",queueIndex+i,input->name);
644        setMapArray(queueMaps->content,"localPath",queueIndex+i,tmp[0]->value);
645        setMapArray(queueMaps->content,"targetPath",queueIndex+i,tmp[1]->value);
646
647      }
648    }
649  }
650  return true;
651}
652
653int fileMd5Check(maps** conf,const char* localPath,const char* targetPath){
654  if(strstr(localPath,".zca")!=NULL){
655    char *logPath=NULL;
656    char *command=(char*)malloc((strlen(targetPath)+27)*sizeof(char));
657    sprintf(command,"md5sum %s | awk {'print $1'}",targetPath);
658    if(ssh_exec(*conf,command,ssh_get_cnt(*conf))<=0){
659      return -1;
660    }else{
661      struct stat f_status={};
662      map* usid=getMapFromMaps(*conf,"lenv","usid");
663      map* tmpMap=getMapFromMaps(*conf,"main","tmpPath");
664      char* tmpPath=zStrdup(localPath);
665      tmpPath[strlen(tmpPath)-2]='m';
666      tmpPath[strlen(tmpPath)-1]='d';
667      free(command);
668      logPath=(char*)malloc((strlen(tmpMap->value)+strlen(usid->value)+11)*sizeof(char));
669      sprintf(logPath,"%s/exec_out_%s",tmpMap->value,usid->value);
670      int ts=stat(logPath, &f_status);
671      if(ts==0) {
672        char* fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
673        FILE* f=fopen(logPath,"rb");
674        fread(fcontent,f_status.st_size,1,f);
675        fcontent[f_status.st_size-1]=0;
676        fclose(f);
677        free(logPath);
678        struct stat f_status1={};
679        int ts1=stat(tmpPath, &f_status1);
680        if(ts1==0) {
681          char* fcontent1=(char*)malloc(sizeof(char)*(f_status.st_size+1));
682          FILE* f1=fopen(tmpPath,"rb");
683          fread(fcontent1,f_status1.st_size,1,f1);
684          fcontent1[f_status1.st_size]=0;
685          fclose(f1);
686          free(tmpPath);
687          if(strcmp(fcontent,fcontent1)==0){
688            free(fcontent);
689            free(fcontent1);
690            return 0;
691          }else{
692            free(fcontent);
693            free(fcontent1);
694            return -1;
695          }
696        }else{
697          free(tmpPath);
698          free(fcontent);
699          return -1;
700        }       
701      }
702      free(logPath);
703      free(tmpPath);
704    }
705  }
706  return -1;
707}
708
709bool runUpload(maps** conf){
710  SSHCON *test=ssh_connect(*conf);
711  if(test==NULL){
712    return false;
713  }
714  map* queueLengthMap=getMapFromMaps(*conf,"uploadQueue","length");
715  maps* queueMaps=getMaps(*conf,"uploadQueue");
716  if(queueLengthMap!=NULL){
717    int cnt=atoi(queueLengthMap->value);
718    int i=0;
719    for(i=0;i<cnt;i++){
720      map* argv[3]={
721        getMapArray(queueMaps->content,"input",i),
722        getMapArray(queueMaps->content,"localPath",i),
723        getMapArray(queueMaps->content,"targetPath",i)
724      };
725      if(fileMd5Check(conf,argv[1]->value,argv[2]->value)<0){
726        /**/zooLock* lck;
727        if((lck=lockFile(*conf,argv[1]->value,'w'))!=NULL){/**/
728          if(ssh_copy(*conf,argv[1]->value,argv[2]->value,ssh_get_cnt(*conf))!=true){
729            char* templateStr=_("Unable to copy over SSH the file requested for setting the value of %s.");
730            char *tmpMessage=(char*)malloc((strlen(templateStr)+strlen(argv[0]->value)+1)*sizeof(char));
731            sprintf(tmpMessage,templateStr,argv[0]->value);
732            setMapInMaps(*conf,"lenv","message",tmpMessage);
733            free(tmpMessage);
734            unlockFile(*conf,lck);
735            return false;
736          }
737          /**/unlockFile(*conf,lck);
738        }else{
739          setMapInMaps(*conf,"lenv","message",_("Unable to lock the file for upload!"));
740          return false;
741        }/**/
742      }
743    }   
744  }
745  while (libssh2_session_disconnect(test->session, "Normal Shutdown, Thank you for using the ZOO-Project sshapi")
746         == LIBSSH2_ERROR_EAGAIN);
747#ifdef WIN32
748  closesocket(test->sock_id);
749#else
750  close(test->sock_id);
751#endif
752  libssh2_session_free(test->session);
753  free(test);
754  test=NULL;
755  sessions[ssh_get_cnt(*conf)-1]=NULL;
756  maps* tmp=getMaps(*conf,"lenv");
757  addIntToMap(tmp->content,"nb_sessions",ssh_get_cnt(*conf)-1); 
758
759  return true; 
760}
Note: See TracBrowser for help on using the repository browser.

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png