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

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

Trivial correction.

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