~clint-fewbar/ubuntu/precise/squid3/ignore-sighup-early

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Luigi Gangitano
  • Date: 2006-11-11 10:32:06 UTC
  • Revision ID: james.westby@ubuntu.com-20061111103206-f3p0r9g0vq44rp3r
Tags: upstream-3.0.PRE5
ImportĀ upstreamĀ versionĀ 3.0.PRE5

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