+ readconfig(configfile);
+ initlogthing("onak", config.logfile);
+ catchsignals();
+
+ if ((argc - optind) < 1) {
+ usage();
+ } else if (!strcmp("dump", argv[optind])) {
+ config.dbbackend->initdb(true);
+ dumpstate.count = dumpstate.filenum = 0;
+ dumpstate.maxcount = 1000000;
+ dumpstate.fd = -1;
+ dumpstate.filebase = "keydump.%d.pgp";
+ config.dbbackend->iterate_keys(dump_func, &dumpstate);
+ if (dumpstate.fd != -1) {
+ close(dumpstate.fd);
+ dumpstate.fd = -1;
+ }
+ config.dbbackend->cleanupdb();
+ } 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);
+
+ config.dbbackend->initdb(false);
+ logthing(LOGTHING_NOTICE, "Got %d new keys.",
+ config.dbbackend->update_keys(&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;
+ }
+ config.dbbackend->cleanupdb();
+ } 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) {
+ keyid = strtoul(search, &end, 16);
+ if (*search != 0 &&
+ end != NULL &&
+ *end == 0) {
+ ishex = true;
+ }
+ }
+ config.dbbackend->initdb(false);
+ if (!strcmp("index", argv[optind])) {
+ find_keys(search, keyid, ishex, fingerprint,
+ false, false);
+ } else if (!strcmp("vindex", argv[optind])) {
+ find_keys(search, keyid, ishex, fingerprint,
+ 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 (config.dbbackend->fetch_key(keyid, &keys,
+ false)) {
+ unsigned char *photo = NULL;
+ size_t length = 0;
+
+ if (getphoto(keys, 0, &photo, &length)) {
+ fwrite(photo,
+ 1,
+ length,
+ stdout);
+ }
+ free_publickey(keys);
+ keys = NULL;
+ } else {
+ puts("Key not found");
+ }
+ } else if (!strcmp("delete", argv[optind])) {
+ config.dbbackend->delete_key(
+ config.dbbackend->getfullkeyid(keyid),
+ false);
+ } else if (!strcmp("get", argv[optind])) {
+ if (!ishex) {
+ puts("Can't get a key on uid text."
+ " You must supply a keyid.");
+ } else if (config.dbbackend->fetch_key(keyid, &keys,
+ false)) {
+ logthing(LOGTHING_INFO, "Got key.");
+ flatten_publickey(keys,
+ &packets,
+ &list_end);
+ free_publickey(keys);
+ armor_openpgp_stream(stdout_putchar,
+ NULL,
+ packets);
+ free_packet_list(packets);
+ packets = NULL;
+ } else {
+ puts("Key not found");
+ }
+ }
+ config.dbbackend->cleanupdb();