aboutsummaryrefslogtreecommitdiff
path: root/pass_util.c
blob: bcc4aaa1f422b692ce1f52a662decca09463b817 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <linux/limits.h>

#include "pass_util.h"
#include "util.h"
#include "gpg.h"

#define DEF_PASS_DIR "pass"

char pass_dir[PATH_MAX];

int set_pass_dir(void);

int set_pass_dir(void) {
	const char *env;

	env = getenv("PASSWORD_STORE_DIR");
	if (env) {
		strncpy(pass_dir, env, sizeof(pass_dir) - 1);
		return 0;
	}

	env = getenv("XDG_DATA_HOME");
	if (env) {
		snprintf(pass_dir, sizeof(pass_dir) - 1, "%s/%s", env,
			DEF_PASS_DIR);
		return 0;
	}

	env = getenv("HOME");
	if (env) {
		snprintf(pass_dir, sizeof(pass_dir) - 1, "%s/%s/%s", env,
			".local/share", DEF_PASS_DIR);
		return 0;
	}

	return 1;
}

int pass_init(const char *fpr)
{
	int r;
	char gpg_id_path[PATH_MAX];
	FILE *gpg_id;

	r = set_pass_dir();
	if (r)
		err_die("PASSWORD_STORE_DIR not set");

	r = gpg_key_validate(fpr);
	if (r)
		err_die("key not usable, try gpg --full-generate-key");


	r = r_mkdir(pass_dir, S_IRWXU);
	if (r)
		err_die("%s %s", pass_dir, strerror(errno));

	r = snprintf(gpg_id_path, sizeof(gpg_id_path), "%s/%s", pass_dir, ".gpg-id");
	if (r > (int) sizeof(gpg_id_path))
		err_die("path exceeded PATH_MAX");

	gpg_id = fopen(gpg_id_path, "w");
	if (!gpg_id)
		err_die("%s %s", gpg_id_path, strerror(errno));

	r = fwrite(fpr, strlen(fpr), 1,gpg_id);
	if (!r)
		err_die("write failed");

	fclose(gpg_id);

	return 0;
}