1 | /* |
---|
2 | * bbftpd/createadir.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 | createadir.c v 1.8.5 2000/04/28 - Creation of the routine. |
---|
31 | v 1.8.6 2000/04/04 - Add portage to OSF1 |
---|
32 | - Avoid buffer overflow |
---|
33 | v 1.8.7 2000/05/24 - Correct portage to OSF1 |
---|
34 | v 1.8.10 2000/08/11 - Portage to Linux |
---|
35 | v 1.9.0 2000/08/18 - Use configure to help portage |
---|
36 | v 1.9.4 2000/10/16 - Supress %m |
---|
37 | - Correct typo |
---|
38 | v 2.0.1 2001/04/23 - Correct indentation |
---|
39 | v 2.1.0 2001/05/30 - Correct bbftpd_log level |
---|
40 | |
---|
41 | *****************************************************************************/ |
---|
42 | #include <errno.h> |
---|
43 | #include <fcntl.h> |
---|
44 | #include <netinet/in.h> |
---|
45 | #include <stdio.h> |
---|
46 | #include <bbftpd_log.h> |
---|
47 | #include <sys/stat.h> |
---|
48 | #include <sys/types.h> |
---|
49 | #include <unistd.h> |
---|
50 | |
---|
51 | #include <bbftpd.h> |
---|
52 | #include <common.h> |
---|
53 | #include <daemon.h> |
---|
54 | #include <structures.h> |
---|
55 | |
---|
56 | |
---|
57 | extern int msgsock ; |
---|
58 | extern int recvcontrolto ; |
---|
59 | |
---|
60 | int createadir(int code, int msglen) { |
---|
61 | |
---|
62 | char receive_buffer[MAXMESSLEN] ; |
---|
63 | int savederrno ; |
---|
64 | char logmessage[256] ; |
---|
65 | |
---|
66 | #ifndef WORDS_BIGENDIAN |
---|
67 | msglen = ntohl(msglen) ; |
---|
68 | #endif |
---|
69 | if ( msglen > MAXMESSLEN ) { |
---|
70 | /* |
---|
71 | ** In order to avoid buffer overflow we reject message to |
---|
72 | ** big |
---|
73 | */ |
---|
74 | bbftpd_log(BBFTPD_ERR,"Message to big in createdir (%d,%d)",msglen,MAXMESSLEN) ; |
---|
75 | reply(MSG_BAD_NO_RETRY,"Directory too long") ; |
---|
76 | return -1 ; |
---|
77 | } |
---|
78 | /* |
---|
79 | ** Read the characteristics of the directory |
---|
80 | */ |
---|
81 | if ( readmessage(msgsock,receive_buffer,msglen,recvcontrolto) < 0 ) { |
---|
82 | /* |
---|
83 | ** Error ... |
---|
84 | */ |
---|
85 | return -1 ; |
---|
86 | } |
---|
87 | /* |
---|
88 | ** receive_buffer contains the directory to create |
---|
89 | */ |
---|
90 | receive_buffer[msglen] = '\0' ; |
---|
91 | if ( code == MSG_MKDIR ) { |
---|
92 | bbftpd_log(BBFTPD_DEBUG,"Creating directory %s",receive_buffer) ; |
---|
93 | } |
---|
94 | /* |
---|
95 | ** We create the directory |
---|
96 | */ |
---|
97 | if ( mkdir(receive_buffer,S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP) < 0 ) { |
---|
98 | /* |
---|
99 | ** Depending on errno we are going to tell the client to |
---|
100 | ** retry or not |
---|
101 | */ |
---|
102 | savederrno = errno ; |
---|
103 | sprintf(logmessage,"Error creation directory %s : %s",receive_buffer,strerror(errno)) ; |
---|
104 | bbftpd_log(BBFTPD_ERR,"Error creation directory %s : %s",receive_buffer,strerror(errno)) ; |
---|
105 | /* |
---|
106 | ** We tell the client not to retry in the following case (even in waiting |
---|
107 | ** WAITRETRYTIME the problem will not be solved) : |
---|
108 | ** EACCES : Search permission denied |
---|
109 | ** EDQUOT : No more quota |
---|
110 | ** ENOSPC : No more space |
---|
111 | ** ELOOP : To many symbolic links on path |
---|
112 | ** ENAMETOOLONG: Path argument too long |
---|
113 | ** ENOTDIR : A component in path is not a directory |
---|
114 | ** EROFS : The path prefix resides on a read-only file system. |
---|
115 | ** ENOENT : A component of the path prefix does not exist or is a null pathname. |
---|
116 | ** EEXIST : The named file already exists. |
---|
117 | */ |
---|
118 | if ( savederrno == EACCES || |
---|
119 | savederrno == EDQUOT || |
---|
120 | savederrno == ENOSPC || |
---|
121 | savederrno == ELOOP || |
---|
122 | savederrno == ENAMETOOLONG || |
---|
123 | savederrno == ENOTDIR || |
---|
124 | savederrno == EROFS || |
---|
125 | savederrno == ENOENT || |
---|
126 | savederrno == EEXIST ) { |
---|
127 | reply(MSG_BAD_NO_RETRY,logmessage) ; |
---|
128 | } else { |
---|
129 | reply(MSG_BAD,logmessage) ; |
---|
130 | } |
---|
131 | return 0 ; |
---|
132 | } |
---|
133 | reply(MSG_OK,"OK") ; |
---|
134 | return 0 ; |
---|
135 | } |
---|