Commit 92654638 authored by Pontus Sköld's avatar Pontus Sköld Committed by Niels Möller

Checked in Pontus Sköld's lsftp client.

* src/sftp/Makefile.am (lsftp_SOURCES):
* src/sftp/commands.c: New file.
* src/sftp/commands.h: New file.
* src/sftp/dc.c: New file.
* src/sftp/dc.h: New file.
* src/sftp/lsftp.c: New file.
* src/sftp/lsftp.h: New file.
* src/sftp/misc_fun.c: New file.
* src/sftp/misc_fun.h: New file.
* src/sftp/rl.c: New file.
* src/sftp/rl.h: New file.
* src/sftp/sftp_bind.c: New file.
* src/sftp/sftp_bind.h: New file.
* src/sftp/sftp_c.c: New file.
* src/sftp/sftp_c.h: New file.
* src/sftp/str_utils.c: New file.
* src/sftp/str_utils.h: New file.

Rev: src/sftp/commands.c:1.1
Rev: src/sftp/commands.h:1.1
Rev: src/sftp/dc.c:1.1
Rev: src/sftp/dc.h:1.1
Rev: src/sftp/lsftp.c:1.1
Rev: src/sftp/lsftp.h:1.1
Rev: src/sftp/misc_fun.c:1.1
Rev: src/sftp/misc_fun.h:1.1
Rev: src/sftp/rl.c:1.1
Rev: src/sftp/rl.h:1.1
Rev: src/sftp/sftp_bind.c:1.1
Rev: src/sftp/sftp_bind.h:1.1
Rev: src/sftp/sftp_c.c:1.1
Rev: src/sftp/sftp_c.h:1.1
Rev: src/sftp/str_utils.c:1.1
Rev: src/sftp/str_utils.h:1.1
parent 9e95d96c
This diff is collapsed.
/*
* @(#) $Id$
*
* commands.h
*/
/* lsftp, an implementation of the sftp protocol
*
* Copyright (C) 2001, Pontus Sköld
* Portions of this code originately from the readline manual
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef LSFTP_COMMANDS_H
#define LSFTP_COMMANDS_H
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <glob.h>
#include <limits.h>
#include <ctype.h>
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include "sftp_bind.h"
#include "dc.h"
#include "str_utils.h"
#include "misc_fun.h"
#define NOCOMMAND -4710
#define UNKNOWNCOMMAND -4711
#define WORDLENMAX 256
extern int mainloop; /* Flag to end mainloop */
extern int sloppy_complete;
extern int buggy_server_treshold;
/* Public functions */
int handle_command(char* s);
/* Internal below */
int com_help( char* arg, char* command );
int com_about( char* arg, char* command );
int com_escape( char* arg, char* command);
int com_quit( char* arg, char* command );
int com_jobs( char* arg, char* command );
int com_close( char* arg, char* command );
int com_open( char* arg, char* command );
int com_cd( char* arg, char* command );
int com_rm( char* arg, char* command );
int com_ls( char* arg, char* command );
int com_pwd( char* arg, char* command );
int com_mv( char* arg, char* command );
int com_ln( char* arg, char* command );
int com_mkdir( char* arg, char* command );
int com_set( char* arg, char* command );
int com_mail( char* arg, char* command );
int com_umask( char* arg, char* command );
int com_get( char* arg, char* command );
int com_put( char* arg, char* command );
int com_lcd( char* arg, char* command );
int com_lrm( char* arg, char* command );
int com_lls( char* arg, char* command );
int com_lpwd( char* arg, char* command );
int com_lmv( char* arg, char* command );
int com_chown( char* arg, char* command );
int com_chgrp( char* arg, char* command );
enum takes_as_arg
{
COMMAND,
LOCALFILE,
REMOTEFILE,
NOARG,
JOBID,
OTHERARG
};
typedef struct
{
char *name; /* User printable name of the function. */
int (*func)(); /* Function to call to do the job. */
char *doc; /* Documentation for this function. */
char* longdoc; /* Longer help to display for help command */
enum takes_as_arg arg_type;
int visible;
int uniquelen;
} command;
command commands[35];
#endif
This diff is collapsed.
/*
* @(#) $Id$
*
* dc.h - dircache functions
*
*/
/* lsftp, an implementation of the sftp protocol
*
* Copyright (C) 2001 Pontus Skld
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef LSFTP_DC_H
#define LSFTP_DC_H
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <glob.h>
#ifdef HAVE_FNMATCH
#include <fnmatch.h>
#endif
#include "sftp_bind.h"
int lsftp_dc_notice( char* name, struct sftp_attrib* a );
int lsftp_dc_remove( char* name );
int lsftp_dc_hash( char* name );
int lsftp_dc_glob_matches( char* fname, char* glob, int period );
int lsftp_dc_l_isdir( char* name );
int lsftp_dc_r_isdir( char* name );
int lsftp_dc_index( char* name );
int lsftp_dc_init( int new_dc_entries );
void lsftp_dc_uninit();
char** lsftp_dc_r_startglob( char* fname, int sloppy, int nocheck );
char** lsftp_dc_r_contglob( char* fname, char** globdata, int nocheck );
char** lsftp_dc_l_startglob( char* fname, int nocheck );
char** lsftp_dc_l_contglob( char* fname, char** globdata, int nocheck );
void lsftp_dc_endglob( char** globdata );
int lsftp_dc_numglob( char** globdata );
struct lsftp_dc_s {
char* name;
int hash;
mode_t posixperms;
};
#endif
/*
* @(#) $Id$
*
* lsftp.c
*/
/* lsftp, an implementation of the sftp protocol
*
* Copyright (C) 2001, Pontus Skld
* Portions of this code originately from the readline manual
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "lsftp.h"
int mainloop; /* "Continue run" flag */
int interactive; /* Interactive mode flag */
int lsftp_num_commands( int argc, char** argv )
{
/*
* Returns how many trailing parameters to skip when
* starting the SecSH client
*
*/
int i = argc - 1; /* First element to investigate (argv[argc] should be NULL) */
while( i > 0 )
if( strcmp( LSFTP_COMMAND_MARKER, argv[i] ) ) /* Not a marker? */
i--;
else
break;
if( i ) /* Found a marker? */
return argc - i; /* The number of parameters not to pass to secsh is argc - i */
return 0;
}
void lsftp_interactive_mainloop()
{
char* current_line;
int fail = 0;
mainloop=1;
while( mainloop && !fail )
{
fd_set rfds;
fd_set wfds;
struct timeval tv;
int retval;
lsftp_rl_init_get_line( "command> " );
while( !lsftp_rl_line_done )
{
int lsh_w = lsftp_fd_write_net();
int lsh_r = lsftp_fd_read_net();
int try_write = 0;
int n;
FD_ZERO( &rfds );
FD_ZERO( &wfds );
/* Watch stdin (fd 0) to see when it has input. */
FD_SET( 0, &rfds );
if( lsftp_connected() ) /* Only if connected */
{
/* Anything from lsh would also be interesting */
FD_SET( lsh_r, &rfds );
/* As would (pherhaps) if we can write to lsh */
if( lsftp_want_to_write() )
{
FD_SET( lsh_w, &wfds );
try_write = 1;
}
tv.tv_sec = 10;
tv.tv_usec = 0;
if(
try_write &&
lsh_w > lsh_r
) /* Calculate the n argument to select */
n = lsh_w + 1;
else
n = lsh_r + 1;
}
else
n = 1;
retval = select(n, &rfds, &wfds, NULL, &tv);
if( FD_ISSET( 0, &rfds ) )
lsftp_rl_check_input();
if( lsftp_connected() )
if( FD_ISSET( lsh_r, &rfds ) ||
(
try_write &&
FD_ISSET( lsh_w, &wfds )
)
)
if( -1 == lsftp_callback() )
fail++;
}
current_line = lsftp_rl_line;
handle_command( current_line );
free( current_line );
}
}
void lsftp_noninteractive_mainloop( int startat, char** argv)
{
int done = 0;
int fail = 0;
mainloop = 1;
while( !done && !fail )
{
while( !fail && lsftp_active_cbs() )
if( -1 == lsftp_callback() ) /* Checks itself with select */
{
printf( "Failure!\n");
fail++;
}
if( argv[startat] && mainloop ) /* More commands? (no quit) */
handle_command( argv[startat++] ); /* Do it */
else
done = 1; /* Finito */
}
}
int main(int argc, char** argv, char** envp)
{
int i;
lsftp_rl_init(); /* Safe even if built for noninteractive use only */
mgetenv_init( envp ); /* Tell mgetenv about environment parameters */
i = lsftp_num_commands( argc, argv );
if( i > 1 ) /* Anything after the last marker? */
interactive = 0; /* Non interactive mode */
if( interactive )
lsftp_welcome();
if( 1 < argc - i ) /* Only connect if given arguments */
{
lsftp_open_connection( argv, argc - i );
lsftp_handshake();
lsftp_sftp_init();
lsftp_do_cd( "" ); /* Change to our home directory */
}
lsftp_dc_init( 1024 );
if( interactive )
lsftp_interactive_mainloop();
else
if( i )
lsftp_noninteractive_mainloop( argc - i + 1, argv );
else
printf( "%s seems to be built for noninteractive mode only,\n"
"but you gave no command.\n", PACKAGE);
lsftp_rl_exit();
lsftp_close_connection();
return 0;
}
/*
* @(#) $Id$
*
* lsftp.h
*/
/* lsftp, an implementation of the sftp protocol
*
* Copyright (C) 2001 Pontus Sköld
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef LSFTP_LSFTP_H
#define LSFTP_LSFTP_H
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "rl.h"
#include "str_utils.h"
#include "gnu_stuff.h"
#include <stdio.h>
#include "sftp_bind.h"
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
extern int lsftp_rl_line_done;
extern char* lsftp_rl_line;
#define LSFTP_COMMAND_MARKER "--" /* Used to separate ssh-parameters and lsftp commands */
#endif
/*
* @(#) $Id$
*
* misc_fun.c
*/
/* lsftp, an implementation of the sftp protocol
*
* Copyright (C) 2001 Pontus Sköld
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "misc_fun.h"
#include <stdlib.h>
#ifndef HAVE_GETENV
char** environ;
#endif
char* mgetenv( char* var )
{
#ifdef HAVE_GETENV
/* If the system provides a getenv function, use it */
return getenv(var);
#else
char* env;
char** lenviron;
int varlen;
lenviron=environ;
valren=strlen(var);
while( ( env = *lenviron++) ) /* Get next enviroment variable */
if( !( strncmp( var, env, varlen ) )) /* Found it? (ignores A==B and AX=B) */
return lsftp_s_skipn( env, "=" )+1;
/* We return whatever is after the equal sign */
return NULL;
#endif /* HAVE_GETENV */
}
void mgetenv_init( char** envp )
{
#ifndef HAVE_GETENV
environ = envp;
#endif
}
char* massage_fname(char* fname, int local)
{
return fname;
}
char* filename_part( char* s )
{
int i = strlen(s);
while( i >= 0 && s[--i] != '/' ) /* Search backwards for a directory separator, stop at the start of the string */
;
if( !s[i+1] ) /* String ended with slash? */
while( i >= 0 && s[--i] != '/' ); /* Redo it */
return s + i + 1;
}
char* lsftp_concat( const char* s1, const char* s2 )
{
int i1,i2;
char* ret;
i1 = strlen( s1 );
i2 = strlen( s2 );
ret = malloc( i1 + i2 + 1 ); /* Get mem */
if( !ret )
return 0;
strncpy( ret, s1, i1 );
strncpy( ret+i1, s2, i2 );
ret[i1+i2] = 0; /* Make sure it's null terminated */
return ret;
}
char* lsftp_skip_common( char* s1, char* s2 )
{
/* Returns the adress of the first charater in s1 not in s2 */
int i = 0;
while( s1[i] && s2[i] && (s1[i] == s2[i]) ) /* Neither string has terminated */
i++;
return s1+i;
}
int lsftp_get_opts( char* options, char* string )
{
/* Search string for charaters in options and return a bitmap of which options were found
*/
int set = 0;
int last;
int sindex = 1;
char c;
if( !options || !options[0] || !string || '-' != string[0] ) /* Nothing to do? */
return 0;
last = strlen( options ) - 1; /* Index of last charater in options */
while( ( c = string[sindex++] ) ) /* Get next (or terminate if finished) */
{
int i = 0;
while( 0 <= last - i )
if( c == options[last - i++] ) /* Search options from right to left for c */
set |= ( 1 << (i-1) ); /* Set corresponding bit in set */
}
return set;
}
int FATAL( char* msg )
{
fprintf( stderr, "Fatal error: %s\n", msg );
exit( 1 );
return 0;
}
/*
* @(#) $Id$
*
* misc_fun.h
*/
/* lsftp, an implementation of the sftp protocol
*
* Copyright (C) 2001 Pontus Skld
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef LSFTP_MISC_FUN_H
#define LSFTP_MISC_FUN_H
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
char* mgetenv( char* var );
void mgetenv_init( char** envp );
char* massage_fname( char* fname, int local );
char* filename_part( char* s );