off by one bug in string length; CR 1159
[Samba.git] / source / libads / ads_utils.c
blob626c1779266c9a2f31b35bae8ddcf6a70f7ebd46
1 /*
2 Unix SMB/CIFS implementation.
3 ads (active directory) utility library
5 Copyright (C) Stefan (metze) Metzmacher 2002
6 Copyright (C) Andrew Tridgell 2001
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.
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.
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.
23 #include "includes.h"
25 /*
26 translated the ACB_CTRL Flags to UserFlags (userAccountControl)
27 */
28 uint32 ads_acb2uf(uint16 acb)
30 uint32 uf = 0x00000000;
32 if (acb & ACB_DISABLED) uf |= UF_ACCOUNTDISABLE;
33 if (acb & ACB_HOMDIRREQ) uf |= UF_HOMEDIR_REQUIRED;
34 if (acb & ACB_PWNOTREQ) uf |= UF_PASSWD_NOTREQD;
35 if (acb & ACB_TEMPDUP) uf |= UF_TEMP_DUPLICATE_ACCOUNT;
36 if (acb & ACB_NORMAL) uf |= UF_NORMAL_ACCOUNT;
37 if (acb & ACB_MNS) uf |= UF_MNS_LOGON_ACCOUNT;
38 if (acb & ACB_DOMTRUST) uf |= UF_INTERDOMAIN_TRUST_ACCOUNT;
39 if (acb & ACB_WSTRUST) uf |= UF_WORKSTATION_TRUST_ACCOUNT;
40 if (acb & ACB_SVRTRUST) uf |= UF_SERVER_TRUST_ACCOUNT;
41 if (acb & ACB_PWNOEXP) uf |= UF_DONT_EXPIRE_PASSWD;
42 if (acb & ACB_AUTOLOCK) uf |= UF_LOCKOUT;
44 return uf;
48 translated the UserFlags (userAccountControl) to ACB_CTRL Flags
50 uint16 ads_uf2acb(uint32 uf)
52 uint16 acb = 0x0000;
54 if (uf & UF_ACCOUNTDISABLE) acb |= ACB_DISABLED;
55 if (uf & UF_HOMEDIR_REQUIRED) acb |= ACB_HOMDIRREQ;
56 if (uf & UF_PASSWD_NOTREQD) acb |= ACB_PWNOTREQ;
57 if (uf & UF_MNS_LOGON_ACCOUNT) acb |= ACB_MNS;
58 if (uf & UF_DONT_EXPIRE_PASSWD) acb |= ACB_PWNOEXP;
59 if (uf & UF_LOCKOUT) acb |= ACB_AUTOLOCK;
61 switch (uf & UF_ACCOUNT_TYPE_MASK)
63 case UF_TEMP_DUPLICATE_ACCOUNT: acb |= ACB_TEMPDUP;break;
64 case UF_NORMAL_ACCOUNT: acb |= ACB_NORMAL;break;
65 case UF_INTERDOMAIN_TRUST_ACCOUNT: acb |= ACB_DOMTRUST;break;
66 case UF_WORKSTATION_TRUST_ACCOUNT: acb |= ACB_WSTRUST;break;
67 case UF_SERVER_TRUST_ACCOUNT: acb |= ACB_SVRTRUST;break;
68 /*Fix Me: what should we do here? */
69 default: acb |= ACB_NORMAL;break;
72 return acb;
75 /*
76 get the accountType from the UserFlags
78 uint32 ads_uf2atype(uint32 uf)
80 uint32 atype = 0x00000000;
82 if (uf & UF_NORMAL_ACCOUNT) atype = ATYPE_NORMAL_ACCOUNT;
83 else if (uf & UF_TEMP_DUPLICATE_ACCOUNT) atype = ATYPE_NORMAL_ACCOUNT;
84 else if (uf & UF_SERVER_TRUST_ACCOUNT) atype = ATYPE_WORKSTATION_TRUST;
85 else if (uf & UF_WORKSTATION_TRUST_ACCOUNT) atype = ATYPE_WORKSTATION_TRUST;
86 else if (uf & UF_INTERDOMAIN_TRUST_ACCOUNT) atype = ATYPE_INTERDOMAIN_TRUST;
88 return atype;
91 /*
92 translated the GROUP_CTRL Flags to GroupType (groupType)
93 */
94 uint32 ads_gcb2gtype(uint16 gcb)
96 uint32 gtype = 0x00000000;
98 if (gcb & GCB_ALIAS_GROUP) gtype |= GTYPE_SECURITY_BUILTIN_LOCAL_GROUP;
99 else if(gcb & GCB_LOCAL_GROUP) gtype |= GTYPE_SECURITY_DOMAIN_LOCAL_GROUP;
100 if (gcb & GCB_GLOBAL_GROUP) gtype |= GTYPE_SECURITY_GLOBAL_GROUP;
102 return gtype;
106 translated the GroupType (groupType) to GROUP_CTRL Flags
108 uint16 ads_gtype2gcb(uint32 gtype)
110 uint16 gcb = 0x0000;
112 switch(gtype) {
113 case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP:
114 gcb = GCB_ALIAS_GROUP;
115 break;
116 case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP:
117 gcb = GCB_LOCAL_GROUP;
118 break;
119 case GTYPE_SECURITY_GLOBAL_GROUP:
120 gcb = GCB_GLOBAL_GROUP;
121 break;
123 case GTYPE_DISTRIBUTION_GLOBAL_GROUP:
124 gcb = GCB_GLOBAL_GROUP;
125 break;
126 case GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP:
127 gcb = GCB_LOCAL_GROUP;
128 break;
129 case GTYPE_DISTRIBUTION_UNIVERSAL_GROUP:
130 gcb = GCB_GLOBAL_GROUP;
131 break;
134 return gcb;
138 get the accountType from the groupType
140 uint32 ads_gtype2atype(uint32 gtype)
142 uint32 atype = 0x00000000;
144 switch(gtype) {
145 case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP:
146 atype = ATYPE_SECURITY_LOCAL_GROUP;
147 break;
148 case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP:
149 atype = ATYPE_SECURITY_LOCAL_GROUP;
150 break;
151 case GTYPE_SECURITY_GLOBAL_GROUP:
152 atype = ATYPE_SECURITY_GLOBAL_GROUP;
153 break;
155 case GTYPE_DISTRIBUTION_GLOBAL_GROUP:
156 atype = ATYPE_DISTRIBUTION_GLOBAL_GROUP;
157 break;
158 case GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP:
159 atype = ATYPE_DISTRIBUTION_UNIVERSAL_GROUP;
160 break;
161 case GTYPE_DISTRIBUTION_UNIVERSAL_GROUP:
162 atype = ATYPE_DISTRIBUTION_LOCAL_GROUP;
163 break;
166 return atype;
169 /* turn a sAMAccountType into a SID_NAME_USE */
170 enum SID_NAME_USE ads_atype_map(uint32 atype)
172 switch (atype & 0xF0000000) {
173 case ATYPE_GLOBAL_GROUP:
174 return SID_NAME_DOM_GRP;
175 case ATYPE_ACCOUNT:
176 return SID_NAME_USER;
177 default:
178 DEBUG(1,("hmm, need to map account type 0x%x\n", atype));
180 return SID_NAME_UNKNOWN;