From f5e1d88824fe53e762d19666e192bd90a57bbe29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AB=8F=E8=A8=AA=E5=AD=90?= Date: Wed, 29 Nov 2023 23:26:26 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=82=E3=81=86=EF=BC=92=E3=81=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 2 +- genpass.c | 29 +++++++++++++++++++++++++++++ genpass.h | 8 ++++++++ listpass.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ listpass.h | 6 ++++++ main.c | 31 ++++++++++++++++++------------- 6 files changed, 113 insertions(+), 14 deletions(-) create mode 100644 genpass.c create mode 100644 genpass.h create mode 100644 listpass.c create mode 100644 listpass.h diff --git a/Makefile b/Makefile index 95f4ea6..41e2fc4 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ VERSION=1.0.0 # Linux、Haiku、かIllumos = /usr、FreeBSDかOpenBSD = /usr/local、NetBSD = /usr/pkg PREFIX=/usr CC=cc -FILES=main.c showpass.c yankpass.c addpass.c delpass.c +FILES=main.c showpass.c yankpass.c addpass.c delpass.c listpass.c genpass.c CFLAGS=-Wall -Wextra -g LDFLAGS=-lgpgme diff --git a/genpass.c b/genpass.c new file mode 100644 index 0000000..3d862b6 --- /dev/null +++ b/genpass.c @@ -0,0 +1,29 @@ +#include +#include +#include + +#include "genpass.h" + +void genpass(int count, bool issecure) { + const char* charset_risky = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + const char* charset_secure = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()=~-^\\|_@`[{]};:+*<>,./?"; + const char* charset = issecure ? charset_secure : charset_risky; + + FILE *fp = fopen("/dev/random", "rb"); + if (fp == NULL) { + perror("/dev/randomを開けられませんでした。"); + exit(EXIT_FAILURE); + } + + char password[count + 1]; + for (int i = 0; i < count; i++) { + unsigned char key; + fread(&key, sizeof(key), 1, fp); + password[i] = charset[key % strlen(charset)]; + } + + password[count] = '\0'; + fclose(fp); + + printf("%s\n", password); +} diff --git a/genpass.h b/genpass.h new file mode 100644 index 0000000..d7f1d0d --- /dev/null +++ b/genpass.h @@ -0,0 +1,8 @@ +#ifndef GENPASS_H +#define GENPASS_H + +#include + +void genpass(int count, bool issecure); + +#endif diff --git a/listpass.c b/listpass.c new file mode 100644 index 0000000..a5e5932 --- /dev/null +++ b/listpass.c @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include + +#include "listpass.h" + +void listpass(char* basePath, int level) { + struct dirent* entry; + DIR* dir = opendir(basePath); + if (!dir) { + perror("ディレクトリを開けられません。"); + return; + } + + while ((entry = readdir(dir)) != NULL) { + if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { + char path[1000]; + int needed = snprintf(path, sizeof(path), "%s/%s", basePath, entry->d_name); + if (needed >= (int)sizeof(path) || needed < 0) { + fprintf(stderr, "エラー:パスが長すぎる、又は長さを受取に失敗。"); + continue; + } + + struct stat statbuf; + if (stat(path, &statbuf) == -1) { + perror("ファイル状況を読込に失敗。"); + continue; + } + + for (int i = 0; i < level; i++) { + printf(" "); + } + + if (S_ISDIR(statbuf.st_mode)) { + 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"); + if (ext) { + *ext = '\0'; + } + printf("|-- %s\n", filename); + } + } + } + + closedir(dir); +} diff --git a/listpass.h b/listpass.h new file mode 100644 index 0000000..b13c294 --- /dev/null +++ b/listpass.h @@ -0,0 +1,6 @@ +#ifndef LISTPASS_H +#define LISTPASS_H + +void listpass(char* basePath, int level); + +#endif diff --git a/main.c b/main.c index e0d80f5..a1ccf4e 100644 --- a/main.c +++ b/main.c @@ -8,10 +8,10 @@ void initpass(char* gpgid); #include "showpass.h" #include "yankpass.h" -void listpass(); +#include "listpass.h" #include "addpass.h" #include "delpass.h" -void genpass(char* file, int count, bool issecure); +#include "genpass.h" void otppass(char* file); void helpme(); @@ -20,16 +20,16 @@ const char* version = "1.0.0"; void helpme() { printf("使い方:\n"); - //printf("%s -i :GPGと使ってパスワードストレージを初期設定\n", sofname); - printf("%s -s <パスワード名> :パスワードを表示\n", sofname); - printf("%s -y <パスワード名> :パスワードを表示せずクリップボードにコピーする\n", sofname); - printf("%s -l :パスワード一覧を表示\n", sofname); - printf("%s -a <パスワード名> :パスワードを追加\n", sofname); - printf("%s -d <パスワード名> :パスワードを削除\n", sofname); - //printf("%s -g <文字数> [risk|secure] <パスワード名> :希望文字数でパスワードをランダムに作成する。risk=英数字のみ(不安)、secure=英数字+特別文字(デフォルト)を使用\n", sofname); - //printf("%s -o <パスワード名>\n :ワンタイムパスワード(TOTP)を表示。存在しなければ、創作する", sofname); - printf("%s -h :ヘルプを表示\n", sofname); - printf("%s -v :バージョンを表示\n", sofname); + /* printf("%s -i :GPGと使ってパスワードストレージを初期設定\n", sofname); */ + printf("%s -s <パスワード名> :パスワードを表示\n", sofname); + printf("%s -y <パスワード名> :パスワードを表示せずクリップボードにコピーする\n", sofname); + printf("%s -l :パスワード一覧を表示\n", sofname); + printf("%s -a <パスワード名> :パスワードを追加\n", sofname); + printf("%s -d <パスワード名> :パスワードを削除\n", sofname); + printf("%s -g <文字数> [risk|secure] :希望文字数でパスワードをランダムに作成する。risk=英数字のみ(不安)、secure=英数字+特別文字(デフォルト)を使用\n", sofname); + /* printf("%s -o <パスワード名>\n :ワンタイムパスワード(TOTP)を表示。存在しなければ、創作する", sofname); */ + printf("%s -h :ヘルプを表示\n", sofname); + printf("%s -v :バージョンを表示\n", sofname); } int main (int argc, char* argv[]) { @@ -56,7 +56,12 @@ int main (int argc, char* argv[]) { } else if (argc == 3 && strcmp(argv[1], "-a") == 0) addpass(argv[2]); else if (argc == 3 && strcmp(argv[1], "-d") == 0) delpass(argv[2]); - else if ((argc == 4 || argc == 5) && strcmp(argv[1], "-g") == 0) printf("TODO: パスワードを創作\n"); + 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); + else helpme(); + } else if (argc == 3 && strcmp(argv[1], "-o") == 0) printf("TODO: otp\n"); else if (argc == 2 && strcmp(argv[1], "-v") == 0) printf("%s-%s\n", sofname, version); else helpme();