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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/trunk/src/bbftp-server-3.2.0/bbftpd/ndg.c@1448
Revision 1431, 6.5 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 * NDG python embedded bbftp daemon module.
3 *
4 * @author Stephen Pascoe
5 *
6 * Copyright (C) 2006 CCLRC & NERC
7 *
8 * This software may be distributed under the terms of the Q Public Licence, version 1.0 or later.
9 *
10 */
11
12#include <bbftpd.h>
13
14#ifdef NDG_PYTHON_EMBED
15#include <Python.h>
16#endif
17#include <ndg.h>
18#include <stdlib.h>
19#include <stdio.h>
20#if HAVE_STRING_H
21# include <string.h>
22#endif
23
24/**
25 * Routine to do private authentication.
26 *
27 * This function is the main entry point from bbftpd into the private authentication system.
28 * See \c bbftpd_private_user.c for full details on the private authentication interface.
29 *
30 * @param logmessage a pointer to a buffer of length \c NDG_MAX_LOGMESSAGE
31 *     for storing an error message when returning -1.
32 * @return 0 on success or -1 on failure.
33 */
34int bbftpd_private_auth(char *logmessage) {
35
36#ifdef NDG_PYTHON_EMBED
37  char *username;
38 
39  if (ndg_auth(logmessage) == -1) {
40    return -1;
41  }
42  if ((username = ndg_getusername(logmessage)) == NULL) {
43    return -1;
44  }
45 
46  sprintf(currentusername, "%.*s", MAXLEN, username);
47#else
48  char *privatestr;
49  int privatestr_len;
50 
51  char *msg;
52  int len;
53 
54  /* Receive version verification message.  */
55  if (ndg_message_recv(&msg, &len, logmessage) == -1) {
56    return -1;
57  }
58  bbftpd_log(LOG_DEBUG, "Received auth message: %s", msg);
59  free(msg);
60
61  /* Send response */
62  if (ndg_message_send(NDG_HANDSHAKE, strlen(NDG_HANDSHAKE), logmessage) == -1) {
63    return -1;
64  }
65
66  /* Receive the privatestr */
67  if (ndg_message_recv(&privatestr, &privatestr_len, logmessage) == -1) {
68    return -1;
69  }
70
71  bbftpd_log(LOG_INFO, "Private string: %s", privatestr);
72  free(privatestr);
73
74#endif
75
76  return 0 ;
77}
78
79/**
80 * Routine to authorise bbftp control commands.
81 *
82 * Commands that require authorisation but do not involve sending or retrieving a file
83 * are authorised by this function.  The argument \a msgcode will be one of the following
84 * constants defined in \c structures.h :
85 *
86 *   \li \c MSG_CHDIR_V2 : a chdir request
87 *   \li \c MSG_LIST_V2 : a ls/dir request
88 *   \li \c MSG_MKDIR_V2 : a mkdir request
89 *   \li \c MSG_RM : a rm request
90 *   \li \c MSG_STAT : a stat request
91 *   \li \c MSG_DF : a statfs request
92 *
93 * Each command is being applied to the file pointed to by \a path.  Some commands may be affected
94 * by the value of \a transferoption.
95 *
96 * @param msgcode the message code from struct message.
97 * @param transferoption \c TROPT_* options.
98 * @param path the path to which the command applies.
99 * @param logmessage pointer to a buffer of length \c NDG_MAX_LOGMESSAGE
100 *     for storing an error message when returning -1.
101 * @return 0 on success or -1 on authorisation failure.
102 */
103int bbftpd_private_authz_control(int msgcode, int transferoption, char *path, char *logmessage)
104{
105
106  switch (msgcode) {
107  case MSG_CHDIR_V2:
108    bbftpd_log(LOG_DEBUG, "Authz: MSG_DIR 0x%x %s", transferoption, path);
109    break;
110  case MSG_LIST_V2:
111    bbftpd_log(LOG_DEBUG, "Authz: MSG_LIST_V2 0x%x %s", transferoption, path);
112    break;
113  case MSG_MKDIR_V2:
114    bbftpd_log(LOG_DEBUG, "Authz: MSG_MKDIR_V2 0x%x %s", transferoption, path);
115    break;
116  case MSG_RM:
117    bbftpd_log(LOG_DEBUG, "Authz: MSG_RM 0x%x %s", transferoption, path);
118    break;
119  case MSG_STAT:
120    bbftpd_log(LOG_DEBUG, "Authz: MSG_STAT 0x%x %s", transferoption, path);
121    break;
122  case MSG_DF:
123    bbftpd_log(LOG_DEBUG, "Authz: MSG_DF 0x%x %s", transferoption, path);
124    break;
125  default:
126    sprintf(logmessage, "Unrecognised message to authorise %d", msgcode);
127    return -1;
128  }
129
130#ifdef NDG_PYTHON_EMBED
131  return ndg_authz_control(msgcode, transferoption, path, logmessage);
132#else
133  return 0;
134#endif // NDG_PYTHON_EMBED
135}
136
137
138/**
139 * Routine to authorise file retrieve requests.
140 *
141 * The client is requesting the retrieval of the file \a path.
142 *
143 * @param path the file being retrieved.
144 * @param logmessage pointer to a buffer of length \c NDG_MAX_LOGMESSAGE
145 *     for storing an error message when returning -1.
146 * @return 0 on success or -1 on authorisation failure.
147 */
148int bbftpd_private_authz_retr(char *path, char *logmessage)
149{
150  bbftpd_log(LOG_DEBUG, "Authz: RETR %s", path);
151
152#ifdef NDG_PYTHON_EMBED
153  return ndg_authz_retr(path, logmessage);
154#else
155  return 0;
156#endif
157}
158
159/**
160 * Routine to authorise file store requests.
161 *
162 * The client is requesting the storage of a file at location \a path.
163 *
164 * @param path the file being stored.
165 * @param logmessage pointer to a buffer of length NDG_MAX_LOGMESSAGE
166 *     for storing an error message when returning -1.
167 * @return 0 on success or -1 on authorisation failure.
168 */
169int bbftpd_private_authz_store(char *path, char *logmessage)
170{
171  bbftpd_log(LOG_DEBUG, "Authz: STORE %s", path);
172
173#ifdef NDG_PYTHON_EMBED
174  return ndg_authz_store(path, logmessage);
175#else
176  return 0;
177#endif
178}
179
180
181/*
182 * Higher level message transfer functions.
183 *
184 */
185
186/**
187 * Send a message of variable length to the client.
188 *
189 * This function sends the length of the message in a header thus the client
190 * does not need to know what length message to expect.
191 *
192 * @param buffer a pointer to the message buffer.
193 * @param length the number of bytes to send.
194 * @param logmessage is filled with the error message on error.
195 * @return 0 if OK, -1 if error.
196 */
197int 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 (bbftpd_private_send(ctrl, NDG_MESSAGE_LEN - 1, logmessage) == -1) {
202    return -1;
203  }
204
205  if (bbftpd_private_send(buffer, length, logmessage) == -1) {
206    return -1;
207  }
208
209  return 0;
210}
211
212/**
213 * Receive a message of variable length from the client.
214 *
215 * The message length is sent in a separate header.
216 * The message is guaranteed to be NULL terminated.  This is done by
217 * allocating length+1 bytes and filling them with \0.
218 *
219 * @param buffer is set to newly allocated message buffer.
220 * @param length is set to the length of the message.
221 * @param logmessage is filled with the logmessage on error.
222 * @return 0 if OK, -1 if error.
223 */
224int ndg_message_recv(char **buffer, int *length, char *logmessage) {
225  char ctrl[NDG_MESSAGE_LEN];
226
227  if (bbftpd_private_recv(ctrl, NDG_MESSAGE_LEN - 1, logmessage) == -1) {
228    return -1;
229  }
230  if (sscanf(ctrl, "NDG-msg: %i", length) != 1) {
231    sprintf(logmessage, "ndg_message_recv ctrl error: %40s", ctrl);
232    return -1;
233  }
234
235  if ((*buffer = (char *)calloc(*length+1, sizeof(char))) == NULL) {
236    sprintf(logmessage, "ngd_message_recv malloc error");
237    return -1;
238  }
239
240  if (bbftpd_private_recv(*buffer, *length, logmessage) == -1) {
241    free(*buffer); *buffer = NULL;
242    return -1;
243  }
244
245  return 0;
246}
Note: See TracBrowser for help on using the repository browser.