]>
Commit | Line | Data |
---|---|---|
1 | /* tools.c - miscellaneous utility functions | |
2 | * Copyright 2000-2004 srvx Development Team | |
3 | * | |
4 | * This file is part of x3. | |
5 | * | |
6 | * srvx is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with srvx; if not, write to the Free Software Foundation, | |
18 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
19 | */ | |
20 | ||
21 | #include "helpfile.h" | |
22 | #include "log.h" | |
23 | #include "nickserv.h" | |
24 | #include "recdb.h" | |
25 | ||
26 | #ifdef HAVE_NETDB_H | |
27 | #include <netdb.h> | |
28 | #endif | |
29 | #ifdef HAVE_SYS_SOCKET_H | |
30 | #include <sys/socket.h> | |
31 | #endif | |
32 | #ifdef HAVE_ARPA_INET_H | |
33 | #include <arpa/inet.h> | |
34 | #endif | |
35 | ||
36 | #define NUMNICKLOG 6 | |
37 | #define NUMNICKBASE (1 << NUMNICKLOG) | |
38 | #define NUMNICKMASK (NUMNICKBASE - 1) | |
39 | ||
40 | /* Yes, P10's encoding here is almost-but-not-quite MIME Base64. Yay | |
41 | * for gratuitous incompatibilities. */ | |
42 | static const char convert2y[256] = { | |
43 | 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P', | |
44 | 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f', | |
45 | 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v', | |
46 | 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','[',']' | |
47 | }; | |
48 | ||
49 | static const unsigned char convert2n[256] = { | |
50 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
51 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
52 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
53 | 52,53,54,55,56,57,58,59,60,61, 0, 0, 0, 0, 0, 0, | |
54 | 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, | |
55 | 15,16,17,18,19,20,21,22,23,24,25,62, 0,63, 0, 0, | |
56 | 0,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, | |
57 | 41,42,43,44,45,46,47,48,49,50,51, 0, 0, 0, 0, 0 | |
58 | }; | |
59 | ||
60 | unsigned long int | |
61 | base64toint(const char* s, int count) | |
62 | { | |
63 | unsigned int i = 0; | |
64 | while (*s && count) { | |
65 | i = (i << NUMNICKLOG) + convert2n[(unsigned char)*s++]; | |
66 | count--; | |
67 | } | |
68 | return i; | |
69 | } | |
70 | ||
71 | const char* inttobase64(char* buf, unsigned int v, unsigned int count) | |
72 | { | |
73 | buf[count] = '\0'; | |
74 | while (count > 0) { | |
75 | buf[--count] = convert2y[(unsigned char)(v & NUMNICKMASK)]; | |
76 | v >>= NUMNICKLOG; | |
77 | } | |
78 | return buf; | |
79 | } | |
80 | ||
81 | static char irc_tolower[256]; | |
82 | #undef tolower | |
83 | #define tolower(X) irc_tolower[(unsigned char)(X)] | |
84 | ||
85 | int | |
86 | irccasecmp(const char *stra, const char *strb) { | |
87 | while (*stra && (tolower(*stra) == tolower(*strb))) | |
88 | stra++, strb++; | |
89 | return tolower(*stra) - tolower(*strb); | |
90 | } | |
91 | ||
92 | int | |
93 | ircncasecmp(const char *stra, const char *strb, unsigned int len) { | |
94 | len--; | |
95 | while (*stra && (tolower(*stra) == tolower(*strb)) && len) | |
96 | stra++, strb++, len--; | |
97 | return tolower(*stra) - tolower(*strb); | |
98 | } | |
99 | ||
100 | const char * | |
101 | irccasestr(const char *haystack, const char *needle) { | |
102 | unsigned int hay_len = strlen(haystack), needle_len = strlen(needle), pos; | |
103 | if (hay_len < needle_len) | |
104 | return NULL; | |
105 | for (pos=0; pos<hay_len+1-needle_len; ++pos) { | |
106 | if ((tolower(haystack[pos]) == tolower(*needle)) | |
107 | && !ircncasecmp(haystack+pos, needle, needle_len)) | |
108 | return haystack+pos; | |
109 | } | |
110 | return NULL; | |
111 | } | |
112 | ||
113 | int | |
114 | split_line(char *line, int irc_colon, int argv_size, char *argv[]) | |
115 | { | |
116 | int argc = 0; | |
117 | int n; | |
118 | while (*line && (argc < argv_size)) { | |
119 | while (*line == ' ') | |
120 | *line++ = 0; | |
121 | if (*line == ':' && irc_colon && argc > 0) { | |
122 | /* the rest is a single parameter */ | |
123 | argv[argc++] = line + 1; | |
124 | break; | |
125 | } | |
126 | if (!*line) | |
127 | break; | |
128 | argv[argc++] = line; | |
129 | if (argc >= argv_size) | |
130 | break; | |
131 | while (*line != ' ' && *line) | |
132 | line++; | |
133 | } | |
134 | #ifdef NDEBUG | |
135 | n = 0; | |
136 | #else | |
137 | for (n=argc; n<argv_size; n++) | |
138 | argv[n] = (char*)0xFEEDBEEF; | |
139 | #endif | |
140 | return argc; | |
141 | } | |
142 | ||
143 | /* This is ircu's mmatch() function, from match.c. */ | |
144 | int mmatch(const char *old_mask, const char *new_mask) | |
145 | { | |
146 | register const char *m = old_mask; | |
147 | register const char *n = new_mask; | |
148 | const char *ma = m; | |
149 | const char *na = n; | |
150 | int wild = 0; | |
151 | int mq = 0, nq = 0; | |
152 | ||
153 | while (1) | |
154 | { | |
155 | if (*m == '*') | |
156 | { | |
157 | while (*m == '*') | |
158 | m++; | |
159 | wild = 1; | |
160 | ma = m; | |
161 | na = n; | |
162 | } | |
163 | ||
164 | if (!*m) | |
165 | { | |
166 | if (!*n) | |
167 | return 0; | |
168 | for (m--; (m > old_mask) && (*m == '?'); m--) | |
169 | ; | |
170 | if ((*m == '*') && (m > old_mask) && (m[-1] != '\\')) | |
171 | return 0; | |
172 | if (!wild) | |
173 | return 1; | |
174 | m = ma; | |
175 | ||
176 | /* Added to `mmatch' : Because '\?' and '\*' now is one character: */ | |
177 | if ((*na == '\\') && ((na[1] == '*') || (na[1] == '?'))) | |
178 | ++na; | |
179 | ||
180 | n = ++na; | |
181 | } | |
182 | else if (!*n) | |
183 | { | |
184 | while (*m == '*') | |
185 | m++; | |
186 | return (*m != 0); | |
187 | } | |
188 | if ((*m == '\\') && ((m[1] == '*') || (m[1] == '?'))) | |
189 | { | |
190 | m++; | |
191 | mq = 1; | |
192 | } | |
193 | else | |
194 | mq = 0; | |
195 | ||
196 | /* Added to `mmatch' : Because '\?' and '\*' now is one character: */ | |
197 | if ((*n == '\\') && ((n[1] == '*') || (n[1] == '?'))) | |
198 | { | |
199 | n++; | |
200 | nq = 1; | |
201 | } | |
202 | else | |
203 | nq = 0; | |
204 | ||
205 | /* | |
206 | * This `if' has been changed compared to match() to do the following: | |
207 | * Match when: | |
208 | * old (m) new (n) boolean expression | |
209 | * * any (*m == '*' && !mq) || | |
210 | * ? any except '*' (*m == '?' && !mq && (*n != '*' || nq)) || | |
211 | * any except * or ? same as m (!((*m == '*' || *m == '?') && !mq) && | |
212 | * toLower(*m) == toLower(*n) && | |
213 | * !((mq && !nq) || (!mq && nq))) | |
214 | * | |
215 | * Here `any' also includes \* and \? ! | |
216 | * | |
217 | * After reworking the boolean expressions, we get: | |
218 | * (Optimized to use boolean shortcircuits, with most frequently occuring | |
219 | * cases upfront (which took 2 hours!)). | |
220 | */ | |
221 | if ((*m == '*' && !mq) || | |
222 | ((!mq || nq) && tolower(*m) == tolower(*n)) || | |
223 | (*m == '?' && !mq && (*n != '*' || nq))) | |
224 | { | |
225 | if (*m) | |
226 | m++; | |
227 | if (*n) | |
228 | n++; | |
229 | } | |
230 | else | |
231 | { | |
232 | if (!wild) | |
233 | return 1; | |
234 | m = ma; | |
235 | ||
236 | /* Added to `mmatch' : Because '\?' and '\*' now is one character: */ | |
237 | if ((*na == '\\') && ((na[1] == '*') || (na[1] == '?'))) | |
238 | ++na; | |
239 | ||
240 | n = ++na; | |
241 | } | |
242 | } | |
243 | } | |
244 | ||
245 | int | |
246 | match_ircglob(const char *text, const char *glob) | |
247 | { | |
248 | const char *m = glob, *n = text; | |
249 | const char *m_tmp = glob, *n_tmp = text; | |
250 | int star_p; | |
251 | ||
252 | for (;;) switch (*m) { | |
253 | case '\0': | |
254 | if (!*n) | |
255 | return 1; | |
256 | backtrack: | |
257 | if (m_tmp == glob) | |
258 | return 0; | |
259 | m = m_tmp; | |
260 | n = ++n_tmp; | |
261 | break; | |
262 | case '\\': | |
263 | m++; | |
264 | /* allow escaping to force capitalization */ | |
265 | if (*m++ != *n++) | |
266 | return 0; | |
267 | break; | |
268 | case '*': case '?': | |
269 | for (star_p = 0; ; m++) { | |
270 | if (*m == '*') | |
271 | star_p = 1; | |
272 | else if (*m == '?') { | |
273 | if (!*n++) | |
274 | goto backtrack; | |
275 | } else break; | |
276 | } | |
277 | if (star_p) { | |
278 | if (!*m) | |
279 | return 1; | |
280 | else if (*m == '\\') { | |
281 | m_tmp = ++m; | |
282 | if (!*m) | |
283 | return 0; | |
284 | for (n_tmp = n; *n && *n != *m; n++) ; | |
285 | } else { | |
286 | m_tmp = m; | |
287 | for (n_tmp = n; *n && tolower(*n) != tolower(*m); n++) ; | |
288 | } | |
289 | } | |
290 | /* and fall through */ | |
291 | default: | |
292 | if (!*n) | |
293 | return *m == '\0'; | |
294 | if (tolower(*m) != tolower(*n)) | |
295 | goto backtrack; | |
296 | m++; | |
297 | n++; | |
298 | break; | |
299 | } | |
300 | } | |
301 | ||
302 | extern const char *hidden_host_suffix; | |
303 | ||
304 | /* Prevent *@* *@** *@*a* type masks, while allowing anything else. This is the best way iv found to detect | |
305 | * a global matching mask; if it matches this string, it'll match almost anything :) */ | |
306 | int is_overmask(char *mask) | |
307 | { | |
308 | return(match_ircglob("abcdefghijklmnopqrstuv!frcmbghilnrtoasde@apdic.yfa.dsfsdaffsdasfdasfd.abcdefghijklmnopqrstuvwxyz.asdfasfdfsdsfdasfda.ydfbe", mask)); | |
309 | } | |
310 | ||
311 | int user_matches_glob(struct userNode *user, const char *orig_glob, int include_nick) | |
312 | { | |
313 | /* A new glob function, the old one had many false positives. | |
314 | * look at IsSetHost(user) etc and include_nick, build a nick!user@host string | |
315 | * from user and save it in a variable. Compare with match_ircglob() and | |
316 | * return the results. Match any of the possible user@host combos (ugh) -Rubin | |
317 | */ | |
318 | char *matchstr_user; | |
319 | char *matchstr_host; | |
320 | char *matchstr_full = NULL; | |
321 | ||
322 | if(IsSetHost(user)) /* S: line sethosts */ | |
323 | { | |
324 | /* Grab host and user from sethost instead of real host */ | |
325 | char *buff; | |
326 | buff = alloca(strlen(user->sethost) + 1); | |
327 | strcpy(buff, user->sethost); | |
328 | matchstr_user = mysep(&buff, "@"); | |
329 | matchstr_host = mysep(&buff, "@"); | |
330 | ||
331 | matchstr_full = alloca(strlen(user->nick) + strlen(matchstr_user) + strlen(matchstr_host) + 4); | |
332 | if(include_nick) | |
333 | sprintf(matchstr_full, "%s!%s@%s", user->nick, matchstr_user, matchstr_host); | |
334 | else | |
335 | sprintf(matchstr_full, "%s@%s", matchstr_user, matchstr_host); | |
336 | if(match_ircglob(matchstr_full, orig_glob)) | |
337 | return(1); | |
338 | } | |
339 | else if(IsFakeHost(user)) /* Fakehost */ | |
340 | { | |
341 | matchstr_full = alloca(strlen(user->nick) + strlen(user->ident) + strlen(user->fakehost) + 4); | |
342 | if(include_nick) | |
343 | sprintf(matchstr_full, "%s!%s@%s", user->nick, user->ident, user->fakehost); | |
344 | else | |
345 | sprintf(matchstr_full, "%s@%s", user->ident, user->fakehost); | |
346 | if(match_ircglob(matchstr_full, orig_glob)) | |
347 | return(1); | |
348 | } | |
349 | else if(hidden_host_suffix && user->handle_info) /* name.users.network.org host */ | |
350 | { | |
351 | matchstr_host = alloca(strlen(user->handle_info->handle) + strlen(hidden_host_suffix) + 2); | |
352 | sprintf(matchstr_host, "%s.%s", user->handle_info->handle, hidden_host_suffix); | |
353 | matchstr_user = user->ident; | |
354 | ||
355 | matchstr_full = alloca(strlen(user->nick) + strlen(user->ident) + strlen(matchstr_host) + 4); | |
356 | if(include_nick) | |
357 | sprintf(matchstr_full, "%s!%s@%s", user->nick, user->ident, matchstr_host); | |
358 | else | |
359 | sprintf(matchstr_full, "%s@%s", user->ident, matchstr_host); | |
360 | if(match_ircglob(matchstr_full, orig_glob)) | |
361 | return(1); | |
362 | } | |
363 | ||
364 | /* Check normal hostname */ | |
365 | matchstr_full = alloca(strlen(user->nick) + strlen(user->ident) + strlen(user->hostname) + 4); | |
366 | if(include_nick) | |
367 | sprintf(matchstr_full, "%s!%s@%s", user->nick, user->ident, user->hostname); | |
368 | else | |
369 | sprintf(matchstr_full, "%s@%s", user->ident, user->hostname); | |
370 | if(match_ircglob(matchstr_full, orig_glob)) | |
371 | return(1); | |
372 | ||
373 | /* Check IP hostname (could skip this if same as above?)*/ | |
374 | matchstr_host = inet_ntoa(user->ip); | |
375 | matchstr_full = alloca(strlen(user->nick) + strlen(user->ident) + strlen(matchstr_host) + 4); | |
376 | if(include_nick) | |
377 | sprintf(matchstr_full, "%s!%s@%s", user->nick, user->ident, matchstr_host); | |
378 | else | |
379 | sprintf(matchstr_full, "%s@%s", user->ident, matchstr_host); | |
380 | if(match_ircglob(matchstr_full, orig_glob)) | |
381 | return(1); | |
382 | ||
383 | return(0); /* Didnt match anything */ | |
384 | } | |
385 | ||
386 | int | |
387 | user_matches_glob_broken(struct userNode *user, const char *orig_glob, int include_nick) | |
388 | { | |
389 | char *glob, *marker; | |
390 | char *setident = NULL, *sethostname = NULL; // sethost - reed/apples | |
391 | ||
392 | /* Make a writable copy of the glob */ | |
393 | glob = alloca(strlen(orig_glob)+1); | |
394 | strcpy(glob, orig_glob); | |
395 | /* Check the nick, if it's present */ | |
396 | if (include_nick) { | |
397 | if (!(marker = strchr(glob, '!'))) { | |
398 | log_module(MAIN_LOG, LOG_ERROR, "user_matches_glob(\"%s\", \"%s\", %d) called, and glob doesn't include a '!'", user->nick, orig_glob, include_nick); | |
399 | return 0; | |
400 | } | |
401 | *marker = 0; | |
402 | if (!match_ircglob(user->nick, glob)) return 0; | |
403 | glob = marker + 1; | |
404 | } | |
405 | /* Check the ident */ | |
406 | if (!(marker = strchr(glob, '@'))) { | |
407 | log_module(MAIN_LOG, LOG_ERROR, "user_matches_glob(\"%s\", \"%s\", %d) called, and glob doesn't include an '@'", user->nick, orig_glob, include_nick); | |
408 | return 0; | |
409 | } | |
410 | *marker = 0; | |
411 | ||
412 | // sethost - reed/apples | |
413 | if (IsSetHost(user)) { | |
414 | setident = alloca(strcspn(user->sethost, "@")+2); | |
415 | safestrncpy(setident, user->sethost, strcspn(user->sethost, "@")+1); | |
416 | sethostname = strchr(user->sethost, '@') + 1; | |
417 | } | |
418 | ||
419 | if (!match_ircglob(user->ident, glob) && (IsSetHost(user) && !match_ircglob(setident, glob))) | |
420 | return 0; | |
421 | glob = marker + 1; | |
422 | /* If it might be an IP glob, test that. */ | |
423 | if (!glob[strspn(glob, "0123456789./*?")] | |
424 | && match_ircglob(inet_ntoa(user->ip), glob)) | |
425 | return 1; | |
426 | /* Check for a fakehost match. */ | |
427 | if (IsFakeHost(user) && match_ircglob(user->fakehost, glob)) | |
428 | return 1; | |
429 | if (IsSetHost(user) && match_ircglob(sethostname, glob)) | |
430 | return 1; | |
431 | /* Check for an account match. */ | |
432 | if (hidden_host_suffix && user->handle_info) { | |
433 | char hidden_host[HOSTLEN+1]; | |
434 | snprintf(hidden_host, sizeof(hidden_host), "%s.%s", user->handle_info->handle, hidden_host_suffix); | |
435 | if (match_ircglob(hidden_host, glob)) | |
436 | return 1; | |
437 | } | |
438 | /* None of the above; could only be a hostname match. */ | |
439 | return match_ircglob(user->hostname, glob); | |
440 | } | |
441 | ||
442 | int | |
443 | is_ircmask(const char *text) | |
444 | { | |
445 | while (*text && (isalnum((char)*text) || strchr("-_[]|\\`^{}?*", *text))) | |
446 | text++; | |
447 | if (*text++ != '!') | |
448 | return 0; | |
449 | while (*text && *text != '@' && !isspace((char)*text)) | |
450 | text++; | |
451 | if (*text++ != '@') | |
452 | return 0; | |
453 | while (*text && !isspace((char)*text)) | |
454 | text++; | |
455 | return !*text; | |
456 | } | |
457 | ||
458 | int | |
459 | is_gline(const char *text) | |
460 | { | |
461 | if (*text == '@') | |
462 | return 0; | |
463 | text += strcspn(text, "@!% \t\r\n"); | |
464 | if (*text++ != '@') | |
465 | return 0; | |
466 | if (!*text) | |
467 | return 0; | |
468 | while (*text && (isalnum((char)*text) || strchr(".-?*:", *text))) | |
469 | text++; | |
470 | return !*text; | |
471 | } | |
472 | ||
473 | int | |
474 | split_ircmask(char *text, char **nick, char **ident, char **host) | |
475 | { | |
476 | char *start; | |
477 | ||
478 | start = text; | |
479 | while (isalnum((char)*text) || strchr("=[]\\`^{}?*", *text)) | |
480 | text++; | |
481 | if (*text != '!' || ((text - start) > NICKLEN)) | |
482 | return 0; | |
483 | *text = 0; | |
484 | if (nick) | |
485 | *nick = start; | |
486 | ||
487 | start = ++text; | |
488 | while (*text && *text != '@' && !isspace((char)*text)) | |
489 | text++; | |
490 | if (*text != '@' || ((text - start) > USERLEN)) | |
491 | return 0; | |
492 | *text = 0; | |
493 | if (ident) | |
494 | *ident = start; | |
495 | ||
496 | start = ++text; | |
497 | while (*text && (isalnum((char)*text) || strchr(".-?*:", *text))) | |
498 | text++; | |
499 | if (host) | |
500 | *host = start; | |
501 | return !*text && ((text - start) <= HOSTLEN) && nick && ident && host; | |
502 | } | |
503 | ||
504 | char * | |
505 | sanitize_ircmask(char *input) | |
506 | { | |
507 | unsigned int length, flag; | |
508 | char *mask, *start, *output; | |
509 | ||
510 | /* Sanitize everything in place; input *must* be a valid | |
511 | hostmask. */ | |
512 | output = input; | |
513 | flag = 0; | |
514 | ||
515 | /* The nick is truncated at the end. */ | |
516 | length = 0; | |
517 | mask = input; | |
518 | while(*input++ != '!') | |
519 | { | |
520 | length++; | |
521 | } | |
522 | if(length > NICKLEN) | |
523 | { | |
524 | mask += NICKLEN; | |
525 | *mask++ = '!'; | |
526 | ||
527 | /* This flag is used to indicate following parts should | |
528 | be shifted. */ | |
529 | flag = 1; | |
530 | } | |
531 | else | |
532 | { | |
533 | mask = input; | |
534 | } | |
535 | ||
536 | /* The ident and host must be truncated at the beginning and | |
537 | replaced with a '*' to be compatible with ircu. */ | |
538 | length = 0; | |
539 | start = input; | |
540 | while(*input++ != '@') | |
541 | { | |
542 | length++; | |
543 | } | |
544 | if(length > USERLEN || flag) | |
545 | { | |
546 | if(length > USERLEN) | |
547 | { | |
548 | start = input - USERLEN; | |
549 | *mask++ = '*'; | |
550 | } | |
551 | while(*start != '@') | |
552 | { | |
553 | *mask++ = *start++; | |
554 | } | |
555 | *mask++ = '@'; | |
556 | ||
557 | flag = 1; | |
558 | } | |
559 | else | |
560 | { | |
561 | mask = input; | |
562 | } | |
563 | ||
564 | length = 0; | |
565 | start = input; | |
566 | while(*input++) | |
567 | { | |
568 | length++; | |
569 | } | |
570 | if(length > HOSTLEN || flag) | |
571 | { | |
572 | if(length > HOSTLEN) | |
573 | { | |
574 | start = input - HOSTLEN; | |
575 | *mask++ = '*'; | |
576 | } | |
577 | while(*start) | |
578 | { | |
579 | *mask++ = *start++; | |
580 | } | |
581 | *mask = '\0'; | |
582 | } | |
583 | ||
584 | return output; | |
585 | } | |
586 | ||
587 | static long | |
588 | TypeLength(char type) | |
589 | { | |
590 | switch (type) { | |
591 | case 'y': return 365*24*60*60; | |
592 | case 'M': return 30*24*60*60; | |
593 | case 'w': return 7*24*60*60; | |
594 | case 'd': return 24*60*60; | |
595 | case 'h': return 60*60; | |
596 | case 'm': return 60; | |
597 | case 's': return 1; | |
598 | default: return 0; | |
599 | } | |
600 | } | |
601 | ||
602 | /* This function is not entirely accurate as it does not take into account leap units | |
603 | * or varying months. TODO: use proper dateadd functions to calculate real seconds | |
604 | * from now for the units (eg 1M should be give us seconds till todays date next month) | |
605 | */ | |
606 | unsigned long | |
607 | ParseInterval(const char *interval) | |
608 | { | |
609 | unsigned long seconds = 0; | |
610 | int partial = 0; | |
611 | char c; | |
612 | ||
613 | /* process the string, resetting the count if we find a unit character */ | |
614 | while ((c = *interval++)) { | |
615 | if (isdigit((int)c)) { | |
616 | partial = partial*10 + c - '0'; | |
617 | } else { | |
618 | seconds += TypeLength(c) * partial; | |
619 | partial = 0; | |
620 | } | |
621 | } | |
622 | /* assume the last chunk is seconds (the normal case) */ | |
623 | return seconds + partial; | |
624 | } | |
625 | ||
626 | static long | |
627 | GetSizeMultiplier(char type) | |
628 | { | |
629 | switch (type) { | |
630 | case 'G': case 'g': return 1024*1024*1024; | |
631 | case 'M': case 'm': return 1024*1024; | |
632 | case 'K': case 'k': return 1024; | |
633 | case 'B': case 'b': return 1; | |
634 | default: return 0; | |
635 | } | |
636 | } | |
637 | ||
638 | unsigned long | |
639 | ParseVolume(const char *volume) | |
640 | { | |
641 | unsigned long accum = 0, partial = 0; | |
642 | char c; | |
643 | while ((c = *volume++)) { | |
644 | if (isdigit((int)c)) { | |
645 | partial = partial*10 + c - '0'; | |
646 | } else { | |
647 | accum += GetSizeMultiplier(c) * partial; | |
648 | partial = 0; | |
649 | } | |
650 | } | |
651 | return accum + partial; | |
652 | } | |
653 | ||
654 | int | |
655 | parse_ipmask(const char *str, struct in_addr *addr, unsigned long *mask) | |
656 | { | |
657 | int accum, pos; | |
658 | unsigned long t_a, t_m; | |
659 | ||
660 | t_a = t_m = pos = 0; | |
661 | if (addr) | |
662 | addr->s_addr = htonl(t_a); | |
663 | if (mask) | |
664 | *mask = t_m; | |
665 | while (*str) { | |
666 | if (!isdigit(*str)) | |
667 | return 0; | |
668 | accum = 0; | |
669 | do { | |
670 | accum = (accum * 10) + *str++ - '0'; | |
671 | } while (isdigit(*str)); | |
672 | if (accum > 255) | |
673 | return 0; | |
674 | t_a = (t_a << 8) | accum; | |
675 | t_m = (t_m << 8) | 255; | |
676 | pos += 8; | |
677 | if (*str == '.') { | |
678 | str++; | |
679 | while (*str == '*') { | |
680 | str++; | |
681 | if (*str == '.') { | |
682 | t_a <<= 8; | |
683 | t_m <<= 8; | |
684 | pos += 8; | |
685 | str++; | |
686 | } else if (*str == 0) { | |
687 | t_a <<= 32 - pos; | |
688 | t_m <<= 32 - pos; | |
689 | pos = 32; | |
690 | goto out; | |
691 | } else | |
692 | return 0; | |
693 | } | |
694 | } else if (*str == '/') { | |
695 | int start = pos; | |
696 | accum = 0; | |
697 | do { | |
698 | accum = (accum * 10) + *str++ - '0'; | |
699 | } while (isdigit(*str)); | |
700 | while (pos < start+accum && pos < 32) { | |
701 | t_a = (t_a << 1) | 0; | |
702 | t_m = (t_m << 1) | 1; | |
703 | pos++; | |
704 | } | |
705 | if (pos != start+accum) | |
706 | return 0; | |
707 | } else if (*str == 0) | |
708 | break; | |
709 | else | |
710 | return 0; | |
711 | } | |
712 | out: | |
713 | if (pos != 32) | |
714 | return 0; | |
715 | if (addr) | |
716 | addr->s_addr = htonl(t_a); | |
717 | if (mask) | |
718 | *mask = t_m; | |
719 | return 1; | |
720 | } | |
721 | ||
722 | char * | |
723 | unsplit_string(char *set[], unsigned int max, char *dest) | |
724 | { | |
725 | static char unsplit_buffer[MAXLEN*2]; | |
726 | unsigned int ii, jj, pos; | |
727 | ||
728 | if (!dest) | |
729 | dest = unsplit_buffer; | |
730 | for (ii=pos=0; ii<max; ii++) { | |
731 | for (jj=0; set[ii][jj]; jj++) | |
732 | dest[pos++] = set[ii][jj]; | |
733 | dest[pos++] = ' '; | |
734 | } | |
735 | dest[--pos] = 0; | |
736 | return dest; | |
737 | } | |
738 | ||
739 | char * | |
740 | intervalString(char *output, time_t interval, struct handle_info *hi) | |
741 | { | |
742 | static const struct { | |
743 | const char *msg_single; | |
744 | const char *msg_plural; | |
745 | long length; | |
746 | } unit[] = { | |
747 | { "MSG_YEAR", "MSG_YEARS", 365 * 24 * 60 * 60 }, | |
748 | { "MSG_WEEK", "MSG_WEEKS", 7 * 24 * 60 * 60 }, | |
749 | { "MSG_DAY", "MSG_DAYS", 24 * 60 * 60 }, | |
750 | { "MSG_HOUR", "MSG_HOURS", 60 * 60 }, | |
751 | { "MSG_MINUTE", "MSG_MINUTES", 60 }, | |
752 | { "MSG_SECOND", "MSG_SECONDS", 1 } | |
753 | }; | |
754 | struct language *lang; | |
755 | const char *msg; | |
756 | unsigned int type, words, pos, count; | |
757 | ||
758 | lang = hi ? hi->language : lang_C; | |
759 | if(!interval) | |
760 | { | |
761 | msg = language_find_message(lang, "MSG_0_SECONDS"); | |
762 | return strcpy(output, msg); | |
763 | } | |
764 | ||
765 | for (type = 0, words = pos = 0; | |
766 | interval && (words < 2) && (type < ArrayLength(unit)); | |
767 | type++) { | |
768 | if (interval < unit[type].length) | |
769 | continue; | |
770 | count = interval / unit[type].length; | |
771 | interval = interval % unit[type].length; | |
772 | ||
773 | if (words++ == 1) { | |
774 | msg = language_find_message(lang, "MSG_AND"); | |
775 | pos += sprintf(output + pos, "%s ", msg); | |
776 | } | |
777 | if (count == 1) | |
778 | msg = language_find_message(lang, unit[type].msg_single); | |
779 | else | |
780 | msg = language_find_message(lang, unit[type].msg_plural); | |
781 | pos += sprintf(output + pos, "%d%s", count, msg); | |
782 | } | |
783 | ||
784 | output[pos] = 0; | |
785 | return output; | |
786 | } | |
787 | ||
788 | int | |
789 | getipbyname(const char *name, unsigned long *ip) | |
790 | { | |
791 | struct hostent *he = gethostbyname(name); | |
792 | if (!he) | |
793 | return 0; | |
794 | if (he->h_addrtype != AF_INET) | |
795 | return 0; | |
796 | memcpy(ip, he->h_addr_list[0], sizeof(*ip)); | |
797 | return 1; | |
798 | } | |
799 | ||
800 | DEFINE_LIST(string_buffer, char) | |
801 | ||
802 | void | |
803 | string_buffer_append_substring(struct string_buffer *buf, const char *tail, unsigned int len) | |
804 | { | |
805 | while (buf->used + len >= buf->size) { | |
806 | if (!buf->size) | |
807 | buf->size = 16; | |
808 | else | |
809 | buf->size <<= 1; | |
810 | buf->list = realloc(buf->list, buf->size*sizeof(buf->list[0])); | |
811 | } | |
812 | memcpy(buf->list + buf->used, tail, len+1); | |
813 | buf->used += len; | |
814 | } | |
815 | ||
816 | void | |
817 | string_buffer_append_string(struct string_buffer *buf, const char *tail) | |
818 | { | |
819 | string_buffer_append_substring(buf, tail, strlen(tail)); | |
820 | } | |
821 | ||
822 | void | |
823 | string_buffer_append_vprintf(struct string_buffer *buf, const char *fmt, va_list args) | |
824 | { | |
825 | va_list working; | |
826 | unsigned int len; | |
827 | int ret; | |
828 | ||
829 | VA_COPY(working, args); | |
830 | len = strlen(fmt); | |
831 | if (!buf->list || ((buf->used + buf->size) < len)) { | |
832 | buf->size = buf->used + len; | |
833 | buf->list = realloc(buf->list, buf->size); | |
834 | } | |
835 | ret = vsnprintf(buf->list + buf->used, buf->size - buf->used, fmt, working); | |
836 | if (ret <= 0) { | |
837 | /* pre-C99 behavior; double buffer size until it is big enough */ | |
838 | va_end(working); | |
839 | VA_COPY(working, args); | |
840 | while ((ret = vsnprintf(buf->list + buf->used, buf->size - buf->used, fmt, working)) <= 0) { | |
841 | buf->size += len; | |
842 | buf->list = realloc(buf->list, buf->size); | |
843 | va_end(working); | |
844 | VA_COPY(working, args); | |
845 | } | |
846 | buf->used += ret; | |
847 | } else if (buf->used + ret < buf->size) { | |
848 | /* no need to increase allocation size */ | |
849 | buf->used += ret; | |
850 | } else { | |
851 | /* now we know exactly how much space we need */ | |
852 | if (buf->size <= buf->used + ret) { | |
853 | buf->size = buf->used + ret + 1; | |
854 | buf->list = realloc(buf->list, buf->size); | |
855 | } | |
856 | va_end(working); | |
857 | VA_COPY(working, args); | |
858 | buf->used += vsnprintf(buf->list + buf->used, buf->size, fmt, working); | |
859 | } | |
860 | va_end(working); | |
861 | va_end(args); | |
862 | } | |
863 | ||
864 | void string_buffer_append_printf(struct string_buffer *buf, const char *fmt, ...) | |
865 | { | |
866 | va_list args; | |
867 | va_start(args, fmt); | |
868 | string_buffer_append_vprintf(buf, fmt, args); | |
869 | } | |
870 | ||
871 | void | |
872 | string_buffer_replace(struct string_buffer *buf, unsigned int from, unsigned int len, const char *repl) | |
873 | { | |
874 | unsigned int repl_len = strlen(repl); | |
875 | if (from > buf->used) | |
876 | return; | |
877 | if (len + from > buf->used) | |
878 | len = buf->used - from; | |
879 | buf->used = buf->used + repl_len - len; | |
880 | if (buf->size <= buf->used) { | |
881 | while (buf->used >= buf->size) | |
882 | buf->size <<= 1; | |
883 | buf->list = realloc(buf->list, buf->size*sizeof(buf->list[0])); | |
884 | } | |
885 | memmove(buf->list+from+repl_len, buf->list+from+len, strlen(buf->list+from+len)); | |
886 | strcpy(buf->list+from, repl); | |
887 | } | |
888 | ||
889 | struct string_list str_tab; | |
890 | ||
891 | const char * | |
892 | strtab(unsigned int ii) { | |
893 | if (ii > 65536) | |
894 | return NULL; | |
895 | if (ii > str_tab.size) { | |
896 | unsigned int old_size = str_tab.size; | |
897 | while (ii >= str_tab.size) | |
898 | str_tab.size <<= 1; | |
899 | str_tab.list = realloc(str_tab.list, str_tab.size*sizeof(str_tab.list[0])); | |
900 | memset(str_tab.list+old_size, 0, (str_tab.size-old_size)*sizeof(str_tab.list[0])); | |
901 | } | |
902 | if (!str_tab.list[ii]) { | |
903 | str_tab.list[ii] = malloc(12); | |
904 | sprintf(str_tab.list[ii], "%u", ii); | |
905 | } | |
906 | return str_tab.list[ii]; | |
907 | } | |
908 | ||
909 | void | |
910 | tools_init(void) | |
911 | { | |
912 | unsigned int upr, lwr; | |
913 | for (lwr=0; lwr<256; ++lwr) | |
914 | tolower(lwr) = lwr; | |
915 | for (upr='A', lwr='a'; lwr <= 'z'; ++upr, ++lwr) | |
916 | tolower(upr) = lwr; | |
917 | #ifdef WITH_PROTOCOL_P10 | |
918 | for (upr='[', lwr='{'; lwr <= '~'; ++upr, ++lwr) | |
919 | tolower(upr) = lwr; | |
920 | for (upr=0xc0, lwr=0xe0; lwr <= 0xf6; ++upr, ++lwr) | |
921 | tolower(upr) = lwr; | |
922 | for (upr=0xd8, lwr=0xf8; lwr <= 0xfe; ++upr, ++lwr) | |
923 | tolower(upr) = lwr; | |
924 | #endif | |
925 | str_tab.size = 1001; | |
926 | str_tab.list = calloc(str_tab.size, sizeof(str_tab.list[0])); | |
927 | } | |
928 | ||
929 | void | |
930 | tools_cleanup(void) | |
931 | { | |
932 | unsigned int ii; | |
933 | for (ii=0; ii<str_tab.size; ++ii) | |
934 | free(str_tab.list[ii]); | |
935 | free(str_tab.list); | |
936 | } | |
937 | ||
938 | /* mysep() is my answer to the strtok/strsep | |
939 | * issue. strsep is nice but doesn't skip | |
940 | * multiple dilimiters, which can really | |
941 | * offset tokens and cause huge corruption | |
942 | * so this function will use strsep but | |
943 | * act like strtok in that sense. | |
944 | */ | |
945 | char *mysep(char **sepstr, char *delim) | |
946 | { | |
947 | static char *retstr; | |
948 | ||
949 | if(!*sepstr || !**sepstr) | |
950 | return(NULL); | |
951 | ||
952 | do | |
953 | { | |
954 | retstr = strsep(sepstr, delim); | |
955 | }while (retstr && !(*retstr)); | |
956 | ||
957 | return(retstr); | |
958 | } | |
959 | ||
960 | char *time2str(time_t thetime) | |
961 | { | |
962 | char *buf, *tmp; | |
963 | ||
964 | buf = ctime(&thetime); | |
965 | tmp = (char *)strchr(buf, '\n'); | |
966 | *tmp = '\0'; | |
967 | return(buf); | |
968 | } | |
969 |