s4:libds/common/flags.h - fix typo
[samba.git] / libds / common / flag_mapping.c
1 /*
2    Unix SMB/CIFS implementation.
3    helper mapping functions for the UF and ACB flags
4
5    Copyright (C) Stefan (metze) Metzmacher 2002
6    Copyright (C) Andrew Tridgell 2004
7    Copyright (C) Matthias Dieter Wallnöfer 2010
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "librpc/gen_ndr/samr.h"
25 #include "../libds/common/flags.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         { UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED, ACB_ENC_TXT_PWD_ALLOWED },
47         { UF_SMARTCARD_REQUIRED, ACB_SMARTCARD_REQUIRED },
48         { UF_TRUSTED_FOR_DELEGATION, ACB_TRUSTED_FOR_DELEGATION },
49         { UF_NOT_DELEGATED, ACB_NOT_DELEGATED },
50         { UF_USE_DES_KEY_ONLY, ACB_USE_DES_KEY_ONLY},
51         { UF_DONT_REQUIRE_PREAUTH, ACB_DONT_REQUIRE_PREAUTH },
52         { UF_PASSWORD_EXPIRED, ACB_PW_EXPIRED },
53         { UF_NO_AUTH_DATA_REQUIRED, ACB_NO_AUTH_DATA_REQD },
54         { UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION, ACB_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION },
55         { UF_PARTIAL_SECRETS_ACCOUNT, ACB_PARTIAL_SECRETS_ACCOUNT },
56         { UF_USE_AES_KEYS, ACB_USE_AES_KEYS }
57 };
58
59 uint32_t ds_acb2uf(uint32_t acb)
60 {
61         unsigned int i;
62         uint32_t ret = 0;
63         for (i=0;i<ARRAY_SIZE(acct_flags_map);i++) {
64                 if (acct_flags_map[i].acb & acb) {
65                         ret |= acct_flags_map[i].uf;
66                 }
67         }
68         return ret;
69 }
70
71 /*
72 translated the UserFlags (userAccountControl) to ACB_CTRL Flags
73 */
74 uint32_t ds_uf2acb(uint32_t uf)
75 {
76         unsigned int i;
77         uint32_t ret = 0;
78         for (i=0;i<ARRAY_SIZE(acct_flags_map);i++) {
79                 if (acct_flags_map[i].uf & uf) {
80                         ret |= acct_flags_map[i].acb;
81                 }
82         }
83         return ret;
84 }
85
86 /*
87 get the accountType from the UserFlags
88 */
89 uint32_t ds_uf2atype(uint32_t uf)
90 {
91         uint32_t atype = 0x00000000;
92
93         if (uf & UF_NORMAL_ACCOUNT)                     atype = ATYPE_NORMAL_ACCOUNT;
94         else if (uf & UF_TEMP_DUPLICATE_ACCOUNT)        atype = ATYPE_NORMAL_ACCOUNT;
95         else if (uf & UF_SERVER_TRUST_ACCOUNT)          atype = ATYPE_WORKSTATION_TRUST;
96         else if (uf & UF_WORKSTATION_TRUST_ACCOUNT)     atype = ATYPE_WORKSTATION_TRUST;
97         else if (uf & UF_INTERDOMAIN_TRUST_ACCOUNT)     atype = ATYPE_INTERDOMAIN_TRUST;
98
99         return atype;
100 }
101
102 /*
103 get the accountType from the groupType
104 */
105 uint32_t ds_gtype2atype(uint32_t gtype)
106 {
107         uint32_t atype = 0x00000000;
108
109         switch(gtype) {
110                 case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP:
111                         atype = ATYPE_SECURITY_LOCAL_GROUP;
112                         break;
113                 case GTYPE_SECURITY_GLOBAL_GROUP:
114                         atype = ATYPE_SECURITY_GLOBAL_GROUP;
115                         break;
116                 case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP:
117                         atype = ATYPE_SECURITY_LOCAL_GROUP;
118                         break;
119                 case GTYPE_SECURITY_UNIVERSAL_GROUP:
120                         atype = ATYPE_SECURITY_UNIVERSAL_GROUP;
121                         break;
122
123                 case GTYPE_DISTRIBUTION_GLOBAL_GROUP:
124                         atype = ATYPE_DISTRIBUTION_GLOBAL_GROUP;
125                         break;
126                 case GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP:
127                         atype = ATYPE_DISTRIBUTION_LOCAL_GROUP;
128                         break;
129                 case GTYPE_DISTRIBUTION_UNIVERSAL_GROUP:
130                         atype = ATYPE_DISTRIBUTION_UNIVERSAL_GROUP;
131                         break;
132         }
133
134         return atype;
135 }
136
137 /* turn a sAMAccountType into a SID_NAME_USE */
138 enum lsa_SidType ds_atype_map(uint32_t atype)
139 {
140         switch (atype & 0xF0000000) {
141         case ATYPE_GLOBAL_GROUP:
142                 return SID_NAME_DOM_GRP;
143         case ATYPE_SECURITY_LOCAL_GROUP:
144                 return SID_NAME_ALIAS;
145         case ATYPE_ACCOUNT:
146                 return SID_NAME_USER;
147         default:
148                 DEBUG(1,("hmm, need to map account type 0x%x\n", atype));
149         }
150         return SID_NAME_UNKNOWN;
151 }
152
153 /* get the default primary group RID for a given userAccountControl
154  * (informations according to MS-SAMR 3.1.1.8.1) */
155 uint32_t ds_uf2prim_group_rid(uint32_t uf)
156 {
157         uint32_t prim_group_rid = DOMAIN_RID_USERS;
158
159         if ((uf & UF_PARTIAL_SECRETS_ACCOUNT)
160          && (uf & UF_WORKSTATION_TRUST_ACCOUNT))    prim_group_rid = DOMAIN_RID_READONLY_DCS;
161         else if (uf & UF_SERVER_TRUST_ACCOUNT)      prim_group_rid = DOMAIN_RID_DCS;
162         else if (uf & UF_WORKSTATION_TRUST_ACCOUNT) prim_group_rid = DOMAIN_RID_DOMAIN_MEMBERS;
163
164         return prim_group_rid;
165 }