Home | History | Annotate | Line # | Download | only in util
      1 /*	$NetBSD: mac_midna.c,v 1.2 2026/05/09 18:49:22 christos Exp $	*/
      2 
      3 /*++
      4 /* NAME
      5 /*	mac_midna 3h
      6 /* SUMMARY
      7 /*	IDNA-based mac_expand() plugin
      8 /* SYNOPSIS
      9 /*	#include <mac_midna.h>
     10 /*
     11 /*	void	mac_midna_register(void)
     12 /* DESCRIPTION
     13 /*	mac_midna_register() registers the domain_to_ascii{} and
     14 /*	domain_to_utf8{} caller-defined functions for mac_expand().
     15 /* SEE ALSO
     16 /*	mac_midna_domain(3)
     17 /* LICENSE
     18 /* .ad
     19 /* .fi
     20 /*	The Secure Mailer license must be distributed with this software.
     21 /* AUTHOR(S)
     22 /*	Wietse Venema
     23 /*	porcupine.org
     24 /*--*/
     25 
     26  /*
     27   * System library.
     28   */
     29 #include <sys_defs.h>
     30 
     31  /*
     32   * Utility library.
     33   */
     34 #include <mac_expand.h>
     35 #include <mac_midna.h>
     36 #include <mac_parse.h>
     37 #include <midna_domain.h>
     38 #include <msg.h>
     39 #include <stringops.h>
     40 #include <vstring.h>
     41 
     42 #define NAME_TO_A_LABEL	"domain_to_ascii"
     43 #define NAME_TO_U_LABEL	"domain_to_utf8"
     44 
     45 static int mac_midna_domain_to_ascii_eval(VSTRING *out, const char *name)
     46 {
     47     const char *aname;
     48 
     49 #ifndef NO_EAI
     50     if (!allascii(name)) {
     51 	if ((aname = midna_domain_to_ascii(name)) == 0) {
     52 	    msg_warn("bad domain argument in: '%s{%s}'", NAME_TO_A_LABEL, name);
     53 	    return (MAC_PARSE_ERROR);
     54 	}
     55 	if (msg_verbose)
     56 	    msg_info("%s: %s asciified to %s", __func__, name, aname);
     57     } else
     58 #endif
     59 	aname = name;
     60     if (out)
     61 	vstring_strcat(out, aname);
     62     return (MAC_PARSE_OK);
     63 }
     64 
     65 static int mac_midna_to_u_label_eval(VSTRING *out, const char *name)
     66 {
     67     const char *uname;
     68 
     69 #ifndef NO_EAI
     70     if (allascii(name)) {
     71 	if ((uname = midna_domain_to_utf8(name)) == 0) {
     72 	    msg_warn("bad domain argument in: '%s{%s}'", NAME_TO_U_LABEL, name);
     73 	    return (MAC_PARSE_ERROR);
     74 	}
     75 	if (msg_verbose)
     76 	    msg_info("%s: %s internationalzied to %s", __func__, name, uname);
     77     } else
     78 #endif
     79 	uname = name;
     80     if (out)
     81 	vstring_strcat(out, uname);
     82     return (MAC_PARSE_OK);
     83 }
     84 
     85 /* mac_midna_register - register caller-defined function */
     86 void    mac_midna_register(void)
     87 {
     88     static int first = 1;
     89 
     90     if (first) {
     91 	mac_expand_add_named_fn(NAME_TO_A_LABEL, mac_midna_domain_to_ascii_eval);
     92 	mac_expand_add_named_fn(NAME_TO_U_LABEL, mac_midna_to_u_label_eval);
     93 	first = 0;
     94     }
     95 }
     96