summaryrefslogtreecommitdiff
path: root/source/tdb/tdb_sec.c
blob: 74da4d393fe91d1a8bc029dc79b542cf94280ffb (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
/* 
   Unix SMB/Netbios implementation.
   Version 2.0.
   a tdb database with record level security.
   Copyright (C) Andrew Tridgell              2000
   Copyright (C) Luke Kenneth Casson Leighton 2000
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*
 *
 * int tdbsec_set(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA secdata)
 *
 *
 * TDB_DATA tdbsec_fetch(TDB_CONTEXT *tdb, TDB_DATA key, 
 *                       int (*sec_check)(TDB_DATA , void *), void *arg)
 *
 *
 * int tdbsec_store(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA data,
 *                  int (*sec_check)(TDB_DATA , void *), void *arg)
 *
 */

#include "includes.h"


#define SEC_KEY "SEC/"
#degine SEC_KEY_LEN 4

static TDB_DATA null_data;

static TDB_DATA tdbsec_genkey(TDB_DATA key)
{
	TDB_DATA key2;

	key2.dsize = SEC_KEY_LEN+key.dsize;
	key2.dptr = (char *)malloc(key2.dsize);
	if (!key2.dptr) return null_data;

	memcpy(key2.dptr, SEC_KEY, SEC_KEY_LEN);
	memcpy(key2.dptr + SEC_KEY_LEN, key.dptr, key.dsize);

	return key2;
}

static void tdbsec_free(TDB_DATA d)
{
	if (d.dptr) free(d.dptr);
}


TDB_DATA tdbsec_fetch(TDB_CONTEXT *tdb, TDB_DATA key, 
		      int (*sec_check)(TDB_DATA , void *), void *arg)
{
	TDB_DATA key2, data2;

	data2 = null_data;

	key2 = tdbsec_genkey(key);
	if (!key2.dptr) goto failed;

	data2 = tdb_fetch(tdb, key2);

	if (sec_check(data2, arg) != 0) {
		goto failed;
	}

	tdbsec_free(key2);
	tdbsec_free(data2);

	return tdb_fetch(tdb, key);

 failed:
	tdbsec_free(key2);
	tdbsec_free(data2);
	return null_data;
}

int tdbsec_store(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA data,
		 int (*sec_check)(TDB_DATA , void *), void *arg)
{
	TDB_DATA key2, data2;

	data2 = null_data;

	key2 = tdbsec_genkey(key);
	if (!key2.dptr) goto failed;

	data2 = tdb_fetch(tdb, key2);

	if (sec_check(data2, arg) != 0) {
		goto failed;
	}

	tdbsec_free(key2);
	tdbsec_free(data2);

	return tdb_store(tdb, key, data, TDB_REPLACE);

 failed:
	tdbsec_free(key2);
	tdbsec_free(data2);
	return -1;
}


int tdbsec_set(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA secdata)
{
	int ret;
	TDB_DATA key2;

	key2 = tdbsec_genkey(key);
	if (!key2.dptr) return -1;
	
	ret = tdb_store(tdb, key2, secdata, TDB_REPLACE);
	
	tdbsec_free(key2);
	return ret;
}