Menu

[r1577]: / advanced / src / xmlrpc_server_info.c  Maximize  Restore  History

Download this file

344 lines (250 with data), 9.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*=============================================================================
xmlrpc_server_info
===============================================================================
The xmlrpc_server_info class.
By Bryan Henderson, San Jose CA 2007.10.17.
Contributed to the public domain by its author
The xmlrpc_server_info class was originally just supposed to be
information about an HTTP server, hence the name. But we think of it
now as a generic carriage parameter, as in the C++ library. In
the future, it should be a union or maybe contain an opaque pointer
to the carriage parameter for a particular kind of transport. That
way, the client XML transports can be more than just HTTP XML
transports.
=============================================================================*/
#include "bool.h"
#include "mallocvar.h"
#include "xmlrpc-c/base.h"
#include "xmlrpc-c/base_int.h"
#include "xmlrpc-c/string_int.h"
#include "xmlrpc-c/client.h"
#include "xmlrpc-c/client_int.h"
xmlrpc_server_info *
xmlrpc_server_info_new(xmlrpc_env * const envP,
const char * const serverUrl) {
xmlrpc_server_info * serverInfoP;
XMLRPC_ASSERT_ENV_OK(envP);
XMLRPC_ASSERT_PTR_OK(serverUrl);
MALLOCVAR(serverInfoP);
if (serverInfoP == NULL)
xmlrpc_faultf(envP, "Couldn't allocate memory for xmlrpc_server_info");
else {
serverInfoP->serverUrl = strdup(serverUrl);
if (serverInfoP->serverUrl == NULL)
xmlrpc_faultf(envP, "Couldn't allocate memory for server URL");
else {
serverInfoP->allowedAuth.basic = false;
serverInfoP->allowedAuth.digest = false;
serverInfoP->allowedAuth.gssnegotiate = false;
serverInfoP->allowedAuth.ntlm = false;
serverInfoP->userNamePw = NULL;
serverInfoP->basicAuthHdrValue = NULL;
if (envP->fault_occurred)
xmlrpc_strfree(serverInfoP->serverUrl);
}
if (envP->fault_occurred)
free(serverInfoP);
}
return serverInfoP;
}
static void
copyUserNamePw(xmlrpc_env * const envP,
const char * const src,
const char ** const dstP) {
if (src == NULL)
*dstP = NULL;
else {
*dstP = strdup(src);
if (*dstP == NULL)
xmlrpc_faultf(envP, "Couldn't allocate memory for user name/pw");
}
}
static void
freeIfNonNull(const char * const arg) {
if (arg)
xmlrpc_strfree(arg);
}
static void
copyBasicAuthHdrValue(xmlrpc_env * const envP,
const char * const src,
const char ** const dstP) {
if (src == NULL)
*dstP = NULL;
else {
*dstP = strdup(src);
if (*dstP == NULL)
xmlrpc_faultf(envP, "Couldn't allocate memory "
"for authentication header value");
}
}
static void
copyServerInfoContent(xmlrpc_env * const envP,
xmlrpc_server_info * const dstP,
const xmlrpc_server_info * const srcP) {
dstP->serverUrl = strdup(srcP->serverUrl);
if (dstP->serverUrl == NULL)
xmlrpc_faultf(envP, "Couldn't allocate memory for server URL");
else {
copyUserNamePw(envP, srcP->userNamePw, &dstP->userNamePw);
if (!envP->fault_occurred) {
copyBasicAuthHdrValue(envP, srcP->basicAuthHdrValue,
&dstP->basicAuthHdrValue);
if (!envP->fault_occurred) {
dstP->allowedAuth.basic =
srcP->allowedAuth.basic;
dstP->allowedAuth.digest =
srcP->allowedAuth.digest;
dstP->allowedAuth.gssnegotiate =
srcP->allowedAuth.gssnegotiate;
dstP->allowedAuth.ntlm =
srcP->allowedAuth.ntlm;
if (envP->fault_occurred)
freeIfNonNull(dstP->basicAuthHdrValue);
}
if (envP->fault_occurred)
freeIfNonNull(dstP->userNamePw);
}
if (envP->fault_occurred)
xmlrpc_strfree(dstP->serverUrl);
}
}
xmlrpc_server_info *
xmlrpc_server_info_copy(xmlrpc_env * const envP,
xmlrpc_server_info * const srcP) {
xmlrpc_server_info * serverInfoP;
XMLRPC_ASSERT_ENV_OK(envP);
XMLRPC_ASSERT_PTR_OK(srcP);
MALLOCVAR(serverInfoP);
if (serverInfoP == NULL)
xmlrpc_faultf(envP,
"Couldn't allocate memory for xmlrpc_server_info");
else {
copyServerInfoContent(envP, serverInfoP, srcP);
if (envP->fault_occurred)
free(serverInfoP);
}
return serverInfoP;
}
void
xmlrpc_server_info_free(xmlrpc_server_info * const serverInfoP) {
XMLRPC_ASSERT_PTR_OK(serverInfoP);
XMLRPC_ASSERT(serverInfoP->serverUrl != XMLRPC_BAD_POINTER);
if (serverInfoP->userNamePw)
xmlrpc_strfree(serverInfoP->userNamePw);
serverInfoP->userNamePw = XMLRPC_BAD_POINTER;
if (serverInfoP->basicAuthHdrValue)
xmlrpc_strfree(serverInfoP->basicAuthHdrValue);
serverInfoP->basicAuthHdrValue = XMLRPC_BAD_POINTER;
xmlrpc_strfree(serverInfoP->serverUrl);
serverInfoP->serverUrl = XMLRPC_BAD_POINTER;
free(serverInfoP);
}
void
xmlrpc_server_info_set_user(xmlrpc_env * const envP,
xmlrpc_server_info * const serverInfoP,
const char * const username,
const char * const password) {
const char * userNamePw;
xmlrpc_mem_block * userNamePw64;
XMLRPC_ASSERT_ENV_OK(envP);
XMLRPC_ASSERT_PTR_OK(serverInfoP);
XMLRPC_ASSERT_PTR_OK(username);
XMLRPC_ASSERT_PTR_OK(password);
xmlrpc_asprintf(&userNamePw, "%s:%s", username, password);
userNamePw64 =
xmlrpc_base64_encode_without_newlines(envP,
(unsigned char*) userNamePw,
strlen(userNamePw));
if (!envP->fault_occurred) {
const char * const data = XMLRPC_MEMBLOCK_CONTENTS(char, userNamePw64);
size_t const len = XMLRPC_MEMBLOCK_SIZE(char, userNamePw64);
const char * const authType = "Basic ";
char * hdrValue;
hdrValue = malloc(strlen(authType) + len + 1);
if (hdrValue == NULL)
xmlrpc_faultf(envP, "Could not allocate memory to store "
"authorization header value.");
else {
strcpy(hdrValue, authType);
strncat(hdrValue, data, len);
if (serverInfoP->basicAuthHdrValue)
xmlrpc_strfree(serverInfoP->basicAuthHdrValue);
serverInfoP->basicAuthHdrValue = hdrValue;
}
XMLRPC_MEMBLOCK_FREE(char, userNamePw64);
}
serverInfoP->userNamePw = userNamePw;
}
void
xmlrpc_server_info_set_basic_auth(xmlrpc_env * const envP,
xmlrpc_server_info * const serverInfoP,
const char * const username,
const char * const password) {
xmlrpc_server_info_set_user(envP, serverInfoP, username, password);
if (!envP->fault_occurred) {
serverInfoP->allowedAuth.basic = true;
serverInfoP->allowedAuth.digest = false;
serverInfoP->allowedAuth.gssnegotiate = false;
serverInfoP->allowedAuth.ntlm = false;
}
}
void
xmlrpc_server_info_allow_auth_basic(xmlrpc_env * const envP,
xmlrpc_server_info * const sP) {
if (sP->userNamePw == NULL)
xmlrpc_faultf(envP, "You must set username/password with "
"xmlrpc_server_info_set_user()");
else
sP->allowedAuth.basic = true;
}
void
xmlrpc_server_info_disallow_auth_basic(
xmlrpc_env * const envP ATTR_UNUSED,
xmlrpc_server_info * const sP) {
sP->allowedAuth.basic = false;
}
void
xmlrpc_server_info_allow_auth_digest(xmlrpc_env * const envP,
xmlrpc_server_info * const sP) {
if (sP->userNamePw == NULL)
xmlrpc_faultf(envP, "You must set username/password with "
"xmlrpc_server_info_set_user()");
else
sP->allowedAuth.digest = true;
}
void
xmlrpc_server_info_disallow_auth_digest(
xmlrpc_env * const envP ATTR_UNUSED,
xmlrpc_server_info * const sP) {
sP->allowedAuth.digest = false;
}
void
xmlrpc_server_info_allow_auth_negotiate(xmlrpc_env * const envP,
xmlrpc_server_info * const sP) {
if (sP->userNamePw == NULL)
xmlrpc_faultf(envP, "You must set username/password with "
"xmlrpc_server_info_set_user()");
else
sP->allowedAuth.gssnegotiate = true;
}
void
xmlrpc_server_info_disallow_auth_negotiate(
xmlrpc_env * const envP ATTR_UNUSED,
xmlrpc_server_info * const sP) {
sP->allowedAuth.gssnegotiate = false;
}
void
xmlrpc_server_info_allow_auth_ntlm(xmlrpc_env * const envP,
xmlrpc_server_info * const sP) {
if (sP->userNamePw == NULL)
xmlrpc_faultf(envP, "You must set username/password with "
"xmlrpc_server_info_set_user()");
else
sP->allowedAuth.ntlm = true;
}
void
xmlrpc_server_info_disallow_auth_ntlm(
xmlrpc_env * const envP ATTR_UNUSED,
xmlrpc_server_info * const sP) {
sP->allowedAuth.ntlm = true;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.