source: TI05-delivery/trunk/src/bbftp-server-3.2.0/bbftpd/sendlist.c @ 773

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/trunk/src/bbftp-server-3.2.0/bbftpd/sendlist.c@983
Revision 773, 4.8 KB checked in by spascoe, 14 years ago (diff)

Initial import of bbftp source

Line 
1/*
2 * bbftpd/sendlist.c
3 * Copyright (C) 1999, 2000, 2001, 2002 IN2P3, CNRS
4 * bbftp@in2p3.fr
5 * http://doc.in2p3.fr/bbftp
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */ 
21
22/****************************************************************************
23
24 
25 RETURN:
26        0  Keep the connection open (does not mean that the list has been
27           successfully created)
28        -1 Tell the calling program to close the connection
29 
30 sendlist.c v 1.8.8  2000/06/05 - Creation of the routine.
31            v 1.8.9  2000/08/08 - Supress debug.
32            v 1.8.10 2000/08/11 - Portage to Linux.
33            v 1.9.0  2000/08/18 - Use configure to help portage
34            v 1.9.4  2000/08/18 - Correct typo
35            v 2.0.1  2001/04/23 - Correct indentation
36            v 2.1.0  2001/05/30 - Correct syslog level
37                         
38 *****************************************************************************/
39#include <errno.h>
40#include <fcntl.h>
41#include <glob.h>
42#include <netinet/in.h>
43#include <stdio.h>
44#include <syslog.h>
45#include <sys/stat.h>
46#include <sys/types.h>
47#include <unistd.h>
48
49#include <bbftpd.h>
50#include <common.h>
51#include <daemon.h>
52#include <structures.h>
53#ifdef DARWIN
54#define GLOB_NOMATCH -3
55#endif
56
57
58extern int msgsock ;
59extern  int     recvcontrolto ;
60
61int sendlist (int code, int msglen) {
62
63    char    receive_buffer[MAXMESSLEN] ;
64    char    send_buffer[MAXMESSLEN];
65    char    logmessage[256] ;
66    glob_t pg ;
67    struct message *msg ;
68    int        i ; 
69    int        retcode ;
70    char    *tmpfile ;
71
72#ifndef WORDS_BIGENDIAN
73    msglen = ntohl(msglen) ;
74#endif
75    if ( msglen > MAXMESSLEN ) {
76        /*
77        ** In order to avoid buffer overflow we reject message to
78        ** big
79        */
80        syslog(BBFTPD_ERR,"Message to big in sendlist (%d,%d)",msglen,MAXMESSLEN) ;
81        reply(MSG_BAD_NO_RETRY,"String too long") ;
82        return -1 ;
83    }
84    /*
85    ** Read the characteristics of the filelist to send
86    */
87    if ( readmessage(msgsock,receive_buffer,msglen,recvcontrolto) < 0 ) {
88        /*
89        ** Error ...
90        */
91        return -1 ;
92    }
93    /*
94    ** receive_buffer contains the directory to create
95    */
96    receive_buffer[msglen] = '\0' ;
97    if ( code == MSG_LIST ) {
98        syslog(BBFTPD_DEBUG,"Getting file list %s",receive_buffer) ;
99    }
100    /*
101    ** We get the filelist
102    */
103    if ( (retcode = glob(receive_buffer,0,0,&pg)) < 0 ) {
104        if ( retcode == GLOB_NOMATCH ) {
105            /*
106            ** Just reply with a message MESS_LIST_REPL with msglen equal to zero
107            */
108            globfree(&pg) ;
109            msg = (struct message *) send_buffer ;
110            msg->code = MSG_LIST_REPL ;
111            msg->msglen = 0 ;
112            if ( writemessage(msgsock,send_buffer,MINMESSLEN,recvcontrolto) < 0 ) {
113                syslog(BBFTPD_ERR,"Error sending LISTREPL part 1") ;
114                return -1 ;
115            }
116            return 0 ;
117        } else {
118            globfree(&pg) ;
119            sprintf(logmessage,"Error during glob call %d",retcode) ;
120            reply(MSG_BAD,logmessage) ;
121            return 0 ;
122        }
123    } else {
124        /*
125        ** The glob call was successfull
126        */
127        /*
128        ** Calculate the total length of filename
129        */
130        msg = (struct message *) send_buffer ;
131        msg->code = MSG_LIST_REPL ;
132        msg->msglen = 0 ;
133        for (i = 0 ; i < pg.gl_pathc ; i++ ) {
134            tmpfile = pg.gl_pathv[i] ;
135            msg->msglen = msg->msglen+strlen(tmpfile)+1 ;
136        }
137#ifndef WORDS_BIGENDIAN
138        msg->msglen = ntohl(msg->msglen) ;
139#endif
140        if ( writemessage(msgsock,send_buffer,MINMESSLEN,recvcontrolto) < 0 ) {
141            syslog(BBFTPD_ERR,"Error sending LISTREPL part 1") ;
142            return -1 ;
143        }
144        /*
145        ** Now send the file name separated by \0
146        */
147        for (i = 0 ; i < pg.gl_pathc ; i++ ) {
148            tmpfile = pg.gl_pathv[i] ;
149            if ( writemessage(msgsock,tmpfile,strlen(tmpfile)+1,recvcontrolto) < 0 ) {
150                syslog(BBFTPD_ERR,"Error sending LISTREPL filename %s",tmpfile) ;
151                return -1 ;
152            }
153        }
154        return 0 ;
155    }       
156}
Note: See TracBrowser for help on using the repository browser.