lsa_EnumSids() now works
[bbaumbach/samba-autobuild/.git] / source4 / torture / rpc / lsa.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for lsa rpc operations
4
5    Copyright (C) Andrew Tridgell 2003
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 /*
25   this really shouldn't be here ....
26 */
27 static char *lsa_sid_string_talloc(TALLOC_CTX *mem_ctx, struct dom_sid *sid)
28 {
29         int i, ofs, maxlen;
30         uint32 ia;
31         char *ret;
32         
33         if (!sid) {
34                 return talloc_asprintf(mem_ctx, "(NULL SID)");
35         }
36
37         maxlen = sid->num_auths * 11 + 25;
38         ret = talloc(mem_ctx, maxlen);
39         if (!ret) return NULL;
40
41         ia = (sid->id_auth[5]) +
42                 (sid->id_auth[4] << 8 ) +
43                 (sid->id_auth[3] << 16) +
44                 (sid->id_auth[2] << 24);
45
46         ofs = snprintf(ret, maxlen, "S-%u-%lu", 
47                        (unsigned int)sid->sid_rev_num, (unsigned long)ia);
48
49         for (i = 0; i < sid->num_auths; i++) {
50                 ofs += snprintf(ret + ofs, maxlen - ofs, "-%lu", (unsigned long)sid->sub_auths[i]);
51         }
52
53         return ret;
54 }
55
56
57 static BOOL test_OpenPolicy(struct dcerpc_pipe *p)
58 {
59         struct lsa_ObjectAttribute attr;
60         struct policy_handle handle;
61         struct lsa_QosInfo qos;
62         NTSTATUS status;
63
64         printf("\ntesting OpenPolicy\n");
65
66         qos.impersonation_level = 2;
67         qos.context_mode = 1;
68         qos.effective_only = 0;
69
70         attr.root_dir = NULL;
71         attr.object_name = NULL;
72         attr.attributes = 0;
73         attr.sec_desc = NULL;
74         attr.sec_qos = &qos;
75
76         status = dcerpc_lsa_OpenPolicy(p, 
77                                        "\\",
78                                        &attr,
79                                        SEC_RIGHTS_MAXIMUM_ALLOWED,
80                                        &handle);
81         if (!NT_STATUS_IS_OK(status)) {
82                 printf("OpenPolicy failed - %s\n", nt_errstr(status));
83                 return False;
84         }
85
86         return True;
87 }
88
89
90 static BOOL test_OpenPolicy2(struct dcerpc_pipe *p, struct policy_handle *handle)
91 {
92         struct lsa_ObjectAttribute attr;
93         struct lsa_QosInfo qos;
94         NTSTATUS status;
95
96         printf("\ntesting OpenPolicy2\n");
97
98         qos.impersonation_level = 2;
99         qos.context_mode = 1;
100         qos.effective_only = 0;
101
102         attr.root_dir = NULL;
103         attr.object_name = NULL;
104         attr.attributes = 0;
105         attr.sec_desc = NULL;
106         attr.sec_qos = &qos;
107
108         status = dcerpc_lsa_OpenPolicy2(p, 
109                                         "\\",
110                                         &attr,
111                                         SEC_RIGHTS_MAXIMUM_ALLOWED,
112                                         handle);
113         if (!NT_STATUS_IS_OK(status)) {
114                 printf("OpenPolicy2 failed - %s\n", nt_errstr(status));
115                 return False;
116         }
117
118         return True;
119 }
120
121
122 static BOOL test_EnumSids(struct dcerpc_pipe *p, 
123                           TALLOC_CTX *mem_ctx, 
124                           struct policy_handle *handle)
125 {
126         NTSTATUS status;
127         struct dom_sid **sids;
128         uint32 num_entries = 100;
129         int i;
130
131         printf("\ntesting EnumSids\n");
132
133         status = dcerpc_lsa_EnumSids(p, mem_ctx, handle, 0, &num_entries, &sids);
134         if (!NT_STATUS_IS_OK(status)) {
135                 printf("EnumSids failed - %s\n", nt_errstr(status));
136                 return False;
137         }
138
139         printf("Got %d sids\n", num_entries);
140
141         for (i=0;i<num_entries;i++) {
142                 printf("%s\n", lsa_sid_string_talloc(mem_ctx, sids[i]));
143         }
144         
145
146         return True;
147 }
148
149 BOOL torture_rpc_lsa(int dummy)
150 {
151         NTSTATUS status;
152         struct dcerpc_pipe *p;
153         TALLOC_CTX *mem_ctx;
154         BOOL ret = True;
155         struct policy_handle handle;
156
157         mem_ctx = talloc_init("torture_rpc_lsa");
158
159         status = torture_rpc_connection(&p, "lsarpc");
160         if (!NT_STATUS_IS_OK(status)) {
161                 return False;
162         }
163         
164         if (!test_OpenPolicy(p)) {
165                 ret = False;
166         }
167
168         if (!test_OpenPolicy2(p, &handle)) {
169                 ret = False;
170         }
171
172         if (!test_EnumSids(p, mem_ctx, &handle)) {
173                 ret = False;
174         }
175         
176         torture_rpc_close(p);
177
178         return ret;
179 }