r4096: move the samdb code to source/dsdb/
[samba.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/ndr_samr.h"
25
26 /* 
27 translated the ACB_CTRL Flags to UserFlags (userAccountControl) 
28 */ 
29 /* mapping between ADS userAccountControl and SAMR acct_flags */
30 static const struct {
31         uint32_t uf;
32         uint16_t acb;
33 } acct_flags_map[] = {
34         { UF_ACCOUNTDISABLE, ACB_DISABLED },
35         { UF_HOMEDIR_REQUIRED, ACB_HOMDIRREQ },
36         { UF_PASSWD_NOTREQD, ACB_PWNOTREQ },
37         { UF_TEMP_DUPLICATE_ACCOUNT, ACB_TEMPDUP },
38         { UF_NORMAL_ACCOUNT, ACB_NORMAL },
39         { UF_MNS_LOGON_ACCOUNT, ACB_MNS },
40         { UF_INTERDOMAIN_TRUST_ACCOUNT, ACB_DOMTRUST },
41         { UF_WORKSTATION_TRUST_ACCOUNT, ACB_WSTRUST },
42         { UF_SERVER_TRUST_ACCOUNT, ACB_SVRTRUST },
43         { UF_DONT_EXPIRE_PASSWD, ACB_PWNOEXP },
44         { UF_LOCKOUT, ACB_AUTOLOCK }
45 };
46
47 uint32_t samdb_acb2uf(uint16_t acb)
48 {
49         uint32_t i, ret = 0;
50         for (i=0;i<ARRAY_SIZE(acct_flags_map);i++) {
51                 if (acct_flags_map[i].acb & acb) {
52                         ret |= acct_flags_map[i].uf;
53                 }
54         }
55         return ret;
56 }
57
58 /*
59 translated the UserFlags (userAccountControl) to ACB_CTRL Flags
60 */
61 uint16_t samdb_uf2acb(uint32_t uf)
62 {
63         uint32_t i;
64         uint16_t ret = 0;
65         for (i=0;i<ARRAY_SIZE(acct_flags_map);i++) {
66                 if (acct_flags_map[i].uf & uf) {
67                         ret |= acct_flags_map[i].acb;
68                 }
69         }
70         return ret;
71 }
72
73 /* 
74 get the accountType from the UserFlags
75 */
76 uint32_t samdb_uf2atype(uint32_t uf)
77 {
78         uint32_t atype = 0x00000000;
79                 
80         if (uf & UF_NORMAL_ACCOUNT)                     atype = ATYPE_NORMAL_ACCOUNT;
81         else if (uf & UF_TEMP_DUPLICATE_ACCOUNT)        atype = ATYPE_NORMAL_ACCOUNT;
82         else if (uf & UF_SERVER_TRUST_ACCOUNT)          atype = ATYPE_WORKSTATION_TRUST;
83         else if (uf & UF_WORKSTATION_TRUST_ACCOUNT)     atype = ATYPE_WORKSTATION_TRUST;
84         else if (uf & UF_INTERDOMAIN_TRUST_ACCOUNT)     atype = ATYPE_INTERDOMAIN_TRUST;
85
86         return atype;
87
88
89 /* 
90 get the accountType from the groupType
91 */
92 uint32_t samdb_gtype2atype(uint32_t gtype)
93 {
94         uint32_t atype = 0x00000000;
95         
96         switch(gtype) {
97                 case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP:
98                         atype = ATYPE_SECURITY_LOCAL_GROUP;
99                         break;
100                 case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP:
101                         atype = ATYPE_SECURITY_LOCAL_GROUP;
102                         break;
103                 case GTYPE_SECURITY_GLOBAL_GROUP:
104                         atype = ATYPE_SECURITY_GLOBAL_GROUP;
105                         break;
106         
107                 case GTYPE_DISTRIBUTION_GLOBAL_GROUP:
108                         atype = ATYPE_DISTRIBUTION_GLOBAL_GROUP;
109                         break;
110                 case GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP:
111                         atype = ATYPE_DISTRIBUTION_UNIVERSAL_GROUP;
112                         break;
113                 case GTYPE_DISTRIBUTION_UNIVERSAL_GROUP:
114                         atype = ATYPE_DISTRIBUTION_LOCAL_GROUP;
115                         break;
116         }
117
118         return atype;
119 }
120
121 /* turn a sAMAccountType into a SID_NAME_USE */
122 enum samr_SidType samdb_atype_map(uint32_t atype)
123 {
124         switch (atype & 0xF0000000) {
125         case ATYPE_GLOBAL_GROUP:
126                 return SID_NAME_DOM_GRP;
127         case ATYPE_SECURITY_LOCAL_GROUP:
128                 return SID_NAME_ALIAS;
129         case ATYPE_ACCOUNT:
130                 return SID_NAME_USER;
131         default:
132                 DEBUG(1,("hmm, need to map account type 0x%x\n", atype));
133         }
134         return SID_NAME_UNKNOWN;
135 }