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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
/* libmsv: MonkeySphere Validation library
* Copyright © 2011-2013 Clint Adams
* libmsv is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
* libmsv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with libmsv; if not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <jansson.h>
#include "msv/msv.h"
struct msv_ctxt
{
char *socket_location;
CURLcode cc;
};
extern const char *
msv_strerror (msv_ctxt_t ctx, int error_code)
{
if (ctx)
{
switch (error_code)
{
case LIBMSV_ERROR_SUCCESS:
return "No error";
case LIBMSV_ERROR_INVALID:
return "Agent reports invalid";
case LIBMSV_ERROR_NOENVVAR:
return "Environment variable not set";
case LIBMSV_ERROR_CURLINIT_FAILED:
return "curl_easy_init() failed!";
case LIBMSV_ERROR_INCOMPATIBLE_AGENT:
return "Validation agent is incompatible";
case LIBMSV_ERROR_BADARG:
return "Bad argument(s)";
case LIBMSV_ERROR_CURLCODE:
return curl_easy_strerror (ctx->cc);
default:
return "Unknown error";
}
}
else
return "Unknown context; misuse of msv_strerror";
}
static size_t
write_function (void *ptr, size_t size, size_t nmemb, void *userdata)
{
char *ud;
ud = malloc (size * nmemb + 1);
if (!ud)
return 0;
memcpy (ud, ptr, size * nmemb);
ud[size * nmemb] = '\0';
*((char **) userdata) = (void *) ud;
return size * nmemb;
}
extern msv_ctxt_t
msv_ctxt_init (const char *url)
{
msv_ctxt_t ctx;
char *env;
ctx = malloc (sizeof (struct msv_ctxt));
if (!ctx)
return NULL;
if (url)
ctx->socket_location = strdup (url);
else if ((env = getenv ("MONKEYSPHERE_VALIDATION_AGENT_SOCKET")))
ctx->socket_location = strdup (env);
else
{
free (ctx);
return NULL;
}
if (!ctx->socket_location)
{
free (ctx);
return NULL;
}
return ctx;
}
extern void
msv_ctxt_destroy (msv_ctxt_t ctx)
{
if (ctx)
{
if (ctx->socket_location)
free (ctx->socket_location);
free (ctx);
}
}
extern int
msv_check_msva (msv_ctxt_t ctx)
{
CURL *c;
char *buf;
json_t *j, *k;
json_error_t je;
void *iter;
int protoversion, available, rv;
if (!ctx)
return LIBMSV_ERROR_BADARG;
if (ctx->socket_location == NULL)
return LIBMSV_ERROR_NOENVVAR;
c = curl_easy_init ();
if (c == NULL)
return LIBMSV_ERROR_CURLINIT_FAILED;
ctx->cc = curl_easy_setopt (c, CURLOPT_URL, ctx->socket_location);
if (ctx->cc)
{
curl_easy_cleanup (c);
return LIBMSV_ERROR_CURLCODE;
}
ctx->cc = curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, write_function);
if (ctx->cc)
{
curl_easy_cleanup (c);
return LIBMSV_ERROR_CURLCODE;
}
ctx->cc = curl_easy_setopt (c, CURLOPT_WRITEDATA, &buf);
if (ctx->cc)
{
curl_easy_cleanup (c);
return LIBMSV_ERROR_CURLCODE;
}
ctx->cc = curl_easy_perform (c);
if (ctx->cc)
{
curl_easy_cleanup (c);
return LIBMSV_ERROR_CURLCODE;
}
if ((j = json_loads (buf, 0, &je)))
{
free (buf);
iter = json_object_iter_at (j, "protoversion");
if (iter)
{
k = json_object_iter_value (iter);
json_unpack (k, "i", &protoversion);
iter = json_object_iter_at (j, "available");
if (iter)
{
k = json_object_iter_value (iter);
json_unpack (k, "b", &available);
rv = 0;
}
else
{
rv = LIBMSV_ERROR_UNEXPECTED_RESPONSE;
}
}
else
{
rv = LIBMSV_ERROR_UNEXPECTED_RESPONSE;
}
json_decref (j);
}
else
{
rv = LIBMSV_ERROR_UNEXPECTED_RESPONSE;
}
curl_easy_cleanup (c);
if (rv)
{
return rv;
}
else
{
if (protoversion == 1 && available == 1)
return LIBMSV_ERROR_SUCCESS;
else
return LIBMSV_ERROR_INCOMPATIBLE_AGENT;
}
}
extern int
msv_query_agent (msv_ctxt_t ctx, struct msv_query q,
struct msv_response **response_ptr)
{
int oldurllen, rv = 0;
CURL *c;
CURLcode cc;
char *buf, *req = NULL, *reviewurl;
json_t *j, *k, *jreq, *pkc, *peer, *j_pkcdata, *j_pkctype, *j_peername,
*j_peertype, *j_context;
json_error_t je;
void *iter;
struct curl_slist *slist = NULL;
struct msv_response *response;
if (!response_ptr)
return LIBMSV_ERROR_BADARG;
*response_ptr = NULL;
if (!ctx)
return LIBMSV_ERROR_BADARG;
if (ctx->socket_location == NULL)
return LIBMSV_ERROR_NOENVVAR;
if (!(q.context && q.peertype && q.peername && q.pkctype && q.pkcdata))
return LIBMSV_ERROR_BADARG;
jreq = json_object ();
pkc = json_object ();
peer = json_object ();
if ((j_pkcdata = json_string (q.pkcdata)) &&
(j_pkctype = json_string (q.pkctype)) &&
(j_peername = json_string (q.peername)) &&
(j_peertype = json_string (q.peertype)) &&
(j_context = json_string (q.context)) &&
(json_object_set (pkc, "data", j_pkcdata) == 0) &&
(json_object_set (pkc, "type", j_pkctype) == 0) &&
(json_object_set (peer, "name", j_peername) == 0) &&
(json_object_set (peer, "type", j_peertype) == 0) &&
(json_object_set (jreq, "pkc", pkc) == 0) &&
(json_object_set (jreq, "context", j_context) == 0) &&
(json_object_set (jreq, "peer", peer) == 0))
{
req = json_dumps (jreq, JSON_PRESERVE_ORDER | JSON_COMPACT);
}
else
{
rv = LIBMSV_ERROR_BADARG;
}
if (j_pkcdata)
json_decref (j_pkcdata);
if (j_pkctype)
json_decref (j_pkctype);
if (j_peername)
json_decref (j_peername);
if (j_peertype)
json_decref (j_peertype);
if (j_context)
json_decref (j_context);
if (peer)
json_decref (peer);
if (pkc)
json_decref (pkc);
if (jreq)
json_decref (jreq);
if (rv)
return rv;
c = curl_easy_init ();
if (c == NULL)
{
free (req);
return LIBMSV_ERROR_CURLINIT_FAILED;
}
oldurllen = strlen (ctx->socket_location);
reviewurl = malloc (oldurllen + 12);
if (!reviewurl)
{
curl_easy_cleanup (c);
free (req);
return LIBMSV_ERROR_NOMEM;
}
sprintf (reviewurl, "%s%s", ctx->socket_location, "/reviewcert");
cc = curl_easy_setopt (c, CURLOPT_URL, reviewurl);
free (reviewurl);
if (cc)
{
curl_easy_cleanup (c);
free (req);
return LIBMSV_ERROR_CURLCODE;
}
cc = curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, write_function);
if (cc)
{
curl_easy_cleanup (c);
free (req);
return LIBMSV_ERROR_CURLCODE;
}
cc = curl_easy_setopt (c, CURLOPT_WRITEDATA, &buf);
if (cc)
{
curl_easy_cleanup (c);
free (req);
return LIBMSV_ERROR_CURLCODE;
}
cc = curl_easy_setopt (c, CURLOPT_POST, 1);
if (cc)
{
curl_easy_cleanup (c);
free (req);
return LIBMSV_ERROR_CURLCODE;
}
slist = curl_slist_append (slist, "Content-Type: application/json");
slist = curl_slist_append (slist, "Connection: close");
slist = curl_slist_append (slist, "Accept: application/json");
slist = curl_slist_append (slist, "Expect:");
cc = curl_easy_setopt (c, CURLOPT_HTTPHEADER, slist);
if (cc)
{
curl_easy_cleanup (c);
free (req);
curl_slist_free_all (slist);
return LIBMSV_ERROR_CURLCODE;
}
cc = curl_easy_setopt (c, CURLOPT_POSTFIELDS, (void *) req);
if (cc)
{
curl_easy_cleanup (c);
free (req);
curl_slist_free_all (slist);
return LIBMSV_ERROR_CURLCODE;
}
cc = curl_easy_perform (c);
if (cc)
{
curl_easy_cleanup (c);
free (req);
curl_slist_free_all (slist);
return LIBMSV_ERROR_CURLCODE;
}
if (!(response = malloc (sizeof (struct msv_response))))
{
curl_easy_cleanup (c);
free (req);
curl_slist_free_all (slist);
return LIBMSV_ERROR_NOMEM;
}
response->message = NULL;
*response_ptr = response;
j = json_loads (buf, 0, &je);
free (buf);
iter = json_object_iter_at (j, "valid");
k = json_object_iter_value (iter);
json_unpack (k, "b", &response->valid);
iter = json_object_iter_at (j, "message");
k = json_object_iter_value (iter);
json_unpack (k, "s", &response->message);
response->message = strdup (response->message);
json_decref (j);
curl_slist_free_all (slist);
curl_easy_cleanup (c);
free (req);
if (!response->message)
{
/* strdup returned NULL, which suggests a memory allocation
failure */
msv_response_destroy (response);
*response_ptr = NULL;
return LIBMSV_ERROR_NOMEM;
}
return !(response->valid);
}
extern void
msv_response_destroy (struct msv_response *response)
{
if (response)
{
if (response->message)
free (response->message);
free (response);
}
}
|