This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.
[samba.git] / source / libads / disp_sec.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions. ADS stuff
4    Copyright (C) Alexey Kotovich 2002
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful, 
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 #ifdef HAVE_ADS
24
25 static struct perm_mask_str {
26         uint32  mask;
27         char   *str;
28 } perms[] = {
29         {SEC_RIGHTS_FULL_CTRL,          "[Full Control]"},
30
31         {SEC_RIGHTS_LIST_CONTENTS,      "[List Contents]"},
32         {SEC_RIGHTS_LIST_OBJECT,        "[List Object]"},
33
34         {SEC_RIGHTS_READ_ALL_PROP,      "[Read All Properties]"},       
35         {SEC_RIGHTS_READ_PERMS,         "[Read Permissions]"},  
36
37         {SEC_RIGHTS_WRITE_ALL_VALID,    "[All validate writes]"},
38         {SEC_RIGHTS_WRITE_ALL_PROP,     "[Write All Properties]"},
39
40         {SEC_RIGHTS_MODIFY_PERMS,       "[Modify Permissions]"},
41         {SEC_RIGHTS_MODIFY_OWNER,       "[Modify Owner]"},
42
43         {SEC_RIGHTS_CREATE_CHILD,       "[Create All Child Objects]"},
44
45         {SEC_RIGHTS_DELETE,             "[Delete]"},
46         {SEC_RIGHTS_DELETE_SUBTREE,     "[Delete Subtree]"},
47         {SEC_RIGHTS_DELETE_CHILD,       "[Delete All Child Objects]"},
48
49         {SEC_RIGHTS_CHANGE_PASSWD,      "[Change Password]"},   
50         {SEC_RIGHTS_RESET_PASSWD,       "[Reset Password]"},
51         {0,                             0}
52 };
53
54 /* convert a security permissions into a string */
55 void ads_disp_perms(uint32 type)
56 {
57         int i = 0;
58         int j = 0;
59
60         printf("Permissions: ");
61         
62         if (type == SEC_RIGHTS_FULL_CTRL) {
63                 printf("%s\n", perms[j].str);
64                 return;
65         }
66
67         for (i = 0; i < 32; i++) {
68                 if (type & (1 << i)) {
69                         for (j = 1; perms[j].str; j ++) {
70                                 if (perms[j].mask == (((unsigned) 1) << i)) {
71                                         printf("\n\t%s", perms[j].str);
72                                 }       
73                         }
74                         type &= ~(1 << i);
75                 }
76         }
77
78         /* remaining bits get added on as-is */
79         if (type != 0) {
80                 printf("[%08x]", type);
81         }
82         puts("");
83 }
84
85 /* Check if ACE has OBJECT type */
86 BOOL ads_ace_object(uint8 type)
87 {
88         if (type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
89             type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT ||
90             type == SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT ||
91             type == SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT) {
92                 return True;
93         }
94         return False;
95 }
96
97 /* display ACE */
98 void ads_disp_ace(SEC_ACE *sec_ace)
99 {
100         char *access_type = "UNKNOWN";
101
102         if (!sec_ace_object(sec_ace->type)) {
103                 printf("------- ACE (type: 0x%02x, flags: 0x%02x, size: 0x%02x, mask: 0x%x)\n", 
104                   sec_ace->type,
105                   sec_ace->flags,
106                   sec_ace->size,
107                   sec_ace->info.mask);                  
108         } else {
109                 printf("------- ACE (type: 0x%02x, flags: 0x%02x, size: 0x%02x, mask: 0x%x, object flags: 0x%x)\n", 
110                   sec_ace->type,
111                   sec_ace->flags,
112                   sec_ace->size,
113                   sec_ace->info.mask,
114                   sec_ace->obj_flags);
115         }
116         
117         if (sec_ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED) {
118                 access_type = "ALLOWED";
119         } else if (sec_ace->type == SEC_ACE_TYPE_ACCESS_DENIED) {
120                 access_type = "DENIED";
121         } else if (sec_ace->type == SEC_ACE_TYPE_SYSTEM_AUDIT) {
122                 access_type = "SYSTEM AUDIT";
123         } else if (sec_ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT) {
124                 access_type = "ALLOWED OBJECT";
125         } else if (sec_ace->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT) {
126                 access_type = "DEINED OBJECT";
127         } else if (sec_ace->type == SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT) {
128                 access_type = "AUDIT OBJECT";
129         }
130
131         printf("access SID:  %s\naccess type: %s\n", 
132                sid_string_static(&sec_ace->trustee), access_type);
133
134         ads_disp_perms(sec_ace->info.mask);
135 }
136
137 /* display ACL */
138 void ads_disp_acl(SEC_ACL *sec_acl, char *type)
139 {
140         if (!sec_acl)
141                 printf("------- (%s) ACL not present\n", type);
142         else {
143                 printf("------- (%s) ACL (revision: %d, size: %d, number of ACEs: %d)\n", 
144                        type,
145                        sec_acl->revision,
146                        sec_acl->size,
147                        sec_acl->num_aces);                      
148         }
149 }
150
151 /* display SD */
152 void ads_disp_sd(SEC_DESC *sd)
153 {
154         int i;
155         
156         printf("-------------- Security Descriptor (revision: %d, type: 0x%02x)\n", 
157                sd->revision,
158                sd->type);
159         printf("owner SID: %s\n", sid_string_static(sd->owner_sid));
160         printf("group SID: %s\n", sid_string_static(sd->grp_sid));
161
162         ads_disp_acl(sd->sacl, "system");
163         for (i = 0; i < sd->sacl->num_aces; i ++)
164                 ads_disp_ace(&sd->sacl->ace[i]);
165         
166         ads_disp_acl(sd->dacl, "user");
167         for (i = 0; i < sd->dacl->num_aces; i ++)
168                 ads_disp_ace(&sd->dacl->ace[i]);
169
170         printf("-------------- End Of Security Descriptor\n");
171 }
172
173 #endif
174