removing more RCS tags
[Samba.git] / source / sam / group.c
blob101e3dd7ce16be0c2124ed2aaa44c72b753d93cc
1 /*
2 Unix SMB/CIFS implementation.
3 SAM_GROUP_HANDLE /SAM_GROUP_ENUM helpers
5 Copyright (C) Stefan (metze) Metzmacher 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.
22 #include "includes.h"
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_SAM
27 /************************************************************
28 Fill the SAM_GROUP_HANDLE with default values.
29 ***********************************************************/
31 static void sam_fill_default_group(SAM_GROUP_HANDLE *group)
33 ZERO_STRUCT(group->private); /* Don't touch the talloc context */
37 static void destroy_sam_group_handle_talloc(SAM_GROUP_HANDLE **group)
39 if (*group) {
41 talloc_destroy((*group)->mem_ctx);
42 *group = NULL;
47 /**********************************************************************
48 Alloc memory and initialises a SAM_GROUP_HANDLE on supplied mem_ctx.
49 ***********************************************************************/
51 NTSTATUS sam_init_group_talloc(TALLOC_CTX *mem_ctx, SAM_GROUP_HANDLE **group)
53 SMB_ASSERT(*group != NULL);
55 if (!mem_ctx) {
56 DEBUG(0,("sam_init_group_talloc: mem_ctx was NULL!\n"));
57 return NT_STATUS_UNSUCCESSFUL;
60 *group=(SAM_GROUP_HANDLE *)talloc(mem_ctx, sizeof(SAM_GROUP_HANDLE));
62 if (*group==NULL) {
63 DEBUG(0,("sam_init_group_talloc: error while allocating memory\n"));
64 return NT_STATUS_NO_MEMORY;
67 (*group)->mem_ctx = mem_ctx;
69 (*group)->free_fn = NULL;
71 sam_fill_default_group(*group);
73 return NT_STATUS_OK;
77 /*************************************************************
78 Alloc memory and initialises a struct SAM_GROUP_HANDLE.
79 ************************************************************/
81 NTSTATUS sam_init_group(SAM_GROUP_HANDLE **group)
83 TALLOC_CTX *mem_ctx;
84 NTSTATUS nt_status;
86 mem_ctx = talloc_init("sam internal SAM_GROUP_HANDLE allocation");
88 if (!mem_ctx) {
89 DEBUG(0,("sam_init_group: error while doing talloc_init()\n"));
90 return NT_STATUS_NO_MEMORY;
93 if (!NT_STATUS_IS_OK(nt_status = sam_init_group_talloc(mem_ctx, group))) {
94 talloc_destroy(mem_ctx);
95 return nt_status;
98 (*group)->free_fn = destroy_sam_group_handle_talloc;
100 return NT_STATUS_OK;
104 /************************************************************
105 Reset the SAM_GROUP_HANDLE.
106 ***********************************************************/
108 NTSTATUS sam_reset_group(SAM_GROUP_HANDLE *group)
110 SMB_ASSERT(group != NULL);
112 sam_fill_default_group(group);
114 return NT_STATUS_OK;
118 /************************************************************
119 Free the SAM_GROUP_HANDLE and the member pointers.
120 ***********************************************************/
122 NTSTATUS sam_free_group(SAM_ACCOUNT_HANDLE **group)
124 SMB_ASSERT(*group != NULL);
126 if ((*group)->free_fn) {
127 (*group)->free_fn(group);
130 return NT_STATUS_OK;
134 /**********************************************************
135 Encode the group control bits into a string.
136 length = length of string to encode into (including terminating
137 null). length *MUST BE MORE THAN 2* !
138 **********************************************************/
140 char *sam_encode_acct_ctrl(uint16 group_ctrl, size_t length)
142 static fstring group_str;
143 size_t i = 0;
145 group_str[i++] = '[';
147 if (group_ctrl & GCB_LOCAL_GROUP ) group_str[i++] = 'L';
148 if (group_ctrl & GCB_GLOBAL_GROUP ) group_str[i++] = 'G';
150 for ( ; i < length - 2 ; i++ )
151 group_str[i] = ' ';
153 i = length - 2;
154 group_str[i++] = ']';
155 group_str[i++] = '\0';
157 return group_str;
160 /**********************************************************
161 Decode the group control bits from a string.
162 **********************************************************/
164 uint16 sam_decode_group_ctrl(const char *p)
166 uint16 group_ctrl = 0;
167 BOOL finished = False;
170 * Check if the account type bits have been encoded after the
171 * NT password (in the form [NDHTUWSLXI]).
174 if (*p != '[')
175 return 0;
177 for (p++; *p && !finished; p++) {
178 switch (*p) {
179 case 'L': { group_ctrl |= GCB_LOCAL_GROUP; break; /* 'L'ocal Aliases Group. */ }
180 case 'G': { group_ctrl |= GCB_GLOBAL_GROUP; break; /* 'G'lobal Domain Group. */ }
182 case ' ': { break; }
183 case ':':
184 case '\n':
185 case '\0':
186 case ']':
187 default: { finished = True; }
191 return group_ctrl;