2 nterface library functions
3 Copyright (C) 2003-2004 Chris Porter.
14 #include "../core/config.h"
15 #include "../lib/irc_string.h"
16 #include "../lib/sha1.h"
17 #include "../lib/helix.h"
21 unsigned char hexlookup
[256] = {
22 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
23 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
24 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
25 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
26 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x01,
27 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xff, 0xff,
28 0xff, 0xff, 0xff, 0xff, 0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
29 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
30 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
31 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0a, 0x0b, 0x0c,
32 0x0d, 0x0e, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
33 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
34 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
35 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
36 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
37 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
38 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
39 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
40 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
41 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
42 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
43 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
44 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
45 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
46 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
47 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
50 FILE *challenge_random_fd
= NULL
;
52 unsigned char entropybuffer
[CHALLENGE_ENTROPYLEN
* CHALLENGE_ENTROPYBUF
];
53 int entropy_remaining
= 0;
55 int getcopyconfigitemint(char *section
, char *key
, int def
, int *value
) {
59 snprintf(buf
, sizeof(buf
), "%d", def
);
60 ini
= getcopyconfigitem(section
, key
, buf
, 6);
65 if(!protectedatoi(ini
->content
, value
))
71 int getcopyconfigitemintpositive(char *section
, char *key
, int def
) {
73 if(!getcopyconfigitemint(section
, key
, def
, &value
))
82 int positive_atoi(char *data
) {
84 if(!protectedatoi(data
, &value
))
93 char *challenge_response(char *challenge
, char *password
) {
94 unsigned char buf
[20];
95 static char output
[sizeof(buf
) * 2 + 1];
99 SHA1Update(&context
, (unsigned char *)password
, strlen(password
));
100 SHA1Update(&context
, (unsigned char *)" ", 1);
101 SHA1Update(&context
, (unsigned char *)challenge
, strlen(challenge
));
102 SHA1Final(buf
, &context
);
104 TwentyByteHex(output
, buf
);
109 /* my entropy function from Q */
110 int get_challenge_entropy(unsigned char *data
) {
111 if (!challenge_random_fd
) {
112 challenge_random_fd
= fopen(CHALLENGE_RANDOM_LOCATION
, "rb");
113 if(!challenge_random_fd
)
117 if(!entropy_remaining
) {
118 if(fread(entropybuffer
, 1, sizeof(entropybuffer
), challenge_random_fd
) != sizeof(entropybuffer
))
120 entropy_remaining
= CHALLENGE_ENTROPYBUF
;
123 memcpy(data
, entropybuffer
+ (CHALLENGE_ENTROPYBUF
- entropy_remaining
) * CHALLENGE_ENTROPYLEN
, CHALLENGE_ENTROPYLEN
);
129 int generate_nonce(unsigned char *nonce
, int nterfacer
) {
130 unsigned char entropy
[CHALLENGE_ENTROPYLEN
];
132 if(!get_challenge_entropy(entropy
))
135 gettimeofday(&tvv
, NULL
);
137 memcpy(nonce
, &tvv
, sizeof(struct timeval
));
138 memcpy(nonce
+ sizeof(struct timeval
) - 2, entropy
, NONCE_LEN
- sizeof(struct timeval
) + 2);
149 char *get_random_hex(void) {
150 static char output
[CHALLENGE_ENTROPYLEN
* 2 + 1];
151 unsigned char stored
[CHALLENGE_ENTROPYLEN
];
152 if(get_challenge_entropy(stored
)) {
153 TwentyByteHex(output
, stored
);
160 char *int_to_hex(unsigned char *input
, char *buf
, int len
) {
163 sprintf(buf
+ i
* 2, "%.2x", input
[i
]);
164 *(buf
+ len
* 2) = '\0';
168 int hex_to_int(char *input
, unsigned char *buf
, int buflen
) {
170 for(i
=0;i
<buflen
;i
++) {
171 if((0xff == hexlookup
[(int)input
[i
* 2]]) || (0xff == hexlookup
[(int)input
[i
* 2 + 1]])) {
174 buf
[i
] = (hexlookup
[(int)input
[i
* 2]] << 4) | hexlookup
[(int)input
[i
* 2 + 1]];
180 char *request_error(int errn
) {
181 static char err
[100];
186 snc(err
, "Memory error");
188 case RE_SERVICE_NOT_FOUND
:
189 snc(err
, "Service not found on this nterfaced instance");
191 case RE_REQUEST_REJECTED
:
192 snc(err
, "Transport handler rejected request");
194 case RE_CONNECTION_CLOSED
:
195 snc(err
, "Transport handler closed connection");
197 case RE_TRANSPORT_NOT_FOUND
:
198 snc(err
, "Transport module not yet loaded for this service");
200 case RE_COMMAND_NOT_FOUND
:
201 snc(err
, "Command not found on this service");
203 case RE_WRONG_ARG_COUNT
:
204 snc(err
, "Wrong number of arguments for this command");
206 case RE_TOO_MANY_ARGS
:
207 snc(err
, "Too many arguments supplied");
209 case RE_SERVICER_NOT_FOUND
:
210 snc(err
, "Service not found on this nterfacer instance");
213 snc(err
, "Unable to find error message");
215 err
[sizeof(err
) - 1] = '\0';