~squid/squid/sbuf-use

« back to all changes in this revision

Viewing changes to helpers/ntlm_auth/SMB/smbval/smbencrypt.c

  • Committer: hno
  • Date: 2001-01-08 06:32:04 UTC
  • Revision ID: cvs-1:hno-20010108063204-w6a8e1zz6eprqnp8
Major rewrite of proxy authentication to support other schemes than
Basic (auth_rewrite branch on SourceForge).
Contributors:
   Andy Doran
   Robert Collins
   Chemolli Francesco
   Henrik Nordstrom

For details about the new API's, see Programmers Guide.

As part of this change everything from auth_modules has been moved to
src/auth/basic/helpers

Show diffs side-by-side

added added

removed removed

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