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