+ readconfig(configfile);
+ initlogthing("onak", config.logfile);
+ catchsignals();
+
+ if ((argc - optind) < 1) {
+ usage();
+ } else if (!strcmp("dump", argv[optind])) {
+ dbctx = config.dbinit(true);
+ dumpstate.count = dumpstate.filenum = 0;
+ dumpstate.maxcount = 100000;
+ dumpstate.fd = -1;
+ dumpstate.filebase = "keydump.%d.pgp";
+ dbctx->iterate_keys(dbctx, dump_func, &dumpstate);
+ if (dumpstate.fd != -1) {
+ close(dumpstate.fd);
+ dumpstate.fd = -1;
+ }
+ dbctx->cleanupdb(dbctx);
+ } else if (!strcmp("add", argv[optind])) {
+ if (binary) {
+ result = read_openpgp_stream(stdin_getchar, NULL,
+ &packets, 0);
+ logthing(LOGTHING_INFO,
+ "read_openpgp_stream: %d", result);
+ } else {
+ dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
+ }
+ if (packets != NULL) {
+ result = parse_keys(packets, &keys);
+ free_packet_list(packets);
+ packets = NULL;
+ logthing(LOGTHING_INFO, "Finished reading %d keys.",
+ result);
+
+ result = cleankeys(keys);
+ logthing(LOGTHING_INFO, "%d keys cleaned.",
+ result);
+
+ dbctx = config.dbinit(false);
+ logthing(LOGTHING_NOTICE, "Got %d new keys.",
+ dbctx->update_keys(dbctx, &keys,
+ false));
+ if (keys != NULL && update) {
+ flatten_publickey(keys,
+ &packets,
+ &list_end);
+ if (binary) {
+ write_openpgp_stream(stdout_putchar,
+ NULL,
+ packets);
+ } else {
+ armor_openpgp_stream(stdout_putchar,
+ NULL,
+ packets);
+ }
+ free_packet_list(packets);
+ packets = NULL;
+ }
+ dbctx->cleanupdb(dbctx);
+ } else {
+ rc = 1;
+ logthing(LOGTHING_NOTICE, "No keys read.");
+ }
+
+ if (keys != NULL) {
+ free_publickey(keys);
+ keys = NULL;
+ } else {
+ rc = 1;
+ logthing(LOGTHING_NOTICE, "No changes.");
+ }
+ } else if (!strcmp("clean", argv[optind])) {
+ if (binary) {
+ result = read_openpgp_stream(stdin_getchar, NULL,
+ &packets, 0);
+ logthing(LOGTHING_INFO,
+ "read_openpgp_stream: %d", result);
+ } else {
+ dearmor_openpgp_stream(stdin_getchar, NULL, &packets);
+ }
+
+ if (packets != NULL) {
+ result = parse_keys(packets, &keys);
+ free_packet_list(packets);
+ packets = NULL;
+ logthing(LOGTHING_INFO, "Finished reading %d keys.",
+ result);
+
+ if (keys != NULL) {
+ result = cleankeys(keys);
+ logthing(LOGTHING_INFO, "%d keys cleaned.",
+ result);
+
+ flatten_publickey(keys,
+ &packets,
+ &list_end);
+
+ if (binary) {
+ write_openpgp_stream(stdout_putchar,
+ NULL,
+ packets);
+ } else {
+ armor_openpgp_stream(stdout_putchar,
+ NULL,
+ packets);
+ }
+ free_packet_list(packets);
+ packets = NULL;
+ }
+ } else {
+ rc = 1;
+ logthing(LOGTHING_NOTICE, "No keys read.");
+ }
+
+ if (keys != NULL) {
+ free_publickey(keys);
+ keys = NULL;
+ }
+ } else if ((argc - optind) == 2) {
+ search = argv[optind+1];
+ if (search != NULL && strlen(search) == 42 &&
+ search[0] == '0' && search[1] == 'x') {
+ for (i = 0; i < MAX_FINGERPRINT_LEN; i++) {
+ fp[i] = (hex2bin(search[2 + i * 2]) << 4) +
+ hex2bin(search[3 + i * 2]);
+ }
+ isfp = true;
+ } else if (search != NULL) {
+ keyid = strtoul(search, &end, 16);
+ if (*search != 0 &&
+ end != NULL &&
+ *end == 0) {
+ ishex = true;
+ }
+ }
+ dbctx = config.dbinit(false);
+ if (!strcmp("index", argv[optind])) {
+ find_keys(dbctx, search, keyid, fp, ishex, isfp,
+ fingerprint, skshash,
+ false, false);
+ } else if (!strcmp("vindex", argv[optind])) {
+ find_keys(dbctx, search, keyid, fp, ishex, isfp,
+ fingerprint, skshash,
+ false, true);
+ } else if (!strcmp("getphoto", argv[optind])) {
+ if (!ishex) {
+ puts("Can't get a key on uid text."
+ " You must supply a keyid.");
+ } else if (dbctx->fetch_key_id(dbctx, keyid, &keys,
+ false)) {
+ unsigned char *photo = NULL;
+ size_t length = 0;
+
+ if (getphoto(keys, 0, &photo,
+ &length) == ONAK_E_OK) {
+ fwrite(photo,
+ 1,
+ length,
+ stdout);
+ }
+ free_publickey(keys);
+ keys = NULL;
+ } else {
+ puts("Key not found");
+ }
+ } else if (!strcmp("delete", argv[optind])) {
+ dbctx->delete_key(dbctx,
+ dbctx->getfullkeyid(dbctx, keyid),
+ false);
+ } else if (!strcmp("get", argv[optind])) {
+ if (!(ishex || isfp)) {
+ puts("Can't get a key on uid text."
+ " You must supply a keyid / "
+ "fingerprint.");
+ } else if ((isfp &&
+ dbctx->fetch_key_fp(dbctx, fp,
+ MAX_FINGERPRINT_LEN,
+ &keys, false)) ||
+ (ishex &&
+ dbctx->fetch_key_id(dbctx, keyid,
+ &keys, false))) {
+ logthing(LOGTHING_INFO, "Got key.");
+ flatten_publickey(keys,
+ &packets,
+ &list_end);
+ free_publickey(keys);
+ if (binary) {
+ write_openpgp_stream(stdout_putchar,
+ NULL,
+ packets);
+ } else {
+ armor_openpgp_stream(stdout_putchar,
+ NULL,
+ packets);
+ }
+ free_packet_list(packets);
+ packets = NULL;
+ } else {
+ puts("Key not found");
+ }
+ } else if (!strcmp("hget", argv[optind])) {
+ if (!parse_skshash(search, &hash)) {
+ puts("Couldn't parse sks hash.");
+ } else if (dbctx->fetch_key_skshash(dbctx, &hash,
+ &keys)) {
+ logthing(LOGTHING_INFO, "Got key.");
+ flatten_publickey(keys,
+ &packets,
+ &list_end);
+ free_publickey(keys);
+ if (binary) {
+ write_openpgp_stream(stdout_putchar,
+ NULL,
+ packets);
+ } else {
+ armor_openpgp_stream(stdout_putchar,
+ NULL,
+ packets);
+ }
+ free_packet_list(packets);
+ packets = NULL;
+ } else {
+ puts("Key not found");
+ }
+ }
+ dbctx->cleanupdb(dbctx);