r13945: Move display_sec.c to lib/ (as suggested by Volker).
[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 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 #include "rpcclient.h"
24
25 /****************************************************************************
26 convert a security permissions into a string
27 ****************************************************************************/
28 char *get_sec_mask_str(uint32 type)
29 {
30         static fstring typestr="";
31
32         typestr[0] = 0;
33
34         if (type & GENERIC_ALL_ACCESS)
35                 fstrcat(typestr, "Generic all access ");
36         if (type & GENERIC_EXECUTE_ACCESS)
37                 fstrcat(typestr, "Generic execute access ");
38         if (type & GENERIC_WRITE_ACCESS)
39                 fstrcat(typestr, "Generic write access ");
40         if (type & GENERIC_READ_ACCESS)
41                 fstrcat(typestr, "Generic read access ");
42         if (type & MAXIMUM_ALLOWED_ACCESS)
43                 fstrcat(typestr, "MAXIMUM_ALLOWED_ACCESS ");
44         if (type & SYSTEM_SECURITY_ACCESS)
45                 fstrcat(typestr, "SYSTEM_SECURITY_ACCESS ");
46         if (type & SYNCHRONIZE_ACCESS)
47                 fstrcat(typestr, "SYNCHRONIZE_ACCESS ");
48         if (type & WRITE_OWNER_ACCESS)
49                 fstrcat(typestr, "WRITE_OWNER_ACCESS ");
50         if (type & WRITE_DAC_ACCESS)
51                 fstrcat(typestr, "WRITE_DAC_ACCESS ");
52         if (type & READ_CONTROL_ACCESS)
53                 fstrcat(typestr, "READ_CONTROL_ACCESS ");
54         if (type & DELETE_ACCESS)
55                 fstrcat(typestr, "DELETE_ACCESS ");
56
57         printf("\t\tSpecific bits: 0x%lx\n", (unsigned long)type&SPECIFIC_RIGHTS_MASK);
58
59         return typestr;
60 }
61
62 /****************************************************************************
63  display sec_access structure
64  ****************************************************************************/
65 void display_sec_access(SEC_ACCESS *info)
66 {
67         printf("\t\tPermissions: 0x%x: %s\n", info->mask, get_sec_mask_str(info->mask));
68 }
69
70 /****************************************************************************
71  display sec_ace structure
72  ****************************************************************************/
73 void display_sec_ace(SEC_ACE *ace)
74 {
75         fstring sid_str;
76
77         printf("\tACE\n\t\ttype: ");
78         switch (ace->type) {
79                 case SEC_ACE_TYPE_ACCESS_ALLOWED:
80                         printf("ACCESS ALLOWED");
81                         break;
82                 case SEC_ACE_TYPE_ACCESS_DENIED:
83                         printf("ACCESS DENIED");
84                         break;
85                 case SEC_ACE_TYPE_SYSTEM_AUDIT:
86                         printf("SYSTEM AUDIT");
87                         break;
88                 case SEC_ACE_TYPE_SYSTEM_ALARM:
89                         printf("SYSTEM ALARM");
90                         break;
91                 default:
92                         printf("????");
93                         break;
94         }
95         printf(" (%d) flags: %d\n", ace->type, ace->flags);
96         display_sec_access(&ace->info);
97         sid_to_string(sid_str, &ace->trustee);
98         printf("\t\tSID: %s\n\n", sid_str);
99 }
100
101 /****************************************************************************
102  display sec_acl structure
103  ****************************************************************************/
104 void display_sec_acl(SEC_ACL *sec_acl)
105 {
106         int i;
107
108         printf("\tACL\tNum ACEs:\t%d\trevision:\t%x\n",
109                          sec_acl->num_aces, sec_acl->revision); 
110         printf("\t---\n");
111
112         if (sec_acl->size != 0 && sec_acl->num_aces != 0)
113                 for (i = 0; i < sec_acl->num_aces; i++)
114                         display_sec_ace(&sec_acl->ace[i]);
115                                 
116 }
117
118 void display_acl_type(uint16 type)
119 {
120         static fstring typestr="";
121
122         typestr[0] = 0;
123
124         if (type & SEC_DESC_OWNER_DEFAULTED)    /* 0x0001 */
125                 fstrcat(typestr, "SEC_DESC_OWNER_DEFAULTED ");
126         if (type & SEC_DESC_GROUP_DEFAULTED)    /* 0x0002 */
127                 fstrcat(typestr, "SEC_DESC_GROUP_DEFAULTED ");
128         if (type & SEC_DESC_DACL_PRESENT)       /* 0x0004 */
129                 fstrcat(typestr, "SEC_DESC_DACL_PRESENT ");
130         if (type & SEC_DESC_DACL_DEFAULTED)     /* 0x0008 */
131                 fstrcat(typestr, "SEC_DESC_DACL_DEFAULTED ");
132         if (type & SEC_DESC_SACL_PRESENT)       /* 0x0010 */
133                 fstrcat(typestr, "SEC_DESC_SACL_PRESENT ");
134         if (type & SEC_DESC_SACL_DEFAULTED)     /* 0x0020 */
135                 fstrcat(typestr, "SEC_DESC_SACL_DEFAULTED ");
136         if (type & SEC_DESC_DACL_TRUSTED)       /* 0x0040 */
137                 fstrcat(typestr, "SEC_DESC_DACL_TRUSTED ");
138         if (type & SEC_DESC_SERVER_SECURITY)    /* 0x0080 */
139                 fstrcat(typestr, "SEC_DESC_SERVER_SECURITY ");
140         if (type & 0x0100) fstrcat(typestr, "0x0100 ");
141         if (type & 0x0200) fstrcat(typestr, "0x0200 ");
142         if (type & 0x0400) fstrcat(typestr, "0x0400 ");
143         if (type & 0x0800) fstrcat(typestr, "0x0800 ");
144         if (type & 0x1000) fstrcat(typestr, "0x1000 ");
145         if (type & 0x2000) fstrcat(typestr, "0x2000 ");
146         if (type & 0x4000) fstrcat(typestr, "0x4000 ");
147         if (type & SEC_DESC_SELF_RELATIVE)      /* 0x8000 */
148                 fstrcat(typestr, "SEC_DESC_SELF_RELATIVE ");
149         
150         printf("type: 0x%04x: %s\n", type, typestr);
151 }
152
153 /****************************************************************************
154  display sec_desc structure
155  ****************************************************************************/
156 void display_sec_desc(SEC_DESC *sec)
157 {
158         fstring sid_str;
159
160         if (!sec) {
161                 printf("NULL\n");
162                 return;
163         }
164
165         printf("revision: %d\n", sec->revision);
166         display_acl_type(sec->type);
167
168         if (sec->sacl) {
169                 printf("SACL\n");
170                 display_sec_acl(sec->sacl);
171         }
172
173         if (sec->dacl) {
174                 printf("DACL\n");
175                 display_sec_acl(sec->dacl);
176         }
177
178         if (sec->owner_sid) {
179                 sid_to_string(sid_str, sec->owner_sid);
180                 printf("\tOwner SID:\t%s\n", sid_str);
181         }
182
183         if (sec->grp_sid) {
184                 sid_to_string(sid_str, sec->grp_sid);
185                 printf("\tParent SID:\t%s\n", sid_str);
186         }
187 }