commit 93e35606b8dc6768262d7dcd04243b5eef5d4680
parent 4c6fe52be63de7ff9c1e6070b8d3955a5754a6eb
Author: Naveen Narayanan zerous <zerous@nocebo.space>
Date: Mon, 11 Dec 2017 13:11:49 +0300
init implemented
Diffstat:
pass.c | | | 99 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- |
1 file changed, 98 insertions(+), 1 deletion(-)
diff --git a/pass.c b/pass.c
@@ -1,10 +1,107 @@
+#include <fcntl.h>
+#include <limits.h>
+#include <getopt.h>
#include <stdio.h>
+#include <err.h>
#include <string.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "pass.h"
+
+extern char* optarg;
+
+void
+usage(void)
+{
+ return;
+}
+
+int
+isInit(void) /* check if .password-store exists */
+{
+ int fp;
+ const char *home;
+ char file[PATH_MAX];
+ struct stat sb;
+
+ if (!(home = getenv("HOME")))
+ errx(1, "$HOME not set, cannot determine password-store location");
+
+ snprintf(file, sizeof(file), "%s/.password-store", home);
+
+#ifdef dbg
+ printf("filename: %s\n", file);
+#endif
+ if ((fp = open(file, O_RDONLY)) != -1)
+ if (!fstat(fp, &sb))
+ if (S_ISDIR(sb.st_mode)) {
+ close(fp);
+ return 1;
+ }
+ return 0;
+}
+
+void
+initPass(char *pgpid)
+{
+ char file[PATH_MAX];
+ const char* home;
+ FILE *gpg;
+ mode_t mode = S_IRWXU;
+
+ if (!isInit())
+ {
+ if (!(home = getenv("HOME")))
+ errx(1, "$HOME not set, cannot determine password-store location");
+
+ snprintf(file, sizeof(file), "%s/.password-store", home);
+
+ if (mkdir(file, mode))
+ err(1, "mkdir");
+
+ snprintf(file, sizeof(file), "%s/.password-store/.gpg-id", home);
+#ifdef dbg
+ printf("file: %s", file);
+#endif
+ if (!(gpg = fopen(file, "a")))
+ err(1, "fopen");
+
+ if (fputs(pgpid, gpg) == EOF)
+ err(1, "fputs");
+
+ printf("Password store initialized\n");
+ fclose(gpg);
+
+ } else {
+ errx(1, "Password store initialized");
+ }
+}
int
-main (int argc, char** argv)
+main(int argc, char** argv)
{
+ int init, c;
+ const char *str;
+ struct option longopts[] = {
+ {"init", required_argument, &init, 1}
+ };
+ c = 0;
+ init = 0;
+ str = "";
+ while ((c = getopt_long(argc, argv, str, longopts, NULL)) != 1)
+ switch (c) {
+ case 0:
+ if (init) {
+ initPass(optarg);
+ }
+ break;
+ default:
+ usage();
+ exit(1);
+ }
return 0;
}