OpenVAS Libraries  9.0.3
proto.h File Reference
#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 167 of file genrand.c.

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

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

Here is the caller graph for this function:

◆ GetTimeOfDay_ntlmssp()

void GetTimeOfDay_ntlmssp ( struct timeval tval)

Definition at line 92 of file time.c.

93 {
94  gettimeofday(tval,NULL);
95 }

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

215 {
216  int c1, c2;
217  bool did_reload = False;
218 
219  /* so that charset_name() works we need to get the UNIX<->UCS2 going
220  first */
221  if (!conv_handles_ntlmssp[CH_UNIX][CH_UTF16LE])
222  conv_handles_ntlmssp[CH_UNIX][CH_UTF16LE] = smb_iconv_open_ntlmssp(charset_name_ntlmssp(CH_UTF16LE), "ASCII");
223 
224  if (!conv_handles_ntlmssp[CH_UTF16LE][CH_UNIX])
225  conv_handles_ntlmssp[CH_UTF16LE][CH_UNIX] = smb_iconv_open_ntlmssp("ASCII", charset_name_ntlmssp(CH_UTF16LE));
226 
227  for (c1=0;c1<NUM_CHARSETS;c1++) {
228  for (c2=0;c2<NUM_CHARSETS;c2++) {
229  const char *n1 = charset_name_ntlmssp((charset_t)c1);
230  const char *n2 = charset_name_ntlmssp((charset_t)c2);
231  if (conv_handles_ntlmssp[c1][c2] &&
232  strcmp(n1, conv_handles_ntlmssp[c1][c2]->from_name) == 0 &&
233  strcmp(n2, conv_handles_ntlmssp[c1][c2]->to_name) == 0)
234  continue;
235 
236  did_reload = True;
237 
238  if (conv_handles_ntlmssp[c1][c2])
239  smb_iconv_close_ntlmssp(conv_handles_ntlmssp[c1][c2]);
240 
241  conv_handles_ntlmssp[c1][c2] = smb_iconv_open_ntlmssp(n2,n1);
242  if (conv_handles_ntlmssp[c1][c2] == (smb_iconv_t)-1) {
243  if (c1 != CH_UTF16LE && c1 != CH_UTF16BE) {
244  n1 = "ASCII";
245  }
246  if (c2 != CH_UTF16LE && c2 != CH_UTF16BE) {
247  n2 = "ASCII";
248  }
249  conv_handles_ntlmssp[c1][c2] = smb_iconv_open_ntlmssp(n2,n1);
250  if (!conv_handles_ntlmssp[c1][c2]) {
251  log_legacy_write ("init_iconv_ntlmssp: conv_handle"
252  " initialization failed");
253  }
254  }
255  }
256  }
257 
258  if (did_reload) {
259  /* XXX: Does this really get called every time the dos
260  * codepage changes? */
261  /* XXX: Is the did_reload test too strict? */
262  conv_silent_ntlmssp = True;
264  conv_silent_ntlmssp = False;
265  }
266 }
charset_t
Definition: charset.h:30
void init_valid_table_ntlmssp(void)
Definition: charcnv.c:108
void log_legacy_write(const char *format,...)
Legacy function to write a log message.
smb_iconv_t smb_iconv_open_ntlmssp(const char *tocode, const char *fromcode)
Definition: iconv.c:105
#define True
Definition: charcnv.c:59
#define False
Definition: charcnv.c:58
int smb_iconv_close_ntlmssp(smb_iconv_t cd)
Definition: iconv.c:195
#define NUM_CHARSETS
Definition: charset.h:32

References False.

◆ lazy_initialize_conv_ntlmssp()

void lazy_initialize_conv_ntlmssp ( void  )

Definition at line 196 of file charcnv.c.

197 {
198  static int initialized = False;
199 
200  if (!initialized) {
201  initialized = True;
203  }
204 }
void init_iconv_ntlmssp(void)
Definition: charcnv.c:214
#define True
Definition: charcnv.c:59
#define False
Definition: charcnv.c:58

◆ put_long_date_ntlmssp()

void put_long_date_ntlmssp ( char *  p,
time_t  t 
)

Definition at line 111 of file time.c.

112 {
113  struct timespec ts;
114  ts.tv_sec = t;
115  ts.tv_nsec = 0;
117 }
void put_long_date_timespec_ntlmssp(char *p, struct timespec ts)
Definition: time.c:103

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

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

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

31 {
32  size_t ind;
33  unsigned char j = 0;
34 
35  for (ind = 0; ind < 256; ind++) {
36  arc4_state_out[ind] = (unsigned char)ind;
37  }
38 
39  for( ind = 0; ind < 256; ind++) {
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 }

◆ smb_iconv_close_ntlmssp()

int smb_iconv_close_ntlmssp ( smb_iconv_t  cd)

Definition at line 195 of file iconv.c.

196 {
197 #ifdef HAVE_NATIVE_ICONV
198  if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
199  if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
200  if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
201 #endif
202 
203  SAFE_FREE(cd->from_name);
204  SAFE_FREE(cd->to_name);
205 
206  memset(cd, 0, sizeof(*cd));
207  SAFE_FREE(cd);
208  return 0;
209 }
void * cd_pull
Definition: smb.h:90
char * to_name
Definition: smb.h:91
void * cd_direct
Definition: smb.h:90
void * cd_push
Definition: smb.h:90
#define SAFE_FREE(x)
Definition: hmacmd5.h:49
char * from_name
Definition: smb.h:91

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

◆ 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() implementaion.

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

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

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.

◆ smb_iconv_open_ntlmssp()

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

Definition at line 105 of file iconv.c.

106 {
107  smb_iconv_t ret;
108  struct charset_functions_ntlmssp *from, *to;
109 
110  ret = SMB_MALLOC_P(struct _smb_iconv_t);
111  if (!ret) {
112  errno = ENOMEM;
113  return (smb_iconv_t)-1;
114  }
115  memset(ret, 0, sizeof(struct _smb_iconv_t));
116 
117  ret->from_name = SMB_STRDUP(fromcode);
118  ret->to_name = SMB_STRDUP(tocode);
119 
120  /* check for the simplest null conversion */
121  if (strcasecmp(fromcode, tocode) == 0) {
122  ret->direct = iconv_copy_ntlmssp;
123  return ret;
124  }
125 
126  /* check if we have a builtin function for this conversion */
127  from = find_charset_functions_ntlmssp(fromcode);
128  if(from)ret->pull = from->pull;
129 
130  to = find_charset_functions_ntlmssp(tocode);
131  if(to)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  ret->cd_pull = iconv_open("UTF-16LE", fromcode);
137  if (ret->cd_pull == (iconv_t)-1)
138  ret->cd_pull = iconv_open("UCS-2LE", fromcode);
139  if (ret->cd_pull != (iconv_t)-1)
140  ret->pull = sys_iconv;
141  }
142 
143  if (!ret->push) {
144  ret->cd_push = iconv_open(tocode, "UTF-16LE");
145  if (ret->cd_push == (iconv_t)-1)
146  ret->cd_push = iconv_open(tocode, "UCS-2LE");
147  if (ret->cd_push != (iconv_t)-1)
148  ret->push = sys_iconv;
149  }
150 #endif
151 
152  if (!ret->push || !ret->pull) {
153  SAFE_FREE(ret->from_name);
154  SAFE_FREE(ret->to_name);
155  SAFE_FREE(ret);
156  errno = EINVAL;
157  return (smb_iconv_t)-1;
158  }
159 
160  /* check for conversion to/from ucs2 */
161  if (is_utf16_ntlmssp(fromcode) && to) {
162  ret->direct = to->push;
163  ret->push = ret->pull = NULL;
164  return ret;
165  }
166 
167  if (is_utf16_ntlmssp(tocode) && from) {
168  ret->direct = from->pull;
169  ret->push = ret->pull = NULL;
170  return ret;
171  }
172 
173  /* Check if we can do the conversion direct */
174 #ifdef HAVE_NATIVE_ICONV
175  if (is_utf16(fromcode)) {
176  ret->direct = sys_iconv;
177  ret->cd_direct = ret->cd_push;
178  ret->cd_push = NULL;
179  return ret;
180  }
181  if (is_utf16(tocode)) {
182  ret->direct = sys_iconv;
183  ret->cd_direct = ret->cd_pull;
184  ret->cd_pull = NULL;
185  return ret;
186  }
187 #endif
188 
189  return ret;
190 }
void * cd_pull
Definition: smb.h:90
size_t(* push)(void *, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition: charset.h:42
char * to_name
Definition: smb.h:91
size_t(* pull)(void *cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition: smb.h:86
size_t(* direct)(void *cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition: smb.h:84
size_t(* push)(void *cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition: smb.h:88
size_t(* pull)(void *, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition: charset.h:40
void * cd_direct
Definition: smb.h:90
#define SMB_MALLOC_P(type)
Definition: smb.h:171
void * cd_push
Definition: smb.h:90
#define SAFE_FREE(x)
Definition: hmacmd5.h:49
#define SMB_STRDUP(s)
Definition: smb.h:178
char * from_name
Definition: smb.h:91

References _smb_iconv_t::direct, _smb_iconv_t::from_name, SMB_MALLOC_P, SMB_STRDUP, and _smb_iconv_t::to_name.