Path: blob/main/crypto/krb5/src/ccapi/common/cci_identifier.c
39537 views
/* ccapi/common/cci_identifier.c */1/*2* Copyright 2006, 2007 Massachusetts Institute of Technology.3* All Rights Reserved.4*5* Export of this software from the United States of America may6* require a specific license from the United States Government.7* It is the responsibility of any person or organization contemplating8* export to obtain such a license before exporting.9*10* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and11* distribute this software and its documentation for any purpose and12* without fee is hereby granted, provided that the above copyright13* notice appear in all copies and that both that copyright notice and14* this permission notice appear in supporting documentation, and that15* the name of M.I.T. not be used in advertising or publicity pertaining16* to distribution of the software without specific, written prior17* permission. Furthermore if you modify this software you must label18* your software as modified software and not distribute it in such a19* fashion that it might be confused with the original M.I.T. software.20* M.I.T. makes no representations about the suitability of21* this software for any purpose. It is provided "as is" without express22* or implied warranty.23*/2425#include "cci_common.h"26#include "cci_os_identifier.h"2728struct cci_identifier_d {29cci_uuid_string_t server_id;30cci_uuid_string_t object_id;31};3233struct cci_identifier_d cci_identifier_initializer = { NULL, NULL };3435#define cci_uninitialized_server_id "NEEDS_SYNC"36#define cci_uninitialized_object_id "NEEDS_SYNC"3738struct cci_identifier_d cci_identifier_uninitialized_d = {39cci_uninitialized_server_id,40cci_uninitialized_object_id41};42const cci_identifier_t cci_identifier_uninitialized = &cci_identifier_uninitialized_d;4344/* ------------------------------------------------------------------------ */4546cc_int32 cci_identifier_new_uuid (cci_uuid_string_t *out_uuid_string)47{48return cci_os_identifier_new_uuid (out_uuid_string);49}5051/* ------------------------------------------------------------------------ */5253static cc_int32 cci_identifier_alloc (cci_identifier_t *out_identifier,54cci_uuid_string_t in_server_id,55cci_uuid_string_t in_object_id)56{57cc_int32 err = ccNoError;58cci_identifier_t identifier = NULL;5960if (!out_identifier) { err = cci_check_error (ccErrBadParam); }61if (!in_server_id ) { err = cci_check_error (ccErrBadParam); }62if (!in_object_id ) { err = cci_check_error (ccErrBadParam); }6364if (!err) {65identifier = malloc (sizeof (*identifier));66if (identifier) {67*identifier = cci_identifier_initializer;68} else {69err = cci_check_error (ccErrNoMem);70}71}7273if (!err) {74identifier->server_id = strdup (in_server_id);75if (!identifier->server_id) { err = cci_check_error (ccErrNoMem); }76}7778if (!err) {79identifier->object_id = strdup (in_object_id);80if (!identifier->object_id) { err = cci_check_error (ccErrNoMem); }81}8283if (!err) {84*out_identifier = identifier;85identifier = NULL; /* take ownership */86}8788cci_identifier_release (identifier);8990return cci_check_error (err);91}9293/* ------------------------------------------------------------------------ */9495cc_int32 cci_identifier_new (cci_identifier_t *out_identifier,96cci_uuid_string_t in_server_id)97{98cc_int32 err = ccNoError;99cci_uuid_string_t object_id = NULL;100101if (!out_identifier) { err = cci_check_error (ccErrBadParam); }102if (!in_server_id ) { err = cci_check_error (ccErrBadParam); }103104if (!err) {105err = cci_os_identifier_new_uuid (&object_id);106}107108if (!err) {109err = cci_identifier_alloc (out_identifier, in_server_id, object_id);110}111112if (object_id) { free (object_id); }113114return cci_check_error (err);115}116117/* ------------------------------------------------------------------------ */118119cc_int32 cci_identifier_copy (cci_identifier_t *out_identifier,120cci_identifier_t in_identifier)121{122cc_int32 err = ccNoError;123124if (!out_identifier) { err = cci_check_error (ccErrBadParam); }125if (!in_identifier ) { err = cci_check_error (ccErrBadParam); }126127if (!err) {128err = cci_identifier_alloc (out_identifier,129in_identifier->server_id,130in_identifier->object_id);131}132133return cci_check_error (err);134}135136/* ------------------------------------------------------------------------ */137138cc_int32 cci_identifier_release (cci_identifier_t in_identifier)139{140cc_int32 err = ccNoError;141142/* Do not free the static "uninitialized" identifier */143if (!err && in_identifier && in_identifier != cci_identifier_uninitialized) {144free (in_identifier->server_id);145free (in_identifier->object_id);146free (in_identifier);147}148149return cci_check_error (err);150}151152#ifdef TARGET_OS_MAC153#pragma mark -154#endif155156/* ------------------------------------------------------------------------ */157158cc_int32 cci_identifier_compare (cci_identifier_t in_identifier,159cci_identifier_t in_compare_to_identifier,160cc_uint32 *out_equal)161{162cc_int32 err = ccNoError;163164if (!in_identifier ) { err = cci_check_error (ccErrBadParam); }165if (!in_compare_to_identifier) { err = cci_check_error (ccErrBadParam); }166if (!out_equal ) { err = cci_check_error (ccErrBadParam); }167168if (!err) {169*out_equal = (!strcmp (in_identifier->object_id,170in_compare_to_identifier->object_id) &&171!strcmp (in_identifier->server_id,172in_compare_to_identifier->server_id));173}174175return cci_check_error (err);176}177178/* ------------------------------------------------------------------------ */179180cc_int32 cci_identifier_is_for_server (cci_identifier_t in_identifier,181cci_uuid_string_t in_server_id,182cc_uint32 *out_is_for_server)183{184cc_int32 err = ccNoError;185186if (!in_identifier ) { err = cci_check_error (ccErrBadParam); }187if (!in_server_id ) { err = cci_check_error (ccErrBadParam); }188if (!out_is_for_server) { err = cci_check_error (ccErrBadParam); }189190if (!err) {191*out_is_for_server = (!strcmp (in_identifier->server_id, in_server_id) ||192!strcmp (in_identifier->server_id, cci_uninitialized_server_id));193}194195return cci_check_error (err);196}197198/* ------------------------------------------------------------------------ */199200cc_int32 cci_identifier_compare_server_id (cci_identifier_t in_identifier,201cci_identifier_t in_compare_to_identifier,202cc_uint32 *out_equal_server_id)203{204cc_int32 err = ccNoError;205206if (!in_identifier ) { err = cci_check_error (ccErrBadParam); }207if (!in_compare_to_identifier) { err = cci_check_error (ccErrBadParam); }208if (!out_equal_server_id ) { err = cci_check_error (ccErrBadParam); }209210if (!err) {211*out_equal_server_id = (!strcmp (in_identifier->server_id,212in_compare_to_identifier->server_id));213}214215return cci_check_error (err);216}217218/* ------------------------------------------------------------------------ */219220cc_int32 cci_identifier_is_initialized (cci_identifier_t in_identifier,221cc_uint32 *out_is_initialized)222{223cc_int32 err = ccNoError;224225if (!in_identifier ) { err = cci_check_error (ccErrBadParam); }226if (!out_is_initialized) { err = cci_check_error (ccErrBadParam); }227228if (!err) {229*out_is_initialized = (strcmp (in_identifier->server_id,230cci_uninitialized_server_id) != 0);231}232233return cci_check_error (err);234}235236#ifdef TARGET_OS_MAC237#pragma mark -238#endif239240/* ------------------------------------------------------------------------ */241242cc_uint32 cci_identifier_read (cci_identifier_t *out_identifier,243k5_ipc_stream io_stream)244{245cc_int32 err = ccNoError;246cci_uuid_string_t server_id = NULL;247cci_uuid_string_t object_id = NULL;248249if (!out_identifier) { err = cci_check_error (ccErrBadParam); }250if (!io_stream ) { err = cci_check_error (ccErrBadParam); }251252if (!err) {253err = krb5int_ipc_stream_read_string (io_stream, &server_id);254}255256if (!err) {257err = krb5int_ipc_stream_read_string (io_stream, &object_id);258}259260if (!err) {261err = cci_identifier_alloc (out_identifier, server_id, object_id);262}263264krb5int_ipc_stream_free_string (server_id);265krb5int_ipc_stream_free_string (object_id);266267return cci_check_error (err);268}269270/* ------------------------------------------------------------------------ */271272cc_uint32 cci_identifier_write (cci_identifier_t in_identifier,273k5_ipc_stream io_stream)274{275cc_int32 err = ccNoError;276277if (!in_identifier) { err = cci_check_error (ccErrBadParam); }278if (!io_stream ) { err = cci_check_error (ccErrBadParam); }279280if (!err) {281err = krb5int_ipc_stream_write_string (io_stream, in_identifier->server_id);282}283284if (!err) {285err = krb5int_ipc_stream_write_string (io_stream, in_identifier->object_id);286}287288return cci_check_error (err);289}290291292