summaryrefslogtreecommitdiffstats
path: root/source/lib/vuser_db.c
blob: a0a1d3193218bd41f4c5c47137f641972c6d8a63 (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
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   Samba utility functions
   Copyright (C) Andrew Tridgell              1992-2000
   Copyright (C) Luke Kenneth Casson Leighton 1996-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.
*/

/*
 * this module stores a user_struct which can be globally accessed (by root)
 * across process boundaries, in order to obtain info about users currently
 * accessing * samba. 
 *
 * the database key is the process number (pid) of the creater of the
 * structure plus the vuid - SMB virtual user id.  NORMALLY, this would
 * be smbd pid and an smbd-generated vuid.
 *
 */

#include "includes.h"

extern int DEBUGLEVEL;

static TDB_CONTEXT *tdb = NULL;

BOOL tdb_lookup_vuid( const vuser_key *uk, user_struct *usr)
{
	prs_struct key;
	prs_struct data;
	vuser_key k = *uk;

	ZERO_STRUCTP(usr);

	if (tdb == NULL)
	{
		if (!vuid_init_db())
		{
			return False;
		}
	}

	DEBUG(10,("lookup user %x,%x\n", uk->pid, uk->vuid));

	prs_init(&key, 0, 4, False);
	if (!vuid_io_key("key", &k, &key, 0))
	{
		return False;
	}

	prs_tdb_fetch(tdb, &key, &data);

	if (!vuid_io_user_struct("usr", usr, &data, 0))
	{
		prs_free_data(&key);
		prs_free_data(&data);
		return False;
	}

	prs_free_data(&key);
	prs_free_data(&data);

	return True;
}

BOOL tdb_store_vuid( const vuser_key *uk, user_struct *usr)
{
	prs_struct key;
	prs_struct data;
	vuser_key k = *uk;

	if (tdb == NULL)
	{
		if (!vuid_init_db())
		{
			return False;
		}
	}

	DEBUG(10,("storing user %x,%x\n", uk->pid, uk->vuid));

	prs_init(&key, 0, 4, False);
	prs_init(&data, 0, 4, False);

	if (!vuid_io_key("key", &k, &key, 0) ||
	    !vuid_io_user_struct("usr", usr, &data, 0) ||
	     prs_tdb_store(tdb, TDB_REPLACE, &key, &data) != 0)
	{
		prs_free_data(&key);
		prs_free_data(&data);
		return False;
	}

	prs_free_data(&key);
	prs_free_data(&data);
	return True;
}

BOOL vuid_init_db(void)
{
	tdb = tdb_open(lock_path("vuid.tdb"), 0, 0, O_RDWR | O_CREAT, 0600);

	if (tdb == NULL)
	{
		DEBUG(0,("vuid_init_db: failed\n"));
		return False;
	}
	
	DEBUG(10,("vuid_init_db: opened\n"));

	return True;
}