-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsrvops.c
2167 lines (2002 loc) · 62.2 KB
/
srvops.c
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
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2008-2014, 2017-2021 Carnegie Mellon University.
* 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. The name "Carnegie Mellon University" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For permission or any other legal
* details, please contact
* Office of Technology Transfer
* Carnegie Mellon University
* 5000 Forbes Avenue
* Pittsburgh, PA 15213-3890
* (412) 268-4387, fax: (412) 268-7395
*
* 4. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by Computing Services
* at Carnegie Mellon University (http://www.cmu.edu/computing/)."
*
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/syslog.h>
#include <sys/signal.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <limits.h>
#define SESS_PRIVATE
#define NEED_KRB5
#define NEED_KADM5
#define NEED_GSSAPI
#define NEED_SQLITE
#include "rekeysrv-locl.h"
#include "rekey-locl.h"
#include "protocol.h"
#include "memmgt.h"
#ifndef USE_GSSAPI_H
#include <gssapi/gssapi_krb5.h>
#endif
static krb5_enctype std_enctypes[] = {
#if !HAVE_DECL_ENCTYPE_AES128_CTS_HMAC_SHA1_96 || HAVE_DECL_ENCTYPE_DES_CBC_CRC
ENCTYPE_DES_CBC_CRC,
#endif
#if !HAVE_DECL_ENCTYPE_AES128_CTS_HMAC_SHA1_96 || HAVE_DECL_ENCTYPE_DES3_CBC_SHA1
ENCTYPE_DES3_CBC_SHA1,
#endif
#if HAVE_DECL_ENCTYPE_AES128_CTS_HMAC_SHA1_96
ENCTYPE_AES128_CTS_HMAC_SHA1_96,
#endif
#if HAVE_DECL_ENCTYPE_AES256_CTS_HMAC_SHA1_96
ENCTYPE_AES256_CTS_HMAC_SHA1_96,
#endif
#if HAVE_DECL_ENCTYPE_ARCFOUR_HMAC
ENCTYPE_ARCFOUR_HMAC,
#endif
ENCTYPE_NULL
};
krb5_enctype *cfg_enctypes = std_enctypes;
static int is_admin(struct rekey_session *sess)
{
if (is_admin_from_file(sess)
#ifdef HAVE_LDAP
|| is_admin_from_ldap(sess)
#endif
)
return 1;
return 0;
}
/* parse the client's name and determine what operations they can perform */
static void check_authz(struct rekey_session *sess)
{
size_t rl;
#if defined(KRB5_PRINCIPAL_HEIMDAL_STYLE)
const char *princ_realm;
#elif defined (KRB5_PRINCIPAL_MIT_STYLE)
krb5_data *princ_realm;
#else
#error Cannot figure out how krb5_principals objects work
#endif
if (krealm_init(sess))
return;
rl = strlen(sess->realm);
#if defined(KRB5_PRINCIPAL_HEIMDAL_STYLE)
princ_realm = krb5_principal_get_realm(sess->kctx, sess->princ);
if (!princ_realm || rl != strlen(princ_realm) ||
strncmp(princ_realm , sess->realm, rl))
return;
#elif defined (KRB5_PRINCIPAL_MIT_STYLE)
princ_realm = krb5_princ_realm(sess->kctx, sess->princ);
if (!princ_realm || rl != princ_realm->length ||
strncmp(princ_realm->data , sess->realm, rl))
return;
if (krb5_princ_size(sess->kctx, sess->princ) != 2)
return;
#endif
if (princ_ncomp_eq(sess->kctx, sess->princ, 2) &&
compare_princ_comp(sess->kctx, sess->princ, 0, "host")) {
sess->is_host = 1;
sess->hostname=dup_comp_string(sess->kctx, sess->princ, 1);
if (!sess->hostname) /* mark not a valid host, since we don't have its identification */
sess->is_host = 0;
return;
}
if (is_admin(sess))
sess->is_admin = 1;
}
/* check that the target principal is valid (in the correct realm, and
any other checks we choose to implement (in testing, this includes
restricting the first principal component to a specific string)) */
/*#define LIMIT_TARGET "test"*/
static int check_target(struct rekey_session *sess, krb5_principal target)
{
if (krealm_init(sess))
return 1;
if (!acl_check(sess, sess->target_acl, target, 0)) {
send_error(sess, ERR_AUTHZ, "Requested principal may not be modified");
return 1;
}
return 0;
}
/* This is the default target ACL, used if no rekey.targets file exists */
static char *builtin_target_acl[] = {
"afs",
"!*",
"!kadmin/**",
"!krbtgt/**",
"!K/M",
"!*/root",
"!*/admin",
"!*/gatekeeper",
"!*/ldap",
"!*/admin-afs",
"!*/cyradm",
"!*/daemon",
"!*/ftp",
"!*/mail",
"!*/misc",
"!*/remote",
"!*/z",
"!*/jabber",
"zephyr/zephyr",
"!*/zephyr",
"*/**",
NULL
};
/* lookup a principal in the local database, and return its id and kvno if
requested */
static int find_principal(struct rekey_session *sess, char *principal, sqlite_int64 *princid, krb5_kvno *kvno)
{
sqlite3_stmt *getprinc=NULL;
int rc, match;
rc = sqlite3_prepare_v2(sess->dbh,
"SELECT id, kvno FROM principals WHERE name=?",
-1, &getprinc, NULL);
if (rc != SQLITE_OK)
goto dberr;
rc = sqlite3_bind_text(getprinc, 1, principal, strlen(principal), SQLITE_STATIC);
if (rc != SQLITE_OK)
goto dberr;
match=0;
while (SQLITE_ROW == sqlite3_step(getprinc)) {
if (princid)
*princid = sqlite3_column_int64(getprinc, 0);
if (kvno)
*kvno = sqlite3_column_int(getprinc, 1);
if (princid && *princid == 0)
goto dberr;
match++;
}
rc = sqlite3_finalize(getprinc);
getprinc=NULL;
if (rc != SQLITE_OK)
goto dberr;
goto freeall;
dberr:
match = -1;
freeall:
if (getprinc)
sqlite3_finalize(getprinc);
return match;
}
/* create a principal in the local database.
gets the new kvno by looking up the old one in the kdb and incrementing it.
Optionally creates the kdb entry if it does not exist */
static sqlite_int64 setup_principal(struct rekey_session *sess, char *principal,
krb5_principal target, int create, krb5_kvno *kvnop)
{
int rc;
struct sqlite3_stmt *ins=NULL;
krb5_kvno kvno;
int match, created=0;
kadm5_principal_ent_rec ke;
sqlite_int64 princid=0;
match = find_principal(sess, principal, NULL, NULL);
if (match < 0)
goto dberr;
if (match) {
send_error(sess, ERR_OTHER, "Rekey for this principal already in progress");
goto freeall;
}
if (kadm_init(sess))
goto interr;
retry:
memset(&ke, 0, sizeof(ke));
rc = kadm5_get_principal(sess->kadm_handle, target, &ke, KADM5_KVNO |
KADM5_ATTRIBUTES | KADM5_PRINC_EXPIRE_TIME);
if (rc) {
if (rc == KADM5_UNK_PRINC) {
if (create && !created) {
krb5_keyblock *new_keys = NULL;
int i, n_new_keys=0;
ke.principal = target;
ke.princ_expire_time = 0;
ke.attributes = KRB5_KDB_DISALLOW_ALL_TIX | KRB5_KDB_NEW_PRINC;
rc = kadm5_create_principal(sess->kadm_handle, &ke,
KADM5_PRINCIPAL |
KADM5_PRINC_EXPIRE_TIME | KADM5_ATTRIBUTES,
"passwordisnotused");
memset(&ke, 0, sizeof(ke));
if (rc) {
prtmsg("Cannot create principal %s: %s", principal, krb5_get_err_text(sess->kctx, rc));
goto interr;
}
created=1;
rc = kadm5_randkey_principal(sess->kadm_handle, target, &new_keys,
&n_new_keys);
if (rc) {
prtmsg("Creating %s failed to randomize keys: %s", principal,
krb5_get_err_text(sess->kctx, rc));
goto interr;
}
for (i=0; i< n_new_keys; i++)
krb5_free_keyblock_contents(sess->kctx, &new_keys[i]);
#if HAVE_DECL_KRB5_XFREE
krb5_xfree(new_keys);
#else
/* This is evil, but there's no other way to free this
object using the kadmin library's free */
kadm5_free_name_list(sess->kadm_handle, (char **)new_keys, 0);
#endif
ke.principal = target;
ke.princ_expire_time = 0;
ke.attributes = 0;
rc = kadm5_modify_principal(sess->kadm_handle, &ke,
KADM5_PRINC_EXPIRE_TIME | KADM5_ATTRIBUTES);
if (rc) {
prtmsg("Creating %s failed to unlock new principal: %s", principal,
krb5_get_err_text(sess->kctx, rc));
goto interr;
}
goto retry;
} else {
if (created) {
prtmsg("Principal %s still does not exist after successful kadm5_create_principal", principal);
send_error(sess, ERR_OTHER, "KDC error: Requested principal does not exist after being created");
} else {
prtmsg("Principal %s does not exist", principal);
send_error(sess, ERR_NOTFOUND, "Requested principal does not exist");
}
goto freeall;
}
if (created)
prtmsg("Created principal %s", principal);
} else {
prtmsg("Unable to lookup principal %s: %s", principal,
krb5_get_err_text(sess->kctx, rc));
goto interr;
}
}
if ((ke.princ_expire_time &&
ke.princ_expire_time < time(0)) ||
(ke.attributes & KRB5_KDB_DISALLOW_ALL_TIX)) {
prtmsg("Principal %s is disabled or expired (%ld %#lx)", principal,
(long)ke.princ_expire_time, (long)ke.attributes);
send_error(sess, ERR_NOTFOUND, "Requested principal is disabled or expired");
}
kvno = ke.kvno + 1;
rc = sqlite3_prepare_v2(sess->dbh,
"INSERT INTO principals (name, kvno) VALUES (?, ?);",
-1, &ins, NULL);
if (rc != SQLITE_OK)
goto dberr;
rc = sqlite3_bind_text(ins, 1, principal, strlen(principal), SQLITE_STATIC);
if (rc != SQLITE_OK)
goto dberr;
rc = sqlite3_bind_int(ins, 2, kvno);
if (rc != SQLITE_OK)
goto dberr;
sqlite3_step(ins);
rc = sqlite3_finalize(ins);
ins=NULL;
if (rc != SQLITE_OK)
goto dberr;
princid = sqlite3_last_insert_rowid(sess->dbh);
if (kvnop)
*kvnop=kvno;
goto freeall;
dberr:
prtmsg("database error: %s", sqlite3_errmsg(sess->dbh));
send_error(sess, ERR_OTHER, "Server internal error (database failure)");
goto freeall;
interr:
send_error(sess, ERR_OTHER, "Server internal error");
freeall:
if (ins)
sqlite3_finalize(ins);
if (sess->kadm_handle)
kadm5_free_principal_ent(sess->kadm_handle, &ke);
sess->kadm_handle = NULL;
return princid;
}
static int check_flags(int flags) {
if (flags & (~REQFLAG_MASK)) {
prtmsg("Invalid flags word %d in newreq", flags);
return 1;
}
if ((flags & (REQFLAG_DESONLY|REQFLAG_NODES))
== (REQFLAG_DESONLY|REQFLAG_NODES)) {
prtmsg("Flags requested both des only and no des");
return 1;
}
return 0;
}
/* generates a keyset and places it in the local database */
static int generate_keys(struct rekey_session *sess, sqlite_int64 princid, int reqflags)
{
krb5_enctype *pEtype;
krb5_keyblock keyblock;
krb5_error_code kc;
sqlite3_stmt *ins=NULL;
int rc, nkeys=0;
if (reqflags & REQFLAG_DESONLY)
pEtype=std_enctypes;
else
pEtype=cfg_enctypes;
rc = sqlite3_prepare_v2(sess->dbh,
"INSERT INTO keys (principal, enctype, key) VALUES (?, ?, ?);",
-1, &ins, NULL);
if (rc != SQLITE_OK)
goto dberr;
for (;*pEtype != ENCTYPE_NULL; pEtype++) {
#if !HAVE_DECL_ENCTYPE_AES128_CTS_HMAC_SHA1_96 || HAVE_DECL_ENCTYPE_DES_CBC_CRC
if (reqflags & REQFLAG_DESONLY && *pEtype > ENCTYPE_DES_CBC_CRC)
continue;
if (reqflags & REQFLAG_NODES && *pEtype == ENCTYPE_DES_CBC_CRC)
continue;
#else
if (reqflags & REQFLAG_DESONLY)
continue;
#endif
#if !HAVE_DECL_ENCTYPE_AES128_CTS_HMAC_SHA1_96 || HAVE_DECL_ENCTYPE_DES3_CBC_SHA1
if (reqflags & REQFLAG_COMPAT_ENCTYPE && *pEtype > ENCTYPE_DES3_CBC_SHA1)
continue;
#else
if (reqflags & REQFLAG_COMPAT_ENCTYPE)
continue;
#endif
#if !HAVE_DECL_ENCTYPE_AES128_CTS_HMAC_SHA1_96 || HAVE_DECL_ENCTYPE_DES3_CBC_SHA1
if (reqflags & REQFLAG_COMPAT_ENCTYPE_RFC8429 && *pEtype == ENCTYPE_DES3_CBC_SHA1)
continue;
#endif
#if HAVE_DECL_ENCTYPE_ARCFOUR_HMAC
if (reqflags & REQFLAG_COMPAT_ENCTYPE_RFC8429 && *pEtype == ENCTYPE_ARCFOUR_HMAC)
continue;
#endif
nkeys++;
kc = krb5_generate_random_keyblock(sess->kctx, *pEtype, &keyblock);
if (kc) {
prtmsg("Cannot generate key for enctype %d (kerberos error %s)",
*pEtype, krb5_get_err_text(sess->kctx, kc));
goto interr;
}
rc = sqlite3_bind_blob(ins, 3, Z_keydata(&keyblock),
Z_keylen(&keyblock), SQLITE_TRANSIENT);
krb5_free_keyblock_contents(sess->kctx, &keyblock);
if (rc != SQLITE_OK)
goto dberr;
rc = sqlite3_bind_int64(ins, 1, princid);
if (rc != SQLITE_OK)
goto dberr;
rc = sqlite3_bind_int(ins, 2, *pEtype);
if (rc != SQLITE_OK)
goto dberr;
sqlite3_step(ins);
rc = sqlite3_reset(ins);
if (rc != SQLITE_OK)
goto dberr;
}
if (nkeys == 0) {
send_error(sess, ERR_OTHER, "Server cannot generate any requested key types");
goto freeall;
}
rc = sqlite3_finalize(ins);
ins=NULL;
if (rc != SQLITE_OK)
goto dberr;
return 0;
dberr:
prtmsg("database error: %s", sqlite3_errmsg(sess->dbh));
send_error(sess, ERR_OTHER, "Server internal error (database failure)");
goto freeall;
interr:
send_error(sess, ERR_OTHER, "Server internal error");
freeall:
return 1;
}
/* Adds a keyset to a partially initialized KEYS response */
static int add_keys_one(struct rekey_session *sess, sqlite_int64 principal, mb_t buf)
{
int rc;
sqlite3_stmt *st=NULL;
int enctype, n;
size_t l, curlen, last;
const unsigned char *key;
last = get_cursor(buf); /* key count goes here */
if (buf_appendint(buf, 0)) /* key count placeholder */
goto memerr;
rc = sqlite3_prepare(sess->dbh,"SELECT enctype, key from keys where principal=?",
-1, &st, NULL);
if (rc != SQLITE_OK)
goto dberr;
rc = sqlite3_bind_int64(st, 1, principal);
if (rc != SQLITE_OK)
goto dberr;
n=0;
while (SQLITE_ROW == sqlite3_step(st)) {
enctype = sqlite3_column_int(st, 0);
key = sqlite3_column_blob(st, 1);
l = sqlite3_column_bytes(st, 1);
if (key == NULL || l == 0)
goto interr;
if (enctype == 0)
goto dberr;
if (buf_appendint(buf, enctype) || buf_appendint(buf, l) ||
buf_appenddata(buf, key, l))
goto memerr;
#if !HAVE_DECL_ENCTYPE_AES128_CTS_HMAC_SHA1_96 || HAVE_DECL_ENCTYPE_DES_CBC_CRC
if (enctype == ENCTYPE_DES_CBC_CRC) {
if (buf_appendint(buf, ENCTYPE_DES_CBC_MD4) || buf_appendint(buf, l) ||
buf_appenddata(buf, key, l))
goto memerr;
if (buf_appendint(buf, ENCTYPE_DES_CBC_MD5) || buf_appendint(buf, l) ||
buf_appenddata(buf, key, l))
goto memerr;
n += 2;
}
#endif
n++;
}
curlen = get_cursor(buf);
set_cursor(buf, last);
if (buf_putint(buf, n))
goto interr;
set_cursor(buf, curlen);
rc = sqlite3_finalize(st);
if (rc != SQLITE_OK)
goto dberr;
if (n == 0)
goto interr;
return 0;
dberr:
prtmsg("database error: %s", sqlite3_errmsg(sess->dbh));
send_error(sess, ERR_OTHER, "Server internal error (database failure)");
goto freeall;
interr:
send_error(sess, ERR_OTHER, "Server internal error");
goto freeall;
memerr:
send_error(sess, ERR_OTHER, "Server internal error (out of memory)");
freeall:
if (st)
sqlite3_finalize(st);
return 1;
}
/* Set up the object used by the kadmin api for storing keys. This
differs between mit and heimdal. */
#ifdef HAVE_KADM5_CHPASS_PRINCIPAL_WITH_KEY
static int prepare_kadm_key(krb5_key_data *k, krb5_kvno kvno, int enctype, int keylen,
const unsigned char *keydata) {
memset(k, 0, sizeof(*k));
k->key_data_ver = 1;
k->key_data_kvno = kvno;
k->key_data_type[0]=enctype;
k->key_data_length[0]=keylen;
k->key_data_contents[0]=malloc(keylen);
if (k->key_data_contents[0] == NULL)
return 1;
memcpy(k->key_data_contents[0], keydata, keylen);
return 0;
}
#else
static int prepare_kadm_key(krb5_keyblock *k, krb5_kvno kvno, int enctype, int keylen,
const unsigned char *keydata) {
Z_enctype(k)=enctype;
Z_keylen(k)=keylen;
Z_keydata(k)=malloc(keylen);
if (Z_keydata(k) == NULL)
return 1;
memcpy(Z_keydata(k), keydata, keylen);
return 0;
}
#endif
/* remove a principal and all dependent objects from the local database */
static int do_purge(struct rekey_session *sess, sqlite_int64 princid)
{
int rc;
struct sqlite3_stmt *del;
rc = sqlite3_prepare_v2(sess->dbh,
"DELETE FROM keys WHERE principal = ?;",
-1, &del, NULL);
if (rc == SQLITE_OK) {
rc = sqlite3_bind_int64(del, 1, princid);
if (rc == SQLITE_OK)
sqlite3_step(del);
rc = sqlite3_finalize(del);
del=0;
}
if (rc == SQLITE_OK)
rc = sqlite3_prepare_v2(sess->dbh,
"DELETE FROM acl WHERE principal = ?;",
-1, &del, NULL);
if (rc == SQLITE_OK) {
rc = sqlite3_bind_int64(del, 1, princid);
if (rc == SQLITE_OK)
sqlite3_step(del);
rc = sqlite3_finalize(del);
del=0;
}
if (rc == SQLITE_OK)
rc = sqlite3_prepare_v2(sess->dbh,
"DELETE FROM principals WHERE id = ?;",
-1, &del, NULL);
if (rc == SQLITE_OK) {
rc = sqlite3_bind_int64(del, 1, princid);
if (rc == SQLITE_OK)
sqlite3_step(del);
rc = sqlite3_finalize(del);
del=0;
}
return rc;
}
/* attempt to update the kdb, given a request that has been commited
by all its clients. If it fails, a message is stored in the database
to help debugging */
static int do_finalize_req(struct rekey_session *sess, int no_send,
char *principal, sqlite_int64 princid,
krb5_principal target, krb5_kvno kvno) {
sqlite3_stmt *updmsg=NULL, *selkey=NULL;
int dbaction=0, rc, ret=1;
unsigned int nk=0, enctype, keylen, i;
kadm5_principal_ent_rec ke;
#ifdef HAVE_KADM5_CHPASS_PRINCIPAL_WITH_KEY
krb5_key_data *k=NULL, *newk;
int ksz = sizeof(krb5_key_data);
#else
krb5_keyblock *k=NULL, *newk;
int ksz = sizeof(krb5_keyblock);
#endif
const unsigned char *keydata;
rc = sqlite3_prepare_v2(sess->dbh,
"UPDATE principals SET message = ? WHERE id = ?;",
-1, &updmsg, NULL);
if (rc != SQLITE_OK)
goto dberr;
rc = sqlite3_bind_int64(updmsg, 2, princid);
if (rc != SQLITE_OK)
goto dberr;
if (kadm_init(sess))
goto interr;
memset(&ke, 0, sizeof(ke));
rc = kadm5_get_principal(sess->kadm_handle, target, &ke, KADM5_KVNO |
KADM5_ATTRIBUTES | KADM5_PRINC_EXPIRE_TIME);
if (rc) {
if (rc == KADM5_UNK_PRINC) {
prtmsg("Principal %s disappeared from kdc", principal);
rc = sqlite3_bind_text(updmsg, 2, "Principal disappeared from kdc",
strlen("Principal disappeared from kdc"),
SQLITE_STATIC);
if (rc == SQLITE_OK) {
sqlite3_step(updmsg); /* finalize in freeall */
}
if (no_send == 0)
send_error(sess, ERR_OTHER, "Principal disappeared from kdc");
goto freeall;
}
prtmsg("Unable to lookup principal %s: %s", principal,
krb5_get_err_text(sess->kctx, rc));
goto interr;
}
if (kvno != ke.kvno + 1) {
prtmsg("kvno of %s changed from %d to %d; not finalizing commit", principal, kvno - 1, ke.kvno);
rc = sqlite3_bind_text(updmsg, 2, "Principal's kvno changed on kdc",
strlen("Principal's kvno changed on kdc"),
SQLITE_STATIC);
if (rc == SQLITE_OK) {
sqlite3_step(updmsg); /* finalize in freeall */
}
if (no_send == 0)
send_error(sess, ERR_OTHER, "Principal's kvno changed on kdc");
goto freeall;
}
rc = sqlite3_prepare_v2(sess->dbh,
"SELECT enctype, key FROM keys WHERE principal = ?;",
-1, &selkey, NULL);
if (rc != SQLITE_OK)
goto dberr;
rc = sqlite3_bind_int64(selkey, 1, princid);
if (rc != SQLITE_OK)
goto dberr;
while (SQLITE_ROW == sqlite3_step(selkey)) {
enctype = sqlite3_column_int(selkey, 0);
keydata = sqlite3_column_blob(selkey, 1);
keylen = sqlite3_column_bytes(selkey, 1);
if (keydata == NULL || keylen == 0)
goto interr;
if (enctype == 0)
goto dberr;
#if !HAVE_DECL_ENCTYPE_AES128_CTS_HMAC_SHA1_96 || HAVE_DECL_ENCTYPE_DES_CBC_CRC
if (enctype == ENCTYPE_DES_CBC_CRC)
newk = realloc(k, ksz * (nk+3));
else
#endif
newk = realloc(k, ksz * (nk+1));
if (newk == NULL)
goto memerr;
k = newk;
if (prepare_kadm_key(&k[nk++], kvno, enctype, keylen, keydata))
goto memerr;
#if !HAVE_DECL_ENCTYPE_AES128_CTS_HMAC_SHA1_96 || HAVE_DECL_ENCTYPE_DES_CBC_CRC
if (enctype == ENCTYPE_DES_CBC_CRC) {
if (prepare_kadm_key(&k[nk++], kvno, ENCTYPE_DES_CBC_MD4, keylen, keydata))
goto memerr;
if (prepare_kadm_key(&k[nk++], kvno, ENCTYPE_DES_CBC_MD5, keylen, keydata))
goto memerr;
}
#endif
}
rc = sqlite3_finalize(selkey);
selkey=NULL;
if (rc != SQLITE_OK)
goto dberr;
if (nk == 0) {
prtmsg("No keys found for %s; cannot commit", principal);
goto interr;
}
#ifdef HAVE_KADM5_CHPASS_PRINCIPAL_WITH_KEY
rc = kadm5_chpass_principal_with_key(sess->kadm_handle, target, nk, k);
#else
rc = kadm5_setkey_principal(sess->kadm_handle, target, k, nk);
#endif
if (rc) {
prtmsg("finalizing %s failed to update kdc with keys: %s", principal,
krb5_get_err_text(sess->kctx, rc));
rc = sqlite3_bind_text(updmsg, 2, "setting keys in kdc failed",
strlen("setting keys in kdc failed"),
SQLITE_STATIC);
if (rc == SQLITE_OK) {
sqlite3_step(updmsg); /* finalize in freeall */
}
if (no_send == 0)
send_error(sess, ERR_OTHER, "Updating kdc failed");
goto freeall;
}
rc = sqlite3_bind_text(updmsg, 2, "kdc update succeeded",
strlen("kdc update succeeded"),
SQLITE_STATIC);
if (rc == SQLITE_OK) {
sqlite3_step(updmsg);
}
sqlite3_finalize(updmsg);
updmsg=NULL;
if (sql_begin_trans(sess))
goto dberr;
dbaction=-1;
rc = do_purge(sess, princid);
if (rc != SQLITE_OK)
goto dberr;
dbaction=1;
ret=0;
prtmsg("Operation(s) on %s completed successfully", principal);
goto freeall;
dberr:
prtmsg("database error: %s", sqlite3_errmsg(sess->dbh));
if (no_send == 0)
send_error(sess, ERR_OTHER, "Server internal error (database failure)");
goto freeall;
interr:
if (no_send == 0)
send_error(sess, ERR_OTHER, "Server internal error");
goto freeall;
memerr:
if (no_send == 0)
send_error(sess, ERR_OTHER, "Server internal error (out of memory)");
goto freeall;
freeall:
if (updmsg)
sqlite3_finalize(updmsg);
if (selkey)
sqlite3_finalize(selkey);
if (dbaction > 0) {
if (sql_commit_trans(sess)) {
sql_rollback_trans(sess);
ret=1;
if (no_send == 0)
send_error(sess, ERR_OTHER, "Server internal error (database failure)");
}
} else if (dbaction < 0)
sql_rollback_trans(sess);
if (k) {
for (i=0; i<nk; i++) {
#ifdef HAVE_KADM5_CHPASS_PRINCIPAL_WITH_KEY
free(k[i].key_data_contents[0]);
#else
free(Z_keydata(&k[i]));
#endif
}
free(k);
}
if (sess->kadm_handle)
kadm5_destroy(sess->kadm_handle);
sess->kadm_handle = NULL;
return ret;
}
/* Check to see if a principal's rekey is ready to be finalized (that is, that
there are no clients that have not commited it) */
static int check_uncommited(struct rekey_session *sess, sqlite_int64 princid)
{
sqlite3_stmt *checkcomp;
int rc, match;
rc = sqlite3_prepare_v2(sess->dbh,
"SELECT principal FROM acl WHERE principal = ? AND completed = 0;",
-1, &checkcomp, NULL);
if (rc != SQLITE_OK)
goto dberr;
rc = sqlite3_bind_int64(checkcomp, 1, princid);
if (rc != SQLITE_OK)
goto dberr;
match=0;
while (SQLITE_ROW == sqlite3_step(checkcomp)) {
match++;
}
rc = sqlite3_finalize(checkcomp);
checkcomp=NULL;
if (rc != SQLITE_OK)
goto dberr;
goto freeall;
dberr:
match = -1;
freeall:
if (checkcomp)
sqlite3_finalize(checkcomp);
return match;
}
/* Process an AUTH request containing a gss context token.
Returns an AUTH or OK response if successful. */
static void s_auth(struct rekey_session *sess, mb_t buf) {
OM_uint32 maj, min, tmin, rflag;
gss_buffer_desc in, out, outname;
unsigned int f, l;
int gss_more_accept=0, gss_more_init=0;
unsigned char *p;
krb5_error_code rc;
if (sess->authstate) {
send_error(sess, ERR_BADOP, "Authentication already complete");
return;
}
reset_cursor(buf);
if (buf_getint(buf, &f))
goto badpkt;
if (f & AUTHFLAG_MORE)
gss_more_init = 1;
if (buf_getint(buf, &l))
goto badpkt;
in.length = l;
in.value = malloc(l);
if (!in.value) {
send_fatal(sess, ERR_OTHER, "Out of memory receiving auth token");
fatal("Out of memory receiving auth token");
}
if (buf_getdata(buf, in.value, l)) {
free(in.value);
goto badpkt;
}
memset(&out, 0, sizeof(out));
maj = gss_accept_sec_context(&min, &sess->gctx, GSS_C_NO_CREDENTIAL,
&in, GSS_C_NO_CHANNEL_BINDINGS,
&sess->name, &sess->mech, &out, &rflag, NULL,
NULL);
free(in.value);
if (GSS_ERROR(maj)) {
if (out.length) {
send_gss_token(sess, RESP_AUTHERR, 0, &out);
gss_release_buffer(&tmin, &out);
prt_gss_error(sess->mech, maj, min);
} else {
send_gss_error(sess, sess->mech, maj, min);
}
if (sess->gctx != GSS_C_NO_CONTEXT)
gss_delete_sec_context(&tmin, &sess->gctx, GSS_C_NO_BUFFER);
return;
}
if (maj & GSS_S_CONTINUE_NEEDED) {
gss_more_accept=1;
if (out.length == 0) {
send_fatal(sess, ERR_OTHER, "Internal gss error on server");
fatal("Authentication failed: not sending a gss token but expects a reply");
}
}
if (out.length && gss_more_init == 0) {
send_fatal(sess, ERR_OTHER, "Internal gss error on server");
fatal("Authentication failed: would send a gss token when remote does not expect one");
}
if (gss_more_accept == 0) {
unsigned short oidl;
if ((~rflag) & (GSS_C_MUTUAL_FLAG|GSS_C_INTEG_FLAG)) {
send_fatal(sess, ERR_AUTHN, "GSSAPI mechanism does not provide data integrity services");
fatal("GSSAPI mechanism does not provide data integrity services");
}
maj = gss_export_name(&min, sess->name, &outname);
if (GSS_ERROR(maj)) {
prt_gss_error(sess->mech, maj, min);
send_fatal(sess, ERR_AUTHN, "Cannot parse authenticated name (cannot export name from GSSAPI)");
fatal("Cannot parse authenticated name (cannot export name from GSSAPI)");
}
/* check for minimum length and correct token header */
if (outname.length < 6 || memcmp(outname.value, "\x04\x01", 2)) {
send_fatal(sess, ERR_AUTHN, "Cannot parse authenticated name (it is not a valid exported name)");
fatal("Cannot parse authenticated name (it is not a valid exported name)");
}
p = outname.value;
p += 2;
/* extract oid wrapper length */
oidl = (p[0] << 8) + p[1];
p+=2;
/* check for oid length, valid oid tag, and correct oid length.
(this isn't really general - a sufficiently long oid would break this,
even if valid) */
if (outname.length < oidl + 4 ||
*p++ != 0x6 || *p >= 0x80 ||
*p++ != oidl - 2 ) {
send_fatal(sess, ERR_AUTHN, "Cannot parse authenticated name (it is not a valid exported name)");
fatal("Cannot parse authenticated name (it is not a valid exported name)");
}
oidl -= 2;
/* check for the krb5 mechanism oid */
if (gss_mech_krb5->length != oidl ||
memcmp(p, gss_mech_krb5->elements, oidl)) {
send_fatal(sess, ERR_AUTHN, "Cannot parse authenticated name (it is not a kerberos name)");
fatal("Cannot parse authenticated name (it is not a kerberos name)");
}
buf_setlength(buf, 0);
if (buf_appenddata(buf, outname.value, outname.length)) {
send_fatal(sess, ERR_OTHER, "Internal error on server");
fatal("internal error: cannot copy name structure");
}
/* skip over the header/oid we already parsed */
set_cursor(buf, oidl + 6);
gss_release_buffer(&tmin, &outname);
if (buf_getstring(buf, &sess->plain_name, malloc)) {
send_fatal(sess, ERR_AUTHN, "Cannot parse authenticated name (unknown error)");
fatal("Cannot parse authenticated name (buffer is too short or memory allocation problem)");
}
if ((rc=krb5_parse_name(sess->kctx, sess->plain_name, &sess->princ))) {
send_fatal(sess, ERR_AUTHN, "Cannot parse authenticated name (unknown error)");
fatal("Cannot parse authenticated name (kerberos error %s)", krb5_get_err_text(sess->kctx, rc));
}
sess->authstate=1;
check_authz(sess);
prtmsg("Authenticated as %s (host? %d admin? %d)", sess->plain_name,
sess->is_host, sess->is_admin);
}
if (out.length) {
send_gss_token(sess, RESP_AUTH, gss_more_accept, &out);
gss_release_buffer(&tmin, &out);
} else {
sess_send(sess, RESP_OK, NULL);
}
return;
badpkt:
send_error(sess, ERR_BADREQ, "Packet was too short for opcode");
return;
}
/* process an AUTHERR request. logs gss error information from client and
terminates channel */
static void s_autherr(struct rekey_session *sess, mb_t buf)
{
OM_uint32 maj, min;
gss_buffer_desc in, out;
unsigned int f, l;
if (sess->authstate) {
send_error(sess, ERR_BADOP, "Authentication already complete");
return;
}
if (buf_getint(buf, &f))
goto badpkt;
if (buf_getint(buf, (unsigned int *)&l))
goto badpkt;
in.length=l;
in.value = malloc(l);
if (!in.value) {
send_fatal(sess, ERR_OTHER, "Out of memory receiving auth token");
fatal("Out of memory receiving auth token");
}
if (buf_getdata(buf, in.value, l)) {
free(in.value);
goto badpkt;
}
memset(&out, 0, sizeof(out));
maj = gss_accept_sec_context(&min, &sess->gctx, GSS_C_NO_CREDENTIAL,
&in, GSS_C_NO_CHANNEL_BINDINGS,
&sess->name, &sess->mech, &out, NULL, NULL,
NULL);