2 Unix SMB/CIFS implementation.
3 Universal groups helpers
4 Copyright (C) Alexander Bokovoy 2002.
5 Copyright (C) Andrew Bartlett 2002.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 This work was sponsored by Optifacio Software Services, Inc.
27 Handle for netlogon_unigrp.tdb database. It is used internally
28 in cli_store_uni_groups_*() and cli_fetch_uni_groups()
29 and is initialized on first call to cli_store_uni_groups_*()
31 static TDB_CONTEXT
*netlogon_unigrp_tdb
= NULL
;
34 Store universal groups info into netlogon_unigrp.tdb for
35 later usage. We use 'domain_SID/user_rid' as key and
36 array of uint32 where array[0] is number of elements
37 and elements are array[1] ... array[array[0]]
40 BOOL
uni_group_cache_init(void)
42 if (!netlogon_unigrp_tdb
) {
43 netlogon_unigrp_tdb
= tdb_open_log(lock_path("netlogon_unigrp.tdb"), 0,
44 TDB_DEFAULT
, O_RDWR
| O_CREAT
, 0644);
47 return (netlogon_unigrp_tdb
!= NULL
);
50 BOOL
uni_group_cache_store_netlogon(TALLOC_CTX
*mem_ctx
, NET_USER_INFO_3
*user
)
56 if (!uni_group_cache_init()) {
57 DEBUG(0,("uni_group_cache_store_netlogon: cannot open netlogon_unigrp.tdb for write!\n"));
61 /* Prepare key as DOMAIN-SID/USER-RID string */
62 slprintf(keystr
, sizeof(keystr
), "%s/%d",
63 sid_string_static(&user
->dom_sid
.sid
), user
->user_rid
);
65 key
.dsize
= strlen(keystr
) + 1;
68 data
.dsize
= (user
->num_groups2
+1)*sizeof(uint32
);
69 data
.dptr
= talloc(mem_ctx
, data
.dsize
);
71 DEBUG(0,("uni_group_cache_store_netlogon: cannot allocate memory!\n"));
72 talloc_destroy(mem_ctx
);
76 /* Store data in byteorder-independent format */
77 SIVAL(&((uint32
*)data
.dptr
)[0],0,user
->num_groups2
);
78 for(i
=1; i
<=user
->num_groups2
; i
++) {
79 SIVAL(&((uint32
*)data
.dptr
)[i
],0,user
->gids
[i
-1].g_rid
);
81 if (tdb_store(netlogon_unigrp_tdb
, key
, data
, TDB_REPLACE
) == -1)
87 Fetch universal groups info from netlogon_unigrp.tdb for given
88 domain sid and user rid and allocate it using given mem_ctx.
89 Universal groups are returned as array of uint32 elements
90 and elements are array[0] ... array[num_elements-1]
93 uint32
* uni_group_cache_fetch(DOM_SID
*domain
, uint32 user_rid
,
94 TALLOC_CTX
*mem_ctx
, uint32
*num_groups
)
103 DEBUG(1,("uni_group_cache_fetch: expected non-null domain sid\n"));
107 DEBUG(1,("uni_group_cache_fetch: expected non-null memory context\n"));
111 DEBUG(1,("uni_group_cache_fetch: expected non-null num_groups\n"));
114 if (!netlogon_unigrp_tdb
) {
115 netlogon_unigrp_tdb
= tdb_open_log(lock_path("netlogon_unigrp.tdb"), 0,
116 TDB_DEFAULT
, O_RDWR
, 0644);
118 if (!netlogon_unigrp_tdb
) {
119 DEBUG(5,("uni_group_cache_fetch: cannot open netlogon_unigrp.tdb for read - normal if not created yet\n"));
125 /* Fetch universal groups */
126 slprintf(keystr
, sizeof(keystr
), "%s/%d",
127 sid_string_static(domain
), user_rid
);
129 key
.dsize
= strlen(keystr
) + 1;
130 data
= tdb_fetch(netlogon_unigrp_tdb
, key
);
132 /* There is no cached universal groups in netlogon_unigrp.tdb */
137 /* Transfer data to receiver's memory context */
138 group_count
= IVAL(&((uint32
*)data
.dptr
)[0],0);
139 groups
= talloc(mem_ctx
, (group_count
)*sizeof(uint32
));
141 for(i
=0; i
<group_count
; i
++) {
142 groups
[i
] = IVAL(&((uint32
*)data
.dptr
)[i
+1],0);
146 DEBUG(1,("uni_group_cache_fetch: cannot allocate uni groups in receiver's memory context\n"));
148 SAFE_FREE(data
.dptr
);
149 *num_groups
= group_count
;
153 /* Shutdown netlogon_unigrp database */
154 BOOL
uni_group_cache_shutdown(void)
156 if(netlogon_unigrp_tdb
)
157 return (tdb_close(netlogon_unigrp_tdb
) == 0);