check a pointer before dereferencing it; not sure why userdata == NULL though
[Samba.git] / source / libsmb / netlogon_unigrp.c
blobfa2fe32f35f7691f05e53215ea615f1790f8c27b
1 /*
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.
24 #include "includes.h"
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)
52 TDB_DATA key,data;
53 fstring keystr;
54 int i;
56 if (!uni_group_cache_init()) {
57 DEBUG(0,("uni_group_cache_store_netlogon: cannot open netlogon_unigrp.tdb for write!\n"));
58 return False;
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);
64 key.dptr = keystr;
65 key.dsize = strlen(keystr) + 1;
67 /* Prepare data */
68 data.dsize = (user->num_groups2+1)*sizeof(uint32);
69 data.dptr = talloc(mem_ctx, data.dsize);
70 if(!data.dptr) {
71 DEBUG(0,("uni_group_cache_store_netlogon: cannot allocate memory!\n"));
72 talloc_destroy(mem_ctx);
73 return False;
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)
82 return False;
83 return True;
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)
96 TDB_DATA key,data;
97 fstring keystr;
98 uint32 *groups;
99 uint32 i;
100 uint32 group_count;
102 if (!domain) {
103 DEBUG(1,("uni_group_cache_fetch: expected non-null domain sid\n"));
104 return NULL;
106 if (!mem_ctx) {
107 DEBUG(1,("uni_group_cache_fetch: expected non-null memory context\n"));
108 return NULL;
110 if (!num_groups) {
111 DEBUG(1,("uni_group_cache_fetch: expected non-null num_groups\n"));
112 return NULL;
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"));
120 return NULL;
123 *num_groups = 0;
125 /* Fetch universal groups */
126 slprintf(keystr, sizeof(keystr), "%s/%d",
127 sid_string_static(domain), user_rid);
128 key.dptr = keystr;
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 */
133 /* for this user. */
134 if (!data.dptr)
135 return NULL;
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));
140 if (groups) {
141 for(i=0; i<group_count; i++) {
142 groups[i] = IVAL(&((uint32*)data.dptr)[i+1],0);
145 } else {
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;
150 return groups;
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);
158 return True;