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

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

Add the capability to publish heatmap or any templated mapfile using the epecific msInclude and msLayer keys for an output. For MapServer? published output, define 4096 as the default maxsize and use pixel width or height for raster files. use the correct MapServer? imagemode depending on GDALGetRasterDataType (MS_IMAGEMODE_BYTE for GDT_Byte, MS_IMAGEMODE_INT16 for GDT_Int16 and MS_IMAGEMODE_FLOAT32 for GDT_Float32). Create a text file (.maps) listing every mapfiles created for a MapServer? published output (or inputs) using saveMapNames function. Fixes in ulinet, use uuid for naming temporary files. Add dialect input to the ogr2ogr service. Use the .maps file for removing a file from the DeleteData? service

  • 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 
99  map* hpc_host=getMapFromMaps(conf,hpc_config->value,"ssh_host");
100  map* hpc_port=getMapFromMaps(conf,hpc_config->value,"ssh_port");
101  map* hpc_user=getMapFromMaps(conf,hpc_config->value,"ssh_user");
102  map* hpc_password=getMapFromMaps(conf,hpc_config->value,"ssh_password");
103  map* hpc_public_key=getMapFromMaps(conf,hpc_config->value,"ssh_key");
104
105  char ip[100];
106  struct hostent *my_hostent;
107  struct in_addr **addrs;
108 
109  if (hpc_host != NULL) {
110    // Fetch ip address for the hostname
111    if ( (my_hostent = gethostbyname( hpc_host->value ) ) == NULL){
112      herror("gethostbyname");
113      setMapInMaps(conf,"lenv","message",_("Issue when invoking gethostbyname!"));
114      return NULL;
115    }
116 
117    addrs = (struct in_addr **) my_hostent->h_addr_list;
118 
119    for(i = 0; addrs[i] != NULL; i++) {
120      strcpy(ip , inet_ntoa(*addrs[i]) );
121      break;
122    }
123  }
124
125#ifdef WIN32
126  WSADATA wsadata;
127  int err;
128  err = WSAStartup(MAKEWORD(2,0), &wsadata);
129  if (err != 0) {
130    sprintf(error, "WSAStartup failed with error: %d\n", err);
131    setMapInMaps(conf,"lenv","message",error);
132    return NULL;
133  }
134#endif
135
136  if (hpc_host != NULL) {
137    hostaddr = inet_addr(ip);
138  } else {
139    setMapInMaps(conf,"lenv","message","No host parameter found in your main.cfg file!\n");
140    return NULL;
141  }
142
143  // Default port is 22
144  if(hpc_port!=NULL){
145    port=atoi(hpc_port->value);
146  }
147
148  // In case there is no HPC > user the it must failed
149  if (hpc_user != NULL) {
150    user = hpc_user->value;
151  }else{
152    setMapInMaps(conf,"lenv","message","No user parameter found in your main.cfg file!");
153    return NULL;
154  }
155
156  // TODO: in case password is available but there is also the public key
157  // defined, then we can consider this password as the pass phrase.
158  if (hpc_password != NULL) {
159    password = hpc_password->value;
160  }else{
161    use_pw=-1;
162    if (hpc_public_key != NULL) {
163      public_key = hpc_public_key->value;
164      private_key = strdup(hpc_public_key->value);
165      private_key[strlen(public_key)-4]=0;
166    }else{
167      setMapInMaps(conf,"lenv","message","No method found to authenticate!");
168      return NULL;
169    }
170  }
171
172  rc = libssh2_init (0);
173  if (rc != 0) {
174    sprintf (error, "libssh2 initialization failed (%d)\n", rc);
175    setMapInMaps(conf,"lenv","message",error);
176    return NULL;
177  }
178
179  result->sock_id = socket(AF_INET, SOCK_STREAM, 0);
180  sin.sin_family = AF_INET;
181  sin.sin_port = htons(port);
182  sin.sin_addr.s_addr = hostaddr;
183  if (connect(result->sock_id, (struct sockaddr*)(&sin),sizeof(struct sockaddr_in)) != 0) {
184    setMapInMaps(conf,"lenv","message","Failed to connect to remote host!");
185    return NULL;
186  }
187
188  result->session = libssh2_session_init();
189  result->sftp_session = NULL;
190  map* tmp=getMapFromMaps(conf,"lenv","nb_sessions");
191  if(tmp!=NULL){
192    char nb_sessions[10];
193    int nb_sess=atoi(tmp->value);
194    sprintf(nb_sessions,"%d",nb_sess+1);
195    setMapInMaps(conf,"lenv","nb_sessions",nb_sessions);
196    result->index=nb_sess+1;
197    sessions[nb_sess+1]=result;
198  }else{
199    setMapInMaps(conf,"lenv","nb_sessions","0");
200    sessions[0]=result;
201    result->index=0;
202  }
203
204  if (!result->session)
205    return NULL;
206
207  libssh2_session_set_blocking(result->session, 1);
208
209  while ((rc = libssh2_session_handshake(result->session, result->sock_id))
210         == LIBSSH2_ERROR_EAGAIN);
211
212  if (rc) {
213    sprintf(error, "Failure establishing SSH session: %d\n", rc);
214    setMapInMaps(conf,"lenv","message",error);
215    return NULL;
216  }
217
218  fingerprint = libssh2_hostkey_hash(result->session, LIBSSH2_HOSTKEY_HASH_SHA1);
219 
220  if (use_pw>0) {
221    while ((rc = libssh2_userauth_password(result->session, user, password)) ==
222           LIBSSH2_ERROR_EAGAIN);
223    if (rc) {
224      setMapInMaps(conf,"lenv","message","Authentication by password failed.");
225      ssh_close(conf);
226      return NULL;
227    }
228  } else {
229    while ((rc = libssh2_userauth_publickey_fromfile(result->session, user,
230                                                     public_key,
231                                                     private_key,
232                                                     password)) ==
233           LIBSSH2_ERROR_EAGAIN);
234    if (rc) {
235      setMapInMaps(conf,"lenv","message","Authentication by public key failed");
236      ssh_close(conf);
237      return NULL;
238    }
239    free(private_key);
240  }
241
242  return result;
243
244}
245
246/**
247 * Get the number of opened SSH connections
248 * @param conf maps pointer to the main configuration maps
249 * @return the number of opened SSH connections
250 */
251int ssh_get_cnt(maps* conf){
252  int result=0;
253  map* myMap=getMapFromMaps(conf,"lenv","nb_sessions");
254  if(myMap!=NULL){
255    result=atoi(myMap->value);
256  }
257  return result;
258}
259
260/**
261 * Verify if a file exists on the remote host
262 * @param conf maps pointer to the main configuration maps
263 * @param targetPath const char* defining the path for storing the file on the
264 * remote host
265 * @return true in case of success, false if failure occured
266 */
267size_t ssh_file_exists(maps* conf,const char* targetPath,int cnt){
268  size_t result=-1;
269  if(cnt>0)
270    cnt-=1;
271  int rc;
272  LIBSSH2_SFTP_ATTRIBUTES attrs;
273  LIBSSH2_SFTP_HANDLE *sftp_handle;
274  do{
275    sftp_handle =
276      libssh2_sftp_open(sessions[cnt]->sftp_session, targetPath, LIBSSH2_FXF_READ,
277                        LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
278                        LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
279    if (!sftp_handle) {
280      fprintf(stderr, "Unable to open file with SFTP: %d %ld\n",__LINE__,
281              libssh2_sftp_last_error(sessions[cnt]->sftp_session));
282      return 0;
283    }
284  }while(!sftp_handle);
285#ifdef SSH_DEBUG
286  fprintf(stderr, "libssh2_sftp_open() is done, get file information\n");
287#endif
288  do {
289  rc = libssh2_sftp_stat_ex(sessions[cnt]->sftp_session, targetPath, strlen(targetPath),
290                            LIBSSH2_SFTP_LSTAT, &attrs );
291  if (rc<0 &&
292      (libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN))
293    {
294      fprintf(stderr, "error trying to fstat_ex, returned %d\n", rc);
295      break;
296    }
297  else
298    {
299#ifdef SSH_DEBUG
300      fprintf(stderr, "Stat Data: RetCode=%d\n", rc);
301      fprintf(stderr, "Stat Data: Size=%llu\n", attrs.filesize);
302      fprintf(stderr, "Stat Data: Perm=%lx\n",  attrs.permissions);
303      fprintf(stderr, "Stat Data: mtime=%lu\n",  attrs.mtime);
304#endif
305      if(rc==0)
306        break;
307      result=attrs.filesize;
308    }
309  } while (true);
310  libssh2_sftp_close(sftp_handle);
311  //libssh2_sftp_shutdown(sessions[cnt]->sftp_session);
312
313  return result;
314}
315
316/**
317 * Upload a file over an opened SSH connection
318 * @param conf maps pointer to the main configuration maps
319 * @param localPath const char* defining the local path for accessing the file
320 * @param targetPath const char* defining the path for storing the file on the
321 * remote host
322 * @return true in case of success, false if failure occured
323 */
324bool ssh_copy(maps* conf,const char* localPath,const char* targetPath,int cnt){
325  char mem[1024 * 1000];
326  size_t nread;
327  size_t memuse=0;
328  time_t start;
329  long total = 0;
330  int duration;
331  int rc;
332  LIBSSH2_SFTP_HANDLE *sftp_handle;
333  //map* myMap=getMapFromMaps(conf,"lenv","cnt_session");
334  if(getMapFromMaps(conf,"lenv","cnt_session")!=NULL){
335    char tmp[10];
336    sprintf(tmp,"%d",cnt+1);
337    setMapInMaps(conf,"lenv","cnt_session",tmp);
338  }else
339    setMapInMaps(conf,"lenv","cnt_session","0");
340  FILE *local = fopen(localPath, "rb");
341  if (!local) {
342    fprintf(stderr, "Can't open local file %s\n", localPath);
343    return false;
344  }
345 
346  do {
347    sessions[cnt]->sftp_session = libssh2_sftp_init(sessions[cnt]->session);
348    if (!sessions[cnt]->sftp_session &&
349        (libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN)) {
350     
351      fprintf(stderr, "Unable to init SFTP session\n");
352      return false;
353    }
354    if(!sessions[cnt]->sftp_session)
355      zSleep(10);
356  } while (!sessions[cnt]->sftp_session);
357
358  do {
359    sftp_handle =
360      libssh2_sftp_open(sessions[cnt]->sftp_session, targetPath,
361                        LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
362                        LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
363                        LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
364
365    if (!sftp_handle &&
366        (libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN)) {
367     
368      fprintf(stderr, "Unable to open file with SFTP\n");
369      return false;
370    }
371    if(!sftp_handle)
372      zSleep(10);
373  } while (!sftp_handle);
374  start = time(NULL);
375 
376  do {
377    nread = fread(&mem[memuse], 1, sizeof(mem)-memuse, local);
378    if (nread <= 0) {
379      if (memuse > 0)
380        nread = 0;
381      else
382        break;
383    }
384    memuse += nread;
385    total += nread;
386   
387    while ((rc = libssh2_sftp_write(sftp_handle, mem, memuse)) ==
388           LIBSSH2_ERROR_EAGAIN) {
389      waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session);
390    }
391    if(rc < 0)
392      break;
393   
394    if(memuse - rc) {
395      memmove(&mem[0], &mem[rc], memuse - rc);
396      memuse -= rc;
397    }
398    else
399      memuse = 0;
400   
401  } while (rc > 0);
402 
403  duration = (int)(time(NULL)-start);
404  fclose(local);
405  libssh2_sftp_close_handle(sftp_handle);
406
407  libssh2_sftp_shutdown(sessions[cnt]->sftp_session);
408  return true;
409}
410
411/**
412 * Download a file over an opened SSH connection
413 * @param conf maps pointer to the main configuration maps
414 * @param localPath const char* defining the local path for storing the file
415 * @param targetPath const char* defining the path for accessing the file on the
416 * remote host
417 * @return 0 in case of success, -1 if failure occured
418 */
419int ssh_fetch(maps* conf,const char* localPath,const char* targetPath,int cnt){
420  size_t nread;
421  size_t memuse=0;
422  time_t start;
423  long total = 0;
424  int duration;
425  int rc;
426  LIBSSH2_SFTP_HANDLE *sftp_handle;
427  FILE *local = fopen(localPath, "wb");
428  if (!local) {
429    fprintf(stderr, "Can't open local file %s\n", localPath);
430    return -1;
431  }
432
433  do {
434    sessions[cnt]->sftp_session = libssh2_sftp_init(sessions[cnt]->session);
435    if (!sessions[cnt]->sftp_session &&
436        (libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN)) {
437      fprintf(stderr, "Unable to init SFTP session\n");
438      return -1;
439    }
440    if(!sessions[cnt]->sftp_session)
441      zSleep(10);
442  } while (!sessions[cnt]->sftp_session);
443  do {
444    sftp_handle = libssh2_sftp_open(sessions[cnt]->sftp_session, targetPath,   
445                                    LIBSSH2_FXF_READ, 0);
446    if (!sftp_handle) {
447      if (libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN) {
448        fprintf(stderr, " ** Unable to open file with SFTP\n");
449        return -1;
450      }
451      else {
452        waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session); 
453      }
454    }
455    if(!sftp_handle)
456      zSleep(100);
457  } while (!sftp_handle);
458 
459  int result=0;
460  do {
461    do {
462      char* mem=(char*)malloc(16*1024*1024);
463      rc = libssh2_sftp_read(sftp_handle, mem,16*1024*1024);
464      if(rc > 0) {
465        fwrite(mem, rc, 1, local);
466      }
467      free(mem);
468    } while (rc > 0);
469   
470    if(rc != LIBSSH2_ERROR_EAGAIN) {
471      result=-1;
472      break;
473    }
474   
475    struct timeval timeout;
476    fd_set fd;
477    timeout.tv_sec = 10;
478    timeout.tv_usec = 0;
479 
480    FD_ZERO(&fd);
481 
482    FD_SET(sessions[cnt]->sock_id, &fd);
483 
484    rc = select(sessions[cnt]->sock_id+1, &fd, &fd, NULL, &timeout);
485    if(rc <= 0) {
486      if(rc==0)
487        fprintf(stderr, "SFTP download timed out: %d\n", rc);
488      else
489        fprintf(stderr, "SFTP download error: %d\n", rc);
490      return -1;
491    }
492   
493  } while (1);
494  duration = (int)(time(NULL)-start);
495  fclose(local);
496  libssh2_sftp_close_handle(sftp_handle);
497  libssh2_sftp_shutdown(sessions[cnt]->sftp_session);
498  return 0;
499}
500
501/**
502 * Execute a command over an opened SSH connection
503 * @param conf maps pointer to the main configuration maps
504 * @param command const char pointer to the command to be executed
505 * @return bytecount resulting from the execution of the command
506 */
507int ssh_exec(maps* conf,const char* command,int cnt){
508  LIBSSH2_CHANNEL *channel;
509  int rc;
510  int bytecount = 0;
511  int exitcode;
512  char *exitsignal=(char *)"none";
513  while( (channel = libssh2_channel_open_session(sessions[cnt]->session)) == NULL &&
514         libssh2_session_last_error(sessions[cnt]->session,NULL,NULL,0) == LIBSSH2_ERROR_EAGAIN ) {
515    waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session);
516  }
517  if( channel == NULL ){
518    fprintf(stderr,"Error\n");
519    return -1;
520  }
521  while( (rc = libssh2_channel_exec(channel, command)) == LIBSSH2_ERROR_EAGAIN ) {
522    waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session);
523  }
524  if( rc != 0 ) {
525    fprintf(stderr,"Error\n");
526    return -1;
527  }
528
529  map* tmpPath=getMapFromMaps(conf,"main","tmpPath");
530  map* uuid=getMapFromMaps(conf,"lenv","usid");
531  char *logPath=(char*)malloc((strlen(tmpPath->value)+strlen(uuid->value)+11)*sizeof(char));
532  sprintf(logPath,"%s/exec_out_%s",tmpPath->value,uuid->value);
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        fprintf(logFile,"%s",buffer);
546        fflush(logFile);
547      }
548    }
549    while( rc > 0 );
550   
551    if( rc == LIBSSH2_ERROR_EAGAIN ) {
552      waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session);
553    }
554    else
555      break;
556  }
557  fclose(logFile);
558  exitcode = 127;
559  while( (rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN )
560    waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session);
561 
562  if( rc == 0 ) {
563    exitcode = libssh2_channel_get_exit_status( channel );
564    libssh2_channel_get_exit_signal(channel, &exitsignal,
565                                    NULL, NULL, NULL, NULL, NULL);
566  }
567 
568  if (exitsignal)
569    fprintf(stderr, "\nGot signal: %s\n", exitsignal);
570  else
571    fprintf(stderr, "\nEXIT: %d bytecount: %d\n", exitcode, bytecount);
572 
573  libssh2_channel_free(channel);
574
575  return bytecount;
576}
577
578/**
579 * Close an opened SSH connection
580 * @param conf maps pointer to the main configuration maps
581 * @param con SSHCON pointer to the SSH connection
582 * @return true in case of success, false if failure occured
583 */
584bool ssh_close_session(maps* conf,SSHCON* con){
585  if(con==NULL)
586    return true;
587  while (libssh2_session_disconnect(con->session, "Normal Shutdown, Thank you for using the ZOO-Project sshapi")
588         == LIBSSH2_ERROR_EAGAIN);
589#ifdef WIN32
590  closesocket(con->sock_id);
591#else
592  close(con->sock_id);
593#endif
594  libssh2_session_free(con->session);
595  con=NULL;
596  return true;
597}
598
599/**
600 * Close all the opened SSH connections
601 * @param conf maps pointer to the main configuration maps
602 * @return true in case of success, false if failure occured
603 */
604bool ssh_close(maps* conf){
605  int i,nb_sessions;
606  map* tmp=getMapFromMaps(conf,"lenv","nb_sessions");
607  if(tmp!=NULL){
608    nb_sessions=atoi(tmp->value);
609    for(i=0;i<nb_sessions;i++)
610      ssh_close_session(conf,sessions[i]);
611  }
612  libssh2_exit();
613  return true;
614}
615
616bool addToUploadQueue(maps** conf,maps* input){
617  map* queueMap=getMapFromMaps(*conf,"uploadQueue","length");
618  if(queueMap==NULL){
619    maps* queueMaps=createMaps("uploadQueue");
620    queueMaps->content=createMap("length","0");
621    addMapsToMaps(conf,queueMaps);
622    freeMaps(&queueMaps);
623    free(queueMaps);
624    queueMap=getMapFromMaps(*conf,"uploadQueue","length");
625  }
626  maps* queueMaps=getMaps(*conf,"uploadQueue");
627  int queueIndex=atoi(queueMap->value);
628  if(input!=NULL){
629    if(getMap(input->content,"cache_file")!=NULL){
630      map* length=getMap(input->content,"length");
631      if(length==NULL){
632        addToMap(input->content,"length","1");
633        length=getMap(input->content,"length");
634      }
635      int len=atoi(length->value);
636      int i=0;
637      for(i=0;i<len;i++){
638       
639        map* tmp[2]={getMapArray(input->content,"localPath",i),
640                     getMapArray(input->content,"targetPath",i)};
641
642        setMapArray(queueMaps->content,"input",queueIndex+i,input->name);
643        setMapArray(queueMaps->content,"localPath",queueIndex+i,tmp[0]->value);
644        setMapArray(queueMaps->content,"targetPath",queueIndex+i,tmp[1]->value);
645
646      }
647    }
648  }
649  return true;
650}
651
652bool runUpload(maps** conf){
653  SSHCON *test=ssh_connect(*conf);
654  if(test==NULL){
655    return false;
656  }
657  map* queueLengthMap=getMapFromMaps(*conf,"uploadQueue","length");
658  maps* queueMaps=getMaps(*conf,"uploadQueue");
659  if(queueLengthMap!=NULL){
660    int cnt=atoi(queueLengthMap->value);
661    int i=0;
662    for(i=0;i<cnt;i++){
663      map* argv[3]={
664        getMapArray(queueMaps->content,"input",i),
665        getMapArray(queueMaps->content,"localPath",i),
666        getMapArray(queueMaps->content,"targetPath",i)
667      };
668      /**/zooLock* lck;
669      if((lck=lockFile(*conf,argv[1]->value,'w'))!=NULL){/**/
670        if(ssh_copy(*conf,argv[1]->value,argv[2]->value,ssh_get_cnt(*conf))!=true){
671          char* templateStr=_("Unable to copy over SSH the file requested for setting the value of %s.");
672          char *tmpMessage=(char*)malloc((strlen(templateStr)+strlen(argv[0]->value)+1)*sizeof(char));
673          sprintf(tmpMessage,templateStr,argv[0]->value);
674          setMapInMaps(*conf,"lenv","message",tmpMessage);
675          free(tmpMessage);
676          unlockFile(*conf,lck);
677          return false;
678        }
679        /**/unlockFile(*conf,lck);
680      }else{
681        setMapInMaps(*conf,"lenv","message",_("Unable to lock the file for upload!"));
682        return false;
683      }/**/
684    }   
685  }
686  while (libssh2_session_disconnect(test->session, "Normal Shutdown, Thank you for using the ZOO-Project sshapi")
687         == LIBSSH2_ERROR_EAGAIN);
688#ifdef WIN32
689  closesocket(test->sock_id);
690#else
691  close(test->sock_id);
692#endif
693  libssh2_session_free(test->session);
694  free(test);
695  test=NULL;
696  sessions[ssh_get_cnt(*conf)-1]=NULL;
697  maps* tmp=getMaps(*conf,"lenv");
698  addIntToMap(tmp->content,"nb_sessions",ssh_get_cnt(*conf)-1); 
699
700  return true; 
701}
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