9
9
#include <stdlib.h>
10
10
#include <stdint.h>
11
11
#include <string.h>
12
- #include <assert.h>
13
12
#include <errno.h>
14
13
#include <ctype.h>
15
14
#include <unistd.h>
24
23
#include "compat.h"
25
24
#include "util.h"
26
25
26
+ #define NTLM_ASSERT_ARG (expr ) do { \
27
+ if (!(expr)) \
28
+ return NTLM_CLIENT_ERROR_INVALID_INPUT; \
29
+ } while(0)
30
+
31
+ #define NTLM_ASSERT (ntlm , expr ) do { \
32
+ if (!(expr)) { \
33
+ ntlm_client_set_errmsg(ntlm, "internal error: " #expr); \
34
+ return -1; \
35
+ } \
36
+ } while(0)
37
+
27
38
unsigned char ntlm_client_signature [] = NTLM_SIGNATURE ;
28
39
29
40
static bool supports_unicode (ntlm_client * ntlm )
@@ -74,7 +85,9 @@ void ntlm_client_set_errmsg(ntlm_client *ntlm, const char *errmsg)
74
85
75
86
const char * ntlm_client_errmsg (ntlm_client * ntlm )
76
87
{
77
- assert (ntlm );
88
+ if (!ntlm )
89
+ return "internal error" ;
90
+
78
91
return ntlm -> errmsg ? ntlm -> errmsg : "no error" ;
79
92
}
80
93
@@ -84,7 +97,7 @@ int ntlm_client_set_version(
84
97
uint8_t minor ,
85
98
uint16_t build )
86
99
{
87
- assert (ntlm );
100
+ NTLM_ASSERT_ARG (ntlm );
88
101
89
102
ntlm -> host_version .major = major ;
90
103
ntlm -> host_version .minor = minor ;
@@ -111,8 +124,7 @@ int ntlm_client_set_hostname(
111
124
const char * hostname ,
112
125
const char * domain )
113
126
{
114
- assert (ntlm );
115
-
127
+ NTLM_ASSERT_ARG (ntlm );
116
128
ENSURE_INITIALIZED (ntlm );
117
129
118
130
free_hostname (ntlm );
@@ -168,8 +180,7 @@ int ntlm_client_set_credentials(
168
180
const char * domain ,
169
181
const char * password )
170
182
{
171
- assert (ntlm );
172
-
183
+ NTLM_ASSERT_ARG (ntlm );
173
184
ENSURE_INITIALIZED (ntlm );
174
185
175
186
free_credentials (ntlm );
@@ -218,8 +229,7 @@ int ntlm_client_set_credentials(
218
229
219
230
int ntlm_client_set_target (ntlm_client * ntlm , const char * target )
220
231
{
221
- assert (ntlm );
222
-
232
+ NTLM_ASSERT_ARG (ntlm );
223
233
ENSURE_INITIALIZED (ntlm );
224
234
225
235
free (ntlm -> target );
@@ -248,14 +258,16 @@ int ntlm_client_set_target(ntlm_client *ntlm, const char *target)
248
258
249
259
int ntlm_client_set_nonce (ntlm_client * ntlm , uint64_t nonce )
250
260
{
251
- assert (ntlm );
261
+ NTLM_ASSERT_ARG (ntlm );
262
+
252
263
ntlm -> nonce = nonce ;
253
264
return 0 ;
254
265
}
255
266
256
267
int ntlm_client_set_timestamp (ntlm_client * ntlm , uint64_t timestamp )
257
268
{
258
- assert (ntlm );
269
+ NTLM_ASSERT_ARG (ntlm );
270
+
259
271
ntlm -> timestamp = timestamp ;
260
272
return 0 ;
261
273
}
@@ -601,7 +613,9 @@ int ntlm_client_negotiate(
601
613
size_t hostname_offset = 0 ;
602
614
uint32_t flags = 0 ;
603
615
604
- assert (out && out_len && ntlm );
616
+ NTLM_ASSERT_ARG (out );
617
+ NTLM_ASSERT_ARG (out_len );
618
+ NTLM_ASSERT_ARG (ntlm );
605
619
606
620
* out = NULL ;
607
621
* out_len = 0 ;
@@ -684,20 +698,22 @@ int ntlm_client_negotiate(
684
698
return -1 ;
685
699
686
700
if (hostname_len > 0 ) {
687
- assert (hostname_offset == ntlm -> negotiate .pos );
701
+ NTLM_ASSERT (ntlm , hostname_offset == ntlm -> negotiate .pos );
702
+
688
703
if (!write_buf (ntlm , & ntlm -> negotiate ,
689
704
(const unsigned char * )ntlm -> hostname , hostname_len ))
690
705
return -1 ;
691
706
}
692
707
693
708
if (domain_len > 0 ) {
694
- assert (domain_offset == ntlm -> negotiate .pos );
709
+ NTLM_ASSERT (ntlm , domain_offset == ntlm -> negotiate .pos );
710
+
695
711
if (!write_buf (ntlm , & ntlm -> negotiate ,
696
712
(const unsigned char * )ntlm -> hostdomain , domain_len ))
697
713
return -1 ;
698
714
}
699
715
700
- assert ( ntlm -> negotiate .pos == ntlm -> negotiate .len );
716
+ NTLM_ASSERT ( ntlm , ntlm -> negotiate .pos == ntlm -> negotiate .len );
701
717
702
718
ntlm -> state = NTLM_STATE_CHALLENGE ;
703
719
@@ -719,7 +735,8 @@ int ntlm_client_set_challenge(
719
735
uint32_t name_offset , info_offset = 0 ;
720
736
bool unicode , has_target_info = false;
721
737
722
- assert (ntlm && (challenge_msg || !challenge_msg_len ));
738
+ NTLM_ASSERT_ARG (ntlm );
739
+ NTLM_ASSERT_ARG (challenge_msg || !challenge_msg_len );
723
740
724
741
ENSURE_INITIALIZED (ntlm );
725
742
@@ -1101,7 +1118,7 @@ static bool generate_ntlm2_hash(
1101
1118
return false;
1102
1119
}
1103
1120
1104
- assert ( out_len == NTLM_NTLM2_HASH_LEN );
1121
+ NTLM_ASSERT ( ntlm , out_len == NTLM_NTLM2_HASH_LEN );
1105
1122
return true;
1106
1123
}
1107
1124
@@ -1122,7 +1139,7 @@ static bool generate_ntlm2_challengehash(
1122
1139
return false;
1123
1140
}
1124
1141
1125
- assert ( out_len == 16 );
1142
+ NTLM_ASSERT ( ntlm , out_len == 16 );
1126
1143
return true;
1127
1144
}
1128
1145
@@ -1143,7 +1160,7 @@ static bool generate_lm2_response(ntlm_client *ntlm,
1143
1160
return false;
1144
1161
}
1145
1162
1146
- assert ( lm2_len == 16 );
1163
+ NTLM_ASSERT ( ntlm , lm2_len == 16 );
1147
1164
1148
1165
memcpy (& ntlm -> lm_response [0 ], lm2_challengehash , 16 );
1149
1166
memcpy (& ntlm -> lm_response [16 ], & local_nonce , 8 );
@@ -1237,7 +1254,9 @@ int ntlm_client_response(
1237
1254
uint32_t flags = 0 ;
1238
1255
bool unicode ;
1239
1256
1240
- assert (out && out_len && ntlm );
1257
+ NTLM_ASSERT_ARG (out );
1258
+ NTLM_ASSERT_ARG (out_len );
1259
+ NTLM_ASSERT_ARG (ntlm );
1241
1260
1242
1261
ENSURE_INITIALIZED (ntlm );
1243
1262
@@ -1362,7 +1381,7 @@ int ntlm_client_response(
1362
1381
!write_buf (ntlm , & ntlm -> response , session , session_len ))
1363
1382
return -1 ;
1364
1383
1365
- assert ( ntlm -> response .pos == ntlm -> response .len );
1384
+ NTLM_ASSERT ( ntlm , ntlm -> response .pos == ntlm -> response .len );
1366
1385
1367
1386
ntlm -> state = NTLM_STATE_COMPLETE ;
1368
1387
@@ -1374,7 +1393,8 @@ int ntlm_client_response(
1374
1393
1375
1394
void ntlm_client_reset (ntlm_client * ntlm )
1376
1395
{
1377
- assert (ntlm );
1396
+ if (!ntlm )
1397
+ return ;
1378
1398
1379
1399
ntlm -> state = NTLM_STATE_NEGOTIATE ;
1380
1400
0 commit comments