File: test_ntlm.c

package info (click to toggle)
libntlm 0.3.10-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,700 kB
  • ctags: 347
  • sloc: sh: 8,758; ansic: 1,867; makefile: 92
file content (165 lines) | stat: -rw-r--r-- 3,922 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* test_ntlm.c --- Test module for libntlm.
 * Copyright (C) 2004, 2005 Frediano Ziglio
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This file is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this file; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 */

#define NTLM_STATIC static
#include "global.h"

#include "md4.c"

static int
md4file (const char *name, unsigned char *hash)
{
  FILE *f;
  char line[1024];
  int res = 0;

  f = fopen (name, "r");
  if (!f)
    return 1;

  res = md4_stream (f, hash);

  fclose (f);

  return res;
}

static int
diffFile (const char *name1, const char *name2)
{
  unsigned char hash1[24], hash2[24];

  if (md4file (name1, hash1) || md4file (name2, hash2))
    return 1;

  if (memcmp (hash1, hash2, 16) != 0)
    return 1;

  return 0;
}

static void
dumpRaw (FILE * fp, const unsigned char *buf, size_t len)
{
  int i;

  for (i = 0; i < len; ++i)
    {
      if (i != 0 && (i & 0xf) == 0)
	fprintf (fp, "\n");
      fprintf (fp, "%02x ", buf[i]);
    }

  fprintf (fp, "\n");
}

static uint16
intelEndian16 (uint16 n)
{
  uint16 u;
  unsigned char *buf = (unsigned char *) &u;
  buf[0] = n & 0xff;
  buf[1] = (n >> 8) & 0xff;
  return u;
}

static uint32
intelEndian32 (uint32 n)
{
  uint32 u;
  unsigned char *buf = (unsigned char *) &u;
  buf[0] = n & 0xff;
  buf[1] = (n >> 8) & 0xff;
  buf[2] = (n >> 16) & 0xff;
  buf[3] = (n >> 24) & 0xff;
  return u;
}

static void
fillUnicode (tSmbStrHeader * header, char *buffer, int buffer_start,
	     int *index, const char *s)
{
  int len = strlen (s);
  header->len = header->maxlen = intelEndian16 (len * 2);
  header->offset = intelEndian32 (*index + buffer_start);
  *index += len * 2;

  for (; len; --len)
    {
      *buffer++ = *s++;
      *buffer++ = 0;
    }
}

static void
fillChallenge (tSmbNtlmAuthChallenge * challenge, const char *domain)
{
  int index = 0;

  memset (challenge, 0, sizeof (*challenge));
  memcpy (challenge->ident, "NTLMSSP\0\0\0", 8);
  challenge->msgType = intelEndian32 (2);
  fillUnicode (&challenge->uDomain, challenge->buffer,
	       challenge->buffer - ((uint8 *) challenge), &index, domain);
  challenge->flags = intelEndian32 (0);
  memcpy (challenge->challengeData, "\x01\x02\x03\x04\xf5\xc3\xb2\x82", 8);
  challenge->bufIndex = intelEndian32 (index);
}

#define DUMP_REQUEST(req) dumpRaw(f, (unsigned char*) req, SmbLength(req))

int
main ()
{
  tSmbNtlmAuthRequest request;
  tSmbNtlmAuthChallenge challenge;
  tSmbNtlmAuthResponse response;

  FILE *f = fopen ("test.out", "w");
  if (!f)
    return 1;

  printf ("ntlm.h %s libntlm %s\n", NTLM_VERSION, ntlm_check_version (NULL));

  if (!ntlm_check_version (NTLM_VERSION))
    return 1;

  /* do some test then dump */
  buildSmbNtlmAuthRequest (&request, "myuser", "mydomain");
  dumpSmbNtlmAuthRequest (f, &request);
  DUMP_REQUEST (&request);

  buildSmbNtlmAuthRequest (&request, "Test_!@xXxX.&", NULL);
  dumpSmbNtlmAuthRequest (f, &request);
  DUMP_REQUEST (&request);

  fillChallenge (&challenge, "mydomain");
  dumpSmbNtlmAuthChallenge (f, &challenge);
  DUMP_REQUEST (&challenge);

  buildSmbNtlmAuthResponse (&challenge, &response, "otheruser", "mypasswd");
  dumpSmbNtlmAuthResponse (f, &response);
  DUMP_REQUEST (&response);

  fclose (f);

  /* now reopen the file and do a diff */
  return diffFile ("test.out", NTLM_SRCDIR "/test.txt");
}