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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/trunk/src/bbftp-server-3.2.0/bbftpd/changetodir.c
Revision 1431, 4.6 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/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#include <bbftpd.h>
40
41#include <errno.h>
42#include <fcntl.h>
43#include <netinet/in.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <bbftpd_private_log.h>
47#include <sys/stat.h>
48#include <sys/types.h>
49#include <unistd.h>
50#include <daemon_proto.h>
51
52#include <common.h>
53#include <daemon.h>
54#include <structures.h>
55
56#if HAVE_STRING_H
57# include <string.h>
58#endif
59
60extern int msgsock ;
61extern  int     recvcontrolto ;
62
63int changetodir (int code, int msglen) {
64
65    char    receive_buffer[MAXMESSLEN] ;
66    int        savederrno ;
67    char    logmessage[256] ;
68    char    *currentdir ;
69
70#ifndef WORDS_BIGENDIAN
71    msglen = ntohl(msglen) ;
72#endif
73    if ( msglen > MAXMESSLEN ) {
74        /*
75        ** In order to avoid buffer overflow we reject message to
76        ** big
77        */
78        bbftpd_log(BBFTPD_ERR,"Message to big in changetodir (%d,%d)",msglen,MAXMESSLEN) ;
79        reply(MSG_BAD_NO_RETRY,"Directory too long") ;
80        return -1 ;
81    }
82    /*
83    ** Read the characteristics of the directory
84    */
85    if ( readmessage(msgsock,receive_buffer,msglen,recvcontrolto) < 0 ) {
86        /*
87        ** Error ...
88        */
89        return -1 ;
90    }
91    /*
92    ** receive_buffer contains the directory to create
93    */
94    receive_buffer[msglen] = '\0' ;
95    if ( code == MSG_CHDIR ) {
96        bbftpd_log(BBFTPD_DEBUG,"Changing directory to %s",receive_buffer) ;
97    }
98    /*
99    ** We change the directory
100    */
101    if ( chdir(receive_buffer) < 0 ) {
102        /*
103        ** Depending on errno we are going to tell the client to
104        ** retry or not
105        */
106        savederrno = errno ;
107        sprintf(logmessage,"Error changing directory %s : %s ",receive_buffer,strerror(errno)) ;
108        bbftpd_log(BBFTPD_ERR,"Error changing directory %s : %s",receive_buffer,strerror(errno)) ;
109        /*
110        ** We tell the client not to retry in the following case (even in waiting
111        ** WAITRETRYTIME the problem will not be solved) :
112        **        EACCES        : Search permission denied
113        **        ELOOP        : To many symbolic links on path
114        **        ENAMETOOLONG: Path argument too long
115        **        ENOTDIR        : A component in path is not a directory
116        **        ENOENT      : A component of the path prefix does not exist or is a null pathname.
117        */
118        if ( savederrno == EACCES ||
119                savederrno == ELOOP ||
120                savederrno == ENAMETOOLONG ||
121                savederrno == ENOTDIR ||
122                savederrno == ENOENT ) {
123            reply(MSG_BAD_NO_RETRY,logmessage) ;
124        } else {
125            reply(MSG_BAD,logmessage) ;
126        }
127        return 0 ;
128    } else {
129        if ( (currentdir = getcwd(NULL,252) ) == NULL ) {
130            /*
131            ** Unable to get current directory
132            */
133            sprintf(logmessage,"Unable to get current directory") ;
134            reply(MSG_BAD,logmessage) ;
135            return 0 ;
136        } else {
137            sprintf(logmessage,"%s",currentdir) ;
138            free(currentdir) ;
139        }
140    }
141    reply(MSG_OK,logmessage) ;
142    return 0 ;
143}
Note: See TracBrowser for help on using the repository browser.