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

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

Fixes for supporting properly the memory=protect which force the ZOO-Kernel to not store any downloaded files in memory. Add footer to the HPC support. Fix the autotools to build service_json and sshapi only when required so, when HPC support is activated, this also avoid adding too much dependencies at compilation time. Store md5 of the downloaded files to avoid uploading on HPC server the same file more than once, in case the md5 correspond.

  • Property svn:keywords set to Id
File size: 20.0 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(10);
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(100);
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  FILE* logFile=fopen(logPath,"wb");
533  free(logPath);
534  while(true){
535    int rc;
536    do {
537      char buffer[0x4000];
538      rc = libssh2_channel_read( channel, buffer, sizeof(buffer) );
539     
540      if( rc > 0 ){
541        int i;
542        bytecount += rc;
543        buffer[rc]=0;
544        fprintf(logFile,"%s",buffer);
545        fflush(logFile);
546      }
547    }
548    while( rc > 0 );
549   
550    if( rc == LIBSSH2_ERROR_EAGAIN ) {
551      waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session);
552    }
553    else
554      break;
555  }
556  fclose(logFile);
557  exitcode = 127;
558  while( (rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN )
559    waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session);
560 
561  if( rc == 0 ) {
562    exitcode = libssh2_channel_get_exit_status( channel );
563    libssh2_channel_get_exit_signal(channel, &exitsignal,
564                                    NULL, NULL, NULL, NULL, NULL);
565  }
566 
567  if (exitsignal)
568    fprintf(stderr, "\nGot signal: %s\n", exitsignal);
569  else
570    fprintf(stderr, "\nEXIT: %d bytecount: %d\n", exitcode, bytecount);
571 
572  libssh2_channel_free(channel);
573
574  return bytecount;
575}
576
577/**
578 * Close an opened SSH connection
579 * @param conf maps pointer to the main configuration maps
580 * @param con SSHCON pointer to the SSH connection
581 * @return true in case of success, false if failure occured
582 */
583bool ssh_close_session(maps* conf,SSHCON* con){
584  if(con==NULL)
585    return true;
586  while (libssh2_session_disconnect(con->session, "Normal Shutdown, Thank you for using the ZOO-Project sshapi")
587         == LIBSSH2_ERROR_EAGAIN);
588#ifdef WIN32
589  closesocket(con->sock_id);
590#else
591  close(con->sock_id);
592#endif
593  libssh2_session_free(con->session);
594  con=NULL;
595  return true;
596}
597
598/**
599 * Close all the opened SSH connections
600 * @param conf maps pointer to the main configuration maps
601 * @return true in case of success, false if failure occured
602 */
603bool ssh_close(maps* conf){
604  int i,nb_sessions;
605  map* tmp=getMapFromMaps(conf,"lenv","nb_sessions");
606  if(tmp!=NULL){
607    nb_sessions=atoi(tmp->value);
608    for(i=0;i<nb_sessions;i++)
609      ssh_close_session(conf,sessions[i]);
610  }
611  libssh2_exit();
612  return true;
613}
614
615bool addToUploadQueue(maps** conf,maps* input){
616  map* queueMap=getMapFromMaps(*conf,"uploadQueue","length");
617  if(queueMap==NULL){
618    maps* queueMaps=createMaps("uploadQueue");
619    queueMaps->content=createMap("length","0");
620    addMapsToMaps(conf,queueMaps);
621    freeMaps(&queueMaps);
622    free(queueMaps);
623    queueMap=getMapFromMaps(*conf,"uploadQueue","length");
624  }
625  maps* queueMaps=getMaps(*conf,"uploadQueue");
626  int queueIndex=atoi(queueMap->value);
627  if(input!=NULL){
628    if(getMap(input->content,"cache_file")!=NULL){
629      map* length=getMap(input->content,"length");
630      if(length==NULL){
631        addToMap(input->content,"length","1");
632        length=getMap(input->content,"length");
633      }
634      int len=atoi(length->value);
635      int i=0;
636      for(i=0;i<len;i++){
637       
638        map* tmp[2]={getMapArray(input->content,"localPath",i),
639                     getMapArray(input->content,"targetPath",i)};
640
641        setMapArray(queueMaps->content,"input",queueIndex+i,input->name);
642        setMapArray(queueMaps->content,"localPath",queueIndex+i,tmp[0]->value);
643        setMapArray(queueMaps->content,"targetPath",queueIndex+i,tmp[1]->value);
644
645      }
646    }
647  }
648  return true;
649}
650
651bool runUpload(maps** conf){
652  SSHCON *test=ssh_connect(*conf);
653  if(test==NULL){
654    return false;
655  }
656  map* queueLengthMap=getMapFromMaps(*conf,"uploadQueue","length");
657  maps* queueMaps=getMaps(*conf,"uploadQueue");
658  if(queueLengthMap!=NULL){
659    int cnt=atoi(queueLengthMap->value);
660    int i=0;
661    for(i=0;i<cnt;i++){
662      map* argv[3]={
663        getMapArray(queueMaps->content,"input",i),
664        getMapArray(queueMaps->content,"localPath",i),
665        getMapArray(queueMaps->content,"targetPath",i)
666      };
667      /**/zooLock* lck;
668      if((lck=lockFile(*conf,argv[1]->value,'w'))!=NULL){/**/
669        if(ssh_copy(*conf,argv[1]->value,argv[2]->value,ssh_get_cnt(*conf))!=true){
670          char* templateStr=_("Unable to copy over SSH the file requested for setting the value of %s.");
671          char *tmpMessage=(char*)malloc((strlen(templateStr)+strlen(argv[0]->value)+1)*sizeof(char));
672          sprintf(tmpMessage,templateStr,argv[0]->value);
673          setMapInMaps(*conf,"lenv","message",tmpMessage);
674          free(tmpMessage);
675          unlockFile(*conf,lck);
676          return false;
677        }
678        /**/unlockFile(*conf,lck);
679      }else{
680        setMapInMaps(*conf,"lenv","message",_("Unable to lock the file for upload!"));
681        return false;
682      }/**/
683    }   
684  }
685  while (libssh2_session_disconnect(test->session, "Normal Shutdown, Thank you for using the ZOO-Project sshapi")
686         == LIBSSH2_ERROR_EAGAIN);
687#ifdef WIN32
688  closesocket(test->sock_id);
689#else
690  close(test->sock_id);
691#endif
692  libssh2_session_free(test->session);
693  free(test);
694  test=NULL;
695  sessions[ssh_get_cnt(*conf)-1]=NULL;
696  maps* tmp=getMaps(*conf,"lenv");
697  addIntToMap(tmp->content,"nb_sessions",ssh_get_cnt(*conf)-1); 
698
699  return true; 
700}
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