int main (void) { char buf[BUFSIZ]; char *p; int rc; size_t i; setlocale (LC_ALL, ""); printf ("Input string encoded as `%s': ", stringprep_locale_charset ()); fflush (stdout); if (!fgets (buf, BUFSIZ, stdin)) perror ("fgets"); buf[strlen (buf) - 1] = '\0'; printf ("Before locale2utf8 (length %ld): ", (long int) strlen (buf)); for (i = 0; i < strlen (buf); i++) printf ("%02x ", buf[i] & 0xFF); printf ("\n"); p = stringprep_locale_to_utf8 (buf); if (p) { strcpy (buf, p); free (p); } else printf ("Could not convert string to UTF-8, continuing anyway...\n"); printf ("Before stringprep (length %ld): ", (long int) strlen (buf)); for (i = 0; i < strlen (buf); i++) printf ("%02x ", buf[i] & 0xFF); printf ("\n"); rc = stringprep (buf, BUFSIZ, 0, stringprep_nameprep); if (rc != STRINGPREP_OK) printf ("Stringprep failed (%d): %s\n", rc, stringprep_strerror (rc)); else { printf ("After stringprep (length %ld): ", (long int) strlen (buf)); for (i = 0; i < strlen (buf); i++) printf ("%02x ", buf[i] & 0xFF); printf ("\n"); } return 0; }
int main (void) { char buf[BUFSIZ]; char *p; int rc; size_t i; setlocale (LC_ALL, ""); printf ("Input domain encoded as `%s': ", stringprep_locale_charset ()); fflush (stdout); if (!fgets (buf, BUFSIZ, stdin)) perror ("fgets"); buf[strlen (buf) - 1] = '\0'; printf ("Read string (length %ld): ", (long int) strlen (buf)); for (i = 0; i < strlen (buf); i++) printf ("%02x ", buf[i] & 0xFF); printf ("\n"); rc = idna_to_ascii_lz (buf, &p, 0); if (rc != IDNA_SUCCESS) { printf ("ToASCII() failed (%d): %s\n", rc, idna_strerror (rc)); return EXIT_FAILURE; } printf ("ACE label (length %ld): '%s'\n", (long int) strlen (p), p); for (i = 0; i < strlen (p); i++) printf ("%02x ", p[i] & 0xFF); printf ("\n"); free (p); return 0; }
int main (int argc, char *argv[]) { char buf[BUFSIZ]; char *p; int rc; size_t i; setlocale (LC_ALL, ""); printf ("Input domain encoded as `%s': ", stringprep_locale_charset ()); fflush (stdout); fgets (buf, BUFSIZ, stdin); buf[strlen (buf) - 1] = '\0'; printf ("Read string (length %d): ", strlen (buf)); for (i = 0; i < strlen (buf); i++) printf ("%02x ", buf[i] & 0xFF); printf ("\n"); rc = idna_to_ascii_lz (buf, &p, 0); if (rc != IDNA_SUCCESS) { printf ("ToASCII() failed... %d\n", rc); exit (1); } printf ("ACE label (length %d): '%s'\n", strlen (p), p); for (i = 0; i < strlen (p); i++) printf ("%02x ", p[i] & 0xFF); printf ("\n"); free (p); return 0; }
/** CryWrap entry point. * This is the main entry point - controls the whole program and so * on... */ int main(int argc, char **argv, char **envp) { crywrap_config_t *config; int server_socket; openlog(__CRYWRAP__, LOG_PID, LOG_DAEMON); gnutls_global_set_audit_log_function(tls_audit_log_func); if (gnutls_global_init() < 0) { cry_error("%s", "Global TLS state initialisation failed."); exit(1); } if (gnutls_certificate_allocate_credentials(&cred) < 0) { cry_error("%s", "Couldn't allocate credentials."); exit(1); } stringprep_locale_charset(); config = _crywrap_config_parse(argc, argv); _crywrap_tls_init(); if (config->inetd) { _crywrap_privs_drop(config); exit(_crywrap_do_one(config, 0, 1)); } if (!config->debug) if (daemon(0, 0)) { cry_error("daemon: %s", strerror(errno)); exit(1); } cry_log("%s", "Crywrap starting..."); server_socket = _crywrap_listen(config); if (server_socket < 0) exit(1); if (!config->debug) _crywrap_setup_pidfile(config); _crywrap_privs_drop(config); signal(SIGTERM, _crywrap_sighandler); signal(SIGQUIT, _crywrap_sighandler); signal(SIGSEGV, _crywrap_sighandler); signal(SIGPIPE, SIG_IGN); signal(SIGHUP, SIG_IGN); signal(SIGCHLD, _crywrap_sigchld_handler); cry_log("%s", "Accepting connections"); for (;;) { int csock; int child; csock = accept(server_socket, NULL, NULL); if (csock < 0) continue; child = fork(); switch (child) { case 0: exit(_crywrap_do_one(config, csock, csock)); break; case -1: cry_error("%s", "Forking error."); exit(1); break; } close(csock); } return 0; }
int main (int argc, char *argv[]) { struct gengetopt_args_info args_info; char readbuf[BUFSIZ]; char *p, *r; uint32_t *q; unsigned cmdn = 0; int rc; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); if (cmdline_parser (argc, argv, &args_info) != 0) return 1; if (!args_info.stringprep_given && !args_info.punycode_encode_given && !args_info.punycode_decode_given && !args_info.idna_to_ascii_given && !args_info.idna_to_unicode_given) args_info.idna_to_ascii_given = 1; if ((args_info.stringprep_given ? 1 : 0) + (args_info.punycode_encode_given ? 1 : 0) + (args_info.punycode_decode_given ? 1 : 0) + (args_info.idna_to_ascii_given ? 1 : 0) + (args_info.idna_to_unicode_given ? 1 : 0) != 1) { fprintf (stderr, _("%s: Only one of -s, -e, -d, -a or -u can be specified.\n"), argv[0]); cmdline_parser_print_help (); return 1; } if (!args_info.quiet_given) fprintf (stderr, "%s %s\n" GREETING, PACKAGE, VERSION); if (args_info.debug_given) fprintf (stderr, _("Charset `%s'.\n"), stringprep_locale_charset ()); if (!args_info.quiet_given && args_info.inputs_num == 0) fprintf (stderr, _("Type each input string on a line by itself, " "terminated by a newline character.\n")); do { if (cmdn < args_info.inputs_num) { strncpy (readbuf, args_info.inputs[cmdn++], BUFSIZ - 1); readbuf[BUFSIZ - 1] = '\0'; } else if (fgets (readbuf, BUFSIZ, stdin) == NULL) { sprintf (readbuf, _("%s: fgets() failed: "), argv[0]); if (!feof (stdin)) perror (readbuf); return 1; } if (readbuf[strlen (readbuf) - 1] == '\n') readbuf[strlen (readbuf) - 1] = '\0'; if (args_info.stringprep_given) { p = stringprep_locale_to_utf8 (readbuf); if (!p) { fprintf (stderr, _("%s: could not convert from %s to UTF-8.\n"), argv[0], stringprep_locale_charset ()); return 1; } q = stringprep_utf8_to_ucs4 (p, -1, NULL); if (!q) { free (p); fprintf (stderr, _("%s: could not convert from UTF-8 to UCS-4.\n"), argv[0]); return 1; } if (args_info.debug_given) { size_t i; for (i = 0; q[i]; i++) fprintf (stderr, _("input[%d] = U+%04x\n"), i, q[i]); } free (q); rc = stringprep_profile (p, &r, args_info.profile_given ? args_info.profile_arg : "Nameprep", 0); free (p); if (rc != STRINGPREP_OK) { fprintf (stderr, _("%s: stringprep_profile() failed with error %d.\n"), argv[0], rc); return 1; } q = stringprep_utf8_to_ucs4 (r, -1, NULL); if (!q) { free (r); fprintf (stderr, _("%s: could not convert from UTF-8 to UCS-4.\n"), argv[0]); return 1; } if (args_info.debug_given) { size_t i; for (i = 0; q[i]; i++) fprintf (stderr, _("output[%d] = U+%04x\n"), i, q[i]); } free (q); p = stringprep_utf8_to_locale (r); free (r); if (!p) { fprintf (stderr, _("%s: could not convert from UTF-8 to %s.\n"), argv[0], stringprep_locale_charset ()); return 1; } fprintf (stdout, "%s\n", p); free (p); } if (args_info.punycode_encode_given) { size_t len, len2; p = stringprep_locale_to_utf8 (readbuf); if (!p) { fprintf (stderr, _("%s: could not convert from %s to UTF-8.\n"), argv[0], stringprep_locale_charset ()); return 1; } q = stringprep_utf8_to_ucs4 (p, -1, &len); free (p); if (!q) { fprintf (stderr, _("%s: could not convert from UTF-8 to UCS-4.\n"), argv[0]); return 1; } if (args_info.debug_given) { size_t i; for (i = 0; i < len; i++) fprintf (stderr, _("input[%d] = U+%04x\n"), i, q[i]); } len2 = BUFSIZ; rc = punycode_encode (len, q, NULL, &len2, readbuf); free (q); if (rc != PUNYCODE_SUCCESS) { fprintf (stderr, _("%s: punycode_encode() failed with error %d.\n"), argv[0], rc); return 1; } readbuf[len2] = '\0'; p = stringprep_utf8_to_locale (readbuf); if (!p) { fprintf (stderr, _("%s: could not convert from UTF-8 to %s.\n"), argv[0], stringprep_locale_charset ()); return 1; } fprintf (stdout, "%s\n", p); free (p); } if (args_info.punycode_decode_given) { size_t len; len = BUFSIZ; q = (uint32_t *) malloc (len * sizeof (q[0])); if (!q) { sprintf (readbuf, _("%s: malloc() failed: "), argv[0]); perror (readbuf); return 1; } rc = punycode_decode (strlen (readbuf), readbuf, &len, q, NULL); if (rc != PUNYCODE_SUCCESS) { free (q); fprintf (stderr, _("%s: punycode_decode() failed with error %d.\n"), argv[0], rc); return 1; } if (args_info.debug_given) { size_t i; for (i = 0; i < len; i++) fprintf (stderr, _("output[%d] = U+%04x\n"), i, q[i]); } q[len] = 0; r = stringprep_ucs4_to_utf8 (q, -1, NULL, NULL); free (q); if (!r) { fprintf (stderr, _("%s: could not convert from UCS-4 to UTF-8.\n"), argv[0]); return 1; } p = stringprep_utf8_to_locale (r); free (r); if (!r) { fprintf (stderr, _("%s: could not convert from UTF-8 to %s.\n"), argv[0], stringprep_locale_charset ()); return 1; } fprintf (stdout, "%s\n", p); free (p); } if (args_info.idna_to_ascii_given) { p = stringprep_locale_to_utf8 (readbuf); if (!p) { fprintf (stderr, _("%s: could not convert from %s to UTF-8.\n"), argv[0], stringprep_locale_charset ()); return 1; } q = stringprep_utf8_to_ucs4 (p, -1, NULL); free (p); if (!q) { fprintf (stderr, _("%s: could not convert from UCS-4 to UTF-8.\n"), argv[0]); return 1; } if (args_info.debug_given) { size_t i; for (i = 0; q[i]; i++) fprintf (stderr, _("input[%d] = U+%04x\n"), i, q[i]); } rc = idna_to_ascii_4z (q, &p, (args_info.allow_unassigned_given ? IDNA_ALLOW_UNASSIGNED : 0) | (args_info.usestd3asciirules_given ? IDNA_USE_STD3_ASCII_RULES : 0)); free (q); if (rc != IDNA_SUCCESS) { fprintf (stderr, _("%s: idna_to_ascii_4z() failed " "with error %d.\n"), argv[0], rc); return 1; } #ifdef WITH_TLD if (args_info.tld_flag) { size_t errpos; rc = idna_to_unicode_8z4z (p, &q, (args_info.allow_unassigned_given ? IDNA_ALLOW_UNASSIGNED : 0) | (args_info.usestd3asciirules_given ? IDNA_USE_STD3_ASCII_RULES : 0)); if (rc != IDNA_SUCCESS) { fprintf (stderr, _("%s: TLD idna_to_unicode_8z8z() failed " "with error %d.\n"), argv[0], rc); return 1; } if (args_info.debug_given) { size_t i; for (i = 0; q[i]; i++) fprintf (stderr, _("tld[%d] = U+%04x\n"), i, q[i]); } rc = tld_check_4z (q, &errpos, NULL); if (rc == TLD_INVALID) { fprintf (stderr, _("%s: string rejected by TLD test " "(Unicode position %d)\n"), argv[0], errpos); free (q); return 1; } if (rc != TLD_SUCCESS) { fprintf (stderr, _("%s: tld_check_4z failed with error %d.\n"), argv[0], rc); free (q); return 1; } free (r); } #endif if (args_info.debug_given) { size_t i; for (i = 0; p[i]; i++) fprintf (stderr, _("output[%d] = U+%04x\n"), i, p[i]); } fprintf (stdout, "%s\n", p); free (p); } if (args_info.idna_to_unicode_given) { p = stringprep_locale_to_utf8 (readbuf); if (!p) { fprintf (stderr, _("%s: could not convert from %s to UTF-8.\n"), argv[0], stringprep_locale_charset ()); return 1; } q = stringprep_utf8_to_ucs4 (p, -1, NULL); if (!q) { free (p); fprintf (stderr, _("%s: could not convert from UCS-4 to UTF-8.\n"), argv[0]); return 1; } if (args_info.debug_given) { size_t i; for (i = 0; q[i]; i++) fprintf (stderr, _("input[%d] = U+%04x\n"), i, q[i]); } free (q); rc = idna_to_unicode_8z4z (p, &q, (args_info.allow_unassigned_given ? IDNA_ALLOW_UNASSIGNED : 0) | (args_info.usestd3asciirules_given ? IDNA_USE_STD3_ASCII_RULES : 0)); free (p); if (rc != IDNA_SUCCESS) { fprintf (stderr, _("%s: idna_to_unicode_8z4z() " "failed with error %d.\n"), argv[0], rc); return 1; } if (args_info.debug_given) { size_t i; for (i = 0; q[i]; i++) fprintf (stderr, _("output[%d] = U+%04x\n"), i, q[i]); } #ifdef WITH_TLD if (args_info.tld_flag) { size_t errpos; rc = tld_check_4z (q, &errpos, NULL); if (rc == TLD_INVALID) { fprintf (stderr, _("%s: string rejected by TLD test " "(Unicode position %d)\n"), argv[0], errpos); free (q); return 1; } if (rc != TLD_SUCCESS) { fprintf (stderr, _("%s: tld_check_4z failed with error %d.\n"), argv[0], rc); free (q); return 1; } } #endif r = stringprep_ucs4_to_utf8 (q, -1, NULL, NULL); free (q); if (!r) { fprintf (stderr, _("%s: could not convert from UTF-8 to UCS-4.\n"), argv[0]); return 1; } p = stringprep_utf8_to_locale (r); free (r); if (!r) { fprintf (stderr, _("%s: could not convert from UTF-8 to %s.\n"), argv[0], stringprep_locale_charset ()); return 1; } fprintf (stdout, "%s\n", p); free (p); } } while (!feof (stdin) && !ferror (stdin) && (args_info.inputs_num == 0 || cmdn < args_info.inputs_num)); return 0; }
int main (int argc, char *argv[]) { char buf[BUFSIZ]; char *p; uint32_t *r; int rc; size_t errpos, i; printf ("Input domain encoded as `%s': ", stringprep_locale_charset ()); fflush (stdout); fgets (buf, BUFSIZ, stdin); buf[strlen (buf) - 1] = '\0'; printf ("Read string (length %d): ", strlen (buf)); for (i = 0; i < strlen (buf); i++) printf ("%02x ", buf[i] & 0xFF); printf ("\n"); p = stringprep_locale_to_utf8 (buf); if (p) { strcpy (buf, p); free (p); } else printf ("Could not convert string to UTF-8, continuing anyway...\n"); rc = idna_to_ascii_8z (buf, &p, 0); if (rc != IDNA_SUCCESS) { printf ("idna_to_ascii_8z failed (%d): %s\n", rc, idna_strerror (rc)); return 2; } printf ("ToASCII string (length %d): %s\n", strlen (p), p); rc = idna_to_unicode_8z4z (p, &r, 0); free (p); if (rc != IDNA_SUCCESS) { printf ("idna_to_unicode_8z4z failed (%d): %s\n", rc, idna_strerror (rc)); return 2; } printf ("ToUnicode string: "); for (i = 0; r[i]; i++) printf ("U+%04x ", r[i]); printf ("\n"); rc = tld_check_4z (r, &errpos, NULL); free (r); if (rc == TLD_INVALID) { printf ("Domain rejected by TLD check, Unicode position %d\n", errpos); return 1; } else if (rc != TLD_SUCCESS) { printf ("tld_check_4z() failed (%d): %s\n", rc, tld_strerror (rc)); return 2; } printf ("Domain accepted by TLD check\n"); return 0; }
/* Find the locale used, or fall back on a default value */ char * find_locale (void) { return (char *) stringprep_locale_charset (); }
/** * stringprep_utf8_to_locale: * @str: input zero terminated string. * * Convert string encoded in UTF-8 into the locale's character set by * using stringprep_convert(). * * Return value: Returns newly allocated zero-terminated string which * is @str transcoded into the locale's character set. **/ char * stringprep_utf8_to_locale (const char *str) { return stringprep_convert (str, stringprep_locale_charset (), "UTF-8"); }
/** * stringprep_locale_to_utf8: * @str: input zero terminated string. * * Convert string encoded in the locale's character set into UTF-8 by * using stringprep_convert(). * * Return value: Returns newly allocated zero-terminated string which * is @str transcoded into UTF-8. **/ char * stringprep_locale_to_utf8 (const char *str) { return stringprep_convert (str, "UTF-8", stringprep_locale_charset ()); }