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

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

bbftpd.h should be the first #include so that glibc headers pick up
any configuration flags.

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 bbftpd_log level
37                         
38 *****************************************************************************/
39#include <bbftpd.h>
40
41#include <errno.h>
42#include <fcntl.h>
43#include <glob.h>
44#include <netinet/in.h>
45#include <stdio.h>
46#include <bbftpd_private_log.h>
47#include <sys/stat.h>
48#include <sys/types.h>
49#include <unistd.h>
50
51#include <common.h>
52#include <daemon.h>
53#include <structures.h>
54#ifdef DARWIN
55#define GLOB_NOMATCH -3
56#endif
57
58#if HAVE_STRING_H
59# include <string.h>
60#endif
61#include <daemon_proto.h>
62
63extern int msgsock ;
64extern  int     recvcontrolto ;
65
66int sendlist (int code, int msglen) {
67
68    char    receive_buffer[MAXMESSLEN] ;
69    char    send_buffer[MAXMESSLEN];
70    char    logmessage[256] ;
71    glob_t pg ;
72    struct message *msg ;
73    int        i ; 
74    int        retcode ;
75    char    *tmpfile ;
76
77#ifndef WORDS_BIGENDIAN
78    msglen = ntohl(msglen) ;
79#endif
80    if ( msglen > MAXMESSLEN ) {
81        /*
82        ** In order to avoid buffer overflow we reject message to
83        ** big
84        */
85        bbftpd_log(BBFTPD_ERR,"Message to big in sendlist (%d,%d)",msglen,MAXMESSLEN) ;
86        reply(MSG_BAD_NO_RETRY,"String too long") ;
87        return -1 ;
88    }
89    /*
90    ** Read the characteristics of the filelist to send
91    */
92    if ( readmessage(msgsock,receive_buffer,msglen,recvcontrolto) < 0 ) {
93        /*
94        ** Error ...
95        */
96        return -1 ;
97    }
98    /*
99    ** receive_buffer contains the directory to create
100    */
101    receive_buffer[msglen] = '\0' ;
102    if ( code == MSG_LIST ) {
103        bbftpd_log(BBFTPD_DEBUG,"Getting file list %s",receive_buffer) ;
104    }
105    /*
106    ** We get the filelist
107    */
108    if ( (retcode = glob(receive_buffer,0,0,&pg)) < 0 ) {
109        if ( retcode == GLOB_NOMATCH ) {
110            /*
111            ** Just reply with a message MESS_LIST_REPL with msglen equal to zero
112            */
113            globfree(&pg) ;
114            msg = (struct message *) send_buffer ;
115            msg->code = MSG_LIST_REPL ;
116            msg->msglen = 0 ;
117            if ( writemessage(msgsock,send_buffer,MINMESSLEN,recvcontrolto) < 0 ) {
118                bbftpd_log(BBFTPD_ERR,"Error sending LISTREPL part 1") ;
119                return -1 ;
120            }
121            return 0 ;
122        } else {
123            globfree(&pg) ;
124            sprintf(logmessage,"Error during glob call %d",retcode) ;
125            reply(MSG_BAD,logmessage) ;
126            return 0 ;
127        }
128    } else {
129        /*
130        ** The glob call was successfull
131        */
132        /*
133        ** Calculate the total length of filename
134        */
135        msg = (struct message *) send_buffer ;
136        msg->code = MSG_LIST_REPL ;
137        msg->msglen = 0 ;
138        for (i = 0 ; i < pg.gl_pathc ; i++ ) {
139            tmpfile = pg.gl_pathv[i] ;
140            msg->msglen = msg->msglen+strlen(tmpfile)+1 ;
141        }
142#ifndef WORDS_BIGENDIAN
143        msg->msglen = ntohl(msg->msglen) ;
144#endif
145        if ( writemessage(msgsock,send_buffer,MINMESSLEN,recvcontrolto) < 0 ) {
146            bbftpd_log(BBFTPD_ERR,"Error sending LISTREPL part 1") ;
147            return -1 ;
148        }
149        /*
150        ** Now send the file name separated by \0
151        */
152        for (i = 0 ; i < pg.gl_pathc ; i++ ) {
153            tmpfile = pg.gl_pathv[i] ;
154            if ( writemessage(msgsock,tmpfile,strlen(tmpfile)+1,recvcontrolto) < 0 ) {
155                bbftpd_log(BBFTPD_ERR,"Error sending LISTREPL filename %s",tmpfile) ;
156                return -1 ;
157            }
158        }
159        return 0 ;
160    }       
161}
Note: See TracBrowser for help on using the repository browser.