Path: blob/main/crypto/krb5/src/ccapi/lib/ccapi_ccache_iterator.c
39536 views
/* ccapi/lib/ccapi_ccache_iterator.c */1/*2* Copyright 2006 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 "ccapi_ccache_iterator.h"26#include "ccapi_ccache.h"27#include "ccapi_ipc.h"2829/* ------------------------------------------------------------------------ */3031typedef struct cci_ccache_iterator_d {32cc_ccache_iterator_f *functions;33#if TARGET_OS_MAC34cc_ccache_iterator_f *vector_functions;35#endif36cci_identifier_t identifier;37char *saved_ccache_name;38} *cci_ccache_iterator_t;3940/* ------------------------------------------------------------------------ */4142struct cci_ccache_iterator_d cci_ccache_iterator_initializer = {43NULL44VECTOR_FUNCTIONS_INITIALIZER,45NULL,46NULL47};4849cc_ccache_iterator_f cci_ccache_iterator_f_initializer = {50ccapi_ccache_iterator_release,51ccapi_ccache_iterator_next,52ccapi_ccache_iterator_clone53};5455/* ------------------------------------------------------------------------ */5657cc_int32 cci_ccache_iterator_new (cc_ccache_iterator_t *out_ccache_iterator,58cci_identifier_t in_identifier)59{60cc_int32 err = ccNoError;61cci_ccache_iterator_t ccache_iterator = NULL;6263if (!in_identifier ) { err = cci_check_error (ccErrBadParam); }64if (!out_ccache_iterator) { err = cci_check_error (ccErrBadParam); }6566if (!err) {67ccache_iterator = malloc (sizeof (*ccache_iterator));68if (ccache_iterator) {69*ccache_iterator = cci_ccache_iterator_initializer;70} else {71err = cci_check_error (ccErrNoMem);72}73}7475if (!err) {76ccache_iterator->functions = malloc (sizeof (*ccache_iterator->functions));77if (ccache_iterator->functions) {78*ccache_iterator->functions = cci_ccache_iterator_f_initializer;79} else {80err = cci_check_error (ccErrNoMem);81}82}8384if (!err) {85err = cci_identifier_copy (&ccache_iterator->identifier, in_identifier);86}8788if (!err) {89*out_ccache_iterator = (cc_ccache_iterator_t) ccache_iterator;90ccache_iterator = NULL; /* take ownership */91}9293ccapi_ccache_iterator_release ((cc_ccache_iterator_t) ccache_iterator);9495return cci_check_error (err);96}9798/* ------------------------------------------------------------------------ */99100cc_int32 cci_ccache_iterator_write (cc_ccache_iterator_t in_ccache_iterator,101k5_ipc_stream in_stream)102{103cc_int32 err = ccNoError;104cci_ccache_iterator_t ccache_iterator = (cci_ccache_iterator_t) in_ccache_iterator;105106if (!in_ccache_iterator) { err = cci_check_error (ccErrBadParam); }107if (!in_stream ) { err = cci_check_error (ccErrBadParam); }108109if (!err) {110err = cci_identifier_write (ccache_iterator->identifier, in_stream);111}112113return cci_check_error (err);114}115116/* ------------------------------------------------------------------------ */117118cc_int32 ccapi_ccache_iterator_release (cc_ccache_iterator_t io_ccache_iterator)119{120cc_int32 err = ccNoError;121cci_ccache_iterator_t ccache_iterator = (cci_ccache_iterator_t) io_ccache_iterator;122123if (!io_ccache_iterator) { err = ccErrBadParam; }124125if (!err) {126cc_uint32 initialized = 0;127128err = cci_identifier_is_initialized (ccache_iterator->identifier,129&initialized);130131if (!err && initialized) {132err = cci_ipc_send (cci_ccache_iterator_release_msg_id,133ccache_iterator->identifier,134NULL,135NULL);136if (err) {137cci_debug_printf ("%s: cci_ipc_send failed with error %d",138__FUNCTION__, err);139err = ccNoError;140}141}142}143144if (!err) {145free ((char *) ccache_iterator->functions);146cci_identifier_release (ccache_iterator->identifier);147free (ccache_iterator->saved_ccache_name);148free (ccache_iterator);149}150151return err;152}153154/* ------------------------------------------------------------------------ */155156cc_int32 ccapi_ccache_iterator_next (cc_ccache_iterator_t in_ccache_iterator,157cc_ccache_t *out_ccache)158{159cc_int32 err = ccNoError;160cci_ccache_iterator_t ccache_iterator = (cci_ccache_iterator_t) in_ccache_iterator;161k5_ipc_stream reply = NULL;162cci_identifier_t identifier = NULL;163164if (!in_ccache_iterator) { err = cci_check_error (ccErrBadParam); }165if (!out_ccache ) { err = cci_check_error (ccErrBadParam); }166167if (!err) {168cc_uint32 initialized = 0;169170err = cci_identifier_is_initialized (ccache_iterator->identifier,171&initialized);172173if (!err && !initialized) {174/* server doesn't actually exist. Pretend we're empty. */175err = cci_check_error (ccIteratorEnd);176}177}178179if (!err) {180err = cci_ipc_send (cci_ccache_iterator_next_msg_id,181ccache_iterator->identifier,182NULL,183&reply);184}185186if (!err) {187err = cci_identifier_read (&identifier, reply);188}189190if (!err) {191err = cci_ccache_new (out_ccache, identifier);192}193194krb5int_ipc_stream_release (reply);195cci_identifier_release (identifier);196197return cci_check_error (err);198}199200/* ------------------------------------------------------------------------ */201202cc_int32 ccapi_ccache_iterator_clone (cc_ccache_iterator_t in_ccache_iterator,203cc_ccache_iterator_t *out_ccache_iterator)204{205cc_int32 err = ccNoError;206cci_ccache_iterator_t ccache_iterator = (cci_ccache_iterator_t) in_ccache_iterator;207k5_ipc_stream reply = NULL;208cc_uint32 initialized = 0;209cci_identifier_t identifier = NULL;210211if (!in_ccache_iterator ) { err = cci_check_error (ccErrBadParam); }212if (!out_ccache_iterator) { err = cci_check_error (ccErrBadParam); }213214if (!err) {215err = cci_identifier_is_initialized (ccache_iterator->identifier,216&initialized);217}218219if (!err) {220if (initialized) {221err = cci_ipc_send (cci_ccache_iterator_next_msg_id,222ccache_iterator->identifier,223NULL,224&reply);225226if (!err) {227err = cci_identifier_read (&identifier, reply);228}229230} else {231/* server doesn't actually exist. Make another dummy one. */232identifier = cci_identifier_uninitialized;233}234}235236if (!err) {237err = cci_ccache_iterator_new (out_ccache_iterator, identifier);238}239240cci_identifier_release (identifier);241krb5int_ipc_stream_release (reply);242243return cci_check_error (err);244}245246/* ------------------------------------------------------------------------ */247248cc_int32 cci_ccache_iterator_get_saved_ccache_name (cc_ccache_iterator_t in_ccache_iterator,249const char **out_saved_ccache_name)250{251cc_int32 err = ccNoError;252cci_ccache_iterator_t ccache_iterator = (cci_ccache_iterator_t) in_ccache_iterator;253254if (!in_ccache_iterator ) { err = cci_check_error (ccErrBadParam); }255if (!out_saved_ccache_name) { err = cci_check_error (ccErrBadParam); }256257if (!err) {258*out_saved_ccache_name = ccache_iterator->saved_ccache_name;259}260261return cci_check_error (err);262}263264/* ------------------------------------------------------------------------ */265266cc_int32 cci_ccache_iterator_set_saved_ccache_name (cc_ccache_iterator_t io_ccache_iterator,267const char *in_saved_ccache_name)268{269cc_int32 err = ccNoError;270cci_ccache_iterator_t ccache_iterator = (cci_ccache_iterator_t) io_ccache_iterator;271char *new_saved_ccache_name = NULL;272273if (!io_ccache_iterator) { err = cci_check_error (ccErrBadParam); }274275if (!err && in_saved_ccache_name) {276new_saved_ccache_name = strdup (in_saved_ccache_name);277if (!new_saved_ccache_name) { err = ccErrNoMem; }278}279280if (!err) {281free (ccache_iterator->saved_ccache_name);282283ccache_iterator->saved_ccache_name = new_saved_ccache_name;284new_saved_ccache_name = NULL; /* take ownership */285}286287free (new_saved_ccache_name);288289return cci_check_error (err);290}291292293