RIP BOOL. Convert BOOL -> bool. I found a few interesting
[nivanova/samba-autobuild/.git] / source3 / libsmb / smbencrypt.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB parameters and setup
4    Copyright (C) Andrew Tridgell 1992-1998
5    Modified by Jeremy Allison 1995.
6    Copyright (C) Jeremy Allison 1995-2000.
7    Copyright (C) Luke Kennethc Casson Leighton 1996-2000.
8    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2002-2003
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "byteorder.h"
26
27 void SMBencrypt_hash(const uchar lm_hash[16], const uchar *c8, uchar p24[24])
28 {
29         uchar p21[21];
30
31         memset(p21,'\0',21);
32         memcpy(p21, lm_hash, 16);
33
34         SMBOWFencrypt(p21, c8, p24);
35
36 #ifdef DEBUG_PASSWORD
37         DEBUG(100,("SMBencrypt_hash: lm#, challenge, response\n"));
38         dump_data(100, p21, 16);
39         dump_data(100, c8, 8);
40         dump_data(100, p24, 24);
41 #endif
42 }
43
44 /*
45    This implements the X/Open SMB password encryption
46    It takes a password ('unix' string), a 8 byte "crypt key" 
47    and puts 24 bytes of encrypted password into p24 
48
49    Returns False if password must have been truncated to create LM hash
50 */
51
52 bool SMBencrypt(const char *passwd, const uchar *c8, uchar p24[24])
53 {
54         bool ret;
55         uchar lm_hash[16];
56
57         ret = E_deshash(passwd, lm_hash); 
58         SMBencrypt_hash(lm_hash, c8, p24);
59         return ret;
60 }
61
62 /**
63  * Creates the MD4 Hash of the users password in NT UNICODE.
64  * @param passwd password in 'unix' charset.
65  * @param p16 return password hashed with md4, caller allocated 16 byte buffer
66  */
67  
68 void E_md4hash(const char *passwd, uchar p16[16])
69 {
70         int len;
71         smb_ucs2_t wpwd[129];
72         
73         /* Password must be converted to NT unicode - null terminated. */
74         push_ucs2(NULL, wpwd, (const char *)passwd, 256, STR_UNICODE|STR_NOALIGN|STR_TERMINATE);
75         /* Calculate length in bytes */
76         len = strlen_w(wpwd) * sizeof(int16);
77
78         mdfour(p16, (unsigned char *)wpwd, len);
79         ZERO_STRUCT(wpwd);      
80 }
81
82 /**
83  * Creates the MD5 Hash of a combination of 16 byte salt and 16 byte NT hash.
84  * @param 16 byte salt.
85  * @param 16 byte NT hash.
86  * @param 16 byte return hashed with md5, caller allocated 16 byte buffer
87  */
88
89 void E_md5hash(const uchar salt[16], const uchar nthash[16], uchar hash_out[16])
90 {
91         struct MD5Context tctx;
92         uchar array[32];
93         
94         memset(hash_out, '\0', 16);
95         memcpy(array, salt, 16);
96         memcpy(&array[16], nthash, 16);
97         MD5Init(&tctx);
98         MD5Update(&tctx, array, 32);
99         MD5Final(hash_out, &tctx);
100 }
101
102 /**
103  * Creates the DES forward-only Hash of the users password in DOS ASCII charset
104  * @param passwd password in 'unix' charset.
105  * @param p16 return password hashed with DES, caller allocated 16 byte buffer
106  * @return False if password was > 14 characters, and therefore may be incorrect, otherwise True
107  * @note p16 is filled in regardless
108  */
109  
110 bool E_deshash(const char *passwd, uchar p16[16])
111 {
112         bool ret = True;
113         fstring dospwd; 
114         ZERO_STRUCT(dospwd);
115         
116         /* Password must be converted to DOS charset - null terminated, uppercase. */
117         push_ascii(dospwd, passwd, sizeof(dospwd), STR_UPPER|STR_TERMINATE);
118        
119         /* Only the fisrt 14 chars are considered, password need not be null terminated. */
120         E_P16((const unsigned char *)dospwd, p16);
121
122         if (strlen(dospwd) > 14) {
123                 ret = False;
124         }
125
126         ZERO_STRUCT(dospwd);    
127
128         return ret;
129 }
130
131 /**
132  * Creates the MD4 and DES (LM) Hash of the users password.  
133  * MD4 is of the NT Unicode, DES is of the DOS UPPERCASE password.
134  * @param passwd password in 'unix' charset.
135  * @param nt_p16 return password hashed with md4, caller allocated 16 byte buffer
136  * @param p16 return password hashed with des, caller allocated 16 byte buffer
137  */
138  
139 /* Does both the NT and LM owfs of a user's password */
140 void nt_lm_owf_gen(const char *pwd, uchar nt_p16[16], uchar p16[16])
141 {
142         /* Calculate the MD4 hash (NT compatible) of the password */
143         memset(nt_p16, '\0', 16);
144         E_md4hash(pwd, nt_p16);
145
146 #ifdef DEBUG_PASSWORD
147         DEBUG(100,("nt_lm_owf_gen: pwd, nt#\n"));
148         dump_data(120, (uint8 *)pwd, strlen(pwd));
149         dump_data(100, nt_p16, 16);
150 #endif
151
152         E_deshash(pwd, (uchar *)p16);
153
154 #ifdef DEBUG_PASSWORD
155         DEBUG(100,("nt_lm_owf_gen: pwd, lm#\n"));
156         dump_data(120, (uint8 *)pwd, strlen(pwd));
157         dump_data(100, p16, 16);
158 #endif
159 }
160
161 /* Does both the NTLMv2 owfs of a user's password */
162 bool ntv2_owf_gen(const uchar owf[16],
163                   const char *user_in, const char *domain_in,
164                   bool upper_case_domain, /* Transform the domain into UPPER case */
165                   uchar kr_buf[16])
166 {
167         smb_ucs2_t *user;
168         smb_ucs2_t *domain;
169         
170         size_t user_byte_len;
171         size_t domain_byte_len;
172
173         HMACMD5Context ctx;
174
175         user_byte_len = push_ucs2_allocate(&user, user_in);
176         if (user_byte_len == (size_t)-1) {
177                 DEBUG(0, ("push_uss2_allocate() for user returned -1 (probably malloc() failure)\n"));
178                 return False;
179         }
180
181         domain_byte_len = push_ucs2_allocate(&domain, domain_in);
182         if (domain_byte_len == (size_t)-1) {
183                 DEBUG(0, ("push_uss2_allocate() for domain returned -1 (probably malloc() failure)\n"));
184                 return False;
185         }
186
187         strupper_w(user);
188
189         if (upper_case_domain)
190                 strupper_w(domain);
191
192         SMB_ASSERT(user_byte_len >= 2);
193         SMB_ASSERT(domain_byte_len >= 2);
194
195         /* We don't want null termination */
196         user_byte_len = user_byte_len - 2;
197         domain_byte_len = domain_byte_len - 2;
198         
199         hmac_md5_init_limK_to_64(owf, 16, &ctx);
200         hmac_md5_update((const unsigned char *)user, user_byte_len, &ctx);
201         hmac_md5_update((const unsigned char *)domain, domain_byte_len, &ctx);
202         hmac_md5_final(kr_buf, &ctx);
203
204 #ifdef DEBUG_PASSWORD
205         DEBUG(100, ("ntv2_owf_gen: user, domain, owfkey, kr\n"));
206         dump_data(100, (uint8 *)user, user_byte_len);
207         dump_data(100, (uint8 *)domain, domain_byte_len);
208         dump_data(100, (uint8 *)owf, 16);
209         dump_data(100, (uint8 *)kr_buf, 16);
210 #endif
211
212         SAFE_FREE(user);
213         SAFE_FREE(domain);
214         return True;
215 }
216
217 /* Does the des encryption from the NT or LM MD4 hash. */
218 void SMBOWFencrypt(const uchar passwd[16], const uchar *c8, uchar p24[24])
219 {
220         uchar p21[21];
221
222         ZERO_STRUCT(p21);
223  
224         memcpy(p21, passwd, 16);    
225         E_P24(p21, c8, p24);
226 }
227
228 /* Does the des encryption from the FIRST 8 BYTES of the NT or LM MD4 hash. */
229 void NTLMSSPOWFencrypt(const uchar passwd[8], const uchar *ntlmchalresp, uchar p24[24])
230 {
231         uchar p21[21];
232  
233         memset(p21,'\0',21);
234         memcpy(p21, passwd, 8);    
235         memset(p21 + 8, 0xbd, 8);    
236
237         E_P24(p21, ntlmchalresp, p24);
238 #ifdef DEBUG_PASSWORD
239         DEBUG(100,("NTLMSSPOWFencrypt: p21, c8, p24\n"));
240         dump_data(100, p21, 21);
241         dump_data(100, ntlmchalresp, 8);
242         dump_data(100, p24, 24);
243 #endif
244 }
245
246
247 /* Does the des encryption. */
248  
249 void SMBNTencrypt_hash(const uchar nt_hash[16], uchar *c8, uchar *p24)
250 {
251         uchar p21[21];
252  
253         memset(p21,'\0',21);
254         memcpy(p21, nt_hash, 16);
255         SMBOWFencrypt(p21, c8, p24);
256
257 #ifdef DEBUG_PASSWORD
258         DEBUG(100,("SMBNTencrypt: nt#, challenge, response\n"));
259         dump_data(100, p21, 16);
260         dump_data(100, c8, 8);
261         dump_data(100, p24, 24);
262 #endif
263 }
264
265 /* Does the NT MD4 hash then des encryption. Plaintext version of the above. */
266
267 void SMBNTencrypt(const char *passwd, uchar *c8, uchar *p24)
268 {
269         uchar nt_hash[16];
270         E_md4hash(passwd, nt_hash);    
271         SMBNTencrypt_hash(nt_hash, c8, p24);
272 }
273
274 /* Does the md5 encryption from the Key Response for NTLMv2. */
275 void SMBOWFencrypt_ntv2(const uchar kr[16],
276                         const DATA_BLOB *srv_chal,
277                         const DATA_BLOB *cli_chal,
278                         uchar resp_buf[16])
279 {
280         HMACMD5Context ctx;
281
282         hmac_md5_init_limK_to_64(kr, 16, &ctx);
283         hmac_md5_update(srv_chal->data, srv_chal->length, &ctx);
284         hmac_md5_update(cli_chal->data, cli_chal->length, &ctx);
285         hmac_md5_final(resp_buf, &ctx);
286
287 #ifdef DEBUG_PASSWORD
288         DEBUG(100, ("SMBOWFencrypt_ntv2: srv_chal, cli_chal, resp_buf\n"));
289         dump_data(100, srv_chal->data, srv_chal->length);
290         dump_data(100, cli_chal->data, cli_chal->length);
291         dump_data(100, resp_buf, 16);
292 #endif
293 }
294
295 void SMBsesskeygen_ntv2(const uchar kr[16],
296                         const uchar * nt_resp, uint8 sess_key[16])
297 {
298         /* a very nice, 128 bit, variable session key */
299         
300         HMACMD5Context ctx;
301
302         hmac_md5_init_limK_to_64(kr, 16, &ctx);
303         hmac_md5_update(nt_resp, 16, &ctx);
304         hmac_md5_final((unsigned char *)sess_key, &ctx);
305
306 #ifdef DEBUG_PASSWORD
307         DEBUG(100, ("SMBsesskeygen_ntv2:\n"));
308         dump_data(100, sess_key, 16);
309 #endif
310 }
311
312 void SMBsesskeygen_ntv1(const uchar kr[16],
313                         const uchar * nt_resp, uint8 sess_key[16])
314 {
315         /* yes, this session key does not change - yes, this 
316            is a problem - but it is 128 bits */
317         
318         mdfour((unsigned char *)sess_key, kr, 16);
319
320 #ifdef DEBUG_PASSWORD
321         DEBUG(100, ("SMBsesskeygen_ntv1:\n"));
322         dump_data(100, sess_key, 16);
323 #endif
324 }
325
326 void SMBsesskeygen_lm_sess_key(const uchar lm_hash[16],
327                         const uchar lm_resp[24], /* only uses 8 */ 
328                         uint8 sess_key[16])
329 {
330         uchar p24[24];
331         uchar partial_lm_hash[16];
332         
333         memcpy(partial_lm_hash, lm_hash, 8);
334         memset(partial_lm_hash + 8, 0xbd, 8);    
335
336         SMBOWFencrypt(partial_lm_hash, lm_resp, p24);
337         
338         memcpy(sess_key, p24, 16);
339
340 #ifdef DEBUG_PASSWORD
341         DEBUG(100, ("SMBsesskeygen_lmv1_jerry:\n"));
342         dump_data(100, sess_key, 16);
343 #endif
344 }
345
346 DATA_BLOB NTLMv2_generate_names_blob(const char *hostname, 
347                                      const char *domain)
348 {
349         DATA_BLOB names_blob = data_blob_null;
350         
351         msrpc_gen(&names_blob, "aaa", 
352                   NTLMSSP_NAME_TYPE_DOMAIN, domain,
353                   NTLMSSP_NAME_TYPE_SERVER, hostname,
354                   0, "");
355         return names_blob;
356 }
357
358 static DATA_BLOB NTLMv2_generate_client_data(const DATA_BLOB *names_blob) 
359 {
360         uchar client_chal[8];
361         DATA_BLOB response = data_blob_null;
362         char long_date[8];
363
364         generate_random_buffer(client_chal, sizeof(client_chal));
365
366         put_long_date(long_date, time(NULL));
367
368         /* See http://www.ubiqx.org/cifs/SMB.html#SMB.8.5 */
369
370         msrpc_gen(&response, "ddbbdb", 
371                   0x00000101,     /* Header  */
372                   0,              /* 'Reserved'  */
373                   long_date, 8,   /* Timestamp */
374                   client_chal, 8, /* client challenge */
375                   0,              /* Unknown */
376                   names_blob->data, names_blob->length);        /* End of name list */
377
378         return response;
379 }
380
381 static DATA_BLOB NTLMv2_generate_response(const uchar ntlm_v2_hash[16],
382                                           const DATA_BLOB *server_chal,
383                                           const DATA_BLOB *names_blob)
384 {
385         uchar ntlmv2_response[16];
386         DATA_BLOB ntlmv2_client_data;
387         DATA_BLOB final_response;
388         
389         /* NTLMv2 */
390         /* generate some data to pass into the response function - including
391            the hostname and domain name of the server */
392         ntlmv2_client_data = NTLMv2_generate_client_data(names_blob);
393
394         /* Given that data, and the challenge from the server, generate a response */
395         SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &ntlmv2_client_data, ntlmv2_response);
396         
397         final_response = data_blob(NULL, sizeof(ntlmv2_response) + ntlmv2_client_data.length);
398
399         memcpy(final_response.data, ntlmv2_response, sizeof(ntlmv2_response));
400
401         memcpy(final_response.data+sizeof(ntlmv2_response), 
402                ntlmv2_client_data.data, ntlmv2_client_data.length);
403
404         data_blob_free(&ntlmv2_client_data);
405
406         return final_response;
407 }
408
409 static DATA_BLOB LMv2_generate_response(const uchar ntlm_v2_hash[16],
410                                         const DATA_BLOB *server_chal)
411 {
412         uchar lmv2_response[16];
413         DATA_BLOB lmv2_client_data = data_blob(NULL, 8);
414         DATA_BLOB final_response = data_blob(NULL, 24);
415         
416         /* LMv2 */
417         /* client-supplied random data */
418         generate_random_buffer(lmv2_client_data.data, lmv2_client_data.length); 
419
420         /* Given that data, and the challenge from the server, generate a response */
421         SMBOWFencrypt_ntv2(ntlm_v2_hash, server_chal, &lmv2_client_data, lmv2_response);
422         memcpy(final_response.data, lmv2_response, sizeof(lmv2_response));
423
424         /* after the first 16 bytes is the random data we generated above, 
425            so the server can verify us with it */
426         memcpy(final_response.data+sizeof(lmv2_response), 
427                lmv2_client_data.data, lmv2_client_data.length);
428
429         data_blob_free(&lmv2_client_data);
430
431         return final_response;
432 }
433
434 bool SMBNTLMv2encrypt_hash(const char *user, const char *domain, const uchar nt_hash[16], 
435                       const DATA_BLOB *server_chal, 
436                       const DATA_BLOB *names_blob,
437                       DATA_BLOB *lm_response, DATA_BLOB *nt_response, 
438                       DATA_BLOB *user_session_key) 
439 {
440         uchar ntlm_v2_hash[16];
441
442         /* We don't use the NT# directly.  Instead we use it mashed up with
443            the username and domain.
444            This prevents username swapping during the auth exchange
445         */
446         if (!ntv2_owf_gen(nt_hash, user, domain, True, ntlm_v2_hash)) {
447                 return False;
448         }
449         
450         if (nt_response) {
451                 *nt_response = NTLMv2_generate_response(ntlm_v2_hash, server_chal,
452                                                         names_blob); 
453                 if (user_session_key) {
454                         *user_session_key = data_blob(NULL, 16);
455                         
456                         /* The NTLMv2 calculations also provide a session key, for signing etc later */
457                         /* use only the first 16 bytes of nt_response for session key */
458                         SMBsesskeygen_ntv2(ntlm_v2_hash, nt_response->data, user_session_key->data);
459                 }
460         }
461         
462         /* LMv2 */
463         
464         if (lm_response) {
465                 *lm_response = LMv2_generate_response(ntlm_v2_hash, server_chal);
466         }
467         
468         return True;
469 }
470
471 /* Plaintext version of the above. */
472
473 bool SMBNTLMv2encrypt(const char *user, const char *domain, const char *password, 
474                       const DATA_BLOB *server_chal, 
475                       const DATA_BLOB *names_blob,
476                       DATA_BLOB *lm_response, DATA_BLOB *nt_response, 
477                       DATA_BLOB *user_session_key) 
478 {
479         uchar nt_hash[16];
480         E_md4hash(password, nt_hash);
481
482         return SMBNTLMv2encrypt_hash(user, domain, nt_hash,
483                                 server_chal,
484                                 names_blob,
485                                 lm_response, nt_response,
486                                 user_session_key);
487 }
488
489 /***********************************************************
490  encode a password buffer with a unicode password.  The buffer
491  is filled with random data to make it harder to attack.
492 ************************************************************/
493 bool encode_pw_buffer(uint8 buffer[516], const char *password, int string_flags)
494 {
495         uchar new_pw[512];
496         size_t new_pw_len;
497
498         /* the incoming buffer can be any alignment. */
499         string_flags |= STR_NOALIGN;
500
501         new_pw_len = push_string(NULL, new_pw,
502                                  password, 
503                                  sizeof(new_pw), string_flags);
504         
505         memcpy(&buffer[512 - new_pw_len], new_pw, new_pw_len);
506
507         generate_random_buffer(buffer, 512 - new_pw_len);
508
509         /* 
510          * The length of the new password is in the last 4 bytes of
511          * the data buffer.
512          */
513         SIVAL(buffer, 512, new_pw_len);
514         ZERO_STRUCT(new_pw);
515         return True;
516 }
517
518
519 /***********************************************************
520  decode a password buffer
521  *new_pw_len is the length in bytes of the possibly mulitbyte
522  returned password including termination.
523 ************************************************************/
524
525 bool decode_pw_buffer(uint8 in_buffer[516], char *new_pwrd,
526                       int new_pwrd_size, uint32 *new_pw_len,
527                       int string_flags)
528 {
529         int byte_len=0;
530
531         /* the incoming buffer can be any alignment. */
532         string_flags |= STR_NOALIGN;
533
534         /*
535           Warning !!! : This function is called from some rpc call.
536           The password IN the buffer may be a UNICODE string.
537           The password IN new_pwrd is an ASCII string
538           If you reuse that code somewhere else check first.
539         */
540
541         /* The length of the new password is in the last 4 bytes of the data buffer. */
542
543         byte_len = IVAL(in_buffer, 512);
544
545 #ifdef DEBUG_PASSWORD
546         dump_data(100, in_buffer, 516);
547 #endif
548
549         /* Password cannot be longer than the size of the password buffer */
550         if ( (byte_len < 0) || (byte_len > 512)) {
551                 DEBUG(0, ("decode_pw_buffer: incorrect password length (%d).\n", byte_len));
552                 DEBUG(0, ("decode_pw_buffer: check that 'encrypt passwords = yes'\n"));
553                 return False;
554         }
555
556         /* decode into the return buffer.  Buffer length supplied */
557         *new_pw_len = pull_string(NULL, 0, new_pwrd,
558                                   &in_buffer[512 - byte_len], new_pwrd_size,
559                                   byte_len, string_flags);
560
561 #ifdef DEBUG_PASSWORD
562         DEBUG(100,("decode_pw_buffer: new_pwrd: "));
563         dump_data(100, (uint8 *)new_pwrd, *new_pw_len);
564         DEBUG(100,("multibyte len:%d\n", *new_pw_len));
565         DEBUG(100,("original char len:%d\n", byte_len/2));
566 #endif
567         
568         return True;
569 }
570
571 /***********************************************************
572  Decode an arc4 encrypted password change buffer.
573 ************************************************************/
574
575 void encode_or_decode_arc4_passwd_buffer(unsigned char pw_buf[532], const DATA_BLOB *psession_key)
576 {
577         struct MD5Context tctx;
578         unsigned char key_out[16];
579
580         /* Confounder is last 16 bytes. */
581
582         MD5Init(&tctx);
583         MD5Update(&tctx, &pw_buf[516], 16);
584         MD5Update(&tctx, psession_key->data, psession_key->length);
585         MD5Final(key_out, &tctx);
586         /* arc4 with key_out. */
587         SamOEMhash(pw_buf, key_out, 516);
588 }
589
590 /***********************************************************
591  Encrypt/Decrypt used for LSA secrets and trusted domain
592  passwords.
593 ************************************************************/
594
595 void sess_crypt_blob(DATA_BLOB *out, const DATA_BLOB *in, const DATA_BLOB *session_key, int forward)
596 {
597         int i, k;
598
599         for (i=0,k=0;
600              i<in->length;
601              i += 8, k += 7) {
602                 uint8 bin[8], bout[8], key[7];
603
604                 memset(bin, 0, 8);
605                 memcpy(bin,  &in->data[i], MIN(8, in->length-i));
606
607                 if (k + 7 > session_key->length) {
608                         k = (session_key->length - k);
609                 }
610                 memcpy(key, &session_key->data[k], 7);
611
612                 des_crypt56(bout, bin, key, forward?1:0);
613
614                 memcpy(&out->data[i], bout, MIN(8, in->length-i));
615         }
616 }
617
618 /* Decrypts password-blob with session-key
619  * @param pass          password for session-key
620  * @param data_in       DATA_BLOB encrypted password
621  *
622  * Returns cleartext password in CH_UNIX 
623  * Caller must free the returned string
624  */
625
626 char *decrypt_trustdom_secret(const char *pass, DATA_BLOB *data_in)
627 {
628         DATA_BLOB data_out, sess_key;
629         uchar nt_hash[16];
630         uint32_t length;
631         uint32_t version;
632         fstring cleartextpwd;
633
634         if (!data_in || !pass)
635                 return NULL;
636
637         /* generate md4 password-hash derived from the NT UNICODE password */
638         E_md4hash(pass, nt_hash);
639
640         /* hashed twice with md4 */
641         mdfour(nt_hash, nt_hash, 16);
642
643         /* 16-Byte session-key */
644         sess_key = data_blob(nt_hash, 16);
645         if (sess_key.data == NULL)
646                 return NULL;
647         
648         data_out = data_blob(NULL, data_in->length);
649         if (data_out.data == NULL)
650                 return NULL;
651         
652         /* decrypt with des3 */
653         sess_crypt_blob(&data_out, data_in, &sess_key, 0);
654
655         /* 4 Byte length, 4 Byte version */
656         length  = IVAL(data_out.data, 0);
657         version = IVAL(data_out.data, 4);
658
659         if (length > data_in->length - 8) {
660                 DEBUG(0,("decrypt_trustdom_secret: invalid length (%d)\n", length));
661                 return NULL;
662         }
663         
664         if (version != 1) {
665                 DEBUG(0,("decrypt_trustdom_secret: unknown version number (%d)\n", version));
666                 return NULL;
667         }
668         
669         rpcstr_pull(cleartextpwd, data_out.data + 8, sizeof(fstring), length, 0 );
670
671 #ifdef DEBUG_PASSWORD
672         DEBUG(100,("decrypt_trustdom_secret: length is: %d, version is: %d, password is: %s\n", 
673                                 length, version, cleartextpwd));
674 #endif
675
676         data_blob_free(&data_out);
677         data_blob_free(&sess_key);
678         
679         return SMB_STRDUP(cleartextpwd);
680
681 }
682