487ac8f4a0babe4659f96111c4f9f498186f2290
[samba.git] / source3 / lib / display_sec.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright (C) Andrew Tridgell 1992-1999
5    Copyright (C) Luke Kenneth Casson Leighton 1996 - 1999
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
23 /****************************************************************************
24 convert a security permissions into a string
25 ****************************************************************************/
26 char *get_sec_mask_str(uint32 type)
27 {
28         static fstring typestr="";
29
30         typestr[0] = 0;
31
32         if (type & GENERIC_ALL_ACCESS)
33                 fstrcat(typestr, "Generic all access ");
34         if (type & GENERIC_EXECUTE_ACCESS)
35                 fstrcat(typestr, "Generic execute access ");
36         if (type & GENERIC_WRITE_ACCESS)
37                 fstrcat(typestr, "Generic write access ");
38         if (type & GENERIC_READ_ACCESS)
39                 fstrcat(typestr, "Generic read access ");
40         if (type & MAXIMUM_ALLOWED_ACCESS)
41                 fstrcat(typestr, "MAXIMUM_ALLOWED_ACCESS ");
42         if (type & SYSTEM_SECURITY_ACCESS)
43                 fstrcat(typestr, "SYSTEM_SECURITY_ACCESS ");
44         if (type & SYNCHRONIZE_ACCESS)
45                 fstrcat(typestr, "SYNCHRONIZE_ACCESS ");
46         if (type & WRITE_OWNER_ACCESS)
47                 fstrcat(typestr, "WRITE_OWNER_ACCESS ");
48         if (type & WRITE_DAC_ACCESS)
49                 fstrcat(typestr, "WRITE_DAC_ACCESS ");
50         if (type & READ_CONTROL_ACCESS)
51                 fstrcat(typestr, "READ_CONTROL_ACCESS ");
52         if (type & DELETE_ACCESS)
53                 fstrcat(typestr, "DELETE_ACCESS ");
54
55         printf("\t\tSpecific bits: 0x%lx\n", (unsigned long)type&SPECIFIC_RIGHTS_MASK);
56
57         return typestr;
58 }
59
60 /****************************************************************************
61  display sec_access structure
62  ****************************************************************************/
63 void display_sec_access(SEC_ACCESS *info)
64 {
65         printf("\t\tPermissions: 0x%x: %s\n", *info, get_sec_mask_str(*info));
66 }
67
68 /****************************************************************************
69  display sec_ace object
70  ****************************************************************************/
71 static void disp_sec_ace_object(struct security_ace_object *object)
72 {
73         if (object->flags & SEC_ACE_OBJECT_PRESENT) {
74                 printf("Object type: SEC_ACE_OBJECT_PRESENT\n");
75                 printf("Object GUID: %s\n", smb_uuid_string_static(
76                         object->type.type));
77         }
78         if (object->flags & SEC_ACE_OBJECT_INHERITED_PRESENT) {
79                 printf("Object type: SEC_ACE_OBJECT_INHERITED_PRESENT\n");
80                 printf("Object GUID: %s\n", smb_uuid_string_static(
81                         object->inherited_type.inherited_type));
82         }
83 }
84
85 /****************************************************************************
86  display sec_ace structure
87  ****************************************************************************/
88 void display_sec_ace(SEC_ACE *ace)
89 {
90         fstring sid_str;
91
92         printf("\tACE\n\t\ttype: ");
93         switch (ace->type) {
94                 case SEC_ACE_TYPE_ACCESS_ALLOWED:
95                         printf("ACCESS ALLOWED");
96                         break;
97                 case SEC_ACE_TYPE_ACCESS_DENIED:
98                         printf("ACCESS DENIED");
99                         break;
100                 case SEC_ACE_TYPE_SYSTEM_AUDIT:
101                         printf("SYSTEM AUDIT");
102                         break;
103                 case SEC_ACE_TYPE_SYSTEM_ALARM:
104                         printf("SYSTEM ALARM");
105                         break;
106                 case SEC_ACE_TYPE_ALLOWED_COMPOUND:
107                         printf("SEC_ACE_TYPE_ALLOWED_COMPOUND");
108                         break;
109                 case SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT:
110                         printf("SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT");
111                         break;
112                 case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
113                         printf("SEC_ACE_TYPE_ACCESS_DENIED_OBJECT");
114                         break;
115                 case SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT:
116                         printf("SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT");
117                         break;
118                 case SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT:
119                         printf("SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT");
120                         break;
121                 default:
122                         printf("????");
123                         break;
124         }
125
126         printf(" (%d) flags: %d\n", ace->type, ace->flags);
127         display_sec_access(&ace->access_mask);
128         sid_to_string(sid_str, &ace->trustee);
129         printf("\t\tSID: %s\n\n", sid_str);
130
131         if (sec_ace_object(ace->type)) {
132                 disp_sec_ace_object(&ace->object.object);
133         }
134
135 }
136
137 /****************************************************************************
138  display sec_acl structure
139  ****************************************************************************/
140 void display_sec_acl(SEC_ACL *sec_acl)
141 {
142         int i;
143
144         printf("\tACL\tNum ACEs:\t%d\trevision:\t%x\n",
145                          sec_acl->num_aces, sec_acl->revision); 
146         printf("\t---\n");
147
148         if (sec_acl->size != 0 && sec_acl->num_aces != 0)
149                 for (i = 0; i < sec_acl->num_aces; i++)
150                         display_sec_ace(&sec_acl->aces[i]);
151 }
152
153 void display_acl_type(uint16 type)
154 {
155         static fstring typestr="";
156
157         typestr[0] = 0;
158
159         if (type & SEC_DESC_OWNER_DEFAULTED)    /* 0x0001 */
160                 fstrcat(typestr, "SEC_DESC_OWNER_DEFAULTED ");
161         if (type & SEC_DESC_GROUP_DEFAULTED)    /* 0x0002 */
162                 fstrcat(typestr, "SEC_DESC_GROUP_DEFAULTED ");
163         if (type & SEC_DESC_DACL_PRESENT)       /* 0x0004 */
164                 fstrcat(typestr, "SEC_DESC_DACL_PRESENT ");
165         if (type & SEC_DESC_DACL_DEFAULTED)     /* 0x0008 */
166                 fstrcat(typestr, "SEC_DESC_DACL_DEFAULTED ");
167         if (type & SEC_DESC_SACL_PRESENT)       /* 0x0010 */
168                 fstrcat(typestr, "SEC_DESC_SACL_PRESENT ");
169         if (type & SEC_DESC_SACL_DEFAULTED)     /* 0x0020 */
170                 fstrcat(typestr, "SEC_DESC_SACL_DEFAULTED ");
171         if (type & SEC_DESC_DACL_TRUSTED)       /* 0x0040 */
172                 fstrcat(typestr, "SEC_DESC_DACL_TRUSTED ");
173         if (type & SEC_DESC_SERVER_SECURITY)    /* 0x0080 */
174                 fstrcat(typestr, "SEC_DESC_SERVER_SECURITY ");
175         if (type & SEC_DESC_DACL_AUTO_INHERIT_REQ) /* 0x0100 */
176                 fstrcat(typestr, "SEC_DESC_DACL_AUTO_INHERIT_REQ ");
177         if (type & SEC_DESC_SACL_AUTO_INHERIT_REQ) /* 0x0200 */
178                 fstrcat(typestr, "SEC_DESC_SACL_AUTO_INHERIT_REQ ");
179         if (type & SEC_DESC_DACL_AUTO_INHERITED) /* 0x0400 */
180                 fstrcat(typestr, "SEC_DESC_DACL_AUTO_INHERITED ");
181         if (type & SEC_DESC_SACL_AUTO_INHERITED) /* 0x0800 */
182                 fstrcat(typestr, "SEC_DESC_SACL_AUTO_INHERITED ");
183         if (type & SEC_DESC_DACL_PROTECTED)     /* 0x1000 */
184                 fstrcat(typestr, "SEC_DESC_DACL_PROTECTED ");
185         if (type & SEC_DESC_SACL_PROTECTED)     /* 0x2000 */
186                 fstrcat(typestr, "SEC_DESC_SACL_PROTECTED ");
187         if (type & SEC_DESC_RM_CONTROL_VALID)   /* 0x4000 */
188                 fstrcat(typestr, "SEC_DESC_RM_CONTROL_VALID ");
189         if (type & SEC_DESC_SELF_RELATIVE)      /* 0x8000 */
190                 fstrcat(typestr, "SEC_DESC_SELF_RELATIVE ");
191         
192         printf("type: 0x%04x: %s\n", type, typestr);
193 }
194
195 /****************************************************************************
196  display sec_desc structure
197  ****************************************************************************/
198 void display_sec_desc(SEC_DESC *sec)
199 {
200         fstring sid_str;
201
202         if (!sec) {
203                 printf("NULL\n");
204                 return;
205         }
206
207         printf("revision: %d\n", sec->revision);
208         display_acl_type(sec->type);
209
210         if (sec->sacl) {
211                 printf("SACL\n");
212                 display_sec_acl(sec->sacl);
213         }
214
215         if (sec->dacl) {
216                 printf("DACL\n");
217                 display_sec_acl(sec->dacl);
218         }
219
220         if (sec->owner_sid) {
221                 sid_to_string(sid_str, sec->owner_sid);
222                 printf("\tOwner SID:\t%s\n", sid_str);
223         }
224
225         if (sec->group_sid) {
226                 sid_to_string(sid_str, sec->group_sid);
227                 printf("\tGroup SID:\t%s\n", sid_str);
228         }
229 }