このコミットが含まれているのは:
守矢諏訪子 2023-11-29 23:26:26 +09:00
コミット f5e1d88824
6個のファイルの変更113行の追加14行の削除

ファイルの表示

@ -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

29
genpass.c ノーマルファイル
ファイルの表示

@ -0,0 +1,29 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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);
}

8
genpass.h ノーマルファイル
ファイルの表示

@ -0,0 +1,8 @@
#ifndef GENPASS_H
#define GENPASS_H
#include <stdbool.h>
void genpass(int count, bool issecure);
#endif

51
listpass.c ノーマルファイル
ファイルの表示

@ -0,0 +1,51 @@
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
#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);
}

6
listpass.h ノーマルファイル
ファイルの表示

@ -0,0 +1,6 @@
#ifndef LISTPASS_H
#define LISTPASS_H
void listpass(char* basePath, int level);
#endif

31
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-id> 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-id> 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();