r14464: Don't include ndr_BASENAME.h files unless strictly required, instead
[garming/samba-autobuild/.git] / source4 / dsdb / common / flag_mapping.c
1 /* 
2    Unix SMB/CIFS implementation.
3    helper mapping functions for the SAMDB server
4    
5    Copyright (C) Stefan (metze) Metzmacher 2002
6    Copyright (C) Andrew Tridgell 2004
7   
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "librpc/gen_ndr/samr.h"
25 #include "ads.h"
26
27 /* 
28 translated the ACB_CTRL Flags to UserFlags (userAccountControl) 
29 */ 
30 /* mapping between ADS userAccountControl and SAMR acct_flags */
31 static const struct {
32         uint32_t uf;
33         uint32_t acb;
34 } acct_flags_map[] = {
35         { UF_ACCOUNTDISABLE, ACB_DISABLED },
36         { UF_HOMEDIR_REQUIRED, ACB_HOMDIRREQ },
37         { UF_PASSWD_NOTREQD, ACB_PWNOTREQ },
38         { UF_TEMP_DUPLICATE_ACCOUNT, ACB_TEMPDUP },
39         { UF_NORMAL_ACCOUNT, ACB_NORMAL },
40         { UF_MNS_LOGON_ACCOUNT, ACB_MNS },
41         { UF_INTERDOMAIN_TRUST_ACCOUNT, ACB_DOMTRUST },
42         { UF_WORKSTATION_TRUST_ACCOUNT, ACB_WSTRUST },
43         { UF_SERVER_TRUST_ACCOUNT, ACB_SVRTRUST },
44         { UF_DONT_EXPIRE_PASSWD, ACB_PWNOEXP },
45         { UF_LOCKOUT, ACB_AUTOLOCK }
46 };
47
48 uint32_t samdb_acb2uf(uint32_t acb)
49 {
50         uint32_t i, ret = 0;
51         for (i=0;i<ARRAY_SIZE(acct_flags_map);i++) {
52                 if (acct_flags_map[i].acb & acb) {
53                         ret |= acct_flags_map[i].uf;
54                 }
55         }
56         return ret;
57 }
58
59 /*
60 translated the UserFlags (userAccountControl) to ACB_CTRL Flags
61 */
62 uint32_t samdb_uf2acb(uint32_t uf)
63 {
64         uint32_t i;
65         uint32_t ret = 0;
66         for (i=0;i<ARRAY_SIZE(acct_flags_map);i++) {
67                 if (acct_flags_map[i].uf & uf) {
68                         ret |= acct_flags_map[i].acb;
69                 }
70         }
71         return ret;
72 }
73
74 /* 
75 get the accountType from the UserFlags
76 */
77 uint32_t samdb_uf2atype(uint32_t uf)
78 {
79         uint32_t atype = 0x00000000;
80                 
81         if (uf & UF_NORMAL_ACCOUNT)                     atype = ATYPE_NORMAL_ACCOUNT;
82         else if (uf & UF_TEMP_DUPLICATE_ACCOUNT)        atype = ATYPE_NORMAL_ACCOUNT;
83         else if (uf & UF_SERVER_TRUST_ACCOUNT)          atype = ATYPE_WORKSTATION_TRUST;
84         else if (uf & UF_WORKSTATION_TRUST_ACCOUNT)     atype = ATYPE_WORKSTATION_TRUST;
85         else if (uf & UF_INTERDOMAIN_TRUST_ACCOUNT)     atype = ATYPE_INTERDOMAIN_TRUST;
86
87         return atype;
88
89
90 /* 
91 get the accountType from the groupType
92 */
93 uint32_t samdb_gtype2atype(uint32_t gtype)
94 {
95         uint32_t atype = 0x00000000;
96         
97         switch(gtype) {
98                 case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP:
99                         atype = ATYPE_SECURITY_LOCAL_GROUP;
100                         break;
101                 case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP:
102                         atype = ATYPE_SECURITY_LOCAL_GROUP;
103                         break;
104                 case GTYPE_SECURITY_GLOBAL_GROUP:
105                         atype = ATYPE_SECURITY_GLOBAL_GROUP;
106                         break;
107         
108                 case GTYPE_DISTRIBUTION_GLOBAL_GROUP:
109                         atype = ATYPE_DISTRIBUTION_GLOBAL_GROUP;
110                         break;
111                 case GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP:
112                         atype = ATYPE_DISTRIBUTION_UNIVERSAL_GROUP;
113                         break;
114                 case GTYPE_DISTRIBUTION_UNIVERSAL_GROUP:
115                         atype = ATYPE_DISTRIBUTION_LOCAL_GROUP;
116                         break;
117         }
118
119         return atype;
120 }
121
122 /* turn a sAMAccountType into a SID_NAME_USE */
123 enum lsa_SidType samdb_atype_map(uint32_t atype)
124 {
125         switch (atype & 0xF0000000) {
126         case ATYPE_GLOBAL_GROUP:
127                 return SID_NAME_DOM_GRP;
128         case ATYPE_SECURITY_LOCAL_GROUP:
129                 return SID_NAME_ALIAS;
130         case ATYPE_ACCOUNT:
131                 return SID_NAME_USER;
132         default:
133                 DEBUG(1,("hmm, need to map account type 0x%x\n", atype));
134         }
135         return SID_NAME_UNKNOWN;
136 }