static void test_zb32enc (void) { static struct { size_t datalen; char *data; const char *expected; } tests[] = { /* From the DESIGN document. */ { 1, "\x00", "y" }, { 1, "\x80", "o" }, { 2, "\x40", "e" }, { 2, "\xc0", "a" }, { 10, "\x00\x00", "yy" }, { 10, "\x80\x80", "on" }, { 20, "\x8b\x88\x80", "tqre" }, { 24, "\xf0\xbf\xc7", "6n9hq" }, { 24, "\xd4\x7a\x04", "4t7ye" }, /* The next vector is strange: The DESIGN document from 2007 gives "8ik66o" as result, the revision from 2009 gives "6im5sd". I look at it for quite some time and came to the conclusion that "6im54d" is the right encoding. */ { 30, "\xf5\x57\xbd\x0c", "6im54d" }, /* From ccrtp's Java code. */ { 40, "\x01\x01\x01\x01\x01", "yryonyeb" }, { 15, "\x01\x01", "yry" }, { 80, "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01", "yryonyebyryonyeb" }, { 15, "\x81\x81", "ogy" }, { 16, "\x81\x81", "ogyo" }, { 20, "\x81\x81\x81", "ogya" }, { 64, "\x81\x81\x81\x81\x81\x81\x81\x81", "ogyadycbogyan" }, /* More tests. */ { 160, "\x80\x61\x58\x70\xF5\xBA\xD6\x90\x33\x36" /* */"\x86\xD0\xF2\xAD\x85\xAC\x1E\x42\xB3\x67", /* */"oboioh8izmmjyc3so5exfmcfioxrfc58" }, { 0, "", "" } }; int tidx; char *output; for (tidx = 0; tidx < DIM(tests); tidx++) { output = zb32_encode (tests[tidx].data, tests[tidx].datalen); if (!output) { fprintf (stderr, PGM": error encoding test %d: %s\n", tidx, strerror (errno)); exit (1); } /* puts (output); */ if (strcmp (output, tests[tidx].expected)) fail (tidx); xfree (output); } }
/* Debug helper to encode or decode to/from zb32. */ static void endecode_file (const char *fname, int decode) { char *buffer; size_t buflen; char *result; if (decode) { fprintf (stderr, PGM": decode mode has not yet been implemented\n"); errcount++; return; } #ifdef HAVE_DOSISH_SYSTEM if (decode) setmode (fileno (stdout), O_BINARY); #endif buffer = read_file (fname, &buflen); if (!buffer) { errcount++; return; } result = zb32_encode (buffer, 8 * buflen); if (!result) { fprintf (stderr, PGM": error encoding data: %s\n", strerror (errno)); errcount++; xfree (buffer); return; } fputs (result, stdout); putchar ('\n'); xfree (result); xfree (buffer); }
/* Helper for gnupg-socketdir. This is a global function, so that * gpgconf can use it for its --create-socketdir command. If * SKIP_CHECKS is set permission checks etc. are not done. The * function always returns a malloced directory name and stores these * bit flags at R_INFO: * * 1 := Internal error, stat failed, out of core, etc. * 2 := No /run/user directory. * 4 := Directory not owned by the user, not a directory * or wrong permissions. * 8 := Same as 4 but for the subdir. * 16 := mkdir failed * 32 := Non default homedir; checking subdir. * 64 := Subdir does not exist. * 128 := Using homedir as fallback. */ char * _gnupg_socketdir_internal (int skip_checks, unsigned *r_info) { #if defined(HAVE_W32_SYSTEM) || !defined(HAVE_STAT) char *name; (void)skip_checks; *r_info = 0; name = xstrdup (gnupg_homedir ()); #else /* Unix and stat(2) available. */ static const char * const bases[] = { "/run", "/var/run", NULL}; int i; struct stat sb; char prefix[13 + 1 + 20 + 6 + 1]; const char *s; char *name = NULL; *r_info = 0; /* First make sure that non_default_homedir can be set. */ gnupg_homedir (); /* It has been suggested to first check XDG_RUNTIME_DIR envvar. * However, the specs state that the lifetime of the directory MUST * be bound to the user being logged in. Now GnuPG may also be run * as a background process with no (desktop) user logged in. Thus * we better don't do that. */ /* Check whether we have a /run/user dir. */ for (i=0; bases[i]; i++) { snprintf (prefix, sizeof prefix, "%s/user/%u", bases[i], (unsigned int)getuid ()); if (!stat (prefix, &sb) && S_ISDIR(sb.st_mode)) break; } if (!bases[i]) { *r_info |= 2; /* No /run/user directory. */ goto leave; } if (sb.st_uid != getuid ()) { *r_info |= 4; /* Not owned by the user. */ if (!skip_checks) goto leave; } if (strlen (prefix) + 7 >= sizeof prefix) { *r_info |= 1; /* Ooops: Buffer too short to append "/gnupg". */ goto leave; } strcat (prefix, "/gnupg"); /* Check whether the gnupg sub directory has proper permissions. */ if (stat (prefix, &sb)) { if (errno != ENOENT) { *r_info |= 1; /* stat failed. */ goto leave; } /* Try to create the directory and check again. */ if (gnupg_mkdir (prefix, "-rwx")) { *r_info |= 16; /* mkdir failed. */ goto leave; } if (stat (prefix, &sb)) { *r_info |= 1; /* stat failed. */ goto leave; } } /* Check that it is a directory, owned by the user, and only the * user has permissions to use it. */ if (!S_ISDIR(sb.st_mode) || sb.st_uid != getuid () || (sb.st_mode & (S_IRWXG|S_IRWXO))) { *r_info |= 4; /* Bad permissions or not a directory. */ if (!skip_checks) goto leave; } /* If a non default homedir is used, we check whether an * corresponding sub directory below the socket dir is available * and use that. We has the non default homedir to keep the new * subdir short enough. */ if (non_default_homedir) { char sha1buf[20]; char *suffix; *r_info |= 32; /* Testing subdir. */ s = gnupg_homedir (); gcry_md_hash_buffer (GCRY_MD_SHA1, sha1buf, s, strlen (s)); suffix = zb32_encode (sha1buf, 8*15); if (!suffix) { *r_info |= 1; /* Out of core etc. */ goto leave; } name = strconcat (prefix, "/d.", suffix, NULL); xfree (suffix); if (!name) { *r_info |= 1; /* Out of core etc. */ goto leave; } /* Stat that directory and check constraints. Note that we * do not auto create such a directory because we would not * have a way to remove it. Thus the directory needs to be * pre-created. The command * gpgconf --create-socketdir * can be used tocreate that directory. */ if (stat (name, &sb)) { if (errno != ENOENT) *r_info |= 1; /* stat failed. */ else *r_info |= 64; /* Subdir does not exist. */ if (!skip_checks) { xfree (name); name = NULL; goto leave; } } else if (!S_ISDIR(sb.st_mode) || sb.st_uid != getuid () || (sb.st_mode & (S_IRWXG|S_IRWXO))) { *r_info |= 8; /* Bad permissions or subdir is not a directory. */ if (!skip_checks) { xfree (name); name = NULL; goto leave; } } } else name = xstrdup (prefix); leave: /* If nothing works fall back to the homedir. */ if (!name) { *r_info |= 128; /* Fallback. */ name = xstrdup (gnupg_homedir ()); } #endif /* Unix */ return name; }