Updates to the build farm tests:
[kai/samba.git] / source / sam / group.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SAM_GROUP_HANDLE /SAM_GROUP_ENUM helpers
4    
5    Copyright (C) Stefan (metze) Metzmacher      2002
6       
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_SAM
26
27 /************************************************************
28  Fill the SAM_GROUP_HANDLE with default values.
29  ***********************************************************/
30
31 static void sam_fill_default_group(SAM_GROUP_HANDLE *group)
32 {
33         ZERO_STRUCT(group->private); /* Don't touch the talloc context */
34
35 }       
36
37 static void destroy_sam_group_handle_talloc(SAM_GROUP_HANDLE **group) 
38 {
39         if (*group) {
40
41                 talloc_destroy((*group)->mem_ctx);
42                 *group = NULL;
43         }
44 }
45
46
47 /**********************************************************************
48  Alloc memory and initialises a SAM_GROUP_HANDLE on supplied mem_ctx.
49 ***********************************************************************/
50
51 NTSTATUS sam_init_group_talloc(TALLOC_CTX *mem_ctx, SAM_GROUP_HANDLE **group)
52 {
53         SMB_ASSERT(*group != NULL);
54
55         if (!mem_ctx) {
56                 DEBUG(0,("sam_init_group_talloc: mem_ctx was NULL!\n"));
57                 return NT_STATUS_UNSUCCESSFUL;
58         }
59
60         *group=(SAM_GROUP_HANDLE *)talloc(mem_ctx, sizeof(SAM_GROUP_HANDLE));
61
62         if (*group==NULL) {
63                 DEBUG(0,("sam_init_group_talloc: error while allocating memory\n"));
64                 return NT_STATUS_NO_MEMORY;
65         }
66
67         (*group)->mem_ctx = mem_ctx;
68
69         (*group)->free_fn = NULL;
70
71         sam_fill_default_group(*group);
72         
73         return NT_STATUS_OK;
74 }
75
76
77 /*************************************************************
78  Alloc memory and initialises a struct SAM_GROUP_HANDLE.
79  ************************************************************/
80
81 NTSTATUS sam_init_group(SAM_GROUP_HANDLE **group)
82 {
83         TALLOC_CTX *mem_ctx;
84         NTSTATUS nt_status;
85         
86         mem_ctx = talloc_init_named("sam internal SAM_GROUP_HANDLE allocation");
87
88         if (!mem_ctx) {
89                 DEBUG(0,("sam_init_group: error while doing talloc_init()\n"));
90                 return NT_STATUS_NO_MEMORY;
91         }
92
93         if (!NT_STATUS_IS_OK(nt_status = sam_init_group_talloc(mem_ctx, group))) {
94                 talloc_destroy(mem_ctx);
95                 return nt_status;
96         }
97         
98         (*group)->free_fn = destroy_sam_group_handle_talloc;
99
100         return NT_STATUS_OK;
101 }
102
103
104 /************************************************************
105  Reset the SAM_GROUP_HANDLE.
106  ***********************************************************/
107
108 NTSTATUS sam_reset_group(SAM_GROUP_HANDLE *group)
109 {
110         SMB_ASSERT(group != NULL);
111
112         sam_fill_default_group(group);
113
114         return NT_STATUS_OK;
115 }
116
117
118 /************************************************************
119  Free the SAM_GROUP_HANDLE and the member pointers.
120  ***********************************************************/
121
122 NTSTATUS sam_free_group(SAM_ACCOUNT_HANDLE **group)
123 {
124         SMB_ASSERT(*group != NULL);
125
126         if ((*group)->free_fn) {
127                 (*group)->free_fn(group);
128         }
129
130         return NT_STATUS_OK;    
131 }
132
133
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  **********************************************************/
139
140 char *sam_encode_acct_ctrl(uint16 group_ctrl, size_t length)
141 {
142         static fstring group_str;
143         size_t i = 0;
144
145         group_str[i++] = '[';
146
147         if (group_ctrl & GCB_LOCAL_GROUP )      group_str[i++] = 'L';
148         if (group_ctrl & GCB_GLOBAL_GROUP )     group_str[i++] = 'G';
149
150         for ( ; i < length - 2 ; i++ )
151                 group_str[i] = ' ';
152
153         i = length - 2;
154         group_str[i++] = ']';
155         group_str[i++] = '\0';
156
157         return group_str;
158 }     
159
160 /**********************************************************
161  Decode the group control bits from a string.
162  **********************************************************/
163
164 uint16 sam_decode_group_ctrl(const char *p)
165 {
166         uint16 group_ctrl = 0;
167         BOOL finished = False;
168
169         /*
170          * Check if the account type bits have been encoded after the
171          * NT password (in the form [NDHTUWSLXI]).
172          */
173
174         if (*p != '[')
175                 return 0;
176
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. */ } 
181                         
182                         case ' ': { break; }
183                         case ':':
184                         case '\n':
185                         case '\0': 
186                         case ']':
187                         default:  { finished = True; }
188                 }
189         }
190
191         return group_ctrl;
192 }
193