OpenVAS Scanner  22.7.9
proto.h File Reference
#include <sys/param.h>
#include <time.h>
#include "smb.h"
Include dependency graph for proto.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void generate_random_buffer_ntlmssp (unsigned char *out, int len)
 
void put_long_date_ntlmssp (char *p, time_t t)
 
void GetTimeOfDay_ntlmssp (struct timeval *tval)
 
size_t smb_iconv_ntlmssp (smb_iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
 
smb_iconv_t smb_iconv_open_ntlmssp (const char *tocode, const char *fromcode)
 
int smb_iconv_close_ntlmssp (smb_iconv_t cd)
 
void smb_arc4_init_ntlmssp (unsigned char arc4_state_out[258], const unsigned char *key, size_t keylen)
 
void smb_arc4_crypt_ntlmssp (unsigned char arc4_state_inout[258], unsigned char *data, size_t len)
 
void lazy_initialize_conv_ntlmssp (void)
 
void init_iconv_ntlmssp (void)
 

Function Documentation

◆ generate_random_buffer_ntlmssp()

void generate_random_buffer_ntlmssp ( unsigned char *  out,
int  len 
)

Definition at line 170 of file genrand.c.

171 {
172  static int urand_fd = -1;
173  unsigned char md4_buf[64];
174  unsigned char tmp_buf[16];
175  unsigned char *p;
176 
177  if (!done_reseed_ntlmssp)
178  {
179  urand_fd = do_reseed_ntlmssp (True, urand_fd);
181  }
182 
183  if (urand_fd != -1 && len > 0)
184  {
185  if (read (urand_fd, out, len) == len)
186  return; /* len bytes of random data read from urandom. */
187 
188  /* Read of urand error, drop back to non urand method. */
189  close (urand_fd);
190  urand_fd = -1;
191  do_reseed_ntlmssp (False, -1);
193  }
194 
195  /*
196  * Generate random numbers in chunks of 64 bytes,
197  * then md4 them & copy to the output buffer.
198  * This way the raw state of the stream is never externally
199  * seen.
200  */
201 
202  p = out;
203  while (len > 0)
204  {
205  int copy_len = len > 16 ? 16 : len;
206 
207  bzero (md4_buf, sizeof (md4_buf));
208  smb_arc4_crypt_ntlmssp (smb_arc4_state, md4_buf, sizeof (md4_buf));
209  mdfour_ntlmssp (tmp_buf, md4_buf, sizeof (md4_buf));
210  memcpy (p, tmp_buf, copy_len);
211  p += copy_len;
212  len -= copy_len;
213  }
214 }

References do_reseed_ntlmssp(), done_reseed_ntlmssp, False, len, mdfour_ntlmssp(), smb_arc4_crypt_ntlmssp(), smb_arc4_state, and True.

Referenced by LMv2_generate_response_ntlmssp(), ntlmssp_genauth_keyexchg(), ntlmssp_genauth_ntlm2(), and NTLMv2_generate_client_data_ntlmssp().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetTimeOfDay_ntlmssp()

void GetTimeOfDay_ntlmssp ( struct timeval tval)

Definition at line 90 of file time.c.

91 {
92  gettimeofday (tval, NULL);
93 }

Referenced by do_reseed_ntlmssp().

Here is the caller graph for this function:

◆ init_iconv_ntlmssp()

void init_iconv_ntlmssp ( void  )

Initialize iconv conversion descriptors.

This is called the first time it is needed, and also called again every time the configuration is reloaded, because the charset or codepage might have changed.

Definition at line 245 of file charcnv.c.

246 {
247  int c1, c2;
248  bool did_reload = False;
249 
250  /* so that charset_name() works we need to get the UNIX<->UCS2 going
251  first */
255 
259 
260  for (c1 = 0; c1 < NUM_CHARSETS; c1++)
261  {
262  for (c2 = 0; c2 < NUM_CHARSETS; c2++)
263  {
264  const char *n1 = charset_name_ntlmssp ((charset_t) c1);
265  const char *n2 = charset_name_ntlmssp ((charset_t) c2);
266  if (conv_handles_ntlmssp[c1][c2]
267  && strcmp (n1, conv_handles_ntlmssp[c1][c2]->from_name) == 0
268  && strcmp (n2, conv_handles_ntlmssp[c1][c2]->to_name) == 0)
269  continue;
270 
271  did_reload = True;
272 
273  if (conv_handles_ntlmssp[c1][c2])
275 
276  conv_handles_ntlmssp[c1][c2] = smb_iconv_open_ntlmssp (n2, n1);
277  if (conv_handles_ntlmssp[c1][c2] == (smb_iconv_t) -1)
278  {
279  if (c1 != CH_UTF16LE && c1 != CH_UTF16BE)
280  {
281  n1 = "ASCII";
282  }
283  if (c2 != CH_UTF16LE && c2 != CH_UTF16BE)
284  {
285  n2 = "ASCII";
286  }
287  conv_handles_ntlmssp[c1][c2] = smb_iconv_open_ntlmssp (n2, n1);
288  if (!conv_handles_ntlmssp[c1][c2])
289  {
290  g_message ("init_iconv_ntlmssp: conv_handle"
291  " initialization failed");
292  }
293  }
294  }
295  }
296 
297  if (did_reload)
298  {
299  /* XXX: Does this really get called every time the dos
300  * codepage changes? */
301  /* XXX: Is the did_reload test too strict? */
305  }
306 }

References CH_UNIX, CH_UTF16BE, CH_UTF16LE, charset_name_ntlmssp(), conv_handles_ntlmssp, conv_silent_ntlmssp, False, init_valid_table_ntlmssp(), NUM_CHARSETS, smb_iconv_close_ntlmssp(), smb_iconv_open_ntlmssp(), and True.

Referenced by lazy_initialize_conv_ntlmssp().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lazy_initialize_conv_ntlmssp()

void lazy_initialize_conv_ntlmssp ( void  )

Definition at line 226 of file charcnv.c.

227 {
228  static int initialized = False;
229 
230  if (!initialized)
231  {
232  initialized = True;
234  }
235 }

References False, init_iconv_ntlmssp(), and True.

Referenced by convert_string_internal_ntlmssp(), and init_valid_table_ntlmssp().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ put_long_date_ntlmssp()

void put_long_date_ntlmssp ( char *  p,
time_t  t 
)

Definition at line 110 of file time.c.

111 {
112  struct timespec ts;
113  ts.tv_sec = t;
114  ts.tv_nsec = 0;
116 }

References put_long_date_timespec_ntlmssp().

Referenced by NTLMv2_generate_client_data_ntlmssp().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ smb_arc4_crypt_ntlmssp()

void smb_arc4_crypt_ntlmssp ( unsigned char  arc4_state_inout[258],
unsigned char *  data,
size_t  len 
)

Definition at line 47 of file arc4.c.

49 {
50  unsigned char index_i = arc4_state_inout[256];
51  unsigned char index_j = arc4_state_inout[257];
52  size_t ind;
53 
54  for (ind = 0; ind < len; ind++)
55  {
56  unsigned char tc;
57  unsigned char t;
58 
59  index_i++;
60  index_j += arc4_state_inout[index_i];
61 
62  tc = arc4_state_inout[index_i];
63  arc4_state_inout[index_i] = arc4_state_inout[index_j];
64  arc4_state_inout[index_j] = tc;
65 
66  t = arc4_state_inout[index_i] + arc4_state_inout[index_j];
67  data[ind] = data[ind] ^ arc4_state_inout[t];
68  }
69 
70  arc4_state_inout[256] = index_i;
71  arc4_state_inout[257] = index_j;
72 }

References len.

Referenced by generate_random_buffer_ntlmssp().

Here is the caller graph for this function:

◆ smb_arc4_init_ntlmssp()

void smb_arc4_init_ntlmssp ( unsigned char  arc4_state_out[258],
const unsigned char *  key,
size_t  keylen 
)

Definition at line 16 of file arc4.c.

18 {
19  size_t ind;
20  unsigned char j = 0;
21 
22  for (ind = 0; ind < 256; ind++)
23  {
24  arc4_state_out[ind] = (unsigned char) ind;
25  }
26 
27  for (ind = 0; ind < 256; ind++)
28  {
29  unsigned char tc;
30 
31  j += (arc4_state_out[ind] + key[ind % keylen]);
32 
33  tc = arc4_state_out[ind];
34  arc4_state_out[ind] = arc4_state_out[j];
35  arc4_state_out[j] = tc;
36  }
37  arc4_state_out[256] = 0;
38  arc4_state_out[257] = 0;
39 }

Referenced by do_reseed_ntlmssp().

Here is the caller graph for this function:

◆ smb_iconv_close_ntlmssp()

int smb_iconv_close_ntlmssp ( smb_iconv_t  cd)

Definition at line 203 of file iconv.c.

204 {
205 #ifdef HAVE_NATIVE_ICONV
206  if (cd->cd_direct)
207  iconv_close ((iconv_t) cd->cd_direct);
208  if (cd->cd_pull)
209  iconv_close ((iconv_t) cd->cd_pull);
210  if (cd->cd_push)
211  iconv_close ((iconv_t) cd->cd_push);
212 #endif
213 
214  g_free (cd->from_name);
215  g_free (cd->to_name);
216 
217  memset (cd, 0, sizeof (*cd));
218  g_free (cd);
219  return 0;
220 }

References _smb_iconv_t::cd_direct, _smb_iconv_t::cd_pull, _smb_iconv_t::cd_push, _smb_iconv_t::from_name, and _smb_iconv_t::to_name.

Referenced by charset_name_ntlmssp(), and init_iconv_ntlmssp().

Here is the caller graph for this function:

◆ smb_iconv_ntlmssp()

size_t smb_iconv_ntlmssp ( smb_iconv_t  cd,
const char **  inbuf,
size_t *  inbytesleft,
char **  outbuf,
size_t *  outbytesleft 
)

This is a simple portable iconv() implementation.

It only knows about a very small number of character sets - just enough that Samba works on systems that don't have iconv.

Definition at line 53 of file iconv.c.

55 {
56  char cvtbuf[2048];
57  char *bufp = cvtbuf;
58  size_t bufsize;
59 
60  /* in many cases we can go direct */
61  if (cd->direct)
62  {
63  return cd->direct (cd->cd_direct, inbuf, inbytesleft, outbuf,
64  outbytesleft);
65  }
66 
67  /* otherwise we have to do it chunks at a time */
68  while (*inbytesleft > 0)
69  {
70  bufp = cvtbuf;
71  bufsize = sizeof (cvtbuf);
72 
73  if (cd->pull (cd->cd_pull, inbuf, inbytesleft, &bufp, &bufsize)
74  == (size_t) -1
75  && errno != E2BIG)
76  return -1;
77 
78  bufp = cvtbuf;
79  bufsize = sizeof (cvtbuf) - bufsize;
80 
81  if (cd->push (cd->cd_push, (const char **) &bufp, &bufsize, outbuf,
82  outbytesleft)
83  == (size_t) -1)
84  return -1;
85  }
86 
87  return 0;
88 }

References _smb_iconv_t::cd_direct, _smb_iconv_t::cd_pull, _smb_iconv_t::cd_push, _smb_iconv_t::direct, _smb_iconv_t::pull, and _smb_iconv_t::push.

Referenced by convert_string_internal_ntlmssp().

Here is the caller graph for this function:

◆ smb_iconv_open_ntlmssp()

smb_iconv_t smb_iconv_open_ntlmssp ( const char *  tocode,
const char *  fromcode 
)

Definition at line 101 of file iconv.c.

102 {
103  smb_iconv_t ret;
104  struct charset_functions_ntlmssp *from, *to;
105 
106  ret = SMB_MALLOC_P (struct _smb_iconv_t);
107  if (!ret)
108  {
109  errno = ENOMEM;
110  return (smb_iconv_t) -1;
111  }
112  memset (ret, 0, sizeof (struct _smb_iconv_t));
113 
114  ret->from_name = SMB_STRDUP (fromcode);
115  ret->to_name = SMB_STRDUP (tocode);
116 
117  /* check for the simplest null conversion */
118  if (strcasecmp (fromcode, tocode) == 0)
119  {
120  ret->direct = iconv_copy_ntlmssp;
121  return ret;
122  }
123 
124  /* check if we have a builtin function for this conversion */
125  from = find_charset_functions_ntlmssp (fromcode);
126  if (from)
127  ret->pull = from->pull;
128 
129  to = find_charset_functions_ntlmssp (tocode);
130  if (to)
131  ret->push = to->push;
132 
133  /* check if we can use iconv for this conversion */
134 #ifdef HAVE_NATIVE_ICONV
135  if (!ret->pull)
136  {
137  ret->cd_pull = iconv_open ("UTF-16LE", fromcode);
138  if (ret->cd_pull == (iconv_t) -1)
139  ret->cd_pull = iconv_open ("UCS-2LE", fromcode);
140  if (ret->cd_pull != (iconv_t) -1)
141  ret->pull = sys_iconv;
142  }
143 
144  if (!ret->push)
145  {
146  ret->cd_push = iconv_open (tocode, "UTF-16LE");
147  if (ret->cd_push == (iconv_t) -1)
148  ret->cd_push = iconv_open (tocode, "UCS-2LE");
149  if (ret->cd_push != (iconv_t) -1)
150  ret->push = sys_iconv;
151  }
152 #endif
153 
154  if (!ret->push || !ret->pull)
155  {
156  g_free (ret->from_name);
157  g_free (ret->to_name);
158  g_free (ret);
159  errno = EINVAL;
160  return (smb_iconv_t) -1;
161  }
162 
163  /* check for conversion to/from ucs2 */
164  if (is_utf16_ntlmssp (fromcode) && to)
165  {
166  ret->direct = to->push;
167  ret->push = ret->pull = NULL;
168  return ret;
169  }
170 
171  if (is_utf16_ntlmssp (tocode) && from)
172  {
173  ret->direct = from->pull;
174  ret->push = ret->pull = NULL;
175  return ret;
176  }
177 
178  /* Check if we can do the conversion direct */
179 #ifdef HAVE_NATIVE_ICONV
180  if (is_utf16 (fromcode))
181  {
182  ret->direct = sys_iconv;
183  ret->cd_direct = ret->cd_push;
184  ret->cd_push = NULL;
185  return ret;
186  }
187  if (is_utf16 (tocode))
188  {
189  ret->direct = sys_iconv;
190  ret->cd_direct = ret->cd_pull;
191  ret->cd_pull = NULL;
192  return ret;
193  }
194 #endif
195 
196  return ret;
197 }

References _smb_iconv_t::cd_direct, _smb_iconv_t::cd_pull, _smb_iconv_t::cd_push, _smb_iconv_t::direct, find_charset_functions_ntlmssp(), _smb_iconv_t::from_name, iconv_copy_ntlmssp(), is_utf16_ntlmssp(), charset_functions_ntlmssp::pull, _smb_iconv_t::pull, charset_functions_ntlmssp::push, _smb_iconv_t::push, SMB_MALLOC_P, SMB_STRDUP, and _smb_iconv_t::to_name.

Referenced by charset_name_ntlmssp(), and init_iconv_ntlmssp().

Here is the call graph for this function:
Here is the caller graph for this function:
_smb_iconv_t::pull
size_t(* pull)(void *cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition: smb.h:78
smb_iconv_close_ntlmssp
int smb_iconv_close_ntlmssp(smb_iconv_t cd)
Definition: iconv.c:203
charset_functions_ntlmssp
Definition: charset.h:41
CH_UNIX
@ CH_UNIX
Definition: charset.h:27
mdfour_ntlmssp
void mdfour_ntlmssp(unsigned char *out, const unsigned char *in, int n)
Definition: md4.c:165
do_reseed_ntlmssp
static int do_reseed_ntlmssp(bool use_fd, int fd)
Definition: genrand.c:116
_smb_iconv_t::from_name
char * from_name
Definition: smb.h:83
NUM_CHARSETS
#define NUM_CHARSETS
Definition: charset.h:34
True
#define True
Definition: genrand.c:45
charset_t
charset_t
Definition: charset.h:24
charset_functions_ntlmssp::push
size_t(* push)(void *, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition: charset.h:45
CH_UTF16LE
@ CH_UTF16LE
Definition: charset.h:25
CH_UTF16BE
@ CH_UTF16BE
Definition: charset.h:31
init_iconv_ntlmssp
void init_iconv_ntlmssp(void)
Definition: charcnv.c:245
smb_arc4_crypt_ntlmssp
void smb_arc4_crypt_ntlmssp(unsigned char arc4_state_inout[258], unsigned char *data, size_t len)
Definition: arc4.c:47
smb_arc4_state
static unsigned char smb_arc4_state[258]
Definition: genrand.c:47
SMB_MALLOC_P
#define SMB_MALLOC_P(type)
Definition: smb.h:172
charset_name_ntlmssp
static const char * charset_name_ntlmssp(charset_t ch)
Definition: charcnv.c:182
_smb_iconv_t::cd_push
void * cd_push
Definition: smb.h:82
conv_handles_ntlmssp
static smb_iconv_t conv_handles_ntlmssp[NUM_CHARSETS][NUM_CHARSETS]
Definition: charcnv.c:119
done_reseed_ntlmssp
static bool done_reseed_ntlmssp
Definition: genrand.c:58
_smb_iconv_t::direct
size_t(* direct)(void *cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition: smb.h:76
_smb_iconv_t::push
size_t(* push)(void *cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition: smb.h:80
len
uint8_t len
Definition: nasl_packet_forgery.c:1
_smb_iconv_t::cd_direct
void * cd_direct
Definition: smb.h:82
charset_functions_ntlmssp::pull
size_t(* pull)(void *, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition: charset.h:43
smb_iconv_open_ntlmssp
smb_iconv_t smb_iconv_open_ntlmssp(const char *tocode, const char *fromcode)
Definition: iconv.c:101
conv_silent_ntlmssp
static bool conv_silent_ntlmssp
Definition: charcnv.c:121
False
#define False
Definition: genrand.c:44
init_valid_table_ntlmssp
static void init_valid_table_ntlmssp(void)
Definition: charcnv.c:124
is_utf16_ntlmssp
static bool is_utf16_ntlmssp(const char *name)
Definition: iconv.c:91
_smb_iconv_t
Definition: smb.h:75
iconv_copy_ntlmssp
static size_t iconv_copy_ntlmssp(void *, const char **, size_t *, char **, size_t *)
Definition: iconv.c:223
find_charset_functions_ntlmssp
static struct charset_functions_ntlmssp * find_charset_functions_ntlmssp(const char *name)
Definition: iconv.c:29
SMB_STRDUP
#define SMB_STRDUP(s)
Definition: smb.h:180
put_long_date_timespec_ntlmssp
static void put_long_date_timespec_ntlmssp(char *p, struct timespec ts)
Definition: time.c:101
False
#define False
Definition: charcnv.c:63
True
#define True
Definition: charcnv.c:64
_smb_iconv_t::cd_pull
void * cd_pull
Definition: smb.h:82
_smb_iconv_t::to_name
char * to_name
Definition: smb.h:83