File: Kadm5.xs

package info (click to toggle)
libheimdal-kadm5-perl 0.08-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 268 kB
  • sloc: perl: 970; ansic: 496; makefile: 6
file content (962 lines) | stat: -rw-r--r-- 23,365 bytes parent folder | download
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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
/*
 * Copyright (c) 2003, Stockholms Universitet
 * (Stockholm University, Stockholm Sweden)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the university nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <sys/un.h>
#include <krb5.h>
#include <kadm5/admin.h>
#include <kadm5/kadm5_err.h>

#include "consts.h"

typedef struct shandle_t {
  int modcount;
  void *ptr;
  kadm5_config_params params;
  krb5_context context;
} shandle_t;

typedef struct sprincipal_t {
  shandle_t               *handle;
  int                      mask;
  kadm5_principal_ent_rec  principal;
} sprincipal_t;

static sprincipal_t *
create_sprincipal(shandle_t *handle)
{
  sprincipal_t *p = (sprincipal_t *)safemalloc(sizeof(sprincipal_t));
  
  memset(p,0,sizeof(*p));
  p->handle = handle;
  return p;
}

static void
destroy_sprincipal(sprincipal_t *spp)
{
  kadm5_free_principal_ent(spp->handle,&spp->principal);
  safefree(spp);
}

static shandle_t *
sv2server_handle(SV *sv)
{
  if (SvROK(sv) && sv_isa(sv,"Heimdal::Kadm5::SHandle"))
    return (shandle_t *)SvIV(SvRV(sv));
  else
    croak("Argument to sv2server_handle not referenced in package \"Heimdal::Kadm5::SHandle\"");
}

static sprincipal_t *
sv2sprincipal(SV *sv)
{
  if (SvROK(sv) && sv_isa(sv,"Heimdal::Kadm5::Principal"))
    return (sprincipal_t *)SvIV(SvRV(sv));
  else
    croak("Argument to sv2kadm5_principal not referenced in package \"Heimdal::Kadm5::Principal\"");
}

static int
set_param_strval(HV *hv, char **str, char *key)
{
  SV **val = hv_fetch(hv, key, strlen(key), 0);
  //fprintf(stderr,"%s=\"%s\"\n",key,val != NULL ? SvPV_nolen(*val):"(null)");
  if (val != NULL)
    {
       *str = SvPV_nolen(*val);
       return 1;
     }
  return 0;
}

static int
set_param_intval(HV *hv, int *ival, char *key)
{
  SV **val = hv_fetch(hv, key, 0, 0);
  if (val)
    {
      *ival = (int)SvIV(*val);
      return 1;
    }
  return 0;
}

static int
not_here(s)
char *s;
{
    croak("%s not implemented on this architecture", s);
    return -1;
}


struct kadm_func {
    kadm5_ret_t (*chpass_principal) (void *, krb5_principal, char*);
    kadm5_ret_t (*create_principal) (void*, kadm5_principal_ent_t, 
                                     u_int32_t, char*);
    kadm5_ret_t (*delete_principal) (void*, krb5_principal);
    kadm5_ret_t (*destroy) (void*);
    kadm5_ret_t (*flush) (void*);
    kadm5_ret_t (*get_principal) (void*, krb5_principal, 
                                  kadm5_principal_ent_t, u_int32_t);
    kadm5_ret_t (*get_principals) (void*, const char*, char***, int*);
    kadm5_ret_t (*get_privs) (void*, u_int32_t*);
    kadm5_ret_t (*modify_principal) (void*, kadm5_principal_ent_t, u_int32_t);
    kadm5_ret_t (*randkey_principal) (void*, krb5_principal, 
                                      krb5_keyblock**, int*);
    kadm5_ret_t (*rename_principal) (void*, krb5_principal, krb5_principal);
    kadm5_ret_t (*chpass_principal_with_key) (void *, krb5_principal,
                                              int, krb5_key_data *);
};

typedef struct kadm5_client_context {
    krb5_context context;
    krb5_boolean my_context;
    struct kadm_func funcs;
    /* */
    krb5_auth_context ac;
    char *realm;
    char *admin_server;
    int kadmind_port;
    int sock;
    char *client_name;
    char *service_name;
    krb5_prompter_fct prompter;
    const char *keytab;
    krb5_ccache ccache;
    kadm5_config_params *realm_params;
}kadm5_client_context;



MODULE = Heimdal::Kadm5::SHandle		PACKAGE = Heimdal::Kadm5::SHandle		PREFIX=kadm5_
PROTOTYPES: ENABLE

shandle_t *
new(self,sv)
     SV *self
     SV *sv
     CODE:
     {
       if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV)
       {
	 HV *hv = (HV *)SvRV(sv);
	 shandle_t *handle = (shandle_t *)safemalloc(sizeof(shandle_t));
	 kadm5_ret_t ret;
	 
	 memset(handle,0,sizeof(*handle));
	 ret = krb5_init_context(&handle->context);
	 if (ret)
	 {
	   safefree(handle);
	   croak("[Heimdal::Kadm5] krb5_init_context failed: %s\n",krb5_get_err_text(handle->context, ret));
	   handle = NULL;
	   goto cleanup;
	 }
 	
	 if (set_param_strval(hv,&handle->params.realm,"Realm"))
	 {
           /* fprintf(stderr,"Realm=\"%s\"\n",handle->params.realm); */
	   krb5_set_default_realm(handle->context, handle->params.realm);
	   handle->params.mask |= KADM5_CONFIG_REALM;
	 }
	 /* set_param_strval(hv,&handle->params.profile,"Profile"); */
	 if (set_param_intval(hv,&handle->params.kadmind_port,"Port"))
       	    handle->params.mask |= KADM5_CONFIG_KADMIND_PORT;
	 if (set_param_strval(hv,&handle->params.admin_server,"Server"))
	    handle->params.mask |= KADM5_CONFIG_ADMIN_SERVER;
	 
	 cleanup:
	 RETVAL = handle;
       }
       else
       {
         croak("[Heimdal::Kadm5] Argument to \"Heimdal::Kadm5::SHandle::new\" must be a hash-reference");
	 RETVAL = NULL;
       }
     }
     OUTPUT:
          RETVAL

void
DESTROY(handle)
     shandle_t *handle
     CODE:
     {
       if (handle->modcount > 0)
	 {
	   kadm5_flush(handle->ptr);
	 }
       if (handle->ptr)
          kadm5_destroy(handle->ptr);
       if (handle->context)
          krb5_free_context(handle->context);
       safefree(handle);
     }

void
kadm5_c_init_with_password (handle, client_name, password, service_name, struct_version, api_version)
     shandle_t *handle
     char *client_name
     char *password
     char *service_name
     unsigned long struct_version
     unsigned long api_version
     CODE:
     {
       kadm5_ret_t ret = kadm5_init_with_password_ctx(handle->context,
							client_name,
							password,
							KADM5_ADMIN_SERVICE, 
							&handle->params,
							struct_version, 
							api_version,
							&handle->ptr);
       if(ret)
	    croak("[Heimdal::Kadm5] kadm5_init_with_password_ctx failed: %s\n",
		  krb5_get_err_text(handle->context, ret));

       if (password != NULL && *password != '\0')
           ((kadm5_client_context *)handle->ptr)->prompter = NULL;
     }

void
kadm5_c_init_with_skey (handle, client_name, keytab, service_name, struct_version, api_version)
     shandle_t *handle
     char *client_name
     char *keytab
     char *service_name
     unsigned long struct_version
     unsigned long api_version
     CODE:
     {
       kadm5_ret_t ret = kadm5_init_with_skey_ctx(handle->context,
						    client_name,
						    keytab,
						    KADM5_ADMIN_SERVICE, 
						    &handle->params,
						    struct_version, 
						    api_version,
						    &handle->ptr);
       if(ret)
	    croak("[Heimdal::Kadm5] kadm5_init_with_skey_ctx failed: %s\n",
		  krb5_get_err_text(handle->context, ret));
     }

void
kadm5_c_flush(handle)
     shandle_t *handle
     CODE:
     {
       kadm5_ret_t ret = kadm5_flush(handle->ptr);
       if (ret)
	 croak("[Heimdal::Kadm5] kadm5_flush failed: %s\n",krb5_get_err_text(handle->context, ret));
       handle->modcount = 0;
     }

void
kadm5_c_modify_principal(handle,spp,mask)
     shandle_t     *handle
     sprincipal_t  *spp
     int            mask
     CODE:
     {
       kadm5_ret_t ret;

       if (mask == 0)
	 mask = spp->mask;
       ret = kadm5_modify_principal(handle->ptr, &spp->principal, mask);
       if (ret)
	 {
	   if (ret)
	     croak("[Heimdal::Kadm5] kadm5_modify_principal failed: %s\n",
		   krb5_get_err_text(handle->context, ret));
	 }
       handle->modcount++;
     }

int
kadm5_c_randkey_principal(handle,name)
     shandle_t    *handle
     char         *name
     CODE:
     {
       krb5_keyblock *new_keys;
       int n_keys, i;
       krb5_principal principal;
       krb5_error_code err;
       kadm5_ret_t ret;

       err = krb5_parse_name(handle->context, name, &principal);
       if (err)
	 croak("[Heimdal::Kadm5] krb5_parse_name failed on \"%s\": %s\n",
	       name,krb5_get_err_text(handle->context, err));
       
       ret = kadm5_randkey_principal(handle->ptr, principal, &new_keys, &n_keys);
       if(ret)
	 {
	   krb5_free_principal(handle->context, principal);
	   croak("[Heimdal::Kadm5] kadm5_randkey_principal failed: %s\n",
		 krb5_get_err_text(handle->context, ret));
	 }
       for(i = 0; i < n_keys; i++)
	 krb5_free_keyblock_contents(handle->context, &new_keys[i]);
       free(new_keys);
       krb5_free_principal(handle->context, principal);
       handle->modcount++;
       RETVAL = n_keys;
     }
     OUTPUT:
     RETVAL

void
kadm5_c_chpass_principal(handle,name,password)
     shandle_t   *handle
     char        *name
     char        *password
     CODE:
     {
       kadm5_ret_t ret;
       krb5_error_code ret2;
       krb5_principal principal;
       
       ret2 = krb5_parse_name(handle->context, name, &principal);
       if (ret2)
	 croak("[Heimdal::Kadm5] krb5_parse_name failed on \"%s\": %s\n",
	       name,krb5_get_err_text(handle->context, ret2));
       
       ret = kadm5_chpass_principal(handle->ptr,principal,password);
       if (ret)
	 croak("[Heimdal::Kadm5] kadm5_chpass_principal failed on \"%s\": %s\n",
	       name,krb5_get_err_text(handle->context, ret));
       handle->modcount++;
     }

void
kadm5_c_create_principal(handle,spp,password,mask)
     shandle_t    *handle
     sprincipal_t *spp
     char         *password
     int           mask
     CODE:
     {
       kadm5_ret_t ret;

       if (mask == 0)
	 mask = spp->mask;
       
       ret = kadm5_create_principal(handle->ptr,&spp->principal,mask,password);
       if (ret)
	 {
	   char *p;
	   krb5_error_code ret2;
	   
	   ret2 = krb5_unparse_name(handle->context,spp->principal.principal,&p);
	   if (ret2)
	     {
	       safefree(p);
	       croak("[Heimdal::Kadm5] krb5_unparse_name failed: %s\n",
		     krb5_get_err_text(spp->handle->context, ret2));
	     }
	   croak("[Heimdal::Kadm5] krb5_create_principal failed on \"%s\": %s\n",
		 p,krb5_get_err_text(handle->context, ret));
	 }
       handle->modcount++;
     }

void
kadm5_c_rename_principal(handle, src, trg)
     shandle_t   *handle
     char        *src
     char        *trg
     CODE:
     {
       krb5_error_code ret;
       krb5_principal source, target;
       kadm5_ret_t err;

       ret = krb5_parse_name(handle->context, src, &source);
       if (ret)
	 {
	   croak("[Heimdal::Kadm5] krb5_parse_name failed on \"%s\": %s\n",
		 src,krb5_get_err_text(handle->context, ret));
	 }
       
       ret = krb5_parse_name(handle->context, trg, &target);
       if (ret)
	 {
	   krb5_free_principal(handle->context, target);
	   croak("[Heimdal::Kadm5] krb5_parse_name failed on \"%s\": %s\n",
		 trg,krb5_get_err_text(handle->context, ret));
	 }
       
       err = kadm5_rename_principal(handle->ptr, source, target);
       if (err)
	 {
	   krb5_free_principal(handle->context, source);
	   krb5_free_principal(handle->context, target);
	   croak("[Heimdal::Kadm5] kadm5_rename_principal \"%s\" to \"%s\" failed: %s\n",
		 src,trg,krb5_get_err_text(handle->context, err));
	 }
       krb5_free_principal(handle->context, source);
       krb5_free_principal(handle->context, target);
       handle->modcount++;
     }

void
kadm5_c_delete_principal(handle,name)
     shandle_t   *handle
     char        *name
     CODE:
     {
       krb5_error_code ret;
       krb5_principal principal;
       kadm5_ret_t err;
       
       ret = krb5_parse_name(handle->context, name, &principal);
       if (ret)
	 croak("[Heimdal::Kadm5] krb5_parse_name failed on \"%s\": %s\n",
	       name,krb5_get_err_text(handle->context, ret));
       
       err = kadm5_delete_principal(handle->ptr,principal);
       if (err)
	 {
	   krb5_free_principal(handle->context, principal);
	   croak("[Heimdal::Kadm5] kadm5_delete_principal failed for \"%s\": %s\n",
		 name,krb5_get_err_text(handle->context, err));
	 }
       handle->modcount++;
       krb5_free_principal(handle->context, principal);
     }


sprincipal_t *
kadm5_c_get_principal(handle, name, mask)
     shandle_t *handle
     char      *name
     IV         mask
     CODE:
     {
       krb5_principal principal;
       krb5_error_code ret;
       sprincipal_t *spp;
       
       ret = krb5_parse_name(handle->context, name, &principal);
       if (ret)
	 croak("[Heimdal::Kadm5] krb5_parse_name failed on \"%s\": %s\n",
	       name,krb5_get_err_text(handle->context, ret));
       
       spp = create_sprincipal(handle);
       ret = kadm5_get_principal(handle->ptr,
				   principal,
				   &spp->principal,
				   mask);
       if (ret)
	 {
           if (ret == KADM5_UNK_PRINC) {
              destroy_sprincipal(spp); 
              spp = NULL;
           } else {
	      krb5_free_principal(handle->context, principal);
              destroy_sprincipal(spp); 
	      croak("[Heimdal::Kadm5] kadm5_get_principal failed for \"%s\": %s\n",
		    name,krb5_get_err_text(handle->context, ret));
           }
	 }
       krb5_free_principal(handle->context,principal);
       RETVAL = spp;
     }
     OUTPUT:
     RETVAL

void
kadm5_c_get_principals(handle,exp)
     shandle_t *handle
     char      *exp
     PPCODE:
     {
       char **princs;
       int num_princs,i;
       kadm5_ret_t ret;

       ret = kadm5_get_principals(handle->ptr,exp,&princs,&num_princs);
       if (ret)
	 {
	   croak("[Heimdal::Kadm5] kadm5_get_principals failed for \"%s\": %s\n",
		 exp,krb5_get_err_text(handle->context, ret));
	 }
       EXTEND(SP,num_princs);
       for (i = 0; i < num_princs; i++)
	 {
	   PUSHs(sv_2mortal(newSVpv(princs[i],0)));
	 }
       kadm5_free_name_list(handle->ptr,princs,&num_princs);
     }

int
kadm5_c_get_privs(handle)
     shandle_t *handle
     CODE:
     {
       int privs;
       kadm5_ret_t ret = kadm5_get_privs(handle->ptr,&privs);
       if (ret)
	 {
	   croak("[Heimdal::Kadm5] kadm5_get_privs failed: %s\n",
		 krb5_get_err_text(handle->context, ret));
	 }
       RETVAL = privs;
     }
     OUTPUT:
     RETVAL

void
kadm5_c_ext_keytab(handle,spp,keytab)
     shandle_t   *handle
     sprincipal_t *spp
     char        *keytab
     CODE:
     {
       int i;
       krb5_keytab kt;
       krb5_error_code ret;
       
       if(keytab)
	 ret = krb5_kt_resolve(handle->context, keytab, &kt);
       else
	 ret = krb5_kt_default(handle->context, &kt);
       
       if (ret)
	 croak("[Heimdal::Kadm5] krb5_kt_resolv failed: %s\n",
	       krb5_get_err_text(handle->context, ret));
       
       for(i = 0; i < spp->principal.n_key_data; i++)
	 {
	   krb5_keytab_entry key;
	   krb5_key_data *k = &spp->principal.key_data[i];
	   
	   key.principal = spp->principal.principal;
	   key.vno = k->key_data_kvno;
	   key.keyblock.keytype = k->key_data_type[0];
	   key.keyblock.keyvalue.length = k->key_data_length[0];
	   key.keyblock.keyvalue.data = k->key_data_contents[0];
	   ret = krb5_kt_add_entry(handle->context, kt, &key);
	   if (ret)
	     croak("[Heimdal::Kadm5] krb5_kt_add_entry failed: %s\n",
		   krb5_get_err_text(handle->context, ret));
	 }
       
       krb5_kt_close(handle->context, kt);
     }

MODULE = Heimdal::Kadm5::Principal         PACKAGE = Heimdal::Kadm5::Principal

sprincipal_t *
new(self,handle)
     SV *self
     shandle_t *handle
     CODE: 
     {
       sprincipal_t *spp = create_sprincipal(handle);
       RETVAL = spp;
     }
     OUTPUT:
     RETVAL

void
DESTROY(spp)
     sprincipal_t *spp
     CODE:
     {
       destroy_sprincipal(spp);
     }

SV *
getPrincipal(spp)
     sprincipal_t *spp
     CODE:
     {
       char *p;
       krb5_error_code ret;
       
       ret = krb5_unparse_name(spp->handle->context,spp->principal.principal,&p);
       if (ret)
	 {
	   safefree(p);
	   croak("[Heimdal::Kadm5] krb5_unparse_name failed: %s\n",
		 krb5_get_err_text(spp->handle->context, ret));
	 }
       RETVAL = newSVpv(p,0);
     }
     OUTPUT:
     RETVAL

void
setPrincipal(spp,p)
     sprincipal_t *spp
     char         *p
     CODE:
     {
       krb5_error_code ret;
       
       ret = krb5_parse_name(spp->handle->context,p,&spp->principal.principal);
       if (ret)
	 {
	   croak("[Heimdal::Kadm5] krb5_parse_name failed for \"%s\": %s\n",
		 p,krb5_get_err_text(spp->handle->context, ret));
	 }
       spp->mask |= KADM5_PRINCIPAL;
     }

int
getPrincExpireTime(spp)
     sprincipal_t *spp
     PPCODE:
     {
       XPUSHi(spp->principal.princ_expire_time);
     }

void
setPrincExpireTime(spp,val)
     sprincipal_t *spp
     IV            val
     CODE:
     {
       spp->principal.princ_expire_time = val;
       spp->mask |= KADM5_PRINC_EXPIRE_TIME;
     }

IV
getLastPwdChange(spp)
     sprincipal_t *spp
     CODE:
     {
       RETVAL = spp->principal.last_pwd_change;
     }
     OUTPUT:
     RETVAL

IV
getKvno(spp)
     sprincipal_t *spp
     CODE:
     {
       RETVAL = spp->principal.kvno;
     }
     OUTPUT:
     RETVAL

IV
getMKvno(spp)
     sprincipal_t *spp
     CODE:
     {
       RETVAL = spp->principal.mkvno;
     }
     OUTPUT:
     RETVAL

IV
getPwExpiration(spp)
     sprincipal_t *spp
     CODE:
     {
       RETVAL = spp->principal.pw_expiration;
     }
     OUTPUT:
     RETVAL

void
setPwExpiration(spp,val)
     sprincipal_t *spp
     IV            val
     CODE:
     {
       spp->principal.pw_expiration = val;
       spp->mask |= KADM5_PW_EXPIRATION;
     }

IV
getMaxLife(spp)
     sprincipal_t *spp
     CODE:
     {
       RETVAL = spp->principal.max_life;
     }
     OUTPUT:
     RETVAL

void
setMaxLife(spp,val)
     sprincipal_t *spp
     IV            val
     CODE:
     {
       spp->principal.max_life = val;
       spp->mask |= KADM5_MAX_LIFE;
     }

SV *
getModName(spp)
     sprincipal_t *spp
     CODE:
     {
       char *p;
       krb5_error_code ret;
       
       if (spp->principal.mod_name) 
       {
         ret = krb5_unparse_name(spp->handle->context,spp->principal.mod_name,&p);
         if (ret)
	 {
	   safefree(p);
	   croak("[Heimdal::Kadm5] krb5_unparse_name failed: %s\n",
		 krb5_get_err_text(spp->handle->context, ret));
	 }
	 RETVAL = newSVpv(p,0);
       }
       else
	 RETVAL = &PL_sv_undef;
     }
     OUTPUT:
     RETVAL

IV
getModDate(spp)
     sprincipal_t *spp
     CODE:
     {
       RETVAL = spp->principal.mod_date;
     }
     OUTPUT:
     RETVAL

SV *
getPolicy(spp)
     sprincipal_t *spp
     CODE:
     {
       if (spp->principal.policy)
	 RETVAL = newSVpv(spp->principal.policy,0);
       else
	 RETVAL = &PL_sv_undef;
     }
     OUTPUT:
     RETVAL

IV
getMaxRenewableLife(spp)
     sprincipal_t *spp
     CODE:
     {
       RETVAL = spp->principal.max_renewable_life;
     }
     OUTPUT:
     RETVAL

void
setMaxRenewableLife(spp,val)
     sprincipal_t *spp
     IV            val
     CODE:
     {
       spp->principal.max_renewable_life = val;
       spp->mask |= KADM5_MAX_RLIFE;
     }

IV
getLastSuccess(spp)
     sprincipal_t *spp
     CODE:
     {
       RETVAL = spp->principal.last_success;
     }
     OUTPUT:
     RETVAL

IV
getLastFailed(spp)
     sprincipal_t *spp
     CODE:
     {
       RETVAL = spp->principal.last_failed;
     }
     OUTPUT:
     RETVAL

IV
getFailAuthCount(spp)
     sprincipal_t *spp
     CODE:
     {
       RETVAL = spp->principal.fail_auth_count;
     }
     OUTPUT:
     RETVAL

IV
getFailAuthCounts(spp)
     sprincipal_t *spp
     CODE:
     {
       RETVAL = spp->principal.fail_auth_count;
     }
     OUTPUT:
     RETVAL

IV
getAttributes(spp)
     sprincipal_t *spp
     CODE:
     {
       RETVAL = spp->principal.attributes;
     }
     OUTPUT:
     RETVAL

void
setAttributes(spp,val)
     sprincipal_t *spp
     IV            val
     CODE:
     {
       spp->principal.attributes = val;
       spp->mask |= KADM5_ATTRIBUTES;
     }

SV *
getKeytypes(spp)
     sprincipal_t *spp
     CODE:
     {
       int i;
       AV *lst = newAV();
       
       for (i = 0; i < spp->principal.n_key_data; ++i) 
	 {
	   krb5_key_data *k = &spp->principal.key_data[i];
	   krb5_error_code ret;
	   char *e_string, *s_string;
	   SV *ksv[2];
	   
	   ret = krb5_enctype_to_string (spp->handle->context,
					 k->key_data_type[0],
					 &e_string);
	   if (ret)
	     asprintf (&e_string, "unknown(%d)", k->key_data_type[0]);
	   ksv[0] = newSVpv(e_string,0);
	   
	   ret = krb5_salttype_to_string (spp->handle->context,
					  k->key_data_type[0],
					  k->key_data_type[1],
					  &s_string);
	   if (ret)
	     asprintf (&s_string, "unknown(%d)", k->key_data_type[1]);
	   ksv[1] = newSVpv(s_string,0);
	   
	   av_push(lst,newRV_inc((SV *)av_make(2,ksv)));
	   free (e_string);
	   free (s_string);
	 }
       RETVAL = newRV_inc((SV *)lst);
     }
     OUTPUT:
     RETVAL

void
delKeytypes(spp,enctype)
     sprincipal_t *spp
     char *enctype
     CODE:
     {
       krb5_key_data *new_key_data = malloc(spp->principal.n_key_data * sizeof(*new_key_data));
       krb5_enctype *etypes        = malloc(sizeof(*etypes));
       int i, j;

       krb5_string_to_enctype(spp->handle->context, enctype, etypes);

       for (i = 0, j = 0; i < spp->principal.n_key_data; ++i) {
           krb5_key_data *key = &spp->principal.key_data[i];

           if (*etypes == key->key_data_type[0]) {
               int16_t ignore = 1;
               kadm5_free_key_data (spp->handle, &ignore, key);
           } else {
               new_key_data[j++] = *key;
           }
       }

       free (spp->principal.key_data);
       spp->principal.n_key_data = j;
       spp->principal.key_data   = new_key_data;

       spp->mask |= KADM5_KEY_DATA;
       spp->handle->modcount++;

     }


SV *
getPassword(spp)
     sprincipal_t *spp
     CODE:
     {
#ifdef KRB5_TL_PASSWORD
       krb5_tl_data *tl = spp->principal.tl_data;

       while (tl != NULL)
         {
	   if (tl->tl_data_type == KRB5_TL_PASSWORD)
	     break;
	   tl = tl->tl_data_next;
         }

       if (tl)
	 RETVAL = newSVpv(tl->tl_data_contents,0);
       else
#endif
	 RETVAL = &PL_sv_undef;
     }
     OUTPUT:
     RETVAL


MODULE = Heimdal::Kadm5                    PACKAGE = Heimdal::Kadm5

double
constant(name,arg)
        char *          name
        int             arg