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

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

Client-side python API now works on all test cases.

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