Permit EAP-Message and State from the home server, so that
[freeradius.git] / src / modules / rlm_smb / smbencrypt.c
1 /*
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    SMB parameters and setup
5    Copyright (C) Andrew Tridgell 1992-1997
6    Modified by Jeremy Allison 1995.
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 "smblib-priv.h"
24 #define uchar unsigned char
25 extern int DEBUGLEVEL;
26
27 #include <string.h>
28
29 #include <string.h>
30 #ifdef HAVE_SYS_VFS_H
31 #include <sys/vfs.h>
32 #endif
33
34 #include "byteorder.h"
35
36 char *StrnCpy(char *dest,char *src,int n);
37 void strupper(char *s);
38
39 /*
40    This implements the X/Open SMB password encryption
41    It takes a password, a 8 byte "crypt key" and puts 24 bytes of
42    encrypted password into p24 */
43 void SMBencrypt(uchar *passwd, uchar *c8, uchar *p24)
44 {
45   uchar p14[15], p21[21];
46
47   memset(p21,'\0',21);
48   memset(p14,'\0',14);
49   StrnCpy((char *)p14,(char *)passwd,14);
50
51   strupper((char *)p14);
52   E_P16(p14, p21);
53   E_P24(p21, c8, p24);
54 }
55
56 /* Routines for Windows NT MD4 Hash functions. */
57 static int _my_wcslen(int16 *str)
58 {
59         int len = 0;
60         while(*str++ != 0)
61                 len++;
62         return len;
63 }
64
65 /*
66  * Convert a string into an NT UNICODE string.
67  * Note that regardless of processor type
68  * this must be in intel (little-endian)
69  * format.
70  */
71
72 static int _my_mbstowcs(int16 *dst, uchar *src, int len)
73 {
74         int i;
75         int16 val;
76
77         for(i = 0; i < len; i++) {
78                 val = *src;
79                 SSVAL(dst,0,val);
80                 dst++;
81                 src++;
82                 if(val == 0)
83                         break;
84         }
85         return i;
86 }
87
88 /*
89  * Creates the MD4 Hash of the users password in NT UNICODE.
90  */
91
92 void E_md4hash(uchar *passwd, uchar *p16)
93 {
94         int len;
95         int16 wpwd[129];
96
97         /* Password cannot be longer than 128 characters */
98         len = strlen((char *)passwd);
99         if(len > 128)
100                 len = 128;
101         /* Password must be converted to NT unicode */
102         _my_mbstowcs(wpwd, passwd, len);
103         wpwd[len] = 0; /* Ensure string is null terminated */
104         /* Calculate length in bytes */
105         len = _my_wcslen(wpwd) * sizeof(int16);
106
107         mdfour(p16, (unsigned char *)wpwd, len);
108 }
109
110 /* Does the NT MD4 hash then des encryption. */
111
112 void SMBNTencrypt(uchar *passwd, uchar *c8, uchar *p24)
113 {
114         uchar p21[21];
115
116         memset(p21,'\0',21);
117
118         E_md4hash(passwd, p21);
119         E_P24(p21, c8, p24);
120 }
121
122 /* Does both the NT and LM owfs of a user's password */
123
124 void nt_lm_owf_gen(char *pwd, char *nt_p16, char *p16)
125 {
126         char passwd[130];
127         StrnCpy(passwd, pwd, sizeof(passwd)-1);
128
129         /* Calculate the MD4 hash (NT compatible) of the password */
130         memset(nt_p16, '\0', 16);
131         E_md4hash((uchar *)passwd, (uchar *)nt_p16);
132
133         /* Mangle the passwords into Lanman format */
134         passwd[14] = '\0';
135         strupper(passwd);
136
137         /* Calculate the SMB (lanman) hash functions of the password */
138
139         memset(p16, '\0', 16);
140         E_P16((uchar *) passwd, (uchar *)p16);
141
142         /* clear out local copy of user's password (just being paranoid). */
143         bzero(passwd, sizeof(passwd));
144 }
145
146 /****************************************************************************
147 line strncpy but always null terminates. Make sure there is room!
148 ****************************************************************************/
149 char *StrnCpy(char *dest,char *src,int n)
150 {
151   char *d = dest;
152   if (!dest) return(NULL);
153   if (!src) {
154     *dest = 0;
155     return(dest);
156   }
157   while (n-- && (*d++ = *src++)) ;
158   *d = 0;
159   return(dest);
160 }
161
162 void strupper(char *s)
163 {
164   while (*s)
165   {
166     /*
167 #if !defined(KANJI_WIN95_COMPATIBILITY)
168     if(lp_client_code_page() == KANJI_CODEPAGE)
169     {
170
171       if (is_shift_jis (*s))
172       {
173         if (is_sj_lower (s[0], s[1]))
174           s[1] = sj_toupper2 (s[1]);
175         s += 2;
176       }
177       else if (is_kana (*s))
178       {
179         s++;
180       }
181       else
182       {
183         if (islower(*s))
184           *s = toupper(*s);
185         s++;
186       }
187     }
188     else
189 #endif */ /* KANJI_WIN95_COMPATIBILITY */
190     {
191       if (islower(*s))
192         *s = toupper(*s);
193       s++;
194     }
195   }
196 }