source: TI05-delivery/trunk/src/bbftp-server-3.2.0/bbftpd/changetodir.c @ 1328

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

I've created a bbftpd log facility to replace syslog.
At present it allows you to log messages to a file in addition
to syslog. This should help testing on other machines.

Line 
1/*
2 * bbftpd/changetodir.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 directory has been
27           successfully created)
28        -1 Tell the calling program to close the connection
29 
30 changetodir.c v 1.8.0  2000/06/02  - Creation of the routine.
31               v 1.8.10 2000/08/11  - Portage to Linux
32               v 1.9.0  2000/08/18  - Use configure to help portage
33               v 1.9.4  2000/10/16  - Supress %m
34                                    - 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
40#include <errno.h>
41#include <fcntl.h>
42#include <netinet/in.h>
43#include <stdio.h>
44#include <bbftpd_log.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
54extern int msgsock ;
55extern  int     recvcontrolto ;
56
57int changetodir (int code, int msglen) {
58
59    char    receive_buffer[MAXMESSLEN] ;
60    int        savederrno ;
61    char    logmessage[256] ;
62    char    *currentdir ;
63
64#ifndef WORDS_BIGENDIAN
65    msglen = ntohl(msglen) ;
66#endif
67    if ( msglen > MAXMESSLEN ) {
68        /*
69        ** In order to avoid buffer overflow we reject message to
70        ** big
71        */
72        bbftpd_log(BBFTPD_ERR,"Message to big in changetodir (%d,%d)",msglen,MAXMESSLEN) ;
73        reply(MSG_BAD_NO_RETRY,"Directory too long") ;
74        return -1 ;
75    }
76    /*
77    ** Read the characteristics of the directory
78    */
79    if ( readmessage(msgsock,receive_buffer,msglen,recvcontrolto) < 0 ) {
80        /*
81        ** Error ...
82        */
83        return -1 ;
84    }
85    /*
86    ** receive_buffer contains the directory to create
87    */
88    receive_buffer[msglen] = '\0' ;
89    if ( code == MSG_CHDIR ) {
90        bbftpd_log(BBFTPD_DEBUG,"Changing directory to %s",receive_buffer) ;
91    }
92    /*
93    ** We change the directory
94    */
95    if ( chdir(receive_buffer) < 0 ) {
96        /*
97        ** Depending on errno we are going to tell the client to
98        ** retry or not
99        */
100        savederrno = errno ;
101        sprintf(logmessage,"Error changing directory %s : %s ",receive_buffer,strerror(errno)) ;
102        bbftpd_log(BBFTPD_ERR,"Error changing directory %s : %s",receive_buffer,strerror(errno)) ;
103        /*
104        ** We tell the client not to retry in the following case (even in waiting
105        ** WAITRETRYTIME the problem will not be solved) :
106        **        EACCES        : Search permission denied
107        **        ELOOP        : To many symbolic links on path
108        **        ENAMETOOLONG: Path argument too long
109        **        ENOTDIR        : A component in path is not a directory
110        **        ENOENT      : A component of the path prefix does not exist or is a null pathname.
111        */
112        if ( savederrno == EACCES ||
113                savederrno == ELOOP ||
114                savederrno == ENAMETOOLONG ||
115                savederrno == ENOTDIR ||
116                savederrno == ENOENT ) {
117            reply(MSG_BAD_NO_RETRY,logmessage) ;
118        } else {
119            reply(MSG_BAD,logmessage) ;
120        }
121        return 0 ;
122    } else {
123        if ( (currentdir = getcwd(NULL,252) ) == NULL ) {
124            /*
125            ** Unable to get current directory
126            */
127            sprintf(logmessage,"Unable to get current directory") ;
128            reply(MSG_BAD,logmessage) ;
129            return 0 ;
130        } else {
131            sprintf(logmessage,"%s",currentdir) ;
132            free(currentdir) ;
133        }
134    }
135    reply(MSG_OK,logmessage) ;
136    return 0 ;
137}
Note: See TracBrowser for help on using the repository browser.