From: 諏訪子 Date: Sun, 4 Feb 2024 16:30:42 +0000 (+0900) Subject: 複数パスワードの確認(未完了) X-Git-Url: http://10.11.0.4:5575/?a=commitdiff_plain;h=a17b7860b387a0030a37416adbe99aa8f63c13ec;p=sp.git 複数パスワードの確認(未完了) --- diff --git a/Makefile b/Makefile index b6f30ff..d4d93d6 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ ifeq ($(UNAME_S),NetBSD) PREFIX=/usr/pkg endif CC=cc -FILES=main.c showpass.c yankpass.c addpass.c delpass.c listpass.c genpass.c initpass.c otppass.c base32.c +FILES=main.c showpass.c yankpass.c addpass.c delpass.c listpass.c genpass.c initpass.c otppass.c base32.c chkpass.c CFLAGS=-Wall -Wextra -O3 -I${PREFIX}/include -L${PREFIX}/lib LDFLAGS=-lgpgme -lcrypto diff --git a/chkpass.c b/chkpass.c new file mode 100644 index 0000000..976efbb --- /dev/null +++ b/chkpass.c @@ -0,0 +1,89 @@ +// TODO:未完了 +#include +#include +#include +#include +#include + +#include "chkpass.h" +#include "showpass.h" + +struct PassList { + char filepath[1024]; + char password[256]; +}; + +void chkpass(char* basePath) { + char *lang = getenv("SP_LANG"); + + // パスワードを保存する + struct PassList* allpass = malloc(2048 * sizeof(struct PassList)); + if (allpass == NULL) { + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to allocating memory"); + else perror("メモリを役割に失敗"); + return; + } + + struct dirent* entry; + DIR* dir = opendir(basePath); + if (!dir) { + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Could not opening directory"); + else perror("ディレクトリを開けられません"); + free(allpass); + return; + } + + int i = 0; + + while ((entry = readdir(dir)) != NULL) { + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { + continue; + } + + 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) 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) perror("Failed to reading file status"); + else perror("ファイル状況を読込に失敗"); + continue; + } + + if (!S_ISREG(statbuf.st_mode)) { + continue; + } + + char* filename = entry->d_name; + char* ext = strstr(filename, ".gpg"); + if (ext) *ext = '\0'; + char spath[2048]; + snprintf(spath, sizeof(spath), "%s/%s", path, filename); + + if (i >= 2048) { + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Too much password"); + else perror("パスワードが多すぎる"); + break; + } + + strcpy(allpass[i].filepath, spath); + strcpy(allpass[i].password, showpass(spath)); + i++; + } + + closedir(dir); + + // TODO: パスワードの確認 + /* for (size_t i = 0; i < sizeof(allpass) / sizeof(allpass[0]); i++) { */ + /* char passpath[1000]; */ + /* snprintf(passpath, sizeof(passpath), "%s/%s", basePath, allpass.filepath); */ + /* } */ + + // 掃除 + free(allpass); +} diff --git a/chkpass.h b/chkpass.h new file mode 100644 index 0000000..26d674b --- /dev/null +++ b/chkpass.h @@ -0,0 +1,6 @@ +#ifndef CHKPASS_H +#define CHKPASS_H + +void chkpass(char* basePath); + +#endif diff --git a/main.c b/main.c index 1d712a1..7564006 100644 --- a/main.c +++ b/main.c @@ -9,7 +9,7 @@ #include "showpass.h" #include "yankpass.h" #include "listpass.h" -/* #include "chkpass.h" */ +#include "chkpass.h" #include "addpass.h" #include "delpass.h" #include "genpass.h" @@ -55,7 +55,42 @@ void helpme_en() { printf("%s -v : Show version\n", sofname); } -int main (int argc, char* argv[]) { +char* getfullpath(char* arg) { + char *lang = getenv("SP_LANG"); + + char* homedir = getenv("HOME"); + if (homedir == NULL) { + if (lang != NULL && 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 (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to allocating memory"); + else perror("メモリの役割に失敗"); + 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 = getenv("SP_LANG"); if (argc < 2) { @@ -64,57 +99,58 @@ int main (int argc, char* argv[]) { return 0; } - if (argc == 3 && strcmp(argv[1], "-i") == 0) initpass(argv[2]); - else if (argc == 3 && strcmp(argv[1], "-s") == 0) printf("%s", 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 (argc == 3) { + if (strcmp(argv[1], "-i") == 0) initpass(argv[2]); + else if (strcmp(argv[1], "-s") == 0) printf("%s", 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; - char* basedir = "/.local/share/sp/"; - snprintf(basePath, sizeof(basePath), "%s%s", homedir, basedir); + otppass(fullPath); + free(fullPath); + } else { + if (lang != NULL && strncmp(lang, "en", 2) == 0) helpme_en(); + else helpme(); + return 1; + } + } else if (argc == 2) { + char* basePath = getfullpath(NULL); + if (basePath == NULL) return -1; - listpass(basePath, 0); - } - /* else if (argc == 3 && strcmp(argv[1], "-c") == 0) chkpass(); */ - 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); - 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); + if (strcmp(argv[1], "-l") == 0) listpass(basePath, 0); + else if (strcmp(argv[1], "-c") == 0) chkpass(basePath); + else if (strcmp(argv[1], "-v") == 0) printf("%s-%s\n", sofname, version); else { if (lang != NULL && strncmp(lang, "en", 2) == 0) helpme_en(); else helpme(); + free(basePath); + 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; + free(basePath); + } else if (strcmp(argv[1], "-g") == 0) { + if (argc != 3 && argc != 4) { + if (lang != NULL && strncmp(lang, "en", 2) == 0) helpme_en(); + else helpme(); + return 1; } - char* basedir = "/.local/share/sp/"; - snprintf(fullPath, sizeof(fullPath), "%s%s%s.gpg", homedir, basedir, argv[2]); - - otppass(fullPath); - } - else if (argc == 2 && strcmp(argv[1], "-v") == 0) printf("%s-%s\n", sofname, version); - else { + 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 if (strcmp(argv[1], "-h") == 0) { + if (lang != NULL && strncmp(lang, "en", 2) == 0) helpme_en(); + else helpme(); + } else { if (lang != NULL && strncmp(lang, "en", 2) == 0) helpme_en(); else helpme(); + return 1; } return 0;