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

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

Eliminated many socket related compile time warnings. These
were due to missing #include statements in the bbftp source.

MOST test cases now succeed on callisto! cross-platform stability
is looking more optimistic :-)

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