nmblookup.c: Added -A ability to do status on ip address.
[kai/samba.git] / source3 / libsmb / credentials.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    code to manipulate domain credentials
5    Copyright (C) Andrew Tridgell 1997
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24
25 /****************************************************************************
26   setup the session key. 
27 Input: 8 byte challenge block
28        8 byte server challenge block
29       16 byte md4 encrypted password
30 Output:
31       8 byte session key
32 ****************************************************************************/
33 void cred_session_key(char *challenge, char *srv_challenge, char *pass, 
34                        char *session_key)
35 {
36         uint32 sum[2];
37         char sum2[8];
38         char buf[8];
39
40         sum[0] = IVAL(challenge, 0) + IVAL(srv_challenge, 0);
41         sum[1] = IVAL(challenge, 4) + IVAL(srv_challenge, 4);
42
43         SIVAL(sum2,0,sum[0]);
44         SIVAL(sum2,4,sum[1]);
45
46         E1(pass,sum2,buf);
47         E1(pass+9,buf,session_key);
48 }
49
50
51 /****************************************************************************
52 create a credential
53
54 Input:
55       8 byte sesssion key
56       8 byte stored credential
57       4 byte timestamp
58
59 Output:
60       8 byte credential
61 ****************************************************************************/
62 void cred_create(char *session_key, char *stored_cred, UTIME timestamp, 
63                  char *cred)
64 {
65         char key2[7];
66         char buf[8];
67         char timecred[8];
68
69         memcpy(timecred, stored_cred, 8);
70         SIVAL(timecred, 0, IVAL(stored_cred, 0) + timestamp.time);
71
72         E1(session_key, timecred, buf);
73         memset(key2, 0, 7);
74         key2[0] = session_key[7];
75         E1(key2, buf, cred);
76 }
77
78
79 /****************************************************************************
80   check a supplied credential
81
82 Input:
83       8 byte received credential
84       8 byte sesssion key
85       8 byte stored credential
86       4 byte timestamp
87
88 Output:
89       returns 1 if computed credential matches received credential
90       returns 0 otherwise
91 ****************************************************************************/
92 int cred_assert(char *cred, char *session_key, char *stored_cred,
93                 NTTIME timestamp)
94 {
95         char cred2[8];
96
97         cred_create(session_key, stored_cred, timestamp, cred2);
98
99         return memcmp(cred, cred2, 8) == 0;
100 }
101