Home | History | Annotate | Line # | Download | only in util
      1 /*	$NetBSD: valid_utf8_hostname.c,v 1.2 2017/02/14 01:16:49 christos Exp $	*/
      2 
      3 /*++
      4 /* NAME
      5 /*	valid_utf8_hostname 3
      6 /* SUMMARY
      7 /*	validate (maybe UTF-8) domain name
      8 /* SYNOPSIS
      9 /*	#include <valid_utf8_hostname.h>
     10 /*
     11 /*	int	valid_utf8_hostname(
     12 /*	int	enable_utf8,
     13 /*	const char *domain,
     14 /*	int	gripe)
     15 /* DESCRIPTION
     16 /*	valid_utf8_hostname() is a wrapper around valid_hostname().
     17 /*	If EAI support is compiled in, and enable_utf8 is true, the
     18 /*	name is converted from UTF-8 to ASCII per IDNA rules, before
     19 /*	invoking valid_hostname().
     20 /* SEE ALSO
     21 /*	valid_hostname(3) STD3 hostname validation.
     22 /* DIAGNOSTICS
     23 /*	Fatal errors: memory allocation problem.
     24 /*	Warnings: malformed domain name.
     25 /* LICENSE
     26 /* .ad
     27 /* .fi
     28 /*	The Secure Mailer license must be distributed with this software.
     29 /* AUTHOR(S)
     30 /*	Wietse Venema
     31 /*	IBM T.J. Watson Research
     32 /*	P.O. Box 704
     33 /*	Yorktown Heights, NY 10598, USA
     34 /*--*/
     35 
     36  /*
     37   * System library.
     38   */
     39 #include <sys_defs.h>
     40 
     41  /*
     42   * Utility library.
     43   */
     44 #include <msg.h>
     45 #include <mymalloc.h>
     46 #include <stringops.h>
     47 #include <valid_hostname.h>
     48 #include <midna_domain.h>
     49 #include <valid_utf8_hostname.h>
     50 
     51 /* valid_utf8_hostname - validate internationalized domain name */
     52 
     53 int     valid_utf8_hostname(int enable_utf8, const char *name, int gripe)
     54 {
     55     static const char myname[] = "valid_utf8_hostname";
     56 
     57     /*
     58      * Trivial cases first.
     59      */
     60     if (*name == 0) {
     61 	if (gripe)
     62 	    msg_warn("%s: empty domain name", myname);
     63 	return (0);
     64     }
     65 
     66     /*
     67      * Convert non-ASCII domain name to ASCII and validate the result per
     68      * STD3. midna_domain_to_ascii() applies valid_hostname() to the result.
     69      * Propagate the gripe parameter for better diagnostics (note that
     70      * midna_domain_to_ascii() logs a problem only when the result is not
     71      * cached).
     72      */
     73 #ifndef NO_EAI
     74     if (enable_utf8 && !allascii(name)) {
     75 	if (midna_domain_to_ascii(name) == 0) {
     76 	    if (gripe)
     77 		msg_warn("%s: malformed UTF-8 domain name", myname);
     78 	    return (0);
     79 	} else {
     80 	    return (1);
     81 	}
     82     }
     83 #endif
     84 
     85     /*
     86      * Validate ASCII name per STD3.
     87      */
     88     return (valid_hostname(name, gripe));
     89 }
     90