OpenVAS Scanner  7.0.0~git
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 184 of file genrand.c.

185 {
186  static int urand_fd = -1;
187  unsigned char md4_buf[64];
188  unsigned char tmp_buf[16];
189  unsigned char *p;
190 
191  if (!done_reseed_ntlmssp)
192  {
193  urand_fd = do_reseed_ntlmssp (True, urand_fd);
195  }
196 
197  if (urand_fd != -1 && len > 0)
198  {
199  if (read (urand_fd, out, len) == len)
200  return; /* len bytes of random data read from urandom. */
201 
202  /* Read of urand error, drop back to non urand method. */
203  close (urand_fd);
204  urand_fd = -1;
205  do_reseed_ntlmssp (False, -1);
207  }
208 
209  /*
210  * Generate random numbers in chunks of 64 bytes,
211  * then md4 them & copy to the output buffer.
212  * This way the raw state of the stream is never externally
213  * seen.
214  */
215 
216  p = out;
217  while (len > 0)
218  {
219  int copy_len = len > 16 ? 16 : len;
220 
221  bzero (md4_buf, sizeof (md4_buf));
222  smb_arc4_crypt_ntlmssp (smb_arc4_state, md4_buf, sizeof (md4_buf));
223  mdfour_ntlmssp (tmp_buf, md4_buf, sizeof (md4_buf));
224  memcpy (p, tmp_buf, copy_len);
225  p += copy_len;
226  len -= copy_len;
227  }
228 }

References do_reseed_ntlmssp(), done_reseed_ntlmssp, False, 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 102 of file time.c.

103 {
104  gettimeofday (tval, NULL);
105 }

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 255 of file charcnv.c.

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

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 236 of file charcnv.c.

237 {
238  static int initialized = False;
239 
240  if (!initialized)
241  {
242  initialized = True;
244  }
245 }

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 122 of file time.c.

123 {
124  struct timespec ts;
125  ts.tv_sec = t;
126  ts.tv_nsec = 0;
128 }

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 58 of file arc4.c.

60 {
61  unsigned char index_i = arc4_state_inout[256];
62  unsigned char index_j = arc4_state_inout[257];
63  size_t ind;
64 
65  for (ind = 0; ind < len; ind++)
66  {
67  unsigned char tc;
68  unsigned char t;
69 
70  index_i++;
71  index_j += arc4_state_inout[index_i];
72 
73  tc = arc4_state_inout[index_i];
74  arc4_state_inout[index_i] = arc4_state_inout[index_j];
75  arc4_state_inout[index_j] = tc;
76 
77  t = arc4_state_inout[index_i] + arc4_state_inout[index_j];
78  data[ind] = data[ind] ^ arc4_state_inout[t];
79  }
80 
81  arc4_state_inout[256] = index_i;
82  arc4_state_inout[257] = index_j;
83 }

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 27 of file arc4.c.

29 {
30  size_t ind;
31  unsigned char j = 0;
32 
33  for (ind = 0; ind < 256; ind++)
34  {
35  arc4_state_out[ind] = (unsigned char) ind;
36  }
37 
38  for (ind = 0; ind < 256; ind++)
39  {
40  unsigned char tc;
41 
42  j += (arc4_state_out[ind] + key[ind % keylen]);
43 
44  tc = arc4_state_out[ind];
45  arc4_state_out[ind] = arc4_state_out[j];
46  arc4_state_out[j] = tc;
47  }
48  arc4_state_out[256] = 0;
49  arc4_state_out[257] = 0;
50 }

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 216 of file iconv.c.

217 {
218 #ifdef HAVE_NATIVE_ICONV
219  if (cd->cd_direct)
220  iconv_close ((iconv_t) cd->cd_direct);
221  if (cd->cd_pull)
222  iconv_close ((iconv_t) cd->cd_pull);
223  if (cd->cd_push)
224  iconv_close ((iconv_t) cd->cd_push);
225 #endif
226 
227  g_free (cd->from_name);
228  g_free (cd->to_name);
229 
230  memset (cd, 0, sizeof (*cd));
231  g_free (cd);
232  return 0;
233 }

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 66 of file iconv.c.

68 {
69  char cvtbuf[2048];
70  char *bufp = cvtbuf;
71  size_t bufsize;
72 
73  /* in many cases we can go direct */
74  if (cd->direct)
75  {
76  return cd->direct (cd->cd_direct, inbuf, inbytesleft, outbuf,
77  outbytesleft);
78  }
79 
80  /* otherwise we have to do it chunks at a time */
81  while (*inbytesleft > 0)
82  {
83  bufp = cvtbuf;
84  bufsize = sizeof (cvtbuf);
85 
86  if (cd->pull (cd->cd_pull, inbuf, inbytesleft, &bufp, &bufsize)
87  == (size_t) -1
88  && errno != E2BIG)
89  return -1;
90 
91  bufp = cvtbuf;
92  bufsize = sizeof (cvtbuf) - bufsize;
93 
94  if (cd->push (cd->cd_push, (const char **) &bufp, &bufsize, outbuf,
95  outbytesleft)
96  == (size_t) -1)
97  return -1;
98  }
99 
100  return 0;
101 }

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 114 of file iconv.c.

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

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:91
smb_iconv_close_ntlmssp
int smb_iconv_close_ntlmssp(smb_iconv_t cd)
Definition: iconv.c:216
charset_functions_ntlmssp
Definition: charset.h:53
CH_UNIX
@ CH_UNIX
Definition: charset.h:40
mdfour_ntlmssp
void mdfour_ntlmssp(unsigned char *out, const unsigned char *in, int n)
Definition: md4.c:174
do_reseed_ntlmssp
static int do_reseed_ntlmssp(bool use_fd, int fd)
Definition: genrand.c:130
put_long_date_timespec_ntlmssp
void put_long_date_timespec_ntlmssp(char *p, struct timespec ts)
Definition: time.c:113
_smb_iconv_t::from_name
char * from_name
Definition: smb.h:96
NUM_CHARSETS
#define NUM_CHARSETS
Definition: charset.h:47
True
#define True
Definition: genrand.c:59
charset_t
charset_t
Definition: charset.h:36
charset_functions_ntlmssp::push
size_t(* push)(void *, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition: charset.h:58
CH_UTF16LE
@ CH_UTF16LE
Definition: charset.h:38
CH_UTF16BE
@ CH_UTF16BE
Definition: charset.h:44
init_iconv_ntlmssp
void init_iconv_ntlmssp(void)
Definition: charcnv.c:255
smb_arc4_crypt_ntlmssp
void smb_arc4_crypt_ntlmssp(unsigned char arc4_state_inout[258], unsigned char *data, size_t len)
Definition: arc4.c:58
smb_arc4_state
static unsigned char smb_arc4_state[258]
Definition: genrand.c:61
SMB_MALLOC_P
#define SMB_MALLOC_P(type)
Definition: smb.h:185
charset_name_ntlmssp
static const char * charset_name_ntlmssp(charset_t ch)
Definition: charcnv.c:192
_smb_iconv_t::cd_push
void * cd_push
Definition: smb.h:95
conv_handles_ntlmssp
static smb_iconv_t conv_handles_ntlmssp[NUM_CHARSETS][NUM_CHARSETS]
Definition: charcnv.c:129
done_reseed_ntlmssp
static bool done_reseed_ntlmssp
Definition: genrand.c:72
_smb_iconv_t::direct
size_t(* direct)(void *cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition: smb.h:89
_smb_iconv_t::push
size_t(* push)(void *cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition: smb.h:93
_smb_iconv_t::cd_direct
void * cd_direct
Definition: smb.h:95
charset_functions_ntlmssp::pull
size_t(* pull)(void *, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition: charset.h:56
smb_iconv_open_ntlmssp
smb_iconv_t smb_iconv_open_ntlmssp(const char *tocode, const char *fromcode)
Definition: iconv.c:114
conv_silent_ntlmssp
static bool conv_silent_ntlmssp
Definition: charcnv.c:131
False
#define False
Definition: genrand.c:58
is_utf16_ntlmssp
static bool is_utf16_ntlmssp(const char *name)
Definition: iconv.c:104
_smb_iconv_t
Definition: smb.h:87
iconv_copy_ntlmssp
static size_t iconv_copy_ntlmssp(void *, const char **, size_t *, char **, size_t *)
Definition: iconv.c:236
find_charset_functions_ntlmssp
static struct charset_functions_ntlmssp * find_charset_functions_ntlmssp(const char *name)
Definition: iconv.c:42
SMB_STRDUP
#define SMB_STRDUP(s)
Definition: smb.h:193
False
#define False
Definition: charcnv.c:76
True
#define True
Definition: charcnv.c:77
init_valid_table_ntlmssp
void init_valid_table_ntlmssp(void)
Definition: charcnv.c:134
_smb_iconv_t::cd_pull
void * cd_pull
Definition: smb.h:95
_smb_iconv_t::to_name
char * to_name
Definition: smb.h:96