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