]>
jfr.im git - irc/quakenet/newserv.git/blob - nterfacer/library.c
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/sha2.h"
17 #include "../lib/rijndael.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 *random_fd
= NULL
;
52 int getcopyconfigitemint(char *section
, char *key
, int def
, int *value
) {
57 snprintf(buf
, sizeof(buf
), "%d", def
);
58 ini
= getcopyconfigitem(section
, key
, buf
, 15);
63 r
= 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
[32];
95 static char output
[sizeof(buf
) * 2 + 1];
98 SHA256_Init(&context
);
99 SHA256_Update(&context
, (unsigned char *)challenge
, strlen(challenge
));
100 SHA256_Update(&context
, (unsigned char *)":", 1);
101 SHA256_Update(&context
, (unsigned char *)password
, strlen(password
));
102 SHA256_Final(buf
, &context
);
104 SHA256_Init(&context
);
105 SHA256_Update(&context
, (unsigned char *)buf
, 32);
106 SHA256_Final(buf
, &context
);
108 ThirtyTwoByteHex(output
, buf
);
113 int get_entropy(unsigned char *buf
, int bytes
) {
115 random_fd
= fopen(RANDOM_LOCATION
, "rb");
120 fread(buf
, 1, bytes
, random_fd
);
125 int generate_nonce(unsigned char *nonce
, int nterfacer
) {
126 unsigned char entropy
[20], output
[32];
130 if(!get_entropy(entropy
, 20))
133 gettimeofday(&tvv
, NULL
);
136 SHA256_Update(&c
, entropy
, 20);
137 SHA256_Update(&c
, (unsigned char *)&tvv
, sizeof(struct timeval
));
138 SHA256_Final(output
, &c
);
139 memcpy(nonce
, output
, 16);
150 char *int_to_hex(unsigned char *input
, char *buf
, int len
) {
153 sprintf(buf
+ i
* 2, "%.2x", input
[i
]);
154 *(buf
+ len
* 2) = '\0';
158 int hex_to_int(char *input
, unsigned char *buf
, int buflen
) {
160 for(i
=0;i
<buflen
;i
++) {
161 if((0xff == hexlookup
[(int)input
[i
* 2]]) || (0xff == hexlookup
[(int)input
[i
* 2 + 1]])) {
164 buf
[i
] = (hexlookup
[(int)input
[i
* 2]] << 4) | hexlookup
[(int)input
[i
* 2 + 1]];
170 char *request_error(int errn
) {
171 static char err
[100];
176 snc(err
, "Memory error");
178 case RE_SERVICE_NOT_FOUND
:
179 snc(err
, "Service not found on this nterfaced instance");
181 case RE_REQUEST_REJECTED
:
182 snc(err
, "Transport handler rejected request");
184 case RE_CONNECTION_CLOSED
:
185 snc(err
, "Transport handler closed connection");
187 case RE_TRANSPORT_NOT_FOUND
:
188 snc(err
, "Transport module not yet loaded for this service");
190 case RE_COMMAND_NOT_FOUND
:
191 snc(err
, "Command not found on this service");
193 case RE_WRONG_ARG_COUNT
:
194 snc(err
, "Wrong number of arguments for this command");
196 case RE_TOO_MANY_ARGS
:
197 snc(err
, "Too many arguments supplied");
199 case RE_SERVICER_NOT_FOUND
:
200 snc(err
, "Service not found on this nterfacer instance");
203 snc(err
, "Unable to find error message");
205 err
[sizeof(err
) - 1] = '\0';