1 | /* |
---|
2 | * bbftpc/bbftp_private_user.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 | |
---|
26 | bbftp_private_user.c v 2.1.0 2001/05/21 - Routines creation |
---|
27 | |
---|
28 | *****************************************************************************/ |
---|
29 | |
---|
30 | #include <bbftp.h> |
---|
31 | |
---|
32 | #include <stdio.h> |
---|
33 | #include <stdlib.h> |
---|
34 | #if HAVE_STRING_H |
---|
35 | # include <string.h> |
---|
36 | #endif |
---|
37 | |
---|
38 | #include <bbftp_private_user.h> |
---|
39 | #include <client.h> |
---|
40 | #include <client_proto.h> |
---|
41 | |
---|
42 | #ifdef NDG_AUTH |
---|
43 | int ndg_client_auth(char *logmessage); |
---|
44 | #endif // NDG_PRIVATE_AUTH |
---|
45 | |
---|
46 | /* |
---|
47 | ** username contain a pointer to the string given with the -u flag |
---|
48 | */ |
---|
49 | extern char *username ; |
---|
50 | /* |
---|
51 | ** privatestr contain a pointer to the string given with the -P flag |
---|
52 | */ |
---|
53 | extern char *privatestr ; |
---|
54 | |
---|
55 | extern int debug; |
---|
56 | |
---|
57 | /* From bbftp_util.c */ |
---|
58 | void PRINTMESSAGE(FILE *strm , int flag, int errcode, int tok, char *fmt, ...); |
---|
59 | |
---|
60 | #ifndef NDG_PYTHON_EMBED |
---|
61 | /* prototypes */ |
---|
62 | static int ndg_message_recv(char **buffer, int *length, char *logmessage); |
---|
63 | static int ndg_message_send(char *buffer, int length, char *logmessage); |
---|
64 | #endif |
---|
65 | |
---|
66 | /******************************************************************************* |
---|
67 | ** bbftp_private_getargs : * |
---|
68 | ** * |
---|
69 | ** Routine to get arguments for private authentication. This routine is * |
---|
70 | ** called after several tests have been made. The variable to be set are * |
---|
71 | ** to be define in bbftp_private_user.h to be used by other routines. * |
---|
72 | ** * |
---|
73 | ** OUPUT variable : * |
---|
74 | ** logmessage : to write the error message in case of error * |
---|
75 | ** * |
---|
76 | ** GLOBAL VARIABLE USED : * |
---|
77 | ** * |
---|
78 | ** * |
---|
79 | ** RETURN: * |
---|
80 | ** -1 Unrecoverable error * |
---|
81 | ** 0 OK * |
---|
82 | ** * |
---|
83 | *******************************************************************************/ |
---|
84 | |
---|
85 | int bbftp_private_getargs(char *logmessage) |
---|
86 | { |
---|
87 | return 0 ; |
---|
88 | } |
---|
89 | |
---|
90 | /******************************************************************************* |
---|
91 | ** bbftp_private_auth : * |
---|
92 | ** * |
---|
93 | ** Routine to do the private authentication. This routine just have to * |
---|
94 | ** send or to receive private data to the bbftpd daemon. For that it will * |
---|
95 | ** use the two routines : * |
---|
96 | ** bbftp_private_send * |
---|
97 | ** bbftp_private_recv * |
---|
98 | ** The reception of the OK or BAD message will be done by the calling * |
---|
99 | ** routine, so if this routine has no check to do (for example just * |
---|
100 | ** sending username and password) it will just call bbftp_private_send * |
---|
101 | ** twice and let the calling program check if it is OK or BAD. * |
---|
102 | ** * |
---|
103 | ** The bbftp_private_send routine has to be called with the following * |
---|
104 | ** parameters: * |
---|
105 | ** * |
---|
106 | ** int bbftp_private_send(char *buffertosend,int buffertosendlength, * |
---|
107 | ** char *logmessage) * |
---|
108 | ** char *buffertosend = string to be send to the daemon * |
---|
109 | ** int buffertosendlength = length of the string * |
---|
110 | ** char *logmessage * |
---|
111 | ** * |
---|
112 | ** and return 0 in case of success, -1 in case of error with logmessage * |
---|
113 | ** filled * |
---|
114 | ** * |
---|
115 | ** The bbftp_private_recv routine has to be called with the following * |
---|
116 | ** parameters: * |
---|
117 | ** * |
---|
118 | ** int bbftp_private_recv(char *buffertorecv,int lengthtoreceive, * |
---|
119 | ** char *logmessage) * |
---|
120 | ** char *buffertorecv = buffer to put data received from the * |
---|
121 | ** daemon * |
---|
122 | ** int lengthtorecv = length of the buffer * |
---|
123 | ** char *logmessage * |
---|
124 | ** * |
---|
125 | ** and return number of byte received in case of success, -1 in case of * |
---|
126 | ** error with logmessage filled * |
---|
127 | ** It is the duty of the programmer to be sure that the buffer is large * |
---|
128 | ** enought * |
---|
129 | ** * |
---|
130 | ** * |
---|
131 | ** OUPUT variable : * |
---|
132 | ** logmessage : to write the error message in case of error * |
---|
133 | ** * |
---|
134 | ** GLOBAL VARIABLE USED : * |
---|
135 | ** * |
---|
136 | ** RETURN: * |
---|
137 | ** -1 Unrecoverable error * |
---|
138 | ** 0 OK * |
---|
139 | ** * |
---|
140 | *******************************************************************************/ |
---|
141 | |
---|
142 | int bbftp_private_auth(char *logmessage) |
---|
143 | { |
---|
144 | |
---|
145 | #ifdef NDG_PYTHON_EMBED |
---|
146 | return ndg_client_auth(logmessage); |
---|
147 | #else |
---|
148 | char *msg; |
---|
149 | char default_privatestr[] = "none"; |
---|
150 | int len; |
---|
151 | |
---|
152 | /* Send version verification message. */ |
---|
153 | if (ndg_message_send(NDG_HANDSHAKE, strlen(NDG_HANDSHAKE), logmessage) == -1) { |
---|
154 | return -1; |
---|
155 | } |
---|
156 | |
---|
157 | /* Receive response */ |
---|
158 | if (ndg_message_recv(&msg, &len, logmessage) == -1) { |
---|
159 | return -1; |
---|
160 | } |
---|
161 | |
---|
162 | if (debug) { |
---|
163 | PRINTMESSAGE(stdout,CASE_NORMAL,0,0,"Received Auth handshake: %s\n", msg) ; |
---|
164 | } |
---|
165 | free(msg); |
---|
166 | |
---|
167 | /* Send the privatestr */ |
---|
168 | // If there is no private string, send default |
---|
169 | if (privatestr == NULL) { |
---|
170 | privatestr = default_privatestr; |
---|
171 | } |
---|
172 | |
---|
173 | if (ndg_message_send(privatestr, strlen(privatestr), logmessage) == -1) { |
---|
174 | return -1; |
---|
175 | } |
---|
176 | |
---|
177 | return 0 ; |
---|
178 | #endif // NDG_PRIVATE_AUTH |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | |
---|
183 | #ifndef NDG_PYTHON_EMBED |
---|
184 | /* |
---|
185 | * Higher level message transfer functions. |
---|
186 | * |
---|
187 | */ |
---|
188 | |
---|
189 | /** |
---|
190 | * Send a message, declaring it's length to the client. |
---|
191 | * |
---|
192 | * @param buffer a pointer to the message buffer |
---|
193 | * @param length the number of bytes to send |
---|
194 | * @param[out] loggmessage is filled with the error message on error |
---|
195 | * @return 0 if OK, -1 if error |
---|
196 | */ |
---|
197 | static int ndg_message_send(char *buffer, int length, char *logmessage) { |
---|
198 | char ctrl[NDG_MESSAGE_LEN]; |
---|
199 | |
---|
200 | sprintf(ctrl, "NDG-msg: %i", length); |
---|
201 | if (bbftp_private_send(ctrl, NDG_MESSAGE_LEN - 1, logmessage) == -1) { |
---|
202 | return -1; |
---|
203 | } |
---|
204 | |
---|
205 | if (bbftp_private_send(buffer, length, logmessage) == -1) { |
---|
206 | return -1; |
---|
207 | } |
---|
208 | |
---|
209 | return 0; |
---|
210 | } |
---|
211 | |
---|
212 | /** |
---|
213 | * Receive a message of variable length |
---|
214 | * |
---|
215 | * @param[out] buffer is set to newly allocated message buffer |
---|
216 | * @param[out] length is set to the length of the message |
---|
217 | * @param[out] logmessage is filled with the logmessage on error |
---|
218 | * @return 0 if OK, -1 if error |
---|
219 | */ |
---|
220 | static int ndg_message_recv(char **buffer, int *length, char *logmessage) { |
---|
221 | char ctrl[NDG_MESSAGE_LEN]; |
---|
222 | |
---|
223 | if (bbftp_private_recv(ctrl, NDG_MESSAGE_LEN - 1, logmessage) == -1) { |
---|
224 | return -1; |
---|
225 | } |
---|
226 | if (sscanf(ctrl, "NDG-msg: %i", length) != 1) { |
---|
227 | sprintf(logmessage, "ndg_message_recv ctrl error: %40s", ctrl); |
---|
228 | return -1; |
---|
229 | } |
---|
230 | |
---|
231 | if ((*buffer = (char *)malloc((*length + 1) * sizeof(char))) == NULL) { |
---|
232 | sprintf(logmessage, "ngd_message_recv malloc error"); |
---|
233 | return -1; |
---|
234 | } |
---|
235 | |
---|
236 | if (bbftp_private_recv(*buffer, *length, logmessage) == -1) { |
---|
237 | free(*buffer); |
---|
238 | return -1; |
---|
239 | } |
---|
240 | |
---|
241 | return 0; |
---|
242 | } |
---|
243 | |
---|
244 | #endif // !NDG_PYTHON_EMBED |
---|