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