summaryrefslogtreecommitdiffstats
path: root/src/kim/lib/kim_selection_hints.c
blob: a04541bd7775d3417ad6876c97ecbfc67e3848ab (plain)
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
/*
 * Copyright 2005-2006 Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 * require a specific license from the United States Government.
 * It is the responsibility of any person or organization contemplating
 * export to obtain such a license before exporting.
 * 
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 */

#include "kim_private.h"

/* ------------------------------------------------------------------------ */

struct kim_selection_hints_opaque {
    kim_string application_identifier;
    kim_string explanation;
    kim_options options;
    kim_boolean allow_user_interaction;
    kim_boolean use_cached_results;
    kim_string service_identity;
    kim_string client_realm;
    kim_string user;
    kim_string service_realm;
    kim_string service;
    kim_string server;
};

struct kim_selection_hints_opaque kim_selection_hints_initializer = { 
    NULL,
    NULL,
    KIM_OPTIONS_DEFAULT,
    TRUE,
    TRUE,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL
};

/* ------------------------------------------------------------------------ */

static inline kim_error kim_selection_hints_allocate (kim_selection_hints *out_selection_hints)
{
    kim_error err = kim_library_init ();
    kim_selection_hints selection_hints = NULL;
    
    if (!err && !out_selection_hints) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        selection_hints = malloc (sizeof (*selection_hints));
        if (!selection_hints) { err = KIM_OUT_OF_MEMORY_ERR; }
    }
    
    if (!err) {
        *selection_hints = kim_selection_hints_initializer;
        *out_selection_hints = selection_hints;
        selection_hints = NULL;
    }
    
    kim_selection_hints_free (&selection_hints);
    
    return check_error (err);    
}

/* ------------------------------------------------------------------------ */

kim_error kim_selection_hints_create (kim_selection_hints *out_selection_hints,
                                      kim_string           in_application_identifier)
{
    kim_error err = KIM_NO_ERROR;
    kim_selection_hints selection_hints = NULL;
    
    if (!err && !out_selection_hints      ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !in_application_identifier) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_selection_hints_allocate (&selection_hints);
    }
    
    if (!err) {
        err = kim_string_copy (&selection_hints->application_identifier, 
                               in_application_identifier);
    }
    
    if (!err) {
        *out_selection_hints = selection_hints;
        selection_hints = NULL;
    }
    
    kim_selection_hints_free (&selection_hints);
    
    return check_error (err);    
}

/* ------------------------------------------------------------------------ */

kim_error kim_selection_hints_copy (kim_selection_hints *out_selection_hints,
                                    kim_selection_hints  in_selection_hints)
{
    kim_error err = KIM_NO_ERROR;
    kim_selection_hints selection_hints = NULL;
    
    if (!err && !out_selection_hints) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !in_selection_hints ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_selection_hints_allocate (&selection_hints);
    }
    
    if (!err) {
        err = kim_string_copy (&selection_hints->application_identifier, 
                               in_selection_hints->application_identifier);
    }
    
    if (!err && in_selection_hints->explanation) {
        err = kim_string_copy (&selection_hints->explanation, 
                               in_selection_hints->explanation);
    }
    
    if (!err && in_selection_hints->options) {
        err = kim_options_copy (&selection_hints->options, 
                                in_selection_hints->options);
    }
    
    if (!err && in_selection_hints->service_identity) {
        err = kim_string_copy (&selection_hints->service_identity, 
                               in_selection_hints->service_identity);
    }
    
    if (!err && in_selection_hints->client_realm) {
        err = kim_string_copy (&selection_hints->client_realm, 
                               in_selection_hints->client_realm);
    }
    
    if (!err && in_selection_hints->user) {
        err = kim_string_copy (&selection_hints->user, 
                               in_selection_hints->user);
    }
    
    if (!err && in_selection_hints->service_realm) {
        err = kim_string_copy (&selection_hints->service_realm, 
                               in_selection_hints->service_realm);
    }
    
    if (!err && in_selection_hints->service) {
        err = kim_string_copy (&selection_hints->service, 
                               in_selection_hints->service);
    }
    
    if (!err && in_selection_hints->server) {
        err = kim_string_copy (&selection_hints->server, 
                               in_selection_hints->server);
    }
    
    if (!err) {
        selection_hints->allow_user_interaction = in_selection_hints->allow_user_interaction;
        selection_hints->use_cached_results = in_selection_hints->use_cached_results;
        
        *out_selection_hints = selection_hints;
        selection_hints = NULL;
    }
    
    kim_selection_hints_free (&selection_hints);
    
    return check_error (err);    
}

/* ------------------------------------------------------------------------ */

kim_error kim_selection_hints_set_hint (kim_selection_hints io_selection_hints,
                                        kim_string          in_hint_key,
                                        kim_string          in_hint_string)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !io_selection_hints) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !in_hint_key       ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !in_hint_string    ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        if (!strcmp (in_hint_key, kim_hint_key_client_realm)) {
            err = kim_string_copy (&io_selection_hints->client_realm, 
                                   in_hint_string);
            
        } else if (!strcmp (in_hint_key, kim_hint_key_user)) {
            err = kim_string_copy (&io_selection_hints->user, 
                                   in_hint_string);
            
        } else if (!strcmp (in_hint_key, kim_hint_key_service_realm)) {
            err = kim_string_copy (&io_selection_hints->service_realm, 
                                   in_hint_string);
            
        } else if (!strcmp (in_hint_key, kim_hint_key_service)) {
            err = kim_string_copy (&io_selection_hints->service, 
                                   in_hint_string);
            
        } else if (!strcmp (in_hint_key, kim_hint_key_server)) {
            err = kim_string_copy (&io_selection_hints->server, 
                                   in_hint_string);
            
        } else if (!strcmp (in_hint_key, kim_hint_key_service_identity)) {
            err = kim_string_copy (&io_selection_hints->service_identity, 
                                   in_hint_string);
            
        } else {
            err = kim_error_set_message_for_code (KIM_UNSUPPORTED_HINT_ERR,
                                                  in_hint_key);
        }
    }
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_selection_hints_get_hint (kim_selection_hints  in_selection_hints,
                                        kim_string           in_hint_key,
                                        kim_string          *out_hint_string)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !in_selection_hints) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !in_hint_key       ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !out_hint_string   ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        if (!strcmp (in_hint_key, kim_hint_key_client_realm)) {
            err = kim_string_copy (out_hint_string, 
                                   in_selection_hints->client_realm);
            
        } else if (!strcmp (in_hint_key, kim_hint_key_user)) {
            err = kim_string_copy (out_hint_string, 
                                   in_selection_hints->user);
            
        } else if (!strcmp (in_hint_key, kim_hint_key_service_realm)) {
            err = kim_string_copy (out_hint_string, 
                                   in_selection_hints->service_realm);
            
        } else if (!strcmp (in_hint_key, kim_hint_key_service)) {
            err = kim_string_copy (out_hint_string, 
                                   in_selection_hints->service);
            
        } else if (!strcmp (in_hint_key, kim_hint_key_server)) {
            err = kim_string_copy (out_hint_string, 
                                   in_selection_hints->server);
            
        } else if (!strcmp (in_hint_key, kim_hint_key_service_identity)) {
            err = kim_string_copy (out_hint_string, 
                                   in_selection_hints->service_identity);
            
        } else {
            err = kim_error_set_message_for_code (KIM_UNSUPPORTED_HINT_ERR,
                                                  in_hint_key);
        }
    }
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_selection_hints_set_explanation (kim_selection_hints io_selection_hints,
                                               kim_string          in_explanation)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !io_selection_hints) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !in_explanation    ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_string_copy (&io_selection_hints->explanation, in_explanation);
    }
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_selection_hints_get_explanation (kim_selection_hints  in_selection_hints,
                                               kim_string          *out_explanation)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !in_selection_hints) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !out_explanation   ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        if (in_selection_hints->explanation) {
            err = kim_string_copy (out_explanation, in_selection_hints->explanation);
        } else {
            *out_explanation = NULL;
        }
    }
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_selection_hints_get_application_id (kim_selection_hints  in_selection_hints,
                                                  kim_string          *out_application_id)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !in_selection_hints) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !out_application_id) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        if (in_selection_hints->application_identifier) {
            err = kim_string_copy (out_application_id, 
                                   in_selection_hints->application_identifier);
        } else {
            *out_application_id = NULL;
        }
    }
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_selection_hints_set_options (kim_selection_hints io_selection_hints,
                                           kim_options         in_options)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !io_selection_hints) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !in_options        ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_options_copy (&io_selection_hints->options, in_options);
    }
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_selection_hints_get_options (kim_selection_hints  in_selection_hints,
                                           kim_options         *out_options)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !in_selection_hints) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !out_options       ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        if (in_selection_hints->options) {
            err = kim_options_copy (out_options, in_selection_hints->options);
        } else {
            *out_options = KIM_OPTIONS_DEFAULT;
        }
    }
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_selection_hints_set_allow_user_interaction (kim_selection_hints io_selection_hints,
                                                          kim_boolean         in_allow_user_interaction)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !io_selection_hints ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        io_selection_hints->allow_user_interaction = in_allow_user_interaction;
    }
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_selection_hints_get_allow_user_interaction (kim_selection_hints  in_selection_hints,
                                                          kim_boolean         *out_allow_user_interaction)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !in_selection_hints        ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !out_allow_user_interaction) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        *out_allow_user_interaction = in_selection_hints->allow_user_interaction;
    }
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_selection_hints_set_remember_identity (kim_selection_hints io_selection_hints,
                                                     kim_boolean         in_use_cached_results)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !io_selection_hints ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        io_selection_hints->use_cached_results = in_use_cached_results;
    }
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_selection_hints_get_remember_identity (kim_selection_hints  in_selection_hints,
                                                     kim_boolean         *out_use_cached_results)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !in_selection_hints    ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !out_use_cached_results) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        *out_use_cached_results = in_selection_hints->use_cached_results;
    }
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_selection_hints_get_identity (kim_selection_hints  in_selection_hints,
                                            kim_identity        *out_identity)
{
    kim_error err = KIM_NO_ERROR;
    kim_identity identity = NULL;
    kim_ccache ccache = NULL;
    
    if (!err && !in_selection_hints) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !out_identity      ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err && in_selection_hints->use_cached_results) {
        err = kim_os_selection_hints_lookup_identity (in_selection_hints, &identity);
    }
    
    if (!err && !identity && in_selection_hints->allow_user_interaction) {
        kim_ui_context context;
        
        err = kim_ui_init (&context);
        
        if (!err) {
            err = kim_ui_select_identity (&context, 
                                          in_selection_hints, 
                                          &identity);
        }
        
        if (context.initialized) {
            kim_error terr = KIM_NO_ERROR;
            terr = kim_ui_fini (&context);
            err = (terr != KIM_NO_ERROR) ? terr : err;
        }
    }
    
    if (!err) {
        *out_identity = identity;
        identity = NULL;
    }
    
    kim_identity_free (&identity);
    kim_ccache_free (&ccache);
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_selection_hints_remember_identity (kim_selection_hints in_selection_hints,
                                                 kim_identity        in_identity)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !in_selection_hints) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !in_identity       ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_os_selection_hints_remember_identity (in_selection_hints, 
                                                        in_identity);
    }
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_selection_hints_forget_identity (kim_selection_hints in_selection_hints)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !in_selection_hints) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_os_selection_hints_forget_identity (in_selection_hints);
    }
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

kim_error kim_selection_hints_get_preference_strings (kim_selection_hints                   in_selection_hints,
                                                      kim_selection_hints_preference_strings *io_preference_strings)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !in_selection_hints   ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !io_preference_strings) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        io_preference_strings->application_identifier = in_selection_hints->application_identifier;
        io_preference_strings->service_identity       = in_selection_hints->service_identity;
        io_preference_strings->client_realm           = in_selection_hints->client_realm;
        io_preference_strings->user                   = in_selection_hints->user;
        io_preference_strings->service_realm          = in_selection_hints->service_realm;
        io_preference_strings->service                = in_selection_hints->service;
        io_preference_strings->server                 = in_selection_hints->server;
    }
    
    return check_error (err);
}

/* ------------------------------------------------------------------------ */

void kim_selection_hints_free (kim_selection_hints *io_selection_hints)
{
    if (io_selection_hints && *io_selection_hints) {
        kim_string_free  (&(*io_selection_hints)->application_identifier);
        kim_string_free  (&(*io_selection_hints)->explanation);
        kim_options_free (&(*io_selection_hints)->options);
        kim_string_free  (&(*io_selection_hints)->service_identity);
        kim_string_free  (&(*io_selection_hints)->client_realm);
        kim_string_free  (&(*io_selection_hints)->user);
        kim_string_free  (&(*io_selection_hints)->service_realm);
        kim_string_free  (&(*io_selection_hints)->service);
        kim_string_free  (&(*io_selection_hints)->server);
        free (*io_selection_hints);
        *io_selection_hints = NULL;
    }
}

#pragma mark -

/* ------------------------------------------------------------------------ */

kim_error kim_selection_hints_write_to_stream (kim_selection_hints in_selection_hints,
                                               k5_ipc_stream       io_stream)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !in_selection_hints) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !io_stream         ) { err = check_error (KIM_NULL_PARAMETER_ERR); }

    if (!err) {
        err = k5_ipc_stream_write_string (io_stream, 
                                          in_selection_hints->application_identifier);
    }
    
    if (!err) {
        kim_string explanation = (in_selection_hints->explanation ?
                                  in_selection_hints->explanation : "");
        err = k5_ipc_stream_write_string (io_stream, explanation);
    }
    
    if (!err) {
        err = kim_options_write_to_stream (in_selection_hints->options, io_stream);
    }
    
    if (!err) {
        kim_string service_identity = (in_selection_hints->service_identity ?
                                       in_selection_hints->service_identity : "");
        err = k5_ipc_stream_write_string (io_stream, service_identity);
    }
    
    if (!err) {
        kim_string client_realm = (in_selection_hints->client_realm ?
                                   in_selection_hints->client_realm : "");
        err = k5_ipc_stream_write_string (io_stream, client_realm);
    }
    
    if (!err) {
        kim_string user = (in_selection_hints->user ?
                           in_selection_hints->user : "");
        err = k5_ipc_stream_write_string (io_stream, user);
    }
    
    if (!err) {
        kim_string service_realm = (in_selection_hints->service_realm ?
                                    in_selection_hints->service_realm : "");
        err = k5_ipc_stream_write_string (io_stream, service_realm);
    }
    
    if (!err) {
        kim_string service = (in_selection_hints->service ?
                              in_selection_hints->service : "");
        err = k5_ipc_stream_write_string (io_stream, service);
    }
    
    if (!err) {
        kim_string server = (in_selection_hints->server ?
                             in_selection_hints->server : "");
        err = k5_ipc_stream_write_string (io_stream, server);
    }
    
    return check_error (err);    
}

/* ------------------------------------------------------------------------ */

kim_error kim_selection_hints_read_from_stream (kim_selection_hints io_selection_hints,
                                                k5_ipc_stream       io_stream)
{
    kim_error err = KIM_NO_ERROR;
    
    if (!err && !io_selection_hints) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !io_stream         ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        char *application_identifier = NULL;
        err = k5_ipc_stream_read_string (io_stream, &application_identifier);
        
        if (!err) {
            err = kim_string_copy (&io_selection_hints->application_identifier, 
                                   application_identifier);
        }
        
        k5_ipc_stream_free_string (application_identifier);
    }
    
    if (!err) {
        char *explanation = NULL;
        err = k5_ipc_stream_read_string (io_stream, &explanation);
        
        if (!err) {
            if (explanation[0]) {
                err = kim_string_copy (&io_selection_hints->explanation, 
                                       explanation);
            } else {
                err = kim_selection_hints_set_explanation (io_selection_hints, 
                                                           NULL);
            }
        }
        
        k5_ipc_stream_free_string (explanation);
    }
    
    if (!err) {
        if (io_selection_hints->options) {
            err = kim_options_read_from_stream (io_selection_hints->options, 
                                                io_stream);
        } else {
            err = kim_options_create_from_stream (&io_selection_hints->options, 
                                                  io_stream);
        }
    }
    
    if (!err) {
        char *service_identity = NULL;
        err = k5_ipc_stream_read_string (io_stream, &service_identity);
        
        if (!err) {
            if (service_identity[0]) {
                err = kim_string_copy (&io_selection_hints->service_identity, 
                                       service_identity);
            } else {
                err = kim_selection_hints_set_hint (io_selection_hints, 
                                                    kim_hint_key_service_identity,
                                                    NULL);
            }
        }
        
        k5_ipc_stream_free_string (service_identity);
    }
    
    if (!err) {
        char *client_realm = NULL;
        err = k5_ipc_stream_read_string (io_stream, &client_realm);
        
        if (!err) {
            if (client_realm[0]) {
                err = kim_string_copy (&io_selection_hints->client_realm, 
                                       client_realm);
            } else {
                err = kim_selection_hints_set_hint (io_selection_hints, 
                                                    kim_hint_key_client_realm,
                                                    NULL);
            }
        }
        
        k5_ipc_stream_free_string (client_realm);
    }
    
    if (!err) {
        char *user = NULL;
        err = k5_ipc_stream_read_string (io_stream, &user);
        
        if (!err) {
            if (user[0]) {
                err = kim_string_copy (&io_selection_hints->user, user);
            } else {
                err = kim_selection_hints_set_hint (io_selection_hints,
                                                    kim_hint_key_user,
                                                    NULL);
            }
        }
        
        k5_ipc_stream_free_string (user);
    }
    
    if (!err) {
        char *service_realm = NULL;
        err = k5_ipc_stream_read_string (io_stream, &service_realm);
        
        if (!err) {
            if (service_realm[0]) {
                err = kim_string_copy (&io_selection_hints->service_realm, 
                                       service_realm);
            } else {
                err = kim_selection_hints_set_hint (io_selection_hints,
                                                    kim_hint_key_service_realm,
                                                    NULL);
            }
        }
        
        k5_ipc_stream_free_string (service_realm);
    }
    
    if (!err) {
        char *service = NULL;
        err = k5_ipc_stream_read_string (io_stream, &service);
        
        if (!err) {
            if (service[0]) {
                err = kim_string_copy (&io_selection_hints->service, service);
            } else {
                err = kim_selection_hints_set_hint (io_selection_hints,
                                                    kim_hint_key_service,
                                                    NULL);
            }
        }
        
        k5_ipc_stream_free_string (service);
    }
    
    if (!err) {
        char *server = NULL;
        err = k5_ipc_stream_read_string (io_stream, &server);
        
        if (!err) {
            if (server[0]) {
                err = kim_string_copy (&io_selection_hints->server, server);
            } else {
                err = kim_selection_hints_set_hint (io_selection_hints,
                                                    kim_hint_key_server,
                                                    NULL);
            }
        }
        
         k5_ipc_stream_free_string (server);
    }
    
    return check_error (err);    
}

/* ------------------------------------------------------------------------ */

kim_error kim_selection_hints_create_from_stream (kim_selection_hints *out_selection_hints,
                                                  k5_ipc_stream        io_stream)
{
    kim_error err = KIM_NO_ERROR;
    kim_selection_hints selection_hints = NULL;
    
    if (!err && !out_selection_hints) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !io_stream          ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    
    if (!err) {
        err = kim_selection_hints_allocate (&selection_hints);
    }
    
    if (!err) {
        err = kim_selection_hints_read_from_stream (selection_hints, io_stream);
    }
    
    if (!err) {
        *out_selection_hints = selection_hints;
        selection_hints = NULL;
    }
    
    kim_selection_hints_free (&selection_hints);
    
    return check_error (err);    
}