s3: piddir creation fix part 2.
[ira/wip.git] / testsuite / smbd / sec_ctx_current_user.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Security context tests
5    Copyright (C) Tim Potter 2000
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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "sec_ctx_utils.h"
23
24 int main(int argc, char **argv)
25 {
26         extern struct current_user current_user;
27         uid_t initial_uid = current_user.uid;
28         gid_t initial_gid = current_user.gid;
29         int ngroups;
30         gid_t *groups;
31
32         init_sec_ctx();
33
34         /* Check initial id */
35
36         if (initial_uid != 0 || initial_gid != 0) {
37                 printf("FAIL: current_user not initialised to root\n");
38                 return 1;
39         }
40
41         /* Push a context and check current user is updated */
42
43         if (!push_sec_ctx()) {
44                 printf("FAIL: push_sec_ctx\n");
45                 return 1;
46         }
47
48         set_sec_ctx(1, 2, 0, NULL);
49
50         if (current_user.uid != 1 || current_user.gid != 2) {
51                 printf("FAIL: current_user id not updated after push\n");
52                 return 1;
53         }
54
55         if (current_user.ngroups != 0 || current_user.groups) {
56                 printf("FAIL: current_user groups not updated after push\n");
57                 return 1;
58         }
59
60         /* Push another */
61
62         get_random_grouplist(&ngroups, &groups);
63
64         if (!push_sec_ctx()) {
65                 printf("FAIL: push_sec_ctx\n");
66                 return 1;
67         }
68
69         set_sec_ctx(2, 3, ngroups, groups);
70
71         if (current_user.uid != 2 || current_user.gid != 3) {
72                 printf("FAIL: current_user id not updated after second "
73                        "push\n");
74                 return 1;
75         }
76
77         if (current_user.ngroups != ngroups || 
78             (memcmp(current_user.groups, groups, 
79                     sizeof(gid_t) * ngroups) != 0)) {
80                 printf("FAIL: current_user groups not updated\n");
81                 return 1;
82         }
83
84         /* Pop them both off */
85
86         if (!pop_sec_ctx()) {
87                 printf("FAIL: pop_sec_ctx\n");
88                 return 1;
89         }
90
91         if (current_user.uid != 1 || current_user.gid != 2) {
92                 printf("FAIL: current_user not updaded pop\n");
93                 return 1;
94         }
95
96         if (!pop_sec_ctx()) {
97                 printf("FAIL: pop_sec_ctx\n");
98                 return 1;
99         }
100
101         /* Check initial state was returned */
102
103         if (current_user.uid != initial_uid || 
104             current_user.gid != initial_gid) {
105                 printf("FAIL: current_user not updaded pop\n");
106                 return 1;
107         }
108
109         /* Everything's cool */
110
111         printf("PASS\n");
112         return 0;
113 }