sp
-dist
-release
.ccls-cache
*.o
-*.tar.gz
+release
+*.core
-UNAME_S!=uname -s
-UNAME_M!=uname -m
+UNAME_S != uname -s
+UNAME_M != uname -m
-NAME!=cat main.c | grep "const char\* sofname" | awk '{print $$5}' | \
+NAME != cat main.c | grep "const char \*sofname" | awk '{print $$5}' | \
sed "s/\"//g" | sed "s/;//"
-VERSION!=cat main.c | grep "const char\* version" | awk '{print $$5}' | \
+VERSION != cat main.c | grep "const char \*version" | awk '{print $$5}' | \
sed "s/\"//g" | sed "s/;//"
-PREFIX=/usr/local
+PREFIX = /usr/local
+
+MANPREFIX = ${PREFIX}/man
.if ${UNAME_S} == "FreeBSD"
-MANPREFIX=${PREFIX}/share/man
+MANPREFIX = ${PREFIX}/share/man
.elif ${UNAME_S} == "Linux"
-PREFIX=/usr
-MANPREFIX=${PREFIX}/share/man
+PREFIX = /usr
+MANPREFIX = ${PREFIX}/share/man
.elif ${UNAME_S} == "NetBSD"
-PREFIX=/usr/pkg
-MANPREFIX=${PREFIX}/share/man
+PREFIX = /usr/pkg
+MANPREFIX = ${PREFIX}/share/man
.endif
-CC=cc
-FILES=main.c src/*.c
-CFLAGS=-Wall -Wextra -O3 -I${PREFIX}/include -L${PREFIX}/lib
+CC = cc
+FILES = main.c src/*.c
+CFLAGS = -Wall -Wextra -O3 -I${PREFIX}/include -L${PREFIX}/lib
.if ${UNAME_S} == "NetBSD"
-CFLAGS+=-I/usr/local/include -L/usr/local/lib -I/usr/include -L/usr/lib
+CFLAGS += -I/usr/local/include -L/usr/local/lib -I/usr/include -L/usr/lib
.endif
-LDFLAGS=-lgpgme -lcrypto
+LDFLAGS = -lgpgme -lcrypto
all:
${CC} ${CFLAGS} -o ${NAME} ${FILES} ${LDFLAGS}
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <stdbool.h>
-
-#include <gpgme.h>
-
+#include "src/common.h"
#include "src/initpass.h"
#include "src/showpass.h"
#include "src/yankpass.h"
#include "src/genpass.h"
#include "src/otppass.h"
-const char* sofname = "sp";
-const char* version = "1.3.0";
+const char *sofname = "sp";
+const char *version = "1.3.0";
void helpme() {
printf("076 %s %s - シンプルなパスワードマネージャー\n", sofname, version);
printf("%s -v : Show version\n", sofname);
}
-int main (int argc, char* argv[]) {
- char *lang = getenv("SP_LANG");
+char *getfullpath(char *arg) {
+ char *lang = getlang();
+
+ char *homedir = getenv("HOME");
+ if (homedir == NULL) {
+ if (strncmp(lang, "en", 2) == 0)
+ perror("Failed to getting home directory");
+ else perror("ホームディレクトリを受取に失敗");
+ return NULL;
+ }
+
+ char *basedir = "/.local/share/sp/";
+ size_t fullPathLen;
+ char *fullPath;
+ if (arg != NULL) {
+ fullPathLen = strlen(homedir) + strlen(basedir) + strlen(arg) + 5;
+ } else {
+ fullPathLen = strlen(homedir) + strlen(basedir);
+ }
+
+ fullPath = (char *)malloc(fullPathLen);
+ if (fullPath == NULL) {
+ if (strncmp(lang, "en", 2) == 0)
+ perror("Failed to allocating memory");
+ else perror("メモリの役割に失敗");
+ if (fullPath) free(fullPath);
+ if (homedir) free(homedir);
+ return NULL;
+ }
+
+ if (arg != NULL) {
+ snprintf(fullPath, fullPathLen, "%s%s%s.gpg", homedir, basedir, arg);
+ } else {
+ snprintf(fullPath, fullPathLen, "%s%s", homedir, basedir);
+ }
+
+ return fullPath;
+}
+
+int main(int argc, char *argv[]) {
+ char *lang = getlang();
if (argc < 2) {
if (lang != NULL && strncmp(lang, "en", 2) == 0) helpme_en();
return 0;
}
- if (argc == 3 && strcmp(argv[1], "-i") == 0) initpass(argv[2]);
- else if (argc == 3 && strcmp(argv[1], "-s") == 0) showpass(argv[2]);
- else if (argc == 3 && strcmp(argv[1], "-y") == 0) yankpass(argv[2]);
- else if (argc == 2 && strcmp(argv[1], "-l") == 0) {
- char basePath[512];
- char* homedir = getenv("HOME");
- if (homedir == NULL) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
- perror("Failed to getting home directory");
- else perror("ホームディレクトリを受取に失敗");
- return -1;
+ if (strcmp(argv[1], "-g") == 0) {
+ if (argc != 3 && argc != 4) {
+ if (strncmp(lang, "en", 2) == 0) helpme_en();
+ else helpme();
+ return 1;
}
- char* basedir = "/.local/share/sp/";
- snprintf(basePath, sizeof(basePath), "%s%s", homedir, basedir);
-
- listpass(basePath, 0);
- }
- else if (argc == 3 && strcmp(argv[1], "-a") == 0) addpass(argv[2]);
- else if (argc == 3 && strcmp(argv[1], "-d") == 0) delpass(argv[2], 0);
- else if (argc == 3 && strcmp(argv[1], "-e") == 0) {
- delpass(argv[2], 1);
- addpass(argv[2]);
- }
- else if (strcmp(argv[1], "-g") == 0) {
- if (argc == 3) genpass(atoi(argv[2]), true);
+ if (argc == 3) genpass(atoi(argv[2]), true);
else if (argc == 4 && strcmp(argv[3], "risk") == 0)
genpass(atoi(argv[2]), false);
else if (argc == 4 && strcmp(argv[3], "secure") == 0)
genpass(atoi(argv[2]), true);
- else {
+
+ return 0;
+ }
+
+ if (argc == 3) {
+ if (strcmp(argv[1], "-i") == 0) initpass(argv[2]);
+ else if (strcmp(argv[1], "-s") == 0) printf("%s\n", showpass(argv[2]));
+ else if (strcmp(argv[1], "-y") == 0) yankpass(argv[2]);
+ else if (strcmp(argv[1], "-a") == 0) addpass(argv[2]);
+ else if (strcmp(argv[1], "-d") == 0) delpass(argv[2], 0);
+ else if (strcmp(argv[1], "-e") == 0) {
+ delpass(argv[2], 1);
+ addpass(argv[2]);
+ }
+ else if (strcmp(argv[1], "-o") == 0) {
+ char *fullPath = getfullpath(argv[2]);
+ if (fullPath == NULL) return -1;
+ otppass(fullPath);
+ if (fullPath) free(fullPath);
+ } else {
if (lang != NULL && strncmp(lang, "en", 2) == 0) helpme_en();
else helpme();
+ return 1;
}
- }
- else if (argc == 3 && strcmp(argv[1], "-o") == 0) {
- char fullPath[512];
- char* homedir = getenv("HOME");
- if (homedir == NULL) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
- perror("Failed to getting home directory");
- else perror("ホームディレクトリを受取に失敗");
- return -1;
- }
-
- char* basedir = "/.local/share/sp/";
- snprintf(fullPath, sizeof(fullPath), "%s%s%s.gpg", homedir, basedir, argv[2]);
+ } else if (argc == 2) {
+ char *basePath = getfullpath(NULL);
+ if (basePath == NULL) return -1;
- otppass(fullPath);
- }
- else if (argc == 2 && strcmp(argv[1], "-v") == 0)
- printf("%s-%s\n", sofname, version);
- else {
- if (lang != NULL && strncmp(lang, "en", 2) == 0) helpme_en();
- else helpme();
+ if (strcmp(argv[1], "-l") == 0) listpass(basePath, 0);
+ else if (strcmp(argv[1], "-v") == 0) printf("%s-%s\n", sofname, version);
+ else {
+ if (strncmp(lang, "en", 2) == 0) helpme_en();
+ else helpme();
+ if (basePath) free(basePath);
+ return 1;
+ }
+ if (basePath) free(basePath);
+ } else {
+ if (strncmp(lang, "en", 2) == 0) helpme_en();
+ else helpme();
+ return 1;
}
return 0;
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <locale.h>
-#include <unistd.h>
-#include <sys/stat.h>
-#include <errno.h>
-
-#include <gpgme.h>
-#include <termios.h>
-
#include "addpass.h"
void cleanup(gpgme_ctx_t ctx, gpgme_key_t key, gpgme_data_t in, gpgme_data_t out) {
gpgme_key_release(key);
}
-int mkdir_r(const char *path, mode_t mode) {
- char tmp[256];
- char *p = NULL;
- size_t len;
-
- snprintf(tmp, sizeof(tmp), "%s", path);
- len = strlen(tmp);
- if (tmp[len - 1] == '/') {
- tmp[len - 1] = 0; // 最後の「/」文字を取り消す
- }
-
- for (p = tmp + 1; *p; p++) {
- if (*p == '/') {
- // 最後の「/」文字を取り消す
- *p = 0;
- if (mkdir(tmp, mode) != 0 && errno != EEXIST) return -1;
- // また追加
- *p = '/';
- }
- }
-
- if (mkdir(tmp, mode) != 0 && errno != EEXIST) {
- return -1;
- }
-
- return 0;
-}
-
-void getpasswd(char* prompt, char*pw, size_t pwlen) {
+void getpasswd(char *prompt, char *pw, size_t pwlen) {
struct termios old, new;
printf("%s", prompt);
tcsetattr(fileno(stdin), TCSANOW, &old);
}
-void addpass(char* file) {
- char *lang = getenv("SP_LANG");
+void addpass(char *file) {
+ char *lang = getlang();
// パスを準備
- char* homedir = getenv("HOME");
+ char *homedir = getenv("HOME");
if (homedir == NULL) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to retrieving home directory");
else perror("ホームディレクトリを受取に失敗");
return;
}
- char* basedir = "/.local/share/sp/";
- char* ext = ".gpg";
+ char *basedir = "/.local/share/sp/";
+ char *ext = ".gpg";
char pass[256];
char knin[256];
int alllen = snprintf(NULL, 0, "%s%s%s%s", homedir, basedir, file, ext) + 1;
- char* gpgpathchk = malloc(alllen);
+ char *gpgpathchk = malloc(alllen);
if (gpgpathchk == NULL) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to allocating memory");
else perror("メモリを割当に失敗");
return;
// ファイルが既に存在するかどうか確認
snprintf(gpgpathchk, alllen, "%s%s%s%s", homedir, basedir, file, ext);
+
if (access(gpgpathchk, F_OK) != -1) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
fprintf(
stderr,
"Password already exist.\nFor edit, please run ' sp -e %s '.\n",
free(gpgpathchk);
// パスワードを受け取る
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
getpasswd("Password: ", pass, sizeof(pass));
else getpasswd("パスワード: ", pass, sizeof(pass));
puts("");
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
getpasswd("Password (for verification): ", knin, sizeof(knin));
else getpasswd("パスワード(確認用): ", knin, sizeof(knin));
puts("");
// パスワードが一致するかどうか確認
if (strcmp(pass, knin) != 0) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Password does not match. Ending...");
else perror("パスワードが一致していません。終了…");
return;
// GPGMEを創作
err = gpgme_new(&ctx);
if (err) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
fprintf(stderr, "Failed to generating GPGME: %s\n", gpgme_strerror(err));
else fprintf(stderr, "GPGMEを創作に失敗:%s\n", gpgme_strerror(err));
return;
// GPGMEは非対話的モードに設定
err = gpgme_set_pinentry_mode(ctx, GPGME_PINENTRY_MODE_LOOPBACK);
if (err) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
fprintf(stderr, "Failed to setting pinentry mode: %s\n", gpgme_strerror(err));
else fprintf(stderr, "pinentryモードを設定に失敗: %s\n", gpgme_strerror(err));
gpgme_release(ctx);
// パスワードからデータオブジェクトを創作
err = gpgme_data_new_from_mem(&in, pass, strlen(pass), 0);
if (err) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
fprintf(stderr, "Failed to making data object: %s\n", gpgme_strerror(err));
else
fprintf(stderr, "データオブジェクトを創作に失敗: %s\n", gpgme_strerror(err));
// 鍵を受け取る
char keypath[256];
snprintf(keypath, sizeof(keypath), "%s%s%s", homedir, basedir, ".gpg-id");
+
keypath[sizeof(keypath) - 1] = '\0';
FILE* keyfile = fopen(keypath, "rb");
if (keyfile == NULL) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0) {
+ if (strncmp(lang, "en", 2) == 0) {
perror("Failed to opening .gpg-id file");
fprintf(stderr, "Failed path: %s\n", keypath);
} else {
return;
}
- char* keyid = malloc(256);
+ char *keyid = malloc(256);
if (!fgets(keyid, 256, keyfile)) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to reading key ID");
else perror("鍵IDを読込に失敗");
fclose(keyfile);
err = gpgme_get_key(ctx, keyid, &key[0], 0);
if (err) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
fprintf(stderr, "Failed to getting key: %s\n", gpgme_strerror(err));
else fprintf(stderr, "鍵を受取に失敗: %s\n", gpgme_strerror(err));
free(keyid);
}
if (key[0] == NULL) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Error: Key is NULL");
+ if (strncmp(lang, "en", 2) == 0) perror("Error: Key is NULL");
else perror("エラー:鍵はNULLです");
free(keyid);
return;
// 暗号化
err = gpgme_op_encrypt(ctx, &key[0], GPGME_ENCRYPT_ALWAYS_TRUST, in, out);
if (err) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
fprintf(stderr, "Failed to encrypt: %s\n", gpgme_strerror(err));
else fprintf(stderr, "暗号化に失敗: %s\n", gpgme_strerror(err));
cleanup(ctx, key[0], in, out);
}
// 暗号化したタイルを開く
- char* gpgpath = malloc(alllen);
+ char *gpgpath = malloc(alllen);
if (gpgpath == NULL) {
cleanup(ctx, key[0], in, out);
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to allocating memory");
else perror("メモリを割当に失敗");
return;
// ディレクトリを創作
char dirpath[512];
snprintf(dirpath, sizeof(dirpath), "%s%s%s", homedir, basedir, file);
+
dirpath[sizeof(dirpath) - 1] = '\0';
- char* lastsla = strrchr(dirpath, '/');
+ char *lastsla = strrchr(dirpath, '/');
if (lastsla != NULL) {
*lastsla = '\0';
if (mkdir_r(dirpath, 0755) != 0) {
free(gpgpath);
cleanup(ctx, key[0], in, out);
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to constructing directory");
else perror("ディレクトリを創作に失敗");
return;
}
snprintf(gpgpath, alllen, "%s%s%s%s", homedir, basedir, file, ext);
+
struct stat statbuf;
if (stat(gpgpath, &statbuf) == 0) {
free(gpgpath);
cleanup(ctx, key[0], in, out);
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Password is already exist");
else perror("パスワードは既に存在しています");
return;
gpgfile = fopen(gpgpath, "wb");
if (gpgfile == NULL) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0) {
+ if (strncmp(lang, "en", 2) == 0) {
perror("Failed to opening file.");
fprintf(stderr, "Failed path: %s\n", gpgpath);
} else {
// データが保存したかどうか確認
ssize_t encrypted_data_size = gpgme_data_seek(out, 0, SEEK_END);
if (encrypted_data_size <= 0) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to saving the data");
else perror("データを保存に失敗");
fclose(gpgfile);
while ((read_bytes = gpgme_data_read(out, buffer, sizeof(buffer))) > 0) {
if (fwrite(buffer, 1, (size_t)read_bytes, gpgfile) != (size_t)read_bytes) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to writing password");
else perror("パスワードを書き込みに失敗");
free(gpgpath);
free(gpgpath);
cleanup(ctx, key[0], in, out);
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
puts("I could save the password");
else puts("パスワードを保存出来ました");
}
#ifndef ADDPASS_H
#define ADDPASS_H
-#include <sys/stat.h>
+#include <locale.h>
+#include <unistd.h>
-int mkdir_r(const char *path, mode_t mode);
-void addpass(char* file);
+#include <termios.h>
+
+#include "common.h"
+
+void addpass(char *file);
#endif
--- /dev/null
+#include "common.h"
+
+char *getlang() {
+ char *lang = NULL;
+
+ lang = getenv("SP_LANG");
+ if (lang == NULL) lang = "ja";
+
+ return lang;
+}
+
+int mkdir_r(const char *path, mode_t mode) {
+ char tmp[256];
+ char *p = NULL;
+ size_t len;
+
+ snprintf(tmp, sizeof(tmp), "%s", path);
+
+ len = strlen(tmp);
+ if (tmp[len - 1] == '/') {
+ tmp[len - 1] = 0; // 最後の「/」文字を取り消す
+ }
+
+ for (p = tmp + 1; *p; p++) {
+ if (*p == '/') {
+ *p = 0; // 最後の「/」文字を取り消す
+ if (mkdir(tmp, mode) != 0 && errno != EEXIST) return -1;
+ *p = '/'; // また追加
+ }
+ }
+
+ if (mkdir(tmp, mode) != 0 && errno != EEXIST) {
+ return -1;
+ }
+
+ return 0;
+}
--- /dev/null
+#ifndef COMMON_H
+#define COMMON_H
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <sys/stat.h>
+#include <errno.h>
+
+#include <gpgme.h>
+
+char *getlang();
+int mkdir_r(const char *path, mode_t mode);
+
+#endif
#include <unistd.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
+#include "common.h"
#include "delpass.h"
-int delpass(char* file, int force) {
- char *lang = getenv("SP_LANG");
+int delpass(char *file, int force) {
+ char *lang = getlang();
// パスを準備
char pwfile[512];
- char* homedir = getenv("HOME");
+ char *homedir = getenv("HOME");
if (homedir == NULL) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to getting home directory");
else perror("ホームディレクトリを受取に失敗");
return -1;
}
- char* basedir = "/.local/share/sp/";
- char* ext = ".gpg";
+ char *basedir = "/.local/share/sp/";
+ char *ext = ".gpg";
int alllen = snprintf(NULL, 0, "%s%s%s%s", homedir, basedir, file, ext) + 1;
- char* gpgpathchk = malloc(alllen);
+ char *gpgpathchk = malloc(alllen);
if (gpgpathchk == NULL) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to allocating memory");
else perror("メモリを割当に失敗");
return -1;
// ファイルが既に存在するかどうか確認
snprintf(gpgpathchk, alllen, "%s%s%s%s", homedir, basedir, file, ext);
+
if (access(gpgpathchk, F_OK) != 0) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Password does not exist");
else perror("パスワードが存在しません");
free(gpgpathchk);
ext
);
if (needed >= (int)sizeof(pwfile)) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Error: Path is too long");
else perror("エラー:パスが長すぎる");
return -1;
}
// 削除を確認する
- if (force == 0) { // パスワードの変更のばあい、確認は不要
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (force == 0) { // パスワードの変更の場合、確認は不要
+ if (strncmp(lang, "en", 2) == 0)
printf("Is it really good if I delete the password '%s'? (y/N): ", file);
printf("パスワード「%s」を本当に削除する事が宜しいでしょうか? (y/N): ", file);
int confirm = getchar();
if (confirm != 'y' && confirm != 'Y') {
- if (lang != NULL && strncmp(lang, "en", 2) == 0) puts("Not deleted");
+ if (strncmp(lang, "en", 2) == 0) puts("Not deleted");
else puts("削除しませんでした");
return -1;
}
}
if (unlink(pwfile) == -1) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Password cannot be delete");
else perror("パスワードを削除出来ませんですた");
return -1;
if (force == 1) return 0;
- if (lang != NULL && strncmp(lang, "en", 2) == 0) puts("Deleted password");
+ if (strncmp(lang, "en", 2) == 0) puts("Deleted password");
else puts("パスワードを削除しました");
+
return 0;
}
#ifndef DELPASS_H
#define DELPASS_H
-int delpass(char* file, int force);
+int delpass(char *file, int force);
#endif
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
+#include "common.h"
#include "genpass.h"
void genpass(int count, bool issecure) {
- char *lang = getenv("SP_LANG");
+ char *lang = getlang();
- const char* charset_risky =
+ const char *charset_risky =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- const char* charset_secure =
+ const char *charset_secure =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()=~-^\\|_@`[{]};:+*<>,./?";
- const char* charset = issecure ? charset_secure : charset_risky;
+ const char *charset = issecure ? charset_secure : charset_risky;
FILE *fp = fopen("/dev/random", "rb");
if (fp == NULL) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Could not opening /dev/random");
else perror("/dev/randomを開けられませんでした");
exit(EXIT_FAILURE);
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/stat.h>
-#include <string.h>
-#include <errno.h>
-
+#include "common.h"
#include "initpass.h"
-#include "addpass.h"
-void initpass(char* gpgid) {
- char *lang = getenv("SP_LANG");
+void initpass(char *gpgid) {
+ char *lang = getlang();
- char* homedir = getenv("HOME");
+ char *homedir = getenv("HOME");
if (homedir == NULL) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to getting home directory.");
else perror("ホームディレクトリを受取に失敗。");
return;
}
- char* basedir = "/.local/share/sp/";
+ char *basedir = "/.local/share/sp/";
char dirpath[256];
snprintf(dirpath, sizeof(dirpath), "%s%s", homedir, basedir);
if (mkdir_r(dirpath, 0755) != 0 && errno != EEXIST) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to creating directory.");
else perror("ディレクトリを作成に失敗。");
return;
struct stat statbuf;
if (stat(gpgidpath, &statbuf) == 0) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror(".gpg-id file is already exist.");
else perror(".gpg-idファイルは既に存在します。");
return;
}
- FILE* gpgidfile = fopen(gpgidpath, "w");
+ FILE *gpgidfile = fopen(gpgidpath, "w");
if (gpgidfile == NULL) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to writing .gpg-id file.");
else perror(".gpg-idファイルを書き込めません。");
fclose(gpgidfile);
}
if (fputs(gpgid, gpgidfile) == EOF) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to writing .gpg-id file.");
else perror(".gpg-idファイルへの書き込みに失敗しました。");
fclose(gpgidfile);
}
fclose(gpgidfile);
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
puts("First time setup was complete.");
else puts("初期設定に完了しました。");
}
-#include <stdio.h>
#include <dirent.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <stdlib.h>
+#include "common.h"
#include "listpass.h"
-void listpass(char* basePath, int level) {
- char *lang = getenv("SP_LANG");
+void listpass(char *basePath, int level) {
+ char *lang = getlang();
- struct dirent* entry;
+ struct dirent *entry;
DIR* dir = opendir(basePath);
if (!dir) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Could not opening directory");
else perror("ディレクトリを開けられません");
return;
char path[1000];
int needed = snprintf(path, sizeof(path), "%s/%s", basePath, entry->d_name);
if (needed >= (int)sizeof(path) || needed < 0) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Error: Path is too long, or failed to getting lenth");
else perror("エラー:パスが長すぎる、又は長さを受取に失敗");
continue;
struct stat statbuf;
if (stat(path, &statbuf) == -1) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to reading file status");
else perror("ファイル状況を読込に失敗");
continue;
printf("|-- %s\n", entry->d_name);
listpass(path, level + 1);
} else if (S_ISREG(statbuf.st_mode)) {
- char* filename = entry->d_name;
- char* ext = strstr(filename, ".gpg");
+ char *filename = entry->d_name;
+ char *ext = strstr(filename, ".gpg");
if (ext) *ext = '\0';
printf("|-- %s\n", filename);
}
#ifndef LISTPASS_H
#define LISTPASS_H
-void listpass(char* basePath, int level);
+void listpass(char *basePath, int level);
#endif
-#include <stdio.h>
-#include <string.h>
#include <openssl/hmac.h>
#include <openssl/sha.h>
-#include <gpgme.h>
#include "base32.h"
+#include "common.h"
#include "otppass.h"
-unsigned char* extract_secret(const char* otpauth_url, size_t* decoded_len) {
- char *lang = getenv("SP_LANG");
+unsigned char *extract_secret(const char *otpauth_url, size_t *decoded_len) {
+ char *lang = getlang();
- const char* secret_start = strstr(otpauth_url, "secret=");
+ const char *secret_start = strstr(otpauth_url, "secret=");
if (!secret_start) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("In the middle of the OTPAuth URL, could not found secret");
else perror("OTPAuth URLの中に、シークレットを見つけられませんでした");
return NULL;
}
secret_start += 7;
- const char* secret_end = strchr(secret_start, '&');
+ const char *secret_end = strchr(secret_start, '&');
if (!secret_end) {
if (secret_start[0] != '\0') {
secret_end = secret_start + strlen(secret_start);
}
if (secret_end < secret_start) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Illegal secret range");
else perror("不正なシークレットの距離");
return NULL;
}
size_t secret_len = secret_end - secret_start;
- char* secret_encoded = (char*)malloc(secret_len + 1);
+ char *secret_encoded = (char *)malloc(secret_len + 1);
if (secret_encoded == NULL) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to allocating memory");
else perror("メモリの役割に失敗");
return NULL;
strncpy(secret_encoded, secret_start, secret_len);
secret_encoded[secret_len] = '\0';
- unsigned char* secret_decoded = base32_decode(secret_encoded, decoded_len);
+ unsigned char *secret_decoded = base32_decode(secret_encoded, decoded_len);
free(secret_encoded);
if (!secret_decoded) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to decrypting of the BASE32");
else perror("BASE32の復号化に失敗");
return NULL;
return truncated_hash % 1000000;
}
-void otppass(char* file) {
- char *lang = getenv("SP_LANG");
+void otppass(char *file) {
+ char *lang = getlang();
gpgme_ctx_t ctx;
gpgme_error_t err;
gpgme_check_version(NULL);
err = gpgme_new(&ctx);
if (err) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to generating the GPG");
else perror("GPGMEを創作に失敗");
exit(1);
err = gpgme_data_new_from_file(&in, file, 1);
if (err) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to reading the GPG file");
else perror("GPGファイルを読込に失敗");
exit(1);
err = gpgme_data_new(&out);
if (err) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to reading the GPG data");
else perror("GPGデータを読込に失敗");
exit(1);
err = gpgme_op_decrypt(ctx, in, out);
if (err) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to decrypting the GPG");
else perror("GPGを復号化に失敗");
exit(1);
}
- char* secret = gpgme_data_release_and_get_mem(out, &secret_len);
+ char *secret = gpgme_data_release_and_get_mem(out, &secret_len);
if (!secret) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to getting the GPG");
else perror("GPGを受取に失敗");
exit(1);
}
+ secret[secret_len] = '\0';
+
size_t decoded_len;
- unsigned char* secret_decoded = extract_secret(secret, &decoded_len);
+ unsigned char *secret_decoded = extract_secret(secret, &decoded_len);
if (!secret_decoded) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to decoding or exporting secret");
else perror("シークレットの抽出又はデコードに失敗しました");
free(secret);
exit(1);
}
+ secret_decoded[decoded_len] = '\0';
+
time_t current_time = time(NULL);
uint64_t counter = current_time / 30;
- uint32_t otp = generate_totp((const char*)secret_decoded, counter);
- printf("%06d\n", otp);
+ uint32_t otp = generate_totp((const char *)secret_decoded, counter);
gpgme_data_release(in);
gpgme_release(ctx);
- free(secret);
+ gpgme_free(secret);
free(secret_decoded);
+
+ printf("%06d\n", otp);
}
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdbool.h>
#include <locale.h>
-#include <gpgme.h>
-#include <string.h>
-
+#include "common.h"
#include "showpass.h"
void clean_up(
gpgme_ctx_t ctx,
gpgme_data_t in,
gpgme_data_t out,
- FILE* gpgfile,
- char* gpgpath
+ FILE *gpgfile,
+ char *gpgpath
) {
if (gpgfile) fclose(gpgfile);
if (gpgpath) free(gpgpath);
gpgme_release(ctx);
}
-void showpass(char* file) {
- char *lang = getenv("SP_LANG");
+const char *showpass(char *file) {
+ char *lang = getlang();
gpgme_ctx_t ctx;
gpgme_error_t err;
// GPGMEを創作
err = gpgme_new(&ctx);
if (err) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
fprintf(stderr, "Failed to generating GPGME: %s\n", gpgme_strerror(err));
else fprintf(stderr, "GPGMEを創作に失敗:%s\n", gpgme_strerror(err));
- return;
+ return NULL;
}
// OpenPGPプロトコールを設定
gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP);
// 暗号化したタイルを開く
- char* homedir = getenv("HOME");
+ char *homedir = getenv("HOME");
if (homedir == NULL) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to getting home directory");
else perror("ホームディレクトリを受取に失敗");
- return;
+ return NULL;
}
- char* basedir = "/.local/share/sp/";
- char* ext = ".gpg";
+ char *basedir = "/.local/share/sp/";
+ char *ext = ".gpg";
int alllen = snprintf(NULL, 0, "%s%s%s%s", homedir, basedir, file, ext) + 1;
- char* gpgpath = malloc(alllen);
+ char *gpgpath = malloc(alllen);
if (gpgpath == NULL) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to allocating memeory");
else perror("メモリを割当に失敗");
- return;
+ return NULL;
}
snprintf(gpgpath, alllen, "%s%s%s%s", homedir, basedir, file, ext);
+
gpgfile = fopen(gpgpath, "rb");
if (gpgfile == NULL) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0) {
+ if (strncmp(lang, "en", 2) == 0) {
perror("Failed to opening file");
fprintf(stderr, "Failing path: %s\n", gpgpath);
} else {
fprintf(stderr, "失敗したパス: %s\n", gpgpath);
}
free(gpgpath);
- return;
+ return NULL;
}
// ファイルからinデータオブジェクトを創作
if (gpgme_data_new_from_stream(&in, gpgfile) != GPG_ERR_NO_ERROR) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to generating the GPGME data object");
else perror("GPGMEデータオブジェクトを創作に失敗");
clean_up(ctx, in, out, gpgfile, gpgpath);
- return;
+ return NULL;
}
// outデータオブジェクトを創作
if (gpgme_data_new(&out) != GPG_ERR_NO_ERROR) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to generating the GPGME data object");
else perror("GPGMEデータオブジェクトを創作に失敗");
clean_up(ctx, in, out, gpgfile, gpgpath);
- return;
+ return NULL;
}
// データオブジェクトを創作
// 復号化して
err = gpgme_op_decrypt(ctx, in, out);
if (err) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
fprintf(stderr, "Failed to decrypting: %s\n", gpgme_strerror(err));
else fprintf(stderr, "復号化に失敗: %s\n", gpgme_strerror(err));
// 掃除
clean_up(ctx, in, out, gpgfile, gpgpath);
- return;
+ return NULL;
}
// 復号化したパスワードを表示する
gpgme_data_seek(out, 0, SEEK_SET);
char buffer[512];
+ char *res = malloc(512 * sizeof(char));
+ if (res == NULL) {
+ if (strncmp(lang, "en", 2) == 0)
+ perror("Failed to allocating memory");
+ else perror("メモリを役割に失敗");
+ clean_up(ctx, in, out, gpgfile, gpgpath);
+ return NULL;
+ }
+
ssize_t read_bytes;
- bool islastnl = false;
+ int i = 0;
while ((read_bytes = gpgme_data_read(out, buffer, sizeof(buffer) - 1)) > 0) {
- fwrite(buffer, 1, read_bytes, stdout);
- if (buffer[read_bytes - 1] == '\n') {
- islastnl = true;
- }
+ memcpy(res + i, buffer, read_bytes);
+ i += read_bytes;
}
- if (!islastnl) {
- putchar('\n');
- }
+ res[i] = '\0';
+ if (res[i-1] == '\n') res[i-1] = '\0';
// 掃除
clean_up(ctx, in, out, gpgfile, gpgpath);
+ return res;
}
#ifndef SHOWPASS_H
#define SHOWPASS_H
-void showpass(char* file);
+const char *showpass(char *file);
#endif
-#include <stdio.h>
-#include <stdlib.h>
#include <locale.h>
#include <unistd.h>
-#include <string.h>
-
-#include <gpgme.h>
+#include "common.h"
#include "yankpass.h"
#include "showpass.h"
-void yankpass(char* file) {
- char *lang = getenv("SP_LANG");
+void yankpass(char *file) {
+ char *lang = getlang();
// Xセッションではない場合(例えば、SSH、TTY、Gayland等)、showpass()を実行して
if (getenv("DISPLAY") == NULL) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
puts("There is no X session, so executing 'sp -s'.");
else puts("Xセッションではありませんので、「sp -s」を実行します。");
showpass(file);
// GPGMEを創作
err = gpgme_new(&ctx);
if (err) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
fprintf(stderr, "Failed to generating GPGME: %s\n", gpgme_strerror(err));
else fprintf(stderr, "GPGMEを創作に失敗:%s\n", gpgme_strerror(err));
return;
// 暗号化したタイルを開く
char* homedir = getenv("HOME");
if (homedir == NULL) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to getting home directory");
else perror("ホームディレクトリを受取に失敗");
return;
int alllen = snprintf(NULL, 0, "%s%s%s%s", homedir, basedir, file, ext) + 1;
char* gpgpath = malloc(alllen);
if (gpgpath == NULL) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Failed to allocating memory");
else perror("メモリを割当に失敗");
return;
snprintf(gpgpath, alllen, "%s%s%s%s", homedir, basedir, file, ext);
gpgfile = fopen(gpgpath, "rb");
if (gpgfile == NULL) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0) {
+ if (strncmp(lang, "en", 2) == 0) {
perror("Failed to opening the file");
fprintf(stderr, "Failed path: %s\n", gpgpath);
} else {
// 復号化して
err = gpgme_op_decrypt(ctx, in, out);
if (err) {
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
fprintf(stderr, "Failed to decryption: %s\n", gpgme_strerror(err));
else fprintf(stderr, "復号化に失敗: %s\n", gpgme_strerror(err));
gpgme_data_release(in);
gpgme_data_release(out);
gpgme_release(ctx);
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
perror("Could not found a clipboard");
else perror("クリップボードを見つけられませんでした");
return;
pclose(pipe);
// 45秒後、クリップボードから削除する
- if (lang != NULL && strncmp(lang, "en", 2) == 0)
+ if (strncmp(lang, "en", 2) == 0)
printf(
- "%s\n%s",
+ "%s\n%s\n",
"Added password to the clipboard.",
- "I will take it away from the clipboard after 45 second"
+ "I will take it away from the clipboard after 45 second."
);
else
printf(
- "%s\n%s",
+ "%s\n%s\n",
"パスワードをクリップボードに追加しました。",
- "45秒後はクリップボードから取り消されます"
+ "45秒後はクリップボードから取り消されます。"
);
sleep(45);
system("echo -n | xclip -selection clipboard");
#ifndef YANKPASS_H
#define YANKPASS_H
-void yankpass(char* file);
+void yankpass(char *file);
#endif