comparison mcabber/src/utils.c @ 452:dfd9c62b3a39

Jabber Id syntax checks
author Mikael Berthe <mikael@lilotux.net>
date Mon, 26 Sep 2005 22:08:48 +0200
parents 4470868f90e5
children 471c9ccde028
comparison
equal deleted inserted replaced
451:8a7b18b837a4 452:dfd9c62b3a39
27 #include <stdarg.h> 27 #include <stdarg.h>
28 #include <time.h> 28 #include <time.h>
29 #include <unistd.h> 29 #include <unistd.h>
30 #include <sys/types.h> 30 #include <sys/types.h>
31 #include <sys/stat.h> 31 #include <sys/stat.h>
32 #include <ctype.h>
32 33
33 #include <config.h> 34 #include <config.h>
34 #include "logprint.h" 35 #include "logprint.h"
35 36
36 static int DebugEnabled; 37 static int DebugEnabled;
246 struct timespec req; 247 struct timespec req;
247 req.tv_sec = 0; 248 req.tv_sec = 0;
248 req.tv_nsec = (long)usec * 1000L; 249 req.tv_nsec = (long)usec * 1000L;
249 nanosleep(&req, NULL); 250 nanosleep(&req, NULL);
250 } 251 }
252
253 /**
254 * Derived from libjabber/jid.c, because the libjabber version is not
255 * really convenient for our usage.
256 *
257 * Check if the full JID is valid
258 * Return 0 if it is valid, non zero otherwise
259 */
260 int check_jid_syntax(char *jid)
261 {
262 char *str;
263 char *domain, *resource;
264 int domlen;
265
266 if (!jid) return 1;
267
268 domain = strchr(jid, '@');
269 if (!domain) return 1;
270
271 /* node identifiers may not be longer than 1023 bytes */
272 if ((domain == jid) || (domain-jid > 1023))
273 return 1;
274 domain++;
275
276 /* check for low and invalid ascii characters in the username */
277 for (str = jid; *str != '@'; str++) {
278 if (*str <= 32 || *str == ':' || *str == '@' ||
279 *str == '<' || *str == '>' || *str == '\'' ||
280 *str == '"' || *str == '&') {
281 return 1;
282 }
283 }
284
285 /* the username is okay as far as we can tell without LIBIDN */
286
287 resource = strchr(domain, '/');
288
289 /* the resource is optional */
290 if (resource) {
291 domlen = resource - domain;
292 resource++;
293 /* resources may not be longer than 1023 bytes */
294 if ((*resource == '\0') || strlen(resource) > 1023)
295 return 1;
296 } else {
297 domlen = strlen(domain);
298 }
299
300 /* there must be a domain identifier */
301 if (domlen == 0) return 1;
302
303 /* and it must not be longer than 1023 bytes */
304 if (domlen > 1023) return 1;
305
306 /* make sure the hostname is valid characters */
307 for (str = domain; *str != '\0' && *str != '/'; str++) {
308 if (!(isalnum(*str) || *str == '.' || *str == '-' || *str == '_'))
309 return 1;
310 }
311
312 /* it's okay as far as we can tell without LIBIDN */
313 return 0;
314 }
315
316 void mc_strtolower(char *str)
317 {
318 if (!str) return;
319 for ( ; *str; str++)
320 *str = tolower(*str);
321 }