00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "syncml.h"
00023
00024 #include "syncml_internals.h"
00025 #include "sml_parse_internals.h"
00026 #include "sml_elements_internals.h"
00027 #include "sml_command_internals.h"
00028 #include "sml_error_internals.h"
00029
00030 SmlLocation *smlLocationNew(const char *locURI, const char *locName, SmlError **error)
00031 {
00032 smlTrace(TRACE_ENTRY, "%s(%s, %s, %p)", __func__, VA_STRING(locURI), VA_STRING(locName), error);
00033 CHECK_ERROR_REF
00034
00035 if (!locURI) {
00036 smlErrorSet(error, SML_ERROR_GENERIC, "No locURI was given");
00037 goto error;
00038 }
00039
00040 SmlLocation *location = smlTryMalloc0(sizeof(SmlLocation), error);
00041 if (!location)
00042 goto error;
00043
00044 location->refCount = 1;
00045 location->locURI = g_strdup(locURI);
00046 location->locName = g_strdup(locName);
00047
00048 smlTrace(TRACE_EXIT, "%s: %p", __func__, location);
00049 return location;
00050
00051 error:
00052 smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
00053 return NULL;
00054 }
00055
00056 SmlLocation *smlLocationRef(SmlLocation *loc)
00057 {
00058 smlTrace(TRACE_ENTRY, "%s(%p)", __func__, loc);
00059 smlAssert(loc);
00060
00061 g_atomic_int_inc(&(loc->refCount));
00062
00063 smlTrace(TRACE_EXIT, "%s: New refcount: %i", __func__, loc->refCount);
00064 return loc;
00065 }
00066
00067 void smlLocationUnref(SmlLocation *loc)
00068 {
00069 smlTrace(TRACE_ENTRY, "%s(%p)", __func__, loc);
00070 smlAssert(loc);
00071
00072 if (g_atomic_int_dec_and_test(&(loc->refCount))) {
00073 smlTrace(TRACE_INTERNAL, "%s: Refcount == 0!", __func__);
00074
00075 if (loc->locURI)
00076 smlSafeCFree(&(loc->locURI));
00077
00078 if (loc->locName)
00079 smlSafeCFree(&(loc->locName));
00080
00081 smlSafeFree((gpointer *)&loc);
00082 }
00083
00084 smlTrace(TRACE_EXIT, "%s", __func__);
00085 }
00086
00087 const char *smlLocationGetURI(SmlLocation *loc)
00088 {
00089 smlAssert(loc);
00090 return loc->locURI;
00091 }
00092
00093 void smlLocationSetURI(SmlLocation *loc, const char *uri)
00094 {
00095 smlAssert(loc);
00096 smlAssert(uri);
00097 if (loc->locURI)
00098 smlSafeCFree(&(loc->locURI));
00099 loc->locURI = g_strdup(uri);
00100 }
00101
00102 const char *smlLocationGetName(SmlLocation *loc)
00103 {
00104 smlAssert(loc);
00105 return loc->locName;
00106 }
00107
00108 void smlLocationSetName(SmlLocation *loc, const char *name)
00109 {
00110 smlAssert(loc);
00111 smlAssert(name);
00112 if (loc->locName)
00113 smlSafeCFree(&(loc->locName));
00114 loc->locName = g_strdup(name);
00115 }
00116
00117 SmlLocation *smlLocationClone(SmlLocation *source, SmlError **error)
00118 {
00119 smlTrace(TRACE_ENTRY, "%s(%p, %p)", __func__, source, error);
00120 CHECK_ERROR_REF
00121 smlAssert(source);
00122
00123 SmlLocation *location = smlTryMalloc0(sizeof(SmlLocation), error);
00124 if (!location)
00125 goto error;
00126 location->refCount = 1;
00127
00128 smlLocationCopy(source, location);
00129
00130 smlTrace(TRACE_EXIT, "%s: %p", __func__, location);
00131 return location;
00132
00133 error:
00134 smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
00135 return NULL;
00136 }
00137
00138 void smlLocationCopy(SmlLocation *source, SmlLocation *target)
00139 {
00140 smlTrace(TRACE_ENTRY, "%s(%p, %p)", __func__, source, target);
00141 smlAssert(source);
00142 smlAssert(target);
00143
00144 if (target->locURI)
00145 smlSafeCFree(&(target->locURI));
00146
00147 if (target->locName)
00148 smlSafeCFree(&(target->locName));
00149
00150 target->locURI = g_strdup(source->locURI);
00151 target->locName = g_strdup(source->locName);
00152
00153 smlTrace(TRACE_EXIT, "%s", __func__);
00154 }
00155
00156 #define URL_DELIMITER "/"
00157
00158 char *strreplace(const char *input, const char *needle, const char *replacement)
00159 {
00160 char *output = g_strdup(input);
00161
00162 while (g_strrstr(output, needle)) {
00163 char *buffer = g_strndup(output, g_strrstr(output, needle) - output);
00164 char *buffer2 = g_strconcat(buffer, replacement ? replacement : "", g_strrstr(output, needle) + strlen(needle), NULL);
00165 smlSafeCFree(&output);
00166 output = buffer2;
00167 smlSafeCFree(&buffer);
00168 }
00169
00170 return output;
00171 }
00172
00173 char *normalizeUrl(const char *url)
00174 {
00175 smlTrace(TRACE_ENTRY, "%s(%s)", __func__, VA_STRING(url));
00176
00177 if (!url)
00178 return NULL;
00179
00180 char *buffer = strreplace(url, "./", "");
00181 char *buffer2 = strreplace(buffer, "//", "/");
00182 smlSafeCFree(&buffer);
00183
00184 if (strlen(buffer2) > 0 && buffer2[strlen(buffer2) - 1] == '/')
00185 buffer2[strlen(buffer2) - 1] = 0;
00186
00187 smlTrace(TRACE_EXIT, "%s: %s", __func__, VA_STRING(buffer2));
00188 return buffer2;
00189 }
00190
00191 SmlBool smlLocationCompare(SmlLocation *objectroot, SmlLocation *object, SmlLocation *urlroot, SmlLocation *url)
00192 {
00193 smlTrace(TRACE_ENTRY, "%s(%p, %p, %p, %p)", __func__, objectroot, object, urlroot, url);
00194 if (objectroot)
00195 smlTrace(TRACE_INTERNAL, "%s: objectroot: %s", __func__, VA_STRING(objectroot->locURI));
00196 if (object)
00197 smlTrace(TRACE_INTERNAL, "%s: object: %s", __func__, VA_STRING(object->locURI));
00198 if (urlroot)
00199 smlTrace(TRACE_INTERNAL, "%s: urlroot: %s", __func__, VA_STRING(urlroot->locURI));
00200 if (url)
00201 smlTrace(TRACE_INTERNAL, "%s: url: %s", __func__, VA_STRING(url->locURI));
00202 char *cmpobjurl = NULL;
00203 char *cmpurl = NULL;
00204 char *buffer = NULL;
00205 char *buffer2 = NULL;
00206 SmlBool ret = FALSE;
00207
00208 if (object && !url) {
00209 smlTrace(TRACE_EXIT, "%s: url not given but object: FALSE", __func__);
00210 return FALSE;
00211 }
00212
00213 if (!object) {
00214 smlTrace(TRACE_EXIT, "%s: No object given: TRUE", __func__);
00215 return TRUE;
00216 }
00217
00218 if (g_path_is_absolute(url->locURI) || !urlroot)
00219 cmpurl = normalizeUrl(url->locURI);
00220 else {
00221 buffer2 = normalizeUrl(url->locURI);
00222 buffer = g_strconcat(urlroot->locURI, "/", buffer2, NULL);
00223 cmpurl = normalizeUrl(buffer);
00224 smlSafeCFree(&buffer);
00225 smlSafeCFree(&buffer2);
00226 }
00227
00228 if (g_path_is_absolute(object->locURI) || !objectroot)
00229 cmpobjurl = normalizeUrl(object->locURI);
00230 else {
00231 buffer2 = normalizeUrl(object->locURI);
00232 buffer = g_strconcat(objectroot->locURI, "/", buffer2, NULL);
00233 cmpobjurl = normalizeUrl(buffer);
00234 smlSafeCFree(&buffer);
00235 smlSafeCFree(&buffer2);
00236 }
00237
00238
00239 smlTrace(TRACE_INTERNAL, "%s: Comparing %s and %s", __func__, VA_STRING(cmpobjurl), VA_STRING(cmpurl));
00240
00241 if (strcmp(cmpobjurl, cmpurl))
00242 ret = FALSE;
00243 else
00244 ret = TRUE;
00245
00246 smlSafeCFree(&cmpobjurl);
00247 smlSafeCFree(&cmpurl);
00248
00249 smlTrace(TRACE_EXIT, "%s: %i", __func__, ret);
00250 return ret;
00251 }
00252
00253 SmlBool smlLocationIsRelative(SmlLocation *location)
00254 {
00255 smlAssert(location);
00256
00257 return g_path_is_absolute(location->locURI) ? FALSE : TRUE;
00258 }
00259
00260 SmlAnchor *smlAnchorNew(const char *last, const char *next, SmlError **error)
00261 {
00262 smlTrace(TRACE_ENTRY, "%s(%s, %s, %p)", __func__, VA_STRING(last), VA_STRING(next), error);
00263 CHECK_ERROR_REF
00264
00265 SmlAnchor *anchor = smlTryMalloc0(sizeof(SmlAnchor), error);
00266 if (!anchor)
00267 goto error;
00268
00269 anchor->last = g_strdup(last);
00270 anchor->next = g_strdup(next);
00271
00272 smlTrace(TRACE_EXIT, "%s: %p", __func__, anchor);
00273 return anchor;
00274
00275 error:
00276 smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
00277 return NULL;
00278 }
00279
00280 void smlAnchorFree(SmlAnchor *anchor)
00281 {
00282 smlTrace(TRACE_ENTRY, "%s(%p)", __func__, anchor);
00283 smlAssert(anchor);
00284 if (anchor->last)
00285 smlSafeCFree(&(anchor->last));
00286
00287 if (anchor->next)
00288 smlSafeCFree(&(anchor->next));
00289
00290 smlSafeFree((gpointer *)&anchor);
00291
00292 smlTrace(TRACE_EXIT, "%s", __func__);
00293 }
00294
00295 void smlHeaderFree(SmlHeader *header)
00296 {
00297 smlTrace(TRACE_ENTRY, "%s(%p)", __func__, header);
00298
00299 if (header->sessionID)
00300 smlSafeCFree(&(header->sessionID));
00301
00302 if (header->emi)
00303 smlSafeCFree(&(header->emi));
00304
00305 if (header->source)
00306 smlLocationUnref(header->source);
00307
00308 if (header->target)
00309 smlLocationUnref(header->target);
00310
00311 if (header->responseURI)
00312 smlSafeCFree(&(header->responseURI));
00313
00314 smlSafeFree((gpointer *)&header);
00315
00316 smlTrace(TRACE_EXIT, "%s", __func__);
00317 }
00318
00319 SmlItem *smlItemNew(unsigned int size, SmlError **error)
00320 {
00321 smlTrace(TRACE_ENTRY, "%s(%i, %p)", __func__, size, error);
00322 CHECK_ERROR_REF
00323
00324 SmlItem *item = smlTryMalloc0(sizeof(SmlItem), error);
00325 if (!item)
00326 goto error;
00327
00328 item->refCount = 1;
00329 item->size = size;
00330
00331 smlTrace(TRACE_EXIT, "%s: %p", __func__, item);
00332 return item;
00333
00334 error:
00335 smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
00336 return NULL;
00337 }
00338
00339
00340 SmlItem *smlItemNewForData(const char *data, unsigned int size, SmlError **error)
00341 {
00342 smlTrace(TRACE_ENTRY, "%s(%p, %i, %p)", __func__, data, size, error);
00343 CHECK_ERROR_REF
00344
00345 SmlItem *item = smlItemNew(size, error);
00346 if (!item)
00347 goto error;
00348
00349 if (data) {
00350 if (!smlItemAddData(item, data, size, error))
00351 goto error_free_item;
00352 }
00353
00354 smlTrace(TRACE_EXIT, "%s: %p", __func__, item);
00355 return item;
00356
00357 error_free_item:
00358 smlItemUnref(item);
00359 error:
00360 smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
00361 return NULL;
00362 }
00363
00364 SmlItem *smlItemRef(SmlItem *item)
00365 {
00366 smlTrace(TRACE_ENTRY, "%s(%p)", __func__, item);
00367 smlAssert(item);
00368
00369 g_atomic_int_inc(&(item->refCount));
00370
00371 smlTrace(TRACE_EXIT, "%s: New refcount: %i", __func__, item->refCount);
00372 return item;
00373 }
00374
00375 void smlItemUnref(SmlItem *item)
00376 {
00377 smlTrace(TRACE_ENTRY, "%s(%p)", __func__, item);
00378 smlAssert(item);
00379
00380 if (g_atomic_int_dec_and_test(&(item->refCount))) {
00381 smlTrace(TRACE_INTERNAL, "%s: Refcount == 0!", __func__);
00382
00383 if (item->source)
00384 smlLocationUnref(item->source);
00385
00386 if (item->target)
00387 smlLocationUnref(item->target);
00388
00389 if (item->sourceParent)
00390 smlLocationUnref(item->sourceParent);
00391
00392 if (item->targetParent)
00393 smlLocationUnref(item->targetParent);
00394
00395 if (item->anchor)
00396 smlAnchorFree(item->anchor);
00397
00398 if (item->buffer)
00399 xmlBufferFree(item->buffer);
00400
00401 if (item->contenttype)
00402 smlSafeCFree(&(item->contenttype));
00403
00404 smlSafeFree((gpointer *)&item);
00405 }
00406
00407 smlTrace(TRACE_EXIT, "%s: %i", __func__, item ? item->refCount : 0);
00408 }
00409
00410 SmlBool smlItemAddData(SmlItem *item, const char *data, unsigned int size, SmlError **error)
00411 {
00412 smlTrace(TRACE_ENTRY, "%s(%p, %p, %i, %p)", __func__, item, data, size, error);
00413 CHECK_ERROR_REF
00414
00415 if (item->size && xmlBufferLength(item->buffer) + size > item->size) {
00416 smlErrorSet(error, SML_ERROR_GENERIC, "Unable to add data. size limit reached");
00417 goto error;
00418 }
00419
00420 if (data) {
00421 if (!item->buffer) {
00422 if (item->size)
00423 item->buffer = xmlBufferCreateSize(item->size);
00424 else
00425 item->buffer = xmlBufferCreateSize(size);
00426 }
00427
00428 if (xmlBufferAdd(item->buffer, (xmlChar *)data, size) != 0) {
00429 smlErrorSet(error, SML_ERROR_GENERIC, "Unable to add data.");
00430 goto error;
00431 }
00432 }
00433
00434 smlTrace(TRACE_EXIT, "%s", __func__);
00435 return TRUE;
00436
00437 error:
00438 smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
00439 return FALSE;
00440
00441 }
00442
00444 SmlBool smlItemCheck(SmlItem *item)
00445 {
00446 smlAssert(xmlBufferLength(item->buffer) >= 0);
00447 smlAssert(item);
00448 if (!item->size)
00449 return TRUE;
00450
00451 if ((unsigned int)xmlBufferLength(item->buffer) != item->size)
00452 {
00453 smlTrace(TRACE_INTERNAL, "%s: failed because size (%d != %d) does not match (%s).",
00454 __func__, item->size,
00455 xmlBufferLength(item->buffer), VA_STRING((char *)xmlBufferContent(item->buffer)));
00456 return FALSE;
00457 }
00458
00459 return TRUE;
00460 }
00461
00462 SmlBool smlItemHasData(SmlItem *item)
00463 {
00464 smlAssert(item);
00465 return item->buffer ? TRUE : FALSE;
00466 }
00467
00470 SmlBool smlItemStealData(SmlItem *item, char **data, unsigned int *size, SmlError **error)
00471 {
00472 smlTrace(TRACE_ENTRY, "%s(%p, %p, %p, %p)", __func__, item, data, size, error);
00473 CHECK_ERROR_REF
00474 smlAssert(size);
00475
00476 if (!smlItemCheck(item)) {
00477 smlErrorSet(error, SML_ERROR_GENERIC, "Item check failed");
00478 goto error;
00479 }
00480
00481 *size = xmlBufferLength(item->buffer);
00482 *data = g_strndup((const char *) xmlBufferContent(item->buffer), *size);
00483 xmlBufferFree(item->buffer);
00484 item->buffer = NULL;
00485
00486 smlTrace(TRACE_EXIT, "%s", __func__);
00487 return TRUE;
00488
00489 error:
00490 smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
00491 return FALSE;
00492 }
00493
00495 SmlBool smlItemGetData(SmlItem *item, char **data, unsigned int *size, SmlError **error)
00496 {
00497 smlTrace(TRACE_ENTRY, "%s(%p, %p, %p, %p)", __func__, item, data, size, error);
00498 CHECK_ERROR_REF
00499
00500 if (!smlItemCheck(item)) {
00501 smlErrorSet(error, SML_ERROR_GENERIC, "Item check failed");
00502 goto error;
00503 }
00504
00505 *data = (char *)xmlBufferContent(item->buffer);
00506 *size = xmlBufferLength(item->buffer);
00507
00508 smlTrace(TRACE_EXIT, "%s", __func__);
00509 return TRUE;
00510
00511 error:
00512 smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
00513 return FALSE;
00514 }
00515
00516 void smlItemSetSource(SmlItem *item, SmlLocation *source)
00517 {
00518 smlAssert(item);
00519 smlAssert(source);
00520
00521 item->source = source;
00522 smlLocationRef(source);
00523 }
00524
00525 SmlLocation *smlItemGetSource(SmlItem *item)
00526 {
00527 smlAssert(item);
00528
00529 return item->source;
00530 }
00531
00532 void smlItemSetTarget(SmlItem *item, SmlLocation *target)
00533 {
00534 smlAssert(item);
00535 smlAssert(target);
00536
00537 item->target = target;
00538 smlLocationRef(target);
00539 }
00540
00541 SmlLocation *smlItemGetTarget(SmlItem *item)
00542 {
00543 smlAssert(item);
00544
00545 return item->target;
00546 }
00547
00548 void smlItemSetSourceParent(SmlItem *item, SmlLocation *sourceParent)
00549 {
00550 smlAssert(item);
00551 smlAssert(sourceParent);
00552
00553 item->sourceParent = sourceParent;
00554 smlLocationRef(sourceParent);
00555 }
00556
00557 SmlLocation *smlItemGetSourceParent(SmlItem *item)
00558 {
00559 smlAssert(item);
00560
00561 return item->sourceParent;
00562 }
00563
00564 void smlItemSetTargetParent(SmlItem *item, SmlLocation *targetParent)
00565 {
00566 smlAssert(item);
00567 smlAssert(targetParent);
00568
00569 item->targetParent = targetParent;
00570 smlLocationRef(targetParent);
00571 }
00572
00573 SmlLocation *smlItemGetTargetParent(SmlItem *item)
00574 {
00575 smlAssert(item);
00576
00577 return item->targetParent;
00578 }
00579
00580 void smlItemSetRaw(SmlItem *item, SmlBool raw)
00581 {
00582 smlAssert(item);
00583
00584 item->raw = raw;
00585 }
00586
00587 SmlCred *smlCredNewFromString(const char *type,
00588 const char *format,
00589 const char *data,
00590 SmlError **error)
00591 {
00592 smlTrace(TRACE_ENTRY, "%s(%s, %s, %s, %p)", __func__, VA_STRING(data), VA_STRING(type), VA_STRING(format), error);
00593 CHECK_ERROR_REF
00594
00595 SmlAuthType smlType = SML_AUTH_TYPE_UNKNOWN;
00596 SmlFormatType smlFormat = SML_FORMAT_TYPE_UNKNOWN;
00597
00598 if (!type || !strcmp(type, SML_AUTH_BASIC)) {
00599 smlType = SML_AUTH_TYPE_BASIC;
00600 } else if (!strcmp(type, SML_AUTH_MD5)) {
00601 smlType = SML_AUTH_TYPE_MD5;
00602 } else {
00603 smlErrorSet(error, SML_ERROR_GENERIC, "Unknown type - %s.", type);
00604 goto error;
00605 }
00606
00607 if (!format || !strcmp(format, SML_BASE64)) {
00608 smlFormat = SML_FORMAT_TYPE_BASE64;
00609 } else {
00610 smlErrorSet(error, SML_ERROR_GENERIC, "SyncML credential: Unknown format - %s.", format);
00611 goto error;
00612 }
00613
00614 if (!data) {
00615 smlErrorSet(error, SML_ERROR_GENERIC, "Data is missing in %s.", __func__);
00616 goto error;
00617 }
00618
00619 smlTrace(TRACE_EXIT, "%s", __func__);
00620 return smlCredNew(smlType, smlFormat, data, NULL, error);
00621 error:
00622 smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
00623 return NULL;
00624 }
00625
00626 SmlCred *smlCredNewAuth(SmlAuthType type,
00627 const char *username,
00628 const char *password,
00629 SmlError **error)
00630 {
00631 smlTrace(TRACE_ENTRY, "%s(%d, %s, %p)", __func__, type, VA_STRING(username), error);
00632 CHECK_ERROR_REF
00633
00634 SmlCred *cred = NULL;
00635
00636 if (username == NULL || !strlen(username)) {
00637 smlErrorSet(error, SML_ERROR_INTERNAL_MISCONFIGURATION,
00638 "If authentication should be used then the username must be set.");
00639 goto error;
00640 }
00641 if (password == NULL || !strlen(password)) {
00642 smlErrorSet(error, SML_ERROR_INTERNAL_MISCONFIGURATION,
00643 "If authentication should be used then the password must be set.");
00644 goto error;
00645 }
00646
00647 cred = smlTryMalloc0(sizeof(SmlCred), error);
00648 if (!cred)
00649 goto error;
00650
00651 cred->refCount = 1;
00652 cred->format = SML_FORMAT_TYPE_BASE64;
00653 cred->type = type;
00654 cred->username = g_strdup(username);
00655 cred->password = g_strdup(password);
00656
00657 smlTrace(TRACE_EXIT, "%s", __func__);
00658 return cred;
00659 error:
00660 if (cred)
00661 smlSafeFree((gpointer *)&cred);
00662 smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
00663 return NULL;
00664 }
00665
00666 SmlCred *smlCredNew(SmlAuthType type,
00667 SmlFormatType format,
00668 const char *data,
00669 const char *username,
00670 SmlError **error)
00671 {
00672 smlTrace(TRACE_ENTRY, "%s(%s, %d, %d, %p)", __func__, VA_STRING(data), type, format, error);
00673 CHECK_ERROR_REF
00674
00675 SmlCred *cred = smlTryMalloc0(sizeof(SmlCred), error);
00676 if (!cred)
00677 goto error;
00678
00679 cred->type = type;
00680 cred->format = format;
00681 cred->data = g_strdup(data);
00682 if (username)
00683 cred->username = g_strdup(username);
00684 else
00685 cred->username = NULL;
00686 cred->refCount = 1;
00687
00688 smlTrace(TRACE_EXIT, "%s: %p", __func__, cred);
00689 return cred;
00690 error:
00691 if (cred->data)
00692 smlSafeCFree(&(cred->data));
00693 if (cred->username)
00694 smlSafeCFree(&(cred->username));
00695 if (cred)
00696 smlSafeFree((gpointer *)&cred);
00697 smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
00698 return NULL;
00699 }
00700
00701 void smlCredRef(SmlCred *cred)
00702 {
00703 smlTrace(TRACE_ENTRY, "%s(%p)", __func__, cred);
00704 smlAssert(cred);
00705
00706 g_atomic_int_inc(&(cred->refCount));
00707
00708 smlTrace(TRACE_EXIT, "%s: New refcount: %i", __func__, cred->refCount);
00709 }
00710
00711 void smlCredUnref(SmlCred *cred)
00712 {
00713 smlTrace(TRACE_ENTRY, "%s(%p)", __func__, cred);
00714 smlAssert(cred);
00715
00716 if (g_atomic_int_dec_and_test(&(cred->refCount))) {
00717 smlTrace(TRACE_INTERNAL, "%s: Refcount == 0!", __func__);
00718
00719 if (cred->data)
00720 smlSafeCFree(&(cred->data));
00721 if (cred->username)
00722 smlSafeCFree(&(cred->username));
00723 if (cred->password)
00724 smlSafeCFree(&(cred->password));
00725
00726 smlSafeFree((gpointer *)&cred);
00727 }
00728
00729 smlTrace(TRACE_EXIT, "%s", __func__);
00730 }
00731
00732
00733 void smlCredFree(SmlCred *cred)
00734 {
00735 smlCredUnref(cred);
00736 }
00737
00738 SmlChal *smlChalNew(SmlAuthType type, SmlError **error)
00739 {
00740 smlTrace(TRACE_ENTRY, "%s(%u, %p)", __func__, type, error);
00741 CHECK_ERROR_REF
00742 SmlChal *chal = NULL;
00743
00744
00745 smlAssert(type != SML_AUTH_TYPE_UNKNOWN);
00746 chal = smlTryMalloc0(sizeof(SmlChal), error);
00747 if (!chal)
00748 goto error;
00749 chal->refCount = 1;
00750 chal->type = type;
00751 chal->format = SML_FORMAT_TYPE_BASE64;
00752
00753 if (type == SML_AUTH_TYPE_MD5)
00754 {
00755
00756
00757
00758
00759
00760 chal->nonce_plain = smlRandStr(26, TRUE);
00761 chal->nonce_length = 26;
00762 chal->nonce_b64 = g_base64_encode(
00763 (const unsigned char *) chal->nonce_plain,
00764 chal->nonce_length);
00765 if (!chal->nonce_b64) {
00766 smlErrorSet(error, SML_ERROR_GENERIC,
00767 "The nonce of the challenge cannot be base64 encoded.");
00768 goto error;
00769 }
00770 }
00771
00772 smlTrace(TRACE_EXIT, "%s", __func__);
00773 return chal;
00774 error:
00775 if (chal->nonce_plain)
00776 smlSafeCFree(&(chal->nonce_plain));
00777 if (chal->nonce_b64)
00778 smlSafeCFree(&(chal->nonce_b64));
00779 if (chal)
00780 smlSafeFree((gpointer *)&chal);
00781 smlTrace(TRACE_EXIT_ERROR, "%s - %s", __func__, smlErrorPrint(error));
00782 return NULL;
00783 }
00784
00785 SmlChal *smlChalNewFromBinary(
00786 SmlAuthType type,
00787 const char *nonce,
00788 size_t length,
00789 SmlError **error)
00790 {
00791 smlTrace(TRACE_ENTRY, "%s", __func__);
00792 CHECK_ERROR_REF
00793 SmlChal *chal = NULL;
00794
00795
00796 smlAssert(type == SML_AUTH_TYPE_MD5);
00797
00798
00799 chal = smlTryMalloc0(sizeof(SmlChal), error);
00800 if (!chal)
00801 goto error;
00802 chal->refCount = 1;
00803
00804
00805 chal->type = SML_AUTH_TYPE_MD5;
00806 chal->format = SML_FORMAT_TYPE_BASE64;
00807 chal->nonce_plain = g_strndup(nonce, length);
00808 chal->nonce_length = length;
00809
00810
00811 chal->nonce_b64 = g_base64_encode((const unsigned char *) nonce, length);
00812 if (!chal->nonce_b64) {
00813 smlErrorSet(error, SML_ERROR_GENERIC,
00814 "The base64 encoding of the nonce failed.");
00815 goto error;
00816 }
00817
00818 smlTrace(TRACE_EXIT, "%s", __func__);
00819 return chal;
00820 error:
00821 smlTrace(TRACE_EXIT_ERROR, "%s - %s", __func__, smlErrorPrint(error));
00822 return NULL;
00823 }
00824
00825 SmlChal *smlChalNewFromBase64(
00826 SmlAuthType type,
00827 const char *nonce,
00828 SmlError **error)
00829 {
00830 smlTrace(TRACE_ENTRY, "%s", __func__);
00831 CHECK_ERROR_REF
00832 SmlChal *chal = NULL;
00833
00834
00835 smlAssert(type == SML_AUTH_TYPE_MD5);
00836
00837
00838 chal = smlTryMalloc0(sizeof(SmlChal), error);
00839 if (!chal)
00840 goto error;
00841 chal->refCount = 1;
00842
00843
00844 chal->type = SML_AUTH_TYPE_MD5;
00845 chal->format = SML_FORMAT_TYPE_BASE64;
00846 chal->nonce_b64 = g_strdup(nonce);
00847
00848
00849 size_t length = 0;
00850 chal->nonce_plain = (char *) g_base64_decode(nonce, &length);
00851 if (!chal->nonce_plain || length < 1) {
00852 smlErrorSet(error, SML_ERROR_GENERIC,
00853 "The base64 encoded nonce cannot be decoded.");
00854 goto error;
00855 }
00856 chal->nonce_length = length;
00857
00858 smlTrace(TRACE_EXIT, "%s", __func__);
00859 return chal;
00860 error:
00861 smlTrace(TRACE_EXIT_ERROR, "%s - %s", __func__, smlErrorPrint(error));
00862 return NULL;
00863 }
00864
00865 void smlChalRef(SmlChal *chal)
00866 {
00867 smlTrace(TRACE_ENTRY, "%s", __func__);
00868 smlAssert(chal);
00869
00870 g_atomic_int_inc(&(chal->refCount));
00871
00872 smlTrace(TRACE_EXIT, "%s: New refcount: %i", __func__, chal->refCount);
00873 }
00874
00875 void smlChalUnref(SmlChal *chal)
00876 {
00877 smlTrace(TRACE_ENTRY, "%s", __func__);
00878 smlAssert(chal);
00879
00880 if (g_atomic_int_dec_and_test(&(chal->refCount))) {
00881 smlTrace(TRACE_INTERNAL, "%s: Refcount == 0!", __func__);
00882
00883 if (chal->nonce_plain)
00884 smlSafeCFree(&(chal->nonce_plain));
00885
00886 if (chal->nonce_b64)
00887 smlSafeCFree(&(chal->nonce_b64));
00888
00889 smlSafeFree((gpointer *)&chal);
00890 }
00891
00892 smlTrace(TRACE_EXIT, "%s", __func__);
00893 }
00894
00895
00896 void smlChalFree(SmlChal *chal)
00897 {
00898 smlChalUnref(chal);
00899 }
00900
00901 SmlMapItem *smlMapItemNew(const char *uid, const char *newuid, SmlError **error)
00902 {
00903 smlTrace(TRACE_ENTRY, "%s(%s, %s, %p)", __func__, VA_STRING(uid), VA_STRING(newuid), error);
00904 CHECK_ERROR_REF
00905 smlAssert(uid);
00906 smlAssert(newuid);
00907
00908 SmlMapItem *item = smlTryMalloc0(sizeof(SmlMapItem), error);
00909 if (!item)
00910 goto error;
00911 item->refCount = 1;
00912
00913 item->source = smlLocationNew(newuid, NULL, error);
00914 if (!item->source)
00915 goto error_free_item;
00916
00917 item->target = smlLocationNew(uid, NULL, error);
00918 if (!item->target)
00919 goto error_free_source;
00920
00921 smlTrace(TRACE_EXIT, "%s: %p", __func__, item);
00922 return item;
00923
00924 error_free_source:
00925 smlLocationUnref(item->source);
00926 error_free_item:
00927 smlSafeFree((gpointer *)&item);
00928 error:
00929 smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
00930 return NULL;
00931 }
00932
00933 SmlMapItem *smlMapItemRef(SmlMapItem *item)
00934 {
00935 smlTrace(TRACE_ENTRY, "%s(%p)", __func__, item);
00936 smlAssert(item);
00937
00938 g_atomic_int_inc(&(item->refCount));
00939
00940 smlTrace(TRACE_EXIT, "%s: New refcount: %i", __func__, item->refCount);
00941 return item;
00942 }
00943
00944 void smlMapItemUnref(SmlMapItem *item)
00945 {
00946 smlTrace(TRACE_ENTRY, "%s(%p)", __func__, item);
00947 smlAssert(item);
00948
00949 if (g_atomic_int_dec_and_test(&(item->refCount))) {
00950 smlTrace(TRACE_INTERNAL, "%s: Refcount == 0!", __func__);
00951
00952 if (item->source)
00953 {
00954 smlLocationUnref(item->source);
00955 item->source = NULL;
00956 }
00957
00958 if (item->target)
00959 {
00960 smlLocationUnref(item->target);
00961 item->target = NULL;
00962 }
00963
00964 smlSafeFree((gpointer *)&item);
00965 }
00966
00967 smlTrace(TRACE_EXIT, "%s", __func__);
00968 }
00969