1 /*
2    Unix SMB/CIFS implementation.
3 
4    Samba KDB plugin for MIT Kerberos
5 
6    Copyright (c) 2010      Simo Sorce <idra@samba.org>.
7    Copyright (c) 2014      Andreas Schneider <asn@samba.org>
8 
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "includes.h"
24 
25 #include "system/kerberos.h"
26 
27 #include <profile.h>
28 #include <kdb.h>
29 
30 #include "kdc/mit_samba.h"
31 #include "kdb_samba.h"
32 
ks_get_context(krb5_context kcontext)33 struct mit_samba_context *ks_get_context(krb5_context kcontext)
34 {
35 	void *db_ctx;
36 	krb5_error_code code;
37 
38 	code = krb5_db_get_context(kcontext, &db_ctx);
39 	if (code != 0) {
40 		return NULL;
41 	}
42 
43 	return (struct mit_samba_context *)db_ctx;
44 }
45 
ks_data_eq_string(krb5_data d,const char * s)46 bool ks_data_eq_string(krb5_data d, const char *s)
47 {
48 	int rc;
49 
50 	if (d.length != strlen(s) || d.length == 0) {
51 		return false;
52 	}
53 
54 	rc = memcmp(d.data, s, d.length);
55 	if (rc != 0) {
56 		return false;
57 	}
58 
59 	return true;
60 }
61 
ks_make_data(void * data,unsigned int len)62 krb5_data ks_make_data(void *data, unsigned int len)
63 {
64 	krb5_data d;
65 
66 	d.magic = KV5M_DATA;
67 	d.data = data;
68 	d.length = len;
69 
70 	return d;
71 }
72 
ks_is_kadmin(krb5_context context,krb5_const_principal princ)73 krb5_boolean ks_is_kadmin(krb5_context context,
74 			  krb5_const_principal princ)
75 {
76 	return krb5_princ_size(context, princ) >= 1 &&
77 	       ks_data_eq_string(princ->data[0], "kadmin");
78 }
79 
ks_is_kadmin_history(krb5_context context,krb5_const_principal princ)80 krb5_boolean ks_is_kadmin_history(krb5_context context,
81 				  krb5_const_principal princ)
82 {
83 	return krb5_princ_size(context, princ) == 2 &&
84 	       ks_data_eq_string(princ->data[0], "kadmin") &&
85 	       ks_data_eq_string(princ->data[1], "history");
86 }
87 
ks_is_kadmin_changepw(krb5_context context,krb5_const_principal princ)88 krb5_boolean ks_is_kadmin_changepw(krb5_context context,
89 				   krb5_const_principal princ)
90 {
91 	return krb5_princ_size(context, princ) == 2 &&
92 	       ks_data_eq_string(princ->data[0], "kadmin") &&
93 	       ks_data_eq_string(princ->data[1], "changepw");
94 }
95 
ks_is_kadmin_admin(krb5_context context,krb5_const_principal princ)96 krb5_boolean ks_is_kadmin_admin(krb5_context context,
97 				krb5_const_principal princ)
98 {
99 	return krb5_princ_size(context, princ) == 2 &&
100 	       ks_data_eq_string(princ->data[0], "kadmin") &&
101 	       ks_data_eq_string(princ->data[1], "admin");
102 }
103