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

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

More missing #includes exposed when compiling on easterly.

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