2 Unix SMB/CIFS implementation.
3 smbpasswd file format routines
5 Copyright (C) Andrew Tridgell 1992-1998
6 Modified by Jeremy Allison 1995.
7 Modified by Gerald (Jerry) Carter 2000-2001
8 Copyright (C) Tim Potter 2001
9 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 /*! \file lib/smbpasswd.c
28 The smbpasswd file is used to store encrypted passwords in a similar
29 fashion to the /etc/passwd file. The format is colon separated fields
30 with one user per line like so:
32 <username>:<uid>:<lanman hash>:<nt hash>:<acb info>:<last change time>
34 The username and uid must correspond to an entry in the /etc/passwd
35 file. The lanman and nt password hashes are 32 hex digits corresponding
36 to the 16-byte lanman and nt hashes respectively.
38 The password last change time is stored as a string of the format
39 LCD-<change time> where the change time is expressed as an
45 'U' User account (normal)
46 'M' MNS logon user account - what is this ?
47 'W' Workstation account
50 'X' No Xpiry on password
51 'I' Interdomain trust account
56 #include "librpc/gen_ndr/ndr_samr.h"
57 #include "system/iconv.h"
59 /*! Convert 32 hex characters into a 16 byte array. */
61 struct samr_Password *smbpasswd_gethexpwd(TALLOC_CTX *mem_ctx, const char *p)
64 unsigned char lonybble, hinybble;
65 const char *hexchars = "0123456789ABCDEF";
67 struct samr_Password *pwd = talloc(mem_ctx, struct samr_Password);
71 for (i = 0; i < (sizeof(pwd->hash) * 2); i += 2)
73 hinybble = toupper(p[i]);
74 lonybble = toupper(p[i + 1]);
76 p1 = strchr_m(hexchars, hinybble);
77 p2 = strchr_m(hexchars, lonybble);
84 hinybble = PTR_DIFF(p1, hexchars);
85 lonybble = PTR_DIFF(p2, hexchars);
87 pwd->hash[i / 2] = (hinybble << 4) | lonybble;
92 /*! Convert a 16-byte array into 32 hex characters. */
93 struct samr_Password *lm_hash_p = NULL;
94 struct samr_Password *nt_hash_p = NULL;
96 char *smbpasswd_sethexpwd(TALLOC_CTX *mem_ctx, struct samr_Password *pwd, uint16_t acb_info)
101 p = talloc_array(mem_ctx, char, 33);
106 for (i = 0; i < sizeof(pwd->hash); i++)
107 slprintf(&p[i*2], 3, "%02X", pwd->hash[i]);
109 if (acb_info & ACB_PWNOTREQ)
110 p = talloc_strdup(mem_ctx, "NO PASSWORDXXXXXXXXXXXXXXXXXXXXX");
112 p = talloc_strdup(mem_ctx, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
117 /*! Decode the account control bits (ACB) info from a string. */
119 uint16_t smbpasswd_decode_acb_info(const char *p)
121 uint16_t acb_info = 0;
122 BOOL finished = False;
125 * Check if the account type bits have been encoded after the
126 * NT password (in the form [NDHTUWSLXI]).
129 if (*p != '[') return 0;
131 for (p++; *p && !finished; p++)
134 case 'N': /* 'N'o password. */
135 acb_info |= ACB_PWNOTREQ;
137 case 'D': /* 'D'isabled. */
138 acb_info |= ACB_DISABLED;
140 case 'H': /* 'H'omedir required. */
141 acb_info |= ACB_HOMDIRREQ;
143 case 'T': /* 'T'emp account. */
144 acb_info |= ACB_TEMPDUP;
146 case 'U': /* 'U'ser account (normal). */
147 acb_info |= ACB_NORMAL;
149 case 'M': /* 'M'NS logon user account. What is this ? */
152 case 'W': /* 'W'orkstation account. */
153 acb_info |= ACB_WSTRUST;
155 case 'S': /* 'S'erver account. */
156 acb_info |= ACB_SVRTRUST;
158 case 'L': /* 'L'ocked account. */
159 acb_info |= ACB_AUTOLOCK;
161 case 'X': /* No 'X'piry on password */
162 acb_info |= ACB_PWNOEXP;
164 case 'I': /* 'I'nterdomain trust account. */
165 acb_info |= ACB_DOMTRUST;
183 /*! Encode account control bits (ACBs) into a string. */
185 char *smbpasswd_encode_acb_info(TALLOC_CTX *mem_ctx, uint16_t acb_info)
187 char *acct_str = talloc_array(mem_ctx, char, 35);
192 if (acb_info & ACB_PWNOTREQ ) acct_str[i++] = 'N';
193 if (acb_info & ACB_DISABLED ) acct_str[i++] = 'D';
194 if (acb_info & ACB_HOMDIRREQ) acct_str[i++] = 'H';
195 if (acb_info & ACB_TEMPDUP ) acct_str[i++] = 'T';
196 if (acb_info & ACB_NORMAL ) acct_str[i++] = 'U';
197 if (acb_info & ACB_MNS ) acct_str[i++] = 'M';
198 if (acb_info & ACB_WSTRUST ) acct_str[i++] = 'W';
199 if (acb_info & ACB_SVRTRUST ) acct_str[i++] = 'S';
200 if (acb_info & ACB_AUTOLOCK ) acct_str[i++] = 'L';
201 if (acb_info & ACB_PWNOEXP ) acct_str[i++] = 'X';
202 if (acb_info & ACB_DOMTRUST ) acct_str[i++] = 'I';
205 acct_str[i++] = '\0';