SVNからのミラー
諏訪子 <suwako@076.moe>、2026-01-21 04:43
コミット 34b8d30c50ff74f4680625e94bcdb53d6b335fec
・ファイルを見る
34個のファイル、+2927 -0
追加
CHANGELOG.md
| @@ -0,0 +1,61 @@ | ||
| 1 | +# 1.5.1 (2025年03月13日) | |
| 2 | +* OTPで、秘密鍵を自動的に大文字にする様に | |
| 3 | + | |
| 4 | +# 1.5.0 (2024年12月17日) | |
| 5 | +* パスワード表示で、「OpenPGP」かどうかの確認の追加 | |
| 6 | +* 侵害されたパスワードの確認の追加 | |
| 7 | +* 複数サイトで同じパスワードを利用かどうか、パスワードの長さ、又はパスワードの強さの確認 | |
| 8 | +* パスワードコピーの期間を設定出来る様に | |
| 9 | +* ワンタイムパスワード(OTP)を表示せずにコピー機能性の追加 | |
| 10 | +* Wayland対応の追加 | |
| 11 | +* コピータイムアウトの間にCTRL+Cを押したら、クリップボードから取り消す様に | |
| 12 | +* コンフィグファイルの追加 | |
| 13 | + | |
| 14 | +# 1.4.0 (2024年09月24日) | |
| 15 | +* Haiku対応 | |
| 16 | +* macOS対応 | |
| 17 | +* パスワードの検索の追加 | |
| 18 | +* 静的リンクはデフォルトに | |
| 19 | +* simpas情報の表示 | |
| 20 | +* ベースディレクトリを関数化 | |
| 21 | +* パスワードの編集する前に、バックアップを作成する様に | |
| 22 | + | |
| 23 | +# 1.3.0 (2024年06月17日) | |
| 24 | +* 英訳の追加 (レミリア・スカーレットさん) | |
| 25 | +* GNU Make → BSD Make | |
| 26 | +* GPLv2 → ISC | |
| 27 | +* OpenBSD向けのリリースコマンドの追加 | |
| 28 | +* FreeBSD向けのリリースコマンドの追加 | |
| 29 | +* Linux向けのリリースコマンドの追加 | |
| 30 | +* 最新ルールに従い | |
| 31 | +* NetBSD向けのリリースコマンドの追加 | |
| 32 | +* OpenBSD 7.5でTOTPの修正 (ヌル終端文字列のバグ) | |
| 33 | +* ヘルプの表示の削除 (manpageをご利用下さい) | |
| 34 | +* パスワード作成関数のデフォルトな長さは64に | |
| 35 | +* manpageを細かくに | |
| 36 | +* パスワードがなくなるまで削除したら、ディレクトリも削除する様に | |
| 37 | + | |
| 38 | +# 1.2.0 (2024年02月01日) | |
| 39 | +* やっとTOTP機能性を修正した | |
| 40 | +* makeを実行したら、バイナリがもっと小さくなる | |
| 41 | +* パスワードの長さの延長 | |
| 42 | +* パスワード追加機能性で、パスワードが既に存在するかどうか確認 | |
| 43 | +* パスワード削除機能性で、パスワードが存在ないかどうか確認 | |
| 44 | +* パスワード変更機能性の追加 | |
| 45 | +* zsh対応の修正 | |
| 46 | + | |
| 47 | +# 1.1.2 (2023年12月01日) | |
| 48 | +* OpenBSDでのコンパイラーが発生された問題を修正した | |
| 49 | + | |
| 50 | +# 1.1.1 (2023年12月01日) | |
| 51 | +* make install-zsh部分を修正 | |
| 52 | + | |
| 53 | +# 1.1.0 (2023年12月01日) | |
| 54 | +* TOTP対応 | |
| 55 | +* READMEファイルで使い方を詳しく説明する | |
| 56 | +* zshキャプチャー | |
| 57 | +* パスワード追加関数を安定化 | |
| 58 | +* パスワード表示とパスワードのコピー関数で、GNU Passで保存したパスワードの場合は改行を追加しない様に | |
| 59 | + | |
| 60 | +# 1.0.0 (2023年11月30日) | |
| 61 | +* 最初リリース |
追加
LICENSE.txt
| @@ -0,0 +1,13 @@ | ||
| 1 | +Copyright © 2018-2024 by 076.moe | |
| 2 | + | |
| 3 | +Permission to use, copy, modify, and/or distribute this software for any | |
| 4 | +purpose with or without fee is hereby granted, provided that the above | |
| 5 | +copyright notice and this permission notice appear in all copies. | |
| 6 | + | |
| 7 | +THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD | |
| 8 | +TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 9 | +FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR | |
| 10 | +CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, | |
| 11 | +DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | |
| 12 | +ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
| 13 | +SOFTWARE. |
追加
Makefile
| @@ -0,0 +1,121 @@ | ||
| 1 | +UNAME_S != uname -s | |
| 2 | +UNAME_M != uname -m | |
| 3 | +OS = ${UNAME_S} | |
| 4 | + | |
| 5 | +.if ${UNAME_S} == "OpenBSD" | |
| 6 | +OS = openbsd | |
| 7 | +.elif ${UNAME_S} == "NetBSD" | |
| 8 | +OS = netbsd | |
| 9 | +.elif ${UNAME_S} == "FreeBSD" | |
| 10 | +OS = freebsd | |
| 11 | +.elif ${UNAME_S} == "Linux" | |
| 12 | +OS = linux | |
| 13 | +.endif | |
| 14 | + | |
| 15 | +.if ${UNAME_M} == "x86_64" | |
| 16 | +UNAME_M = amd64 | |
| 17 | +.endif | |
| 18 | + | |
| 19 | +NAME != cat main.c | grep "const char \*sofname" | awk '{print $$5}' | \ | |
| 20 | + sed "s/\"//g" | sed "s/;//" | |
| 21 | +VERSION != cat main.c | grep "const char \*version" | awk '{print $$5}' | \ | |
| 22 | + sed "s/\"//g" | sed "s/;//" | |
| 23 | +PREFIX = /usr/local | |
| 24 | +.if ${UNAME_S} == "Haiku" | |
| 25 | +PREFIX = /boot/home/config/non-packaged | |
| 26 | +.elif ${UNAME_S} == "Linux" | |
| 27 | +PREFIX = /usr | |
| 28 | +.endif | |
| 29 | + | |
| 30 | +MANPREFIX = ${PREFIX}/share/man | |
| 31 | +.if ${UNAME_S} == "OpenBSD" | |
| 32 | +MANPREFIX = ${PREFIX}/man | |
| 33 | +.elif ${UNAME_S} == "Haiku" | |
| 34 | +MANPREFIX = ${PREFIX}/documentation/man | |
| 35 | +.endif | |
| 36 | + | |
| 37 | +DATAPREFIX = ${PREFIX}/share | |
| 38 | +.if ${UNAME_S} == "Haiku" | |
| 39 | +DATAPREFIX = ${PREFIX}/data | |
| 40 | +.endif | |
| 41 | + | |
| 42 | +CC = cc | |
| 43 | +FILES = main.c src/*.c | |
| 44 | + | |
| 45 | +CFLAGS = -Wall -Wextra -I/usr/include -L/usr/lib | |
| 46 | +.if ${UNAME_S} == "NetBSD" | |
| 47 | +CFLAGS += -I/usr/pkg/include -L/usr/pkg/lib -I/usr/local/include -L/usr/local/lib | |
| 48 | +.elif ${UNAME_S} == "OpenBSD" || ${UNAME_S} == "FreeBSD" | |
| 49 | +CFLAGS += -I/usr/local/include -L/usr/local/lib | |
| 50 | +.endif | |
| 51 | + | |
| 52 | +LDFLAGS = -lgpgme -lcrypto | |
| 53 | + | |
| 54 | +.if ${UNAME_S} == "OpenBSD" | |
| 55 | +LDFLAGS += -lc -lassuan -lgpg-error -lintl -liconv | |
| 56 | +.elif ${UNAME_S} == "FreeBSD" | |
| 57 | +LDFLAGS += -lc -lassuan -lgpg-error -lthr -lintl | |
| 58 | +.elif ${UNAME_S} == "NetBSD" | |
| 59 | +LDFLAGS += -lcrypt -lc -lassuan -lgpg-error -lintl | |
| 60 | +.elif ${UNAME_S} == "Linux" | |
| 61 | +LDFLAGS += -lc -lassuan -lgpg-error | |
| 62 | +.endif | |
| 63 | + | |
| 64 | +all: | |
| 65 | + ${CC} -O3 ${CFLAGS} -o ${NAME} ${FILES} -static ${LDFLAGS} | |
| 66 | + strip ${NAME} | |
| 67 | + | |
| 68 | +debug: | |
| 69 | + ${CC} -g ${CFLAGS} -o ${NAME} ${FILES} ${LDFLAGS} | |
| 70 | + | |
| 71 | +clean: | |
| 72 | + rm -f ${NAME} | |
| 73 | + | |
| 74 | +dist: clean | |
| 75 | + mkdir -p release/src ${NAME}-${VERSION} | |
| 76 | + cp -R LICENSE.txt Makefile README.md CHANGELOG.md \ | |
| 77 | + ${NAME}-completion.zsh man main.c src ${NAME}-${VERSION} | |
| 78 | + tar zcfv release/src/${NAME}-${VERSION}.tar.gz ${NAME}-${VERSION} | |
| 79 | + rm -rf ${NAME}-${VERSION} | |
| 80 | + | |
| 81 | +man: | |
| 82 | + mkdir -p release/man/${VERSION} | |
| 83 | + sed "s/VERSION/${VERSION}/g" < man/${NAME}-en.1 > \ | |
| 84 | + release/man/${VERSION}/${NAME}-en.1 | |
| 85 | + sed "s/VERSION/${VERSION}/g" < man/${NAME}-jp.1 > \ | |
| 86 | + release/man/${VERSION}/${NAME}-jp.1 | |
| 87 | + | |
| 88 | +release: | |
| 89 | + mkdir -p release/bin/${VERSION}/${OS}/${UNAME_M} | |
| 90 | + ${CC} -O3 ${CFLAGS} -o release/bin/${VERSION}/${OS}/${UNAME_M}/${NAME} ${FILES} \ | |
| 91 | + -static ${LDFLAGS} | |
| 92 | + strip release/bin/${VERSION}/${OS}/${UNAME_M}/${NAME} | |
| 93 | + | |
| 94 | +install: | |
| 95 | + mkdir -p ${DESTDIR}${PREFIX}/bin ${DESTDIR}${MANPREFIX}/man1 | |
| 96 | + cp -f ${NAME} ${DESTDIR}${PREFIX}/bin | |
| 97 | + chmod 755 ${DESTDIR}${PREFIX}/bin/${NAME} | |
| 98 | + mkdir -p ${DESTDIR}${MANPREFIX}/man1 | |
| 99 | + sed "s/VERSION/${VERSION}/g" < man/${NAME}-en.1 > \ | |
| 100 | + ${DESTDIR}${MANPREFIX}/man1/${NAME}-en.1 | |
| 101 | + sed "s/VERSION/${VERSION}/g" < man/${NAME}-jp.1 > \ | |
| 102 | + ${DESTDIR}${MANPREFIX}/man1/${NAME}-jp.1 | |
| 103 | + chmod 644 ${DESTDIR}${MANPREFIX}/man1/${NAME}-en.1 | |
| 104 | + chmod 644 ${DESTDIR}${MANPREFIX}/man1/${NAME}-jp.1 | |
| 105 | + | |
| 106 | +install-zsh: | |
| 107 | + mkdir -p ${DESTDIR}${DATAPREFIX}/zsh/site-functions | |
| 108 | + cp ${NAME}-completion.zsh ${DESTDIR}${DATAPREFIX}/zsh/site-functions/_${NAME} | |
| 109 | + | |
| 110 | +publish: | |
| 111 | + rsync -rtvzP release/bin/${VERSION} 192.168.0.143:/zroot/repo/bin/${NAME} | |
| 112 | + rsync -rtvzP release/src/* 192.168.0.143:/zroot/repo/src/${NAME} | |
| 113 | + rsync -rtvzP release/man/* 192.168.0.143:/zroot/repo/man/${NAME} | |
| 114 | + | |
| 115 | +uninstall: | |
| 116 | + rm -rf ${DESTDIR}${PREFIX}/bin/${NAME} | |
| 117 | + rm -rf ${DESTDIR}${MANPREFIX}/man1/${NAME}-en.1 | |
| 118 | + rm -rf ${DESTDIR}${MANPREFIX}/man1/${NAME}-jp.1 | |
| 119 | + rm -rf ${DESTDIR}${DATAPREFIX}/zsh/site-functions/_${NAME} | |
| 120 | + | |
| 121 | +.PHONY: all clean dist man release install install-zsh uninstall |
追加
README.md
| @@ -0,0 +1,151 @@ | ||
| 1 | +# sp | |
| 2 | +シンプルなパスワードマネージャー | |
| 3 | +Simple Password Manager | |
| 4 | + | |
| 5 | +## インストールする方法 | Installation | |
| 6 | +### CRUX | |
| 7 | +```sh | |
| 8 | +doas prt-get depinst gpgme gnupg pinentry bmake | |
| 9 | +bmake | |
| 10 | +doas bmake install PREFIX=/usr | |
| 11 | +doas bmake install-zsh PREFIX=/usr | |
| 12 | +``` | |
| 13 | + | |
| 14 | +### Artix | |
| 15 | +```sh | |
| 16 | +doas pacman -S base-devel gpgme gnupg pinentry bmake | |
| 17 | +bmake | |
| 18 | +doas bmake install PREFIX=/usr | |
| 19 | +doas bmake install-zsh PREFIX=/usr | |
| 20 | +``` | |
| 21 | + | |
| 22 | +### OpenBSD | |
| 23 | +```sh | |
| 24 | +doas pkg_add gpgme gnupg pinentry | |
| 25 | +make | |
| 26 | +doas make install | |
| 27 | +doas make install-zsh | |
| 28 | +``` | |
| 29 | + | |
| 30 | +### NetBSD | |
| 31 | +```sh | |
| 32 | +doas pkgin install gpgme gnupg pinentry | |
| 33 | +make | |
| 34 | +doas make install | |
| 35 | +doas make install-zsh | |
| 36 | +``` | |
| 37 | + | |
| 38 | +### FreeBSD | |
| 39 | +```sh | |
| 40 | +doas pkg install gpgme gnupg pinentry | |
| 41 | +make | |
| 42 | +doas make install | |
| 43 | +doas make install-zsh | |
| 44 | +``` | |
| 45 | + | |
| 46 | +### Haiku | |
| 47 | +```sh | |
| 48 | +pkgman install bmake libassuan libassuan_devel gpgme gpgme_devel gnupg llvm18_clang | |
| 49 | +bmake | |
| 50 | +bmake install | |
| 51 | +bmake install-zsh | |
| 52 | +``` | |
| 53 | + | |
| 54 | +### macOS | |
| 55 | +```sh | |
| 56 | +brew install bmake libassuan gpgme gnupg pinentry pinentry-mac | |
| 57 | +bmake | |
| 58 | +doas bmake install | |
| 59 | +doas bmake install-zsh | |
| 60 | +``` | |
| 61 | + | |
| 62 | +## 初期設定 | Initial setup | |
| 63 | +「gpg -k」でGPG鍵IDを確認して、「sp -i [GPG ID]」を実行して下さい。 | |
| 64 | +Confirm your GPG key ID with "gpg -k", and run "sp -i [GPG ID]". | |
| 65 | + | |
| 66 | +## 使い方 | Usage | |
| 67 | +### パスワードの作成 | Password generation | |
| 68 | +#### 強いパスワードの場合 | Strong password | |
| 69 | +```sh | |
| 70 | +$ sp -g 64 | |
| 71 | +nSYGSF2lWGCUsqKCRB_~mZm+spaU<zvtt%um'01$tj4h,^nB6JqX#Cm$!U+s;c7: | |
| 72 | +``` | |
| 73 | +又は | |
| 74 | +Or | |
| 75 | +```sh | |
| 76 | +$ sp -g 64 secure | |
| 77 | +nSYGSF2lWGCUsqKCRB_~mZm+spaU<zvtt%um'01$tj4h,^nB6JqX#Cm$!U+s;c7: | |
| 78 | +``` | |
| 79 | + | |
| 80 | +#### 弱いパスワードの場合 | Weak password | |
| 81 | +```sh | |
| 82 | +$ sp -g 12 risk | |
| 83 | +2aKBwb858mzg | |
| 84 | +``` | |
| 85 | + | |
| 86 | +### パスワードの追加 | Add password | |
| 87 | +「suwako」というユーザー名の場合: | |
| 88 | +If the username is "suwake": | |
| 89 | +```sh | |
| 90 | +$ sp -a 076.moe/suwako | |
| 91 | +パスワード: | |
| 92 | +パスワード(確認用): | |
| 93 | +パスワードを保存出来ました。 | |
| 94 | +``` | |
| 95 | +入力中は何も表示されないので、ご注意下さい。 | |
| 96 | + | |
| 97 | +### 英語で表示する場合 | For English | |
| 98 | +```sh | |
| 99 | +$ SP_LANG=en sp -a 076.moe/suwako | |
| 100 | +Password: | |
| 101 | +Password (for verification): | |
| 102 | +The password got saved. | |
| 103 | +``` | |
| 104 | + | |
| 105 | +### パスワードの表示 | Show password | |
| 106 | +```sh | |
| 107 | +$ sp -s 076.moe/suwako | |
| 108 | +nSYGSF2lWGCUsqKCRB_~mZm+spaU<zvtt%um'01$tj4h,^nB6JqX#Cm$!U+s;c7: | |
| 109 | +``` | |
| 110 | +表示せずコピーする場合は、「sp -y 076.moe/suwako」を使用して下さい。 | |
| 111 | +If you want to copy the password to the clipboard without showing it, | |
| 112 | +use "sp -y 076.moe/suwako" instead. | |
| 113 | + | |
| 114 | +### パスワードの削除 | Delete password | |
| 115 | +```sh | |
| 116 | +$ sp -d 076.moe/suwako | |
| 117 | +パスワード「076.moe/suwako」を本当に削除する事が宜しいでしょうか? (y/N): y | |
| 118 | +パスワードを削除しました。 | |
| 119 | +``` | |
| 120 | + | |
| 121 | +### パスワードの編集 | Edit password | |
| 122 | +```sh | |
| 123 | +$ sp -e 076.moe/suwako | |
| 124 | +パスワード: | |
| 125 | +パスワード(確認用): | |
| 126 | +パスワードを保存出来ました。 | |
| 127 | +``` | |
| 128 | + | |
| 129 | +## TOTP(ワンタイムパスワード) | TOTP (one time password) | |
| 130 | +### QRコードから | From QR code | |
| 131 | +QRコードをダウンロードし、zbarimgを使用して「QR-Code:」以降の部分をコピーし、 | |
| 132 | +spに追加して下さい。\ | |
| 133 | +`sp -a`を実行すると、「パスワード」を聞かれますが、 | |
| 134 | +TOTPの場合は「otpauth://」から始まる文字列をコピペして下さい。 | |
| 135 | + | |
| 136 | +Download the QR code image, copy everything after "QR-Code:" | |
| 137 | +you get after using zbarimg, and add into sp. | |
| 138 | +If you run "sp -a", it'll ask for a "password", but in the case of TOTP, | |
| 139 | +paste everything starting from "otpauth://". | |
| 140 | +```sh | |
| 141 | +$ zbarimg -q gitler.png | |
| 142 | +QR-Code:otpauth://totp/Gitler%20%28gitler.moe%29:suwako?algorithm=SHA1&digits=6&issuer=Gitler%20%28gitler.moe%29&period=30&secret=〇〇 | |
| 143 | + | |
| 144 | +$ sp -a gitler-otp | |
| 145 | +パスワード: | |
| 146 | +パスワード(確認用): | |
| 147 | +パスワードを保存出来ました。 | |
| 148 | + | |
| 149 | +$ sp -o gitler-otp | |
| 150 | +123456 | |
| 151 | +``` |
追加
main.c
| @@ -0,0 +1,170 @@ | ||
| 1 | +#include "src/common.h" | |
| 2 | +#include "src/initpass.h" | |
| 3 | +#include "src/showpass.h" | |
| 4 | +#include "src/yankpass.h" | |
| 5 | +#include "src/listpass.h" | |
| 6 | +#include "src/addpass.h" | |
| 7 | +#include "src/delpass.h" | |
| 8 | +#include "src/genpass.h" | |
| 9 | +#include "src/otppass.h" | |
| 10 | +#include "src/findpass.h" | |
| 11 | +#include "src/vulnpass.h" | |
| 12 | +#include "src/chkpass.h" | |
| 13 | + | |
| 14 | +#include <unistd.h> | |
| 15 | + | |
| 16 | +const char *sofname = "sp"; | |
| 17 | +const char *version = "1.5.1"; | |
| 18 | +const char *avalopt = "abcdefgiloOsvy"; | |
| 19 | +const char *madefor = "simpas 1.1.1"; | |
| 20 | + | |
| 21 | +void usage() { | |
| 22 | + printf("%s-%s (%s)\nusage: %s [-%s]\n", | |
| 23 | + sofname, version, madefor, sofname, avalopt); | |
| 24 | +} | |
| 25 | + | |
| 26 | +char *getfullpath(char *arg) { | |
| 27 | + char *lang = getlang(); | |
| 28 | + | |
| 29 | + char *basedir = getbasedir(1); | |
| 30 | + size_t fullPathLen; | |
| 31 | + char *fullPath; | |
| 32 | + fullPathLen = strlen(basedir) + (arg != NULL ? strlen(arg) + 5 : 0); | |
| 33 | + | |
| 34 | + fullPath = (char *)malloc(fullPathLen); | |
| 35 | + if (fullPath == NULL) { | |
| 36 | + if (strncmp(lang, "en", 2) == 0) | |
| 37 | + perror("Failed to allocating memory"); | |
| 38 | + else perror("メモリの役割に失敗"); | |
| 39 | + free(basedir); | |
| 40 | + return NULL; | |
| 41 | + } | |
| 42 | + | |
| 43 | + if (arg != NULL) { | |
| 44 | + snprintf(fullPath, fullPathLen, "%s%s.gpg", basedir, arg); | |
| 45 | + } else { | |
| 46 | + snprintf(fullPath, fullPathLen, "%s", basedir); | |
| 47 | + } | |
| 48 | + | |
| 49 | + return fullPath; | |
| 50 | +} | |
| 51 | + | |
| 52 | +void editpass(char *file) { | |
| 53 | + char *lang = getlang(); | |
| 54 | + | |
| 55 | +#if defined(__HAIKU__) | |
| 56 | + const char *tmpfile = "/boot/system/cache/sp-tmp.gpg"; | |
| 57 | +#else | |
| 58 | + const char *tmpfile = "/tmp/sp-tmp.gpg"; | |
| 59 | +#endif | |
| 60 | + tmpcopy(file, tmpfile); | |
| 61 | + if (delpass(file, 1) != 0) { | |
| 62 | + if (strncmp(lang, "en", 2) == 0) | |
| 63 | + perror("Editing failed: Failed to delete."); | |
| 64 | + else perror("編集に失敗:削除に失敗"); | |
| 65 | + return; | |
| 66 | + } | |
| 67 | + if (addpass(file) != 0) { | |
| 68 | + tmpcopy(tmpfile, file); | |
| 69 | + unlink(tmpfile); | |
| 70 | + if (strncmp(lang, "en", 2) == 0) | |
| 71 | + perror("Editing failed: Failed to add."); | |
| 72 | + else perror("編集に失敗:追加に失敗"); | |
| 73 | + return; | |
| 74 | + } | |
| 75 | + | |
| 76 | + unlink(tmpfile); | |
| 77 | +} | |
| 78 | + | |
| 79 | +int main(int argc, char *argv[]) { | |
| 80 | + if (argc < 2) { | |
| 81 | + usage(); | |
| 82 | + return 0; | |
| 83 | + } | |
| 84 | + | |
| 85 | + if (strcmp(argv[1], "-g") == 0) { | |
| 86 | + if (argc > 4) { | |
| 87 | + usage(); | |
| 88 | + return 1; | |
| 89 | + } | |
| 90 | + | |
| 91 | + if (argc == 2) genpass(64, true); | |
| 92 | + else if (argc == 3) genpass(atoi(argv[2]), true); | |
| 93 | + else if (argc == 4 && strcmp(argv[3], "risk") == 0) | |
| 94 | + genpass(atoi(argv[2]), false); | |
| 95 | + else if (argc == 4 && strcmp(argv[3], "secure") == 0) | |
| 96 | + genpass(atoi(argv[2]), true); | |
| 97 | + | |
| 98 | + return 0; | |
| 99 | + } | |
| 100 | + | |
| 101 | + if (argc == 3) { | |
| 102 | + if (strcmp(argv[1], "-i") == 0) initpass(argv[2]); | |
| 103 | + else if (strcmp(argv[1], "-s") == 0) { | |
| 104 | + const char *pass = showpass(argv[2]); | |
| 105 | + if (pass == NULL) return -1; | |
| 106 | + printf("%s\n", pass); | |
| 107 | + } | |
| 108 | + else if (strcmp(argv[1], "-y") == 0) yankpass(argv[2], 45); | |
| 109 | + else if (strcmp(argv[1], "-a") == 0) addpass(argv[2]); | |
| 110 | + else if (strcmp(argv[1], "-d") == 0) delpass(argv[2], 0); | |
| 111 | + else if (strcmp(argv[1], "-e") == 0) { | |
| 112 | + editpass(argv[2]); | |
| 113 | + } else if (strcmp(argv[1], "-o") == 0) { | |
| 114 | + char *fullPath = getfullpath(argv[2]); | |
| 115 | + if (fullPath == NULL) return -1; | |
| 116 | + otppass(fullPath, 0, 0); | |
| 117 | + free(fullPath); | |
| 118 | + } else if (strcmp(argv[1], "-O") == 0) { | |
| 119 | + char *fullPath = getfullpath(argv[2]); | |
| 120 | + if (fullPath == NULL) return -1; | |
| 121 | + otppass(fullPath, 1, 30); | |
| 122 | + free(fullPath); | |
| 123 | + } else if (strcmp(argv[1], "-f") == 0) { | |
| 124 | + char *fullPath = getfullpath(NULL); | |
| 125 | + if (fullPath == NULL) return -1; | |
| 126 | + findpass(fullPath, argv[2]); | |
| 127 | + free(fullPath); | |
| 128 | + } else if (strcmp(argv[1], "-c") == 0) { | |
| 129 | + char *fullPath = getfullpath(NULL); | |
| 130 | + if (fullPath == NULL) return -1; | |
| 131 | + chkpass(fullPath, argv[2]); | |
| 132 | + free(fullPath); | |
| 133 | + } else { | |
| 134 | + usage(); | |
| 135 | + return 1; | |
| 136 | + } | |
| 137 | + } else if (argc == 4) { | |
| 138 | + if (strcmp(argv[1], "-y") == 0) { | |
| 139 | + int i; | |
| 140 | + if (sscanf(argv[3], "%d", &i) == 0) yankpass(argv[2], 45); | |
| 141 | + else yankpass(argv[2], atoi(argv[3])); | |
| 142 | + } else if (strcmp(argv[1], "-O") == 0) { | |
| 143 | + char *fullPath = getfullpath(argv[2]); | |
| 144 | + if (fullPath == NULL) return -1; | |
| 145 | + int i; | |
| 146 | + if (sscanf(argv[3], "%d", &i) == 0) otppass(fullPath, 1, 30); | |
| 147 | + else otppass(fullPath, 1, atoi(argv[3])); | |
| 148 | + free(fullPath); | |
| 149 | + } | |
| 150 | + } else if (argc == 2) { | |
| 151 | + char *basePath = getfullpath(NULL); | |
| 152 | + if (basePath == NULL) return -1; | |
| 153 | + | |
| 154 | + if (strcmp(argv[1], "-l") == 0) listpass(basePath, 0); | |
| 155 | + else if (strcmp(argv[1], "-b") == 0) vulnpass(basePath); | |
| 156 | + else if (strcmp(argv[1], "-v") == 0) { | |
| 157 | + printf("%s-%s (%s)\n", sofname, version, madefor); | |
| 158 | + } else { | |
| 159 | + usage(); | |
| 160 | + free(basePath); | |
| 161 | + return 1; | |
| 162 | + } | |
| 163 | + free(basePath); | |
| 164 | + } else { | |
| 165 | + usage(); | |
| 166 | + return 1; | |
| 167 | + } | |
| 168 | + | |
| 169 | + return 0; | |
| 170 | +} |
追加
man/sp-en.1
| @@ -0,0 +1,191 @@ | ||
| 1 | +.TH SP 1 VERSION | |
| 2 | +.SH NAME | |
| 3 | +sp - Simple Password Manager | |
| 4 | +.br | |
| 5 | +.B sp | |
| 6 | +[-bfi] [-i \fI\,gpg-id\fR] [-adeos \fI\,password\fR] [-c \fI,all\fR|\fI,length\fR|\fI,strength\fR|\fI,duplicate\fR] [-g [\fI\,length\fR] [\fI\,secure\fR|\fI\,risk\fR]] [-Oy \fI\,password\fR [\fI,seconds\fR]] | |
| 7 | +.SH DESCRIPTION | |
| 8 | +.PP | |
| 9 | +Simple Password Manager | |
| 10 | +.TP | |
| 11 | +\fB\,a\fR \fI\,password\fR | |
| 12 | +Add password | |
| 13 | +.TP | |
| 14 | +\fB\,b\fR | |
| 15 | +Check for breached passwords (internet connection is required) | |
| 16 | +.TP | |
| 17 | +\fB\,c\fR \fI,all|length|strength|duplicate\fR | |
| 18 | +Check for password length, strength, or duplicates. | |
| 19 | +.br | |
| 20 | +length = Check for password length | |
| 21 | +strength = Check for password strength | |
| 22 | +duplicate = Check for duplicate passwords | |
| 23 | +all = Check all of the above | |
| 24 | +.TP | |
| 25 | +\fB\,d\fR \fI\,password\fR | |
| 26 | +Delete password | |
| 27 | +.TP | |
| 28 | +\fB\,e\fR \fI\,password\fR | |
| 29 | +Edit password | |
| 30 | +.TP | |
| 31 | +\fB\,g\fR \fI,length\fR \fI,risk|secure\fR | |
| 32 | +Generate a random password using the preferred amount of digits. | |
| 33 | +.br | |
| 34 | +The default is 64 characters. | |
| 35 | +.br | |
| 36 | +secure = Lower and uppercase + digits + special characters (default) | |
| 37 | +.br | |
| 38 | +risk = only lowercase and uppercase + digits | |
| 39 | +.TP | |
| 40 | +\fB\,i\fR \fI\,gpg-id\fR | |
| 41 | +Initialize password storage using GPG | |
| 42 | +.TP | |
| 43 | +\fB\,l\fR | |
| 44 | +Show a list of passwords | |
| 45 | +.TP | |
| 46 | +\fB\,o\fR \fI\,password\fR | |
| 47 | +Show one time password (TOTP) | |
| 48 | +.TP | |
| 49 | +\fB\,O\fR \fI\,password\fR \fI\,seconds\fR | |
| 50 | +Copy one time password (TOTP) | |
| 51 | +.br | |
| 52 | +The default is 30 seconds. | |
| 53 | +.TP | |
| 54 | +\fB\,s\fR \fI\,password\fR | |
| 55 | +Show password | |
| 56 | +.TP | |
| 57 | +\fB\,v\fR | |
| 58 | +Show version | |
| 59 | +.TP | |
| 60 | +\fB\,y\fR \fI\,password\fR \fI,seconds\fR | |
| 61 | +Copy password to the clipboard without showing the password (Xorg-only) | |
| 62 | +.br | |
| 63 | +The default is 45 seconds. | |
| 64 | +.SH LANGUAGE | |
| 65 | +The default language is Japanese. In order to use English, | |
| 66 | +.br | |
| 67 | +please add "SP_LANG=en" to the `.zshrc` or `.bashrc` file. | |
| 68 | +.SH EXAMPLES | |
| 69 | +\&... | |
| 70 | + | |
| 71 | +$ sp -i 12345678901234567890ABCDEFABCDEF1234ABCD | |
| 72 | +.br | |
| 73 | +初期設定に完了しました。 | |
| 74 | + | |
| 75 | +$ cat ~/.local/share/sp/.gpg-id | |
| 76 | +.br | |
| 77 | +12345678901234567890ABCDEFABCDEF1234ABCD | |
| 78 | +.ED | |
| 79 | + | |
| 80 | +\&... | |
| 81 | + | |
| 82 | +$ sp -a 076.moe/nitori | |
| 83 | +.br | |
| 84 | +Password: | |
| 85 | +.br | |
| 86 | +Password (for verification): | |
| 87 | +.br | |
| 88 | +The password got saved. | |
| 89 | + | |
| 90 | +$ sp -s 076.moe/nitori | |
| 91 | +.br | |
| 92 | +kyuuri | |
| 93 | + | |
| 94 | +$ sp -d 076.moe/nitori | |
| 95 | +.br | |
| 96 | +Are you sure you want to delete the password '076.moe/nitori' (y/N): y | |
| 97 | +.br | |
| 98 | +The password got deleted | |
| 99 | +.br | |
| 100 | + | |
| 101 | +$ sp -s 076.moe/nitori | |
| 102 | +.br | |
| 103 | +Failed to open file: No such file or directory | |
| 104 | +.ED | |
| 105 | + | |
| 106 | +\&... | |
| 107 | + | |
| 108 | +$ sp -a 076.moe/nitori | |
| 109 | +.br | |
| 110 | +Password: | |
| 111 | +.br | |
| 112 | +Password (for verification): | |
| 113 | +.br | |
| 114 | +The password got saved. | |
| 115 | + | |
| 116 | +$ sp -s 076.moe/nitori | |
| 117 | +.br | |
| 118 | +kyuuri | |
| 119 | + | |
| 120 | +$ sp -a 076.moe/nitori | |
| 121 | +.br | |
| 122 | +パスワードが既に存在しています。 | |
| 123 | +.br | |
| 124 | +変更するには、「 sp -e 076.moe/nitori 」を実行して下さい。 | |
| 125 | + | |
| 126 | +$ SP_LANG=en sp -a 076.moe/nitori | |
| 127 | +.br | |
| 128 | +Password already exist. | |
| 129 | +.br | |
| 130 | +For edit, please run ' sp -e 076.moe/nitori '. | |
| 131 | + | |
| 132 | +$ sp -e 076.moe/nitori | |
| 133 | +.br | |
| 134 | +Password: | |
| 135 | +.br | |
| 136 | +Password (for verification): | |
| 137 | +.br | |
| 138 | +The password got saved. | |
| 139 | + | |
| 140 | +$ sp -s 076.moe/nitori | |
| 141 | +.br | |
| 142 | +kappa | |
| 143 | +.ED | |
| 144 | + | |
| 145 | +\&... | |
| 146 | + | |
| 147 | +$ sp -g | |
| 148 | +.br | |
| 149 | +\5C'F6=8r&:OO=P?{Ry-3d4%z#7Hki}965l`j2xJSRoHQkvj^nz+YPx4g74yu_OT | |
| 150 | + | |
| 151 | +$ sp -g 12 | |
| 152 | +.br | |
| 153 | +{%#upiPiayqZ | |
| 154 | + | |
| 155 | +$ sp -g 12 risk | |
| 156 | +.br | |
| 157 | +iwxoumJC9wZH | |
| 158 | +.ED | |
| 159 | + | |
| 160 | +\&... | |
| 161 | + | |
| 162 | +$ doas pkg_add zbar | |
| 163 | +.br | |
| 164 | +quirks-7.14 signed on 2024-05-21T22:52:07Z | |
| 165 | +.br | |
| 166 | +zbar-0.23.90p2: ok | |
| 167 | + | |
| 168 | +$ zbarimg -q --raw Untitled.png | |
| 169 | +.br | |
| 170 | +otpauth://totp/GitHub:TechnicalSuwako?secret=ABCDEFGHIJKLMNOP&issuer=GitHub | |
| 171 | + | |
| 172 | +$ sp -a github.com/2fa | |
| 173 | +.br | |
| 174 | +Password: | |
| 175 | +.br | |
| 176 | +Password (for verification): | |
| 177 | +.br | |
| 178 | +The password got saved. | |
| 179 | + | |
| 180 | +$ sp -s github.com/2fa | |
| 181 | +.br | |
| 182 | +otpauth://totp/GitHub:TechnicalSuwako?secret=ABCDEFGHIJKLMNOP&issuer=GitHub | |
| 183 | + | |
| 184 | +$ sp -o github.com/2fa | |
| 185 | +.br | |
| 186 | +123456 | |
| 187 | +.Ed | |
| 188 | +.SH AUTHORS | |
| 189 | +.PP | |
| 190 | +Technical Suwako (developer) | |
| 191 | +Remilia Scarlet (English translation) |
追加
man/sp-jp.1
| @@ -0,0 +1,191 @@ | ||
| 1 | +.TH SP 1 VERSION | |
| 2 | +.SH NAME | |
| 3 | +sp - Simple Password Manager | |
| 4 | +.br | |
| 5 | +.B sp | |
| 6 | +[-bfi] [-i \fI\,gpg-id\fR] [-adeos \fI\,password\fR] [-c \fI,all\fR|\fI,length\fR|\fI,strength\fR|\fI,duplicate\fR] [-g [\fI\,length\fR] [\fI\,secure\fR|\fI\,risk\fR]] [-Oy \fI\,password\fR [\fI,seconds\fR]] | |
| 7 | +.SH 説明 | |
| 8 | +.PP | |
| 9 | +シンプルなパスワードマネージャー。 | |
| 10 | +.TP | |
| 11 | +\fB\,a\fR \fI\,password\fR | |
| 12 | +パスワードを追加 | |
| 13 | +.TP | |
| 14 | +\fB\,b\fR | |
| 15 | +漏洩されたパスワードを確認(インターネット接続が必須) | |
| 16 | +.TP | |
| 17 | +\fB\,c\fR \fI,all|length|strength|duplicate\fR | |
| 18 | +パスワードの長さ、強さ、や服数度を確認 | |
| 19 | +.br | |
| 20 | +length=パスワードの長さの確認 | |
| 21 | +strength=パスワードの強さの確認 | |
| 22 | +duplicate=服数パスワードが同じかどうか確認 | |
| 23 | +all=上記の全部の確認 | |
| 24 | +.TP | |
| 25 | +\fB\,d\fR \fI\,password\fR | |
| 26 | +パスワードを削除 | |
| 27 | +.TP | |
| 28 | +\fB\,e\fR \fI\,password\fR | |
| 29 | +パスワードを変更 | |
| 30 | +.TP | |
| 31 | +\fB\,g\fR \fI,length\fR \fI,risk|secure\fR | |
| 32 | +希望文字数でパスワードをランダムに作成する。 | |
| 33 | +.br | |
| 34 | +デフォルトな長さは64文字です。 | |
| 35 | +.br | |
| 36 | +secure=英数字+記号文字(デフォルト)を使用 | |
| 37 | +.br | |
| 38 | +risk=英数字のみ(不安)を使用 | |
| 39 | +.TP | |
| 40 | +\fB\,i\fR \fI\,gpg-id\fR | |
| 41 | +GPGと使ってパスワードストレージを初期設定 | |
| 42 | +.TP | |
| 43 | +\fB\,l\fR | |
| 44 | +パスワード一覧を表示 | |
| 45 | +.TP | |
| 46 | +\fB\,o\fR \fI\,password\fR | |
| 47 | +ワンタイムパスワード(TOTP)を表示 | |
| 48 | +.TP | |
| 49 | +\fB\,O\fR \fI\,password\fR \fI,seconds\fR | |
| 50 | +ワンタイムパスワード(TOTP)をコピー | |
| 51 | +.br | |
| 52 | +デフォルトは30秒です。 | |
| 53 | +.TP | |
| 54 | +\fB\,s\fR \fI\,password\fR | |
| 55 | +パスワードを表示 | |
| 56 | +.TP | |
| 57 | +\fB\,v\fR | |
| 58 | +バージョンを表示 | |
| 59 | +.TP | |
| 60 | +\fB\,y\fR \fI\,password\fR \fI,seconds\fR | |
| 61 | +パスワードを表示せずクリップボードにコピーする(Xorgのみ) | |
| 62 | +.br | |
| 63 | +デフォルトは45秒です。 | |
| 64 | +.SH LANGUAGE | |
| 65 | +デフォルトの言語は日本語ですが、英語で利用するには、 | |
| 66 | +.br | |
| 67 | +「.zshrc」、「.bashrc」等ファイルで「SP_LANG=en」を追加して下さい。 | |
| 68 | +.SH EXAMPLES | |
| 69 | +\&... | |
| 70 | + | |
| 71 | +$ sp -i 12345678901234567890ABCDEFABCDEF1234ABCD | |
| 72 | +.br | |
| 73 | +初期設定に完了しました。 | |
| 74 | + | |
| 75 | +$ cat ~/.local/share/sp/.gpg-id | |
| 76 | +.br | |
| 77 | +12345678901234567890ABCDEFABCDEF1234ABCD | |
| 78 | +.ED | |
| 79 | + | |
| 80 | +\&... | |
| 81 | + | |
| 82 | +$ sp -a 076.moe/nitori | |
| 83 | +.br | |
| 84 | +パスワード: | |
| 85 | +.br | |
| 86 | +パスワード(確認用): | |
| 87 | +.br | |
| 88 | +パスワードを保存出来ました | |
| 89 | + | |
| 90 | +$ sp -s 076.moe/nitori | |
| 91 | +.br | |
| 92 | +kyuuri | |
| 93 | + | |
| 94 | +$ sp -d 076.moe/nitori | |
| 95 | +.br | |
| 96 | +パスワード「076.moe/nitori」を本当に削除する事が宜しいでしょうか? (y/N): y | |
| 97 | +.br | |
| 98 | +パスワードを削除しました | |
| 99 | +.br | |
| 100 | + | |
| 101 | +$ sp -s 076.moe/nitori | |
| 102 | +.br | |
| 103 | +ファイルを開くに失敗: No such file or directory | |
| 104 | +.ED | |
| 105 | + | |
| 106 | +\&... | |
| 107 | + | |
| 108 | +$ sp -a 076.moe/nitori | |
| 109 | +.br | |
| 110 | +パスワード: | |
| 111 | +.br | |
| 112 | +パスワード(確認用): | |
| 113 | +.br | |
| 114 | +パスワードを保存出来ました | |
| 115 | + | |
| 116 | +$ sp -s 076.moe/nitori | |
| 117 | +.br | |
| 118 | +kyuuri | |
| 119 | + | |
| 120 | +$ sp -a 076.moe/nitori | |
| 121 | +.br | |
| 122 | +パスワードが既に存在しています。 | |
| 123 | +.br | |
| 124 | +変更するには、「 sp -e 076.moe/nitori 」を実行して下さい。 | |
| 125 | + | |
| 126 | +$ SP_LANG=en sp -a 076.moe/nitori | |
| 127 | +.br | |
| 128 | +Password already exist. | |
| 129 | +.br | |
| 130 | +For edit, please run ' sp -e 076.moe/nitori '. | |
| 131 | + | |
| 132 | +$ sp -e 076.moe/nitori | |
| 133 | +.br | |
| 134 | +パスワード: | |
| 135 | +.br | |
| 136 | +パスワード(確認用): | |
| 137 | +.br | |
| 138 | +パスワードを保存出来ました | |
| 139 | + | |
| 140 | +$ sp -s 076.moe/nitori | |
| 141 | +.br | |
| 142 | +kappa | |
| 143 | +.ED | |
| 144 | + | |
| 145 | +\&... | |
| 146 | + | |
| 147 | +$ sp -g | |
| 148 | +.br | |
| 149 | +\5C'F6=8r&:OO=P?{Ry-3d4%z#7Hki}965l`j2xJSRoHQkvj^nz+YPx4g74yu_OT | |
| 150 | + | |
| 151 | +$ sp -g 12 | |
| 152 | +.br | |
| 153 | +{%#upiPiayqZ | |
| 154 | + | |
| 155 | +$ sp -g 12 risk | |
| 156 | +.br | |
| 157 | +iwxoumJC9wZH | |
| 158 | +.ED | |
| 159 | + | |
| 160 | +\&... | |
| 161 | + | |
| 162 | +$ doas pkg_add zbar | |
| 163 | +.br | |
| 164 | +quirks-7.14 signed on 2024-05-21T22:52:07Z | |
| 165 | +.br | |
| 166 | +zbar-0.23.90p2: ok | |
| 167 | + | |
| 168 | +$ zbarimg -q --raw Untitled.png | |
| 169 | +.br | |
| 170 | +otpauth://totp/GitHub:TechnicalSuwako?secret=ABCDEFGHIJKLMNOP&issuer=GitHub | |
| 171 | + | |
| 172 | +$ sp -a github.com/2fa | |
| 173 | +.br | |
| 174 | +パスワード: | |
| 175 | +.br | |
| 176 | +パスワード(確認用): | |
| 177 | +.br | |
| 178 | +パスワードを保存出来ました | |
| 179 | + | |
| 180 | +$ sp -s github.com/2fa | |
| 181 | +.br | |
| 182 | +otpauth://totp/GitHub:TechnicalSuwako?secret=ABCDEFGHIJKLMNOP&issuer=GitHub | |
| 183 | + | |
| 184 | +$ sp -o github.com/2fa | |
| 185 | +.br | |
| 186 | +123456 | |
| 187 | +.Ed | |
| 188 | +.SH AUTHORS | |
| 189 | +.PP | |
| 190 | +テクニカル諏訪子(開発者) | |
| 191 | +レミリア・スカーレット(英訳) |
| @@ -0,0 +1,70 @@ | ||
| 1 | +#compdef sp | |
| 2 | +#autoload | |
| 3 | + | |
| 4 | +_sp () { | |
| 5 | + local cmd | |
| 6 | + if (( CURRENT > 2)); then | |
| 7 | + cmd=${words[2]} | |
| 8 | + curcontext="${curcontext%:*:*}:pass-$cmd" | |
| 9 | + (( CURRENT-- )) | |
| 10 | + shift words | |
| 11 | + case "${cmd}" in | |
| 12 | + -i) | |
| 13 | + _sp_complete_keys | |
| 14 | + ;; | |
| 15 | + -i|-y|-d|-a|-e|-o|-O) | |
| 16 | + _sp_complete_entries_helper | |
| 17 | + ;; | |
| 18 | + -g) | |
| 19 | + local -a subcommands | |
| 20 | + subcommands=( | |
| 21 | + "secure:英数字+記号文字(デフォルト)" | |
| 22 | + "risk:英数字のみ(不安)" | |
| 23 | + ) | |
| 24 | + ;; | |
| 25 | + -s) | |
| 26 | + _sp_cmd_show | |
| 27 | + ;; | |
| 28 | + esac | |
| 29 | + else | |
| 30 | + local -a subcommands | |
| 31 | + subcommands=( | |
| 32 | + "-a:パスワードを追加" | |
| 33 | + "-b:漏洩されたパスワードを確認(インターネット接続が必須)" | |
| 34 | + "-c:パスワードの長さ、強さ、や服数度を確認する。length=パスワードの長さの確認、strength=パスワードの強さの確認、duplicate=服数パスワードが同じかどうか確認、all=上記の全部の確認" | |
| 35 | + "-d:パスワードを削除" | |
| 36 | + "-e:パスワードを変更" | |
| 37 | + "-g:希望文字数でパスワードをランダムに作成する。デフォルトな長さは64文字です。secure=英数字+記号文字(デフォルト)を使用、risk=英数字のみ(不安)を使用" | |
| 38 | + "-i:GPGと使ってパスワードストレージを初期設定" | |
| 39 | + "-l:パスワード一覧を表示" | |
| 40 | + "-o:ワンタイムパスワード(TOTP)を表示" | |
| 41 | + "-O:ワンタイムパスワード(TOTP)をコピー。デフォルトは30秒です。" | |
| 42 | + "-s:パスワードを表示" | |
| 43 | + "-v:バージョンを表示" | |
| 44 | + "-y:パスワードを表示せずクリップボードにコピーする(Xorgのみ)。デフォルトは45秒です。" | |
| 45 | + ) | |
| 46 | + _sp_cmd_show | |
| 47 | + fi | |
| 48 | +} | |
| 49 | + | |
| 50 | +_sp_cmd_show () { | |
| 51 | + _sp_complete_entries | |
| 52 | +} | |
| 53 | + | |
| 54 | +_sp_complete_entries() { | |
| 55 | + _sp_complete_entries_helper -type f | |
| 56 | +} | |
| 57 | + | |
| 58 | +_sp_complete_entries_helper () { | |
| 59 | + local IFS=$'\n' | |
| 60 | + local prefix | |
| 61 | + zstyle -s ":completion:${curcontext}:" prefix prefix || prefix="${SP_DIR:-$HOME/.local/share/sp}" | |
| 62 | + _values -C 'passwords' ${$(find -L "$prefix" \( -name .git -o -name .gpg-id \) -prune -o $@ -print 2>/dev/null | sed -e "s#${prefix}/\{0,1\}##" -e 's#\.gpg##' -e 's#\\#\\\\#g' -e 's#:#\\:#g' | sort):-""} | |
| 63 | +} | |
| 64 | + | |
| 65 | +_sp_complete_keys () { | |
| 66 | + local IFS=$'\n' | |
| 67 | + _values 'gpg keys' $(gpg2 --list-secret-keys --with-colons | cut -d : -f 10 | sort -u | sed '/^$/d') | |
| 68 | +} | |
| 69 | + | |
| 70 | +_sp |
| @@ -0,0 +1,288 @@ | ||
| 1 | +#include "addpass.h" | |
| 2 | + | |
| 3 | +void cleanup(gpgme_ctx_t ctx, gpgme_key_t key, gpgme_data_t in, gpgme_data_t out) { | |
| 4 | + gpgme_data_release(in); | |
| 5 | + gpgme_data_release(out); | |
| 6 | + gpgme_release(ctx); | |
| 7 | + gpgme_key_release(key); | |
| 8 | +} | |
| 9 | + | |
| 10 | +void getpasswd(char *prompt, char *pw, size_t pwlen) { | |
| 11 | + struct termios old, new; | |
| 12 | + printf("%s", prompt); | |
| 13 | + | |
| 14 | + // 端末設定を受け取る | |
| 15 | + tcgetattr(fileno(stdin), &old); | |
| 16 | + new = old; | |
| 17 | + | |
| 18 | + // echoを無効にして | |
| 19 | + new.c_lflag &= ~ECHO; | |
| 20 | + tcsetattr(fileno(stdin), TCSANOW, &new); | |
| 21 | + | |
| 22 | + // パスワードを読み込む | |
| 23 | + if (fgets(pw, pwlen, stdin) != NULL) { | |
| 24 | + // あれば、改行を取り消す | |
| 25 | + size_t len = strlen(pw); | |
| 26 | + if (len > 0 && pw[len - 1] == '\n') { | |
| 27 | + pw[len - 1] = '\0'; | |
| 28 | + } else { | |
| 29 | + // 掃除 | |
| 30 | + int c; | |
| 31 | + while ((c = getchar()) != '\n' && c != EOF); | |
| 32 | + } | |
| 33 | + } | |
| 34 | + | |
| 35 | + // 端末設定をもとに戻す | |
| 36 | + tcsetattr(fileno(stdin), TCSANOW, &old); | |
| 37 | +} | |
| 38 | + | |
| 39 | +int addpass(char *file) { | |
| 40 | + char *lang = getlang(); | |
| 41 | + | |
| 42 | + // パスを準備 | |
| 43 | + char *basedir = getbasedir(1); | |
| 44 | + char *ext = ".gpg"; | |
| 45 | + | |
| 46 | + char pass[256]; | |
| 47 | + char knin[256]; | |
| 48 | + | |
| 49 | + int alllen = snprintf(NULL, 0, "%s%s%s", basedir, file, ext) + 1; | |
| 50 | + char *gpgpathchk = malloc(alllen); | |
| 51 | + if (gpgpathchk == NULL) { | |
| 52 | + const char *ero = (strncmp(lang, "en", 2) == 0 ? | |
| 53 | + "Failed to allocate memory" : "メモリを割当に失敗"); | |
| 54 | + fprintf(stderr, "%s\n", ero); | |
| 55 | + return -1; | |
| 56 | + } | |
| 57 | + | |
| 58 | + // ファイルが既に存在するかどうか確認 | |
| 59 | + snprintf(gpgpathchk, alllen, "%s%s%s", basedir, file, ext); | |
| 60 | + | |
| 61 | + if (access(gpgpathchk, F_OK) != -1) { | |
| 62 | + if (strncmp(lang, "en", 2) == 0) | |
| 63 | + fprintf( | |
| 64 | + stderr, | |
| 65 | + "Password already exist.\nTo edit, please run ' sp -e %s '.\n", | |
| 66 | + file | |
| 67 | + ); | |
| 68 | + else | |
| 69 | + fprintf( | |
| 70 | + stderr, | |
| 71 | + "%s\n変更するには、「 sp -e %s 」を実行して下さい。\n", | |
| 72 | + "パスワードが既に存在しています。", | |
| 73 | + file | |
| 74 | + ); | |
| 75 | + free(gpgpathchk); | |
| 76 | + return -1; | |
| 77 | + } | |
| 78 | + free(gpgpathchk); | |
| 79 | + | |
| 80 | + // パスワードを受け取る | |
| 81 | + if (strncmp(lang, "en", 2) == 0) | |
| 82 | + getpasswd("Password: ", pass, sizeof(pass)); | |
| 83 | + else getpasswd("パスワード: ", pass, sizeof(pass)); | |
| 84 | + puts(""); | |
| 85 | + if (strncmp(lang, "en", 2) == 0) | |
| 86 | + getpasswd("Password (for verification): ", knin, sizeof(knin)); | |
| 87 | + else getpasswd("パスワード(確認用): ", knin, sizeof(knin)); | |
| 88 | + puts(""); | |
| 89 | + | |
| 90 | + // パスワードが一致するかどうか確認 | |
| 91 | + if (strcmp(pass, knin) != 0) { | |
| 92 | + const char *ero = (strncmp(lang, "en", 2) == 0 ? | |
| 93 | + "Password does not match. Terminating..." : | |
| 94 | + "パスワードが一致していません。終了…"); | |
| 95 | + fprintf(stderr, "%s\n", ero); | |
| 96 | + return -1; | |
| 97 | + } | |
| 98 | + | |
| 99 | + // パスワードを保存する | |
| 100 | + gpgme_ctx_t ctx; | |
| 101 | + gpgme_error_t err; | |
| 102 | + gpgme_key_t key[2] = {NULL, NULL}; | |
| 103 | + gpgme_data_t in, out; | |
| 104 | + FILE *gpgfile; | |
| 105 | + | |
| 106 | + // GPGMEライブラリを設置 | |
| 107 | + setlocale(LC_ALL, ""); | |
| 108 | + gpgme_check_version(NULL); | |
| 109 | + gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL)); | |
| 110 | + | |
| 111 | + // GPGMEを創作 | |
| 112 | + err = gpgme_new(&ctx); | |
| 113 | + if (err) { | |
| 114 | + const char *ero = (strncmp(lang, "en", 2) == 0 ? | |
| 115 | + "Failed to generate GPGME" : "GPGMEを創作に失敗"); | |
| 116 | + fprintf(stderr, "%s: %s\n", ero, gpgme_strerror(err)); | |
| 117 | + return -1; | |
| 118 | + } | |
| 119 | + | |
| 120 | + // GPGMEは非対話的モードに設定 | |
| 121 | + err = gpgme_set_pinentry_mode(ctx, GPGME_PINENTRY_MODE_LOOPBACK); | |
| 122 | + if (err) { | |
| 123 | + const char *ero = (strncmp(lang, "en", 2) == 0 ? | |
| 124 | + "Failed to set pinentry mode" : "pinentryモードを設定に失敗"); | |
| 125 | + fprintf(stderr, "%s: %s\n", ero, gpgme_strerror(err)); | |
| 126 | + gpgme_release(ctx); | |
| 127 | + return -1; | |
| 128 | + } | |
| 129 | + | |
| 130 | + // パスワードからデータオブジェクトを創作 | |
| 131 | + err = gpgme_data_new_from_mem(&in, pass, strlen(pass), 0); | |
| 132 | + if (err) { | |
| 133 | + const char *ero = (strncmp(lang, "en", 2) == 0 ? | |
| 134 | + "Failed to make data object" : "データオブジェクトを創作に失敗"); | |
| 135 | + fprintf(stderr, "%s: %s\n", ero, gpgme_strerror(err)); | |
| 136 | + gpgme_release(ctx); | |
| 137 | + return -1; | |
| 138 | + } | |
| 139 | + | |
| 140 | + gpgme_data_new(&out); | |
| 141 | + | |
| 142 | + // 鍵を受け取る | |
| 143 | + char keypath[256]; | |
| 144 | + snprintf(keypath, sizeof(keypath), "%s%s", basedir, ".gpg-id"); | |
| 145 | + | |
| 146 | + keypath[sizeof(keypath) - 1] = '\0'; | |
| 147 | + | |
| 148 | + FILE* keyfile = fopen(keypath, "rb"); | |
| 149 | + if (keyfile == NULL) { | |
| 150 | + const char *ero = (strncmp(lang, "en", 2) == 0 ? | |
| 151 | + "Failed to open .gpg-id file" : ".gpg-idファイルを開くに失敗"); | |
| 152 | + fprintf(stderr, "%s\n", ero); | |
| 153 | + return -1; | |
| 154 | + } | |
| 155 | + | |
| 156 | + char *keyid = malloc(256); | |
| 157 | + if (!fgets(keyid, 256, keyfile)) { | |
| 158 | + const char *ero = (strncmp(lang, "en", 2) == 0 ? | |
| 159 | + "Failed to reading key ID" : "鍵IDを読込に失敗"); | |
| 160 | + fprintf(stderr, "%s\n", ero); | |
| 161 | + fclose(keyfile); | |
| 162 | + free(keyid); | |
| 163 | + return -1; | |
| 164 | + } | |
| 165 | + | |
| 166 | + keyid[strcspn(keyid, "\n")] = 0; | |
| 167 | + fclose(keyfile); | |
| 168 | + | |
| 169 | + err = gpgme_get_key(ctx, keyid, &key[0], 0); | |
| 170 | + if (err) { | |
| 171 | + const char *ero = (strncmp(lang, "en", 2) == 0 ? | |
| 172 | + "Failed to get key" : "鍵Dを受取に失敗"); | |
| 173 | + fprintf(stderr, "%s: %s\n", ero, gpgme_strerror(err)); | |
| 174 | + free(keyid); | |
| 175 | + return -1; | |
| 176 | + } | |
| 177 | + | |
| 178 | + if (key[0] == NULL) { | |
| 179 | + const char *ero = (strncmp(lang, "en", 2) == 0 ? | |
| 180 | + "Error: Key is NULL" : "エラー:鍵はNULLです"); | |
| 181 | + fprintf(stderr, "%s\n", ero); | |
| 182 | + free(keyid); | |
| 183 | + return -1; | |
| 184 | + } | |
| 185 | + | |
| 186 | + free(keyid); | |
| 187 | + | |
| 188 | + // 暗号化 | |
| 189 | + err = gpgme_op_encrypt(ctx, &key[0], GPGME_ENCRYPT_ALWAYS_TRUST, in, out); | |
| 190 | + if (err) { | |
| 191 | + const char *ero = (strncmp(lang, "en", 2) == 0 ? | |
| 192 | + "Failed to encrypt" : "暗号化に失敗"); | |
| 193 | + fprintf(stderr, "%s: %s\n", ero, gpgme_strerror(err)); | |
| 194 | + cleanup(ctx, key[0], in, out); | |
| 195 | + return -1; | |
| 196 | + } | |
| 197 | + | |
| 198 | + // 暗号化したファイルを開く | |
| 199 | + char *gpgpath = malloc(alllen); | |
| 200 | + if (gpgpath == NULL) { | |
| 201 | + const char *ero = (strncmp(lang, "en", 2) == 0 ? | |
| 202 | + "Failed to allocate memory" : "メモリを割当に失敗"); | |
| 203 | + fprintf(stderr, "%s\n", ero); | |
| 204 | + cleanup(ctx, key[0], in, out); | |
| 205 | + return -1; | |
| 206 | + } | |
| 207 | + | |
| 208 | + // ディレクトリを創作 | |
| 209 | + char dirpath[512]; | |
| 210 | + snprintf(dirpath, sizeof(dirpath), "%s%s", basedir, file); | |
| 211 | + | |
| 212 | + dirpath[sizeof(dirpath) - 1] = '\0'; | |
| 213 | + | |
| 214 | + char *lastsla = strrchr(dirpath, '/'); | |
| 215 | + if (lastsla != NULL) { | |
| 216 | + *lastsla = '\0'; | |
| 217 | + if (mkdir_r(dirpath, 0755) != 0) { | |
| 218 | + const char *ero = (strncmp(lang, "en", 2) == 0 ? | |
| 219 | + "Failed to create directory" : "ディレクトリを創作に失敗"); | |
| 220 | + fprintf(stderr, "%s\n", ero); | |
| 221 | + free(gpgpath); | |
| 222 | + cleanup(ctx, key[0], in, out); | |
| 223 | + return -1; | |
| 224 | + } | |
| 225 | + } | |
| 226 | + | |
| 227 | + snprintf(gpgpath, alllen, "%s%s%s", basedir, file, ext); | |
| 228 | + | |
| 229 | + struct stat statbuf; | |
| 230 | + if (stat(gpgpath, &statbuf) == 0) { | |
| 231 | + const char *ero = (strncmp(lang, "en", 2) == 0 ? | |
| 232 | + "Password already exists" : "パスワードは既に存在しています"); | |
| 233 | + fprintf(stderr, "%s\n", ero); | |
| 234 | + free(gpgpath); | |
| 235 | + cleanup(ctx, key[0], in, out); | |
| 236 | + return -1; | |
| 237 | + } | |
| 238 | + | |
| 239 | + gpgfile = fopen(gpgpath, "wb"); | |
| 240 | + if (gpgfile == NULL) { | |
| 241 | + const char *ero = (strncmp(lang, "en", 2) == 0 ? | |
| 242 | + "Failed to open file." : "ファイルを開くに失敗。"); | |
| 243 | + fprintf(stderr, "%s\n", ero); | |
| 244 | + free(gpgpath); | |
| 245 | + cleanup(ctx, key[0], in, out); | |
| 246 | + return -1; | |
| 247 | + } | |
| 248 | + | |
| 249 | + // データが保存したかどうか確認 | |
| 250 | + ssize_t encrypted_data_size = gpgme_data_seek(out, 0, SEEK_END); | |
| 251 | + if (encrypted_data_size <= 0) { | |
| 252 | + const char *ero = (strncmp(lang, "en", 2) == 0 ? | |
| 253 | + "Failed to store the data" : "データを保存に失敗"); | |
| 254 | + fprintf(stderr, "%s\n", ero); | |
| 255 | + fclose(gpgfile); | |
| 256 | + free(gpgpath); | |
| 257 | + cleanup(ctx, key[0], in, out); | |
| 258 | + return -1; | |
| 259 | + } | |
| 260 | + | |
| 261 | + // 復号化したパスワードを表示する | |
| 262 | + gpgme_data_seek(out, 0, SEEK_SET); | |
| 263 | + | |
| 264 | + char buffer[512]; | |
| 265 | + ssize_t read_bytes; | |
| 266 | + | |
| 267 | + while ((read_bytes = gpgme_data_read(out, buffer, sizeof(buffer))) > 0) { | |
| 268 | + if (fwrite(buffer, 1, (size_t)read_bytes, gpgfile) != (size_t)read_bytes) { | |
| 269 | + const char *ero = (strncmp(lang, "en", 2) == 0 ? | |
| 270 | + "Failed to write password" : "パスワードを書き込みに失敗"); | |
| 271 | + fprintf(stderr, "%s\n", ero); | |
| 272 | + free(gpgpath); | |
| 273 | + cleanup(ctx, key[0], in, out); | |
| 274 | + return -1; | |
| 275 | + } | |
| 276 | + } | |
| 277 | + | |
| 278 | + // 掃除 | |
| 279 | + fclose(gpgfile); | |
| 280 | + free(gpgpath); | |
| 281 | + cleanup(ctx, key[0], in, out); | |
| 282 | + | |
| 283 | + const char *msg = (strncmp(lang, "en", 2) == 0 ? | |
| 284 | + "The password got saved." : "パスワードを保存出来ました。"); | |
| 285 | + printf("%s\n", msg); | |
| 286 | + | |
| 287 | + return 0; | |
| 288 | +} |
| @@ -0,0 +1,13 @@ | ||
| 1 | +#ifndef ADDPASS_H | |
| 2 | +#define ADDPASS_H | |
| 3 | + | |
| 4 | +#include <locale.h> | |
| 5 | +#include <unistd.h> | |
| 6 | + | |
| 7 | +#include <termios.h> | |
| 8 | + | |
| 9 | +#include "common.h" | |
| 10 | + | |
| 11 | +int addpass(char *file); | |
| 12 | + | |
| 13 | +#endif |
追加
src/base32.c
| @@ -0,0 +1,65 @@ | ||
| 1 | +#include <ctype.h> | |
| 2 | +#include <stddef.h> | |
| 3 | +#include <stdio.h> | |
| 4 | +#include <string.h> | |
| 5 | + | |
| 6 | +#include "base32.h" | |
| 7 | + | |
| 8 | +int char_to_val(char c) { | |
| 9 | + const char *base32_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; | |
| 10 | + char *ptr = strchr(base32_alphabet, c); | |
| 11 | + return ptr ? ptr - base32_alphabet : -1; | |
| 12 | +} | |
| 13 | + | |
| 14 | +char *to_upper(const char *str) { | |
| 15 | + if (str == NULL) return NULL; | |
| 16 | + | |
| 17 | + size_t len = strlen(str); | |
| 18 | + char *res = (char *)malloc(len + 1); | |
| 19 | + | |
| 20 | + if (res == NULL) return NULL; | |
| 21 | + | |
| 22 | + for (size_t i = 0; i < len; i++) { | |
| 23 | + res[i] = toupper(str[i]); | |
| 24 | + } | |
| 25 | + | |
| 26 | + res[len] = '\0'; | |
| 27 | + | |
| 28 | + return res; | |
| 29 | +} | |
| 30 | + | |
| 31 | +unsigned char *base32_decode(const char *encoded, size_t *out_len) { | |
| 32 | + char *encoded_up = to_upper(encoded); | |
| 33 | + size_t encoded_len = strlen(encoded_up); | |
| 34 | + size_t padding = 0; | |
| 35 | + for (int i = encoded_len - 1; i >= 0 && encoded_up[i] == '='; --i) { | |
| 36 | + ++padding; | |
| 37 | + } | |
| 38 | + | |
| 39 | + *out_len = (encoded_len - padding) * 5 / 8; | |
| 40 | + if (*out_len == 0) return NULL; | |
| 41 | + | |
| 42 | + unsigned char *decoded = malloc(*out_len); | |
| 43 | + if (!decoded) return NULL; | |
| 44 | + | |
| 45 | + int buffer = 0, bits_left = 0, count = 0; | |
| 46 | + for (size_t i = 0; i < encoded_len - padding; ++i) { | |
| 47 | + int val = char_to_val(encoded_up[i]); | |
| 48 | + if (val < 0) { | |
| 49 | + free(decoded); | |
| 50 | + return NULL; | |
| 51 | + } | |
| 52 | + | |
| 53 | + buffer <<= 5; | |
| 54 | + buffer |= val; | |
| 55 | + bits_left += 5; | |
| 56 | + | |
| 57 | + if (bits_left >= 8) { | |
| 58 | + decoded[count++] = (unsigned char) (buffer >> (bits_left - 8)); | |
| 59 | + bits_left -= 8; | |
| 60 | + } | |
| 61 | + } | |
| 62 | + | |
| 63 | + *out_len = count; | |
| 64 | + return decoded; | |
| 65 | +} |
追加
src/base32.h
| @@ -0,0 +1,8 @@ | ||
| 1 | +#ifndef BASE32_H | |
| 2 | +#define BASE32_H | |
| 3 | + | |
| 4 | +#include <stdlib.h> | |
| 5 | + | |
| 6 | +unsigned char *base32_decode(const char *encoded, size_t *out_len); | |
| 7 | + | |
| 8 | +#endif |
| @@ -0,0 +1,184 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | +#include <string.h> | |
| 3 | +#include <ctype.h> | |
| 4 | +#include <unistd.h> | |
| 5 | +#include <sys/types.h> | |
| 6 | +#include <dirent.h> | |
| 7 | + | |
| 8 | +#include "chkpass.h" | |
| 9 | +#include "showpass.h" | |
| 10 | +#include "common.h" | |
| 11 | + | |
| 12 | +List chkFullpaths; | |
| 13 | +List chkDispaths; | |
| 14 | +List dupPasswd; | |
| 15 | +List dupPath; | |
| 16 | + | |
| 17 | +int vulncount = 0; | |
| 18 | +int weaklencount = 0; | |
| 19 | +int weakcharcount = 0; | |
| 20 | +int duppasscount = 0; | |
| 21 | + | |
| 22 | +size_t minimumlen = 12; | |
| 23 | +size_t recommendlen = 64; | |
| 24 | + | |
| 25 | +const char *spchar = "!@#$%^&*()-_=+[]{}|;:'\",.<>?/\\`~"; | |
| 26 | + | |
| 27 | +void chkLenPass(const char *path, const char *pass, char *lang) { | |
| 28 | + if (strlen(pass) > 0 && strlen(pass) < minimumlen) { | |
| 29 | + printf("【E】"); | |
| 30 | + if (strncmp(lang, "en", 2) == 0) { | |
| 31 | + printf("The password \"%s\" is too short, minimum length should be " | |
| 32 | + "%ld characters, recommended is %ld characters.\n", | |
| 33 | + path, minimumlen, recommendlen); | |
| 34 | + } else { | |
| 35 | + printf("パスワード「%s」は短すぎます。最短パスワードの長さは%ld文字ですが、" | |
| 36 | + "勧めが%ld文字です。\n", | |
| 37 | + path, minimumlen, recommendlen); | |
| 38 | + } | |
| 39 | + | |
| 40 | + weaklencount++; | |
| 41 | + vulncount++; | |
| 42 | + } else if (strlen(pass) >= minimumlen && strlen(pass) < recommendlen) { | |
| 43 | + printf("【W】"); | |
| 44 | + if (strncmp(lang, "en", 2) == 0) { | |
| 45 | + printf("The password \"%s\" is long enough, but for optimal security, " | |
| 46 | + "%ld characters is recommended.\n", | |
| 47 | + path, recommendlen); | |
| 48 | + } else { | |
| 49 | + printf("パスワード「%s」の長さは十分ですが、最強のセキュリティには%ld文字が" | |
| 50 | + "勧めします。\n", | |
| 51 | + path, recommendlen); | |
| 52 | + } | |
| 53 | + } | |
| 54 | +} | |
| 55 | + | |
| 56 | +void chkCharPass(const char *path, const char *pass, char *lang) { | |
| 57 | + int isUpper = 0; | |
| 58 | + int isLower = 0; | |
| 59 | + int isDigit = 0; | |
| 60 | + int isSpecial = 0; | |
| 61 | + | |
| 62 | + for (size_t j = 0; j < strlen(pass); j++) { | |
| 63 | + if (isupper(pass[j])) isUpper = 1; | |
| 64 | + else if (islower(pass[j])) isLower = 1; | |
| 65 | + else if (isdigit(pass[j])) isDigit = 1; | |
| 66 | + else if (strchr(spchar, pass[j])) isSpecial = 1; | |
| 67 | + } | |
| 68 | + | |
| 69 | + if (isUpper == 0 || isLower == 0 || isDigit == 0 || isSpecial == 0) { | |
| 70 | + printf("【E】"); | |
| 71 | + if (strncmp(lang, "en", 2) == 0) { | |
| 72 | + printf("The password \"%s\" is too weak! A strong password contains at least " | |
| 73 | + "1 uppercase, 1 lowercase, 1 digit, and 1 special character.\n", | |
| 74 | + path); | |
| 75 | + } else { | |
| 76 | + printf("パスワード「%s」は弱すぎます!強いパスワードは最大1大文字、1小文字、" | |
| 77 | + "1数字、及び1記号の文字が含みます。\n", | |
| 78 | + path); | |
| 79 | + } | |
| 80 | + | |
| 81 | + weakcharcount++; | |
| 82 | + vulncount++; | |
| 83 | + } | |
| 84 | +} | |
| 85 | + | |
| 86 | +void chkDupPass(const char *path, const char *pass, char *lang) { | |
| 87 | + for (size_t k = 0; k < dupPasswd.size; k++) { | |
| 88 | + if (strncmp(getElement(&dupPasswd, k), pass, strlen(pass)) == 0) { | |
| 89 | + printf("【E】"); | |
| 90 | + if (strncmp(lang, "en", 2) == 0) { | |
| 91 | + printf("The password \"%s\" is the same as \"%s\". " | |
| 92 | + "For security, please keep passwords unique!\n", | |
| 93 | + path, getElement(&dupPath, k)); | |
| 94 | + } else { | |
| 95 | + printf("パスワード「%s」は「%s」と一致しています。" | |
| 96 | + "セキュリティの為、各パスワードはユニークにする様にして下さい!\n", | |
| 97 | + path, getElement(&dupPath, k)); | |
| 98 | + } | |
| 99 | + | |
| 100 | + duppasscount++; | |
| 101 | + vulncount++; | |
| 102 | + } | |
| 103 | + } | |
| 104 | + | |
| 105 | + addElement(&dupPath, path); | |
| 106 | + addElement(&dupPasswd, pass); | |
| 107 | +} | |
| 108 | + | |
| 109 | +void chkpass(const char *dpath, const char *mode) { | |
| 110 | + char *lang = getlang(); | |
| 111 | + | |
| 112 | + if (strncmp(mode, "all", 3) != 0 && strncmp(mode, "length", 6) != 0 && | |
| 113 | + strncmp(mode, "strength", 8) != 0 && strncmp(mode, "duplicate", 9) != 0) { | |
| 114 | + if (strncmp(lang, "en", 2) == 0) | |
| 115 | + printf("Invalid mode. Possible modes: all, length, strength, duplicate\n"); | |
| 116 | + else | |
| 117 | + printf("不正なモード。可能なモード: all(全部)、length(長さ)、" | |
| 118 | + "strength(強さ)、duplicate(同じパスワードの確認)\n"); | |
| 119 | + return; | |
| 120 | + } | |
| 121 | + | |
| 122 | + // パスワードをスキャンして | |
| 123 | + List fpaths; | |
| 124 | + initList(&fpaths); | |
| 125 | + initList(&chkFullpaths); | |
| 126 | + initList(&chkDispaths); | |
| 127 | + initList(&dupPasswd); | |
| 128 | + initList(&dupPath); | |
| 129 | + scanDir(dpath, dpath, &fpaths, &chkFullpaths, &chkDispaths); | |
| 130 | + | |
| 131 | + puts(strncmp(lang, "en", 2) == 0 ? | |
| 132 | + "Checking, please wait for a while..." : "確認中。暫くお待ち下さい・・・"); | |
| 133 | + | |
| 134 | + for (size_t i = 0; i < chkDispaths.size; i++) { | |
| 135 | + const char *pass = showpass(getElement(&chkDispaths, i)); | |
| 136 | + if (!pass) continue; | |
| 137 | + if (strncmp(pass, "otpauth://totp/", strlen(pass)) == 0) continue; | |
| 138 | + | |
| 139 | + if (strncmp(mode, "all", 3) == 0) { | |
| 140 | + // 全部を確認する | |
| 141 | + chkLenPass(getElement(&chkDispaths, i), pass, lang); | |
| 142 | + chkCharPass(getElement(&chkDispaths, i), pass, lang); | |
| 143 | + chkDupPass(getElement(&chkDispaths, i), pass, lang); | |
| 144 | + } else if (strncmp(mode, "length", 6) == 0) { | |
| 145 | + // 長さの確認 | |
| 146 | + chkLenPass(getElement(&chkDispaths, i), pass, lang); | |
| 147 | + } else if (strncmp(mode, "strength", 8) == 0) { | |
| 148 | + // 強さの確認 | |
| 149 | + chkCharPass(getElement(&chkDispaths, i), pass, lang); | |
| 150 | + } else if (strncmp(mode, "duplicate", 9) == 0) { | |
| 151 | + // 複数回同じパスワードの確認 | |
| 152 | + chkDupPass(getElement(&chkDispaths, i), pass, lang); | |
| 153 | + } | |
| 154 | + } | |
| 155 | + | |
| 156 | + if (strncmp(lang, "en", 2) == 0) { | |
| 157 | + if (strncmp(mode, "all", 3) == 0 || strncmp(mode, "length", 6) == 0) | |
| 158 | + printf("\nShort password count: %d\n", weaklencount); | |
| 159 | + if (strncmp(mode, "all", 3) == 0 || strncmp(mode, "strength", 8) == 0) | |
| 160 | + printf("\nWeak password count: %d\n", weakcharcount); | |
| 161 | + if (strncmp(mode, "all", 3) == 0 || strncmp(mode, "duplicate", 9) == 0) | |
| 162 | + printf("\nDuplicate password count: %d\n", duppasscount); | |
| 163 | + | |
| 164 | + printf("\nTotal weak passwords: %d\n", vulncount); | |
| 165 | + if (vulncount > 0) | |
| 166 | + printf("It's advised to change any of the weak " | |
| 167 | + "passwords as soon as possible!\n"); | |
| 168 | + } else { | |
| 169 | + if (strncmp(mode, "all", 3) == 0 || strncmp(mode, "length", 6) == 0) | |
| 170 | + printf("\n短いパスワード数: %d\n", weaklencount); | |
| 171 | + if (strncmp(mode, "all", 3) == 0 || strncmp(mode, "strength", 8) == 0) | |
| 172 | + printf("\n弱いパスワード数: %d\n", weakcharcount); | |
| 173 | + if (strncmp(mode, "all", 3) == 0 || strncmp(mode, "duplicate", 9) == 0) | |
| 174 | + printf("\n同じパスワード数: %d\n", duppasscount); | |
| 175 | + | |
| 176 | + printf("\n不安のパスワードの合計: %d\n", vulncount); | |
| 177 | + if (vulncount > 0) | |
| 178 | + printf("不安的なパスワードは出来るだけ早く変更する事をお勧めします!\n"); | |
| 179 | + } | |
| 180 | + | |
| 181 | + freeList(&fpaths); | |
| 182 | + freeList(&dupPath); | |
| 183 | + freeList(&dupPasswd); | |
| 184 | +} |
| @@ -0,0 +1,18 @@ | ||
| 1 | +#ifndef CHKPASS_H | |
| 2 | +#define CHKPASS_H | |
| 3 | + | |
| 4 | +#include <stdlib.h> | |
| 5 | + | |
| 6 | +void chkpass(const char *dpath, const char *mode); | |
| 7 | + | |
| 8 | +extern int vulncount; | |
| 9 | +extern int weaklencount; | |
| 10 | +extern int weakcharcount; | |
| 11 | +extern int duppasscount; | |
| 12 | + | |
| 13 | +extern size_t minimumlen; | |
| 14 | +extern size_t recommendlen; | |
| 15 | + | |
| 16 | +extern const char *spchar; | |
| 17 | + | |
| 18 | +#endif |
追加
src/common.c
| @@ -0,0 +1,273 @@ | ||
| 1 | +#include "common.h" | |
| 2 | + | |
| 3 | +#include <stdio.h> | |
| 4 | +#include <string.h> | |
| 5 | +#include <dirent.h> | |
| 6 | +#include <sys/stat.h> | |
| 7 | + | |
| 8 | +#define MAXFINDLEN 1024 | |
| 9 | + | |
| 10 | +char *getbasedir(int trailing) { | |
| 11 | + char *lang = getlang(); | |
| 12 | + | |
| 13 | + char *homedir = getenv("HOME"); | |
| 14 | + if (homedir == NULL) { | |
| 15 | + if (strncmp(lang, "en", 2) == 0) | |
| 16 | + perror("Failed to get home directory"); | |
| 17 | + else perror("ホームディレクトリを受取に失敗"); | |
| 18 | + return NULL; | |
| 19 | + } | |
| 20 | + | |
| 21 | +#if defined(__HAIKU__) | |
| 22 | + char *basedir = "/config/settings/sp"; | |
| 23 | + char *slash = "/"; | |
| 24 | +#elif defined(_WIN32) | |
| 25 | + char *basedir = "\\AppData\\Local\\076\\sp"; | |
| 26 | + char *slash = "\\"; | |
| 27 | +#else | |
| 28 | + char *basedir = "/.local/share/sp"; | |
| 29 | + char *slash = "/"; | |
| 30 | +#endif | |
| 31 | + | |
| 32 | + size_t len = strlen(homedir) + strlen(basedir) + strlen(slash) + 4; | |
| 33 | + char *res = malloc(len); | |
| 34 | + if (res == NULL) { | |
| 35 | + if (strncmp(lang, "en", 2) == 0) | |
| 36 | + perror("Failed to allocate memory"); | |
| 37 | + else perror("メモリの役割に失敗"); | |
| 38 | + return NULL; | |
| 39 | + } | |
| 40 | + if (trailing == 1) | |
| 41 | + snprintf(res, len, "%s%s%s", homedir, basedir, slash); | |
| 42 | + else | |
| 43 | + snprintf(res, len, "%s%s", homedir, basedir); | |
| 44 | + | |
| 45 | + return res; | |
| 46 | +} | |
| 47 | + | |
| 48 | +char *getlang() { | |
| 49 | + char *lang = NULL; | |
| 50 | + | |
| 51 | + lang = getenv("SP_LANG"); | |
| 52 | + if (lang == NULL) lang = "ja"; | |
| 53 | + | |
| 54 | + return lang; | |
| 55 | +} | |
| 56 | + | |
| 57 | +int mkdir_r(const char *path, mode_t mode) { | |
| 58 | + char tmp[256]; | |
| 59 | + char *p = NULL; | |
| 60 | + size_t len; | |
| 61 | + | |
| 62 | + snprintf(tmp, sizeof(tmp), "%s", path); | |
| 63 | + | |
| 64 | + len = strlen(tmp); | |
| 65 | + if (tmp[len - 1] == '/') { | |
| 66 | + tmp[len - 1] = 0; // 最後の「/」文字を取り消す | |
| 67 | + } | |
| 68 | + | |
| 69 | + for (p = tmp + 1; *p; p++) { | |
| 70 | + if (*p == '/') { | |
| 71 | + *p = 0; // 最後の「/」文字を取り消す | |
| 72 | + if (mkdir(tmp, mode) != 0 && errno != EEXIST) return -1; | |
| 73 | + *p = '/'; // また追加 | |
| 74 | + } | |
| 75 | + } | |
| 76 | + | |
| 77 | + if (mkdir(tmp, mode) != 0 && errno != EEXIST) { | |
| 78 | + return -1; | |
| 79 | + } | |
| 80 | + | |
| 81 | + return 0; | |
| 82 | +} | |
| 83 | + | |
| 84 | +int tmpcopy(const char *inpath, const char *outpath) { | |
| 85 | + FILE *src = fopen(inpath, "rb"); | |
| 86 | + if (!src) { | |
| 87 | + return -1; | |
| 88 | + } | |
| 89 | + | |
| 90 | + FILE *dst = fopen(outpath, "wb"); | |
| 91 | + if (!dst) { | |
| 92 | + fclose(src); | |
| 93 | + return -1; | |
| 94 | + } | |
| 95 | + | |
| 96 | + size_t n, m; | |
| 97 | + unsigned char buf[8192]; | |
| 98 | + do { | |
| 99 | + n = fread(buf, 1, sizeof buf, src); | |
| 100 | + if (n) m = fwrite(buf, 1, n, dst); | |
| 101 | + else m = 0; | |
| 102 | + } while ((n > 0) && (n == m)); | |
| 103 | + if (m) return -1; | |
| 104 | + | |
| 105 | + fclose(src); | |
| 106 | + fclose(dst); | |
| 107 | + | |
| 108 | + return 0; | |
| 109 | +} | |
| 110 | + | |
| 111 | +#if defined(__linux__) | |
| 112 | +char *strcasestr(const char *haystack, const char *needle) { | |
| 113 | + size_t needle_len = strlen(needle); | |
| 114 | + if (needle_len == 0) { | |
| 115 | + return (char *)haystack; | |
| 116 | + } | |
| 117 | + | |
| 118 | + while (*haystack) { | |
| 119 | + if (strncasecmp(haystack, needle, needle_len) == 0) { | |
| 120 | + return (char *)haystack; | |
| 121 | + } | |
| 122 | + haystack++; | |
| 123 | + } | |
| 124 | + return NULL; | |
| 125 | +} | |
| 126 | +#endif | |
| 127 | + | |
| 128 | +void rmext(char *filename) { | |
| 129 | + char *ext = strrchr(filename, '.'); | |
| 130 | + if (ext != NULL && strcmp(ext, ".gpg") == 0) { | |
| 131 | + *ext = '\0'; | |
| 132 | + } | |
| 133 | +} | |
| 134 | + | |
| 135 | +void scanDir(const char *dpath, const char *rpath, List *fpaths, | |
| 136 | + List *fullpaths, List *dispaths) { | |
| 137 | + char *lang = getlang(); | |
| 138 | + | |
| 139 | + DIR *dir = opendir(dpath); | |
| 140 | + if (!dir) { | |
| 141 | + if (strncmp(lang, "en", 2) == 0) | |
| 142 | + perror("Could not open directory"); | |
| 143 | + else perror("ディレクトリを開けられません"); | |
| 144 | + exit(EXIT_FAILURE); | |
| 145 | + } | |
| 146 | + | |
| 147 | + struct dirent *entry; | |
| 148 | + while ((entry = readdir(dir)) != NULL) { | |
| 149 | + const char *name = entry->d_name; | |
| 150 | + if (strncmp(name, ".", 1) == 0 || strncmp(name, "..", 2) == 0) continue; | |
| 151 | + | |
| 152 | + char fpath[MAXFINDLEN]; | |
| 153 | + snprintf(fpath, sizeof(fpath), "%s/%s", dpath, name); | |
| 154 | + | |
| 155 | + struct stat s; | |
| 156 | + if (stat(fpath, &s) != 0) { | |
| 157 | + closedir(dir); | |
| 158 | + return; | |
| 159 | + } | |
| 160 | + | |
| 161 | + if (S_ISDIR(s.st_mode)) { | |
| 162 | + scanDir(fpath, rpath, fpaths, fullpaths, dispaths); | |
| 163 | + } else if (strstr(name, ".gpg") != NULL) { | |
| 164 | + const char *rel = fpath + strlen(rpath) + 1; | |
| 165 | + addElement(fpaths, rel); | |
| 166 | + addElement(fullpaths, fpath); | |
| 167 | + char *disname = strdup(rel); | |
| 168 | + rmext(disname); | |
| 169 | + addElement(dispaths, disname); | |
| 170 | + | |
| 171 | + free(disname); | |
| 172 | + } | |
| 173 | + } | |
| 174 | + | |
| 175 | + closedir(dir); | |
| 176 | +} | |
| 177 | + | |
| 178 | +void initList(List *list) { | |
| 179 | + list->head = NULL; | |
| 180 | + list->tail = NULL; | |
| 181 | + list->size = 0; | |
| 182 | +} | |
| 183 | + | |
| 184 | +void addElement(List *list, const char *data) { | |
| 185 | + Node *newNode = (Node *)malloc(sizeof(Node)); | |
| 186 | + if (!newNode) { | |
| 187 | + return; | |
| 188 | + } | |
| 189 | + | |
| 190 | + newNode->data = strdup(data); | |
| 191 | + if (!newNode->data) { | |
| 192 | + free(newNode); | |
| 193 | + return; | |
| 194 | + } | |
| 195 | + newNode->next = NULL; | |
| 196 | + | |
| 197 | + if (list->tail) { | |
| 198 | + list->tail->next = newNode; | |
| 199 | + } else { | |
| 200 | + list->head = newNode; | |
| 201 | + } | |
| 202 | + | |
| 203 | + list->tail = newNode; | |
| 204 | + list->size++; | |
| 205 | +} | |
| 206 | + | |
| 207 | +char *getElement(List *list, size_t idx) { | |
| 208 | + if (idx >= list->size) return NULL; | |
| 209 | + | |
| 210 | + Node *current = list->head; | |
| 211 | + for (size_t i = 0; i < idx; i++) | |
| 212 | + current = current->next; | |
| 213 | + | |
| 214 | + return current->data; | |
| 215 | +} | |
| 216 | + | |
| 217 | +void rmElement(List *list, size_t idx) { | |
| 218 | + if (idx >= list->size) return; | |
| 219 | + | |
| 220 | + Node *current = list->head; | |
| 221 | + Node *previous = NULL; | |
| 222 | + | |
| 223 | + if (idx == 0) { | |
| 224 | + list->head = current->next; | |
| 225 | + if (list->size == 1) list->tail = NULL; | |
| 226 | + } else { | |
| 227 | + for (size_t i = 0; i < idx; i++) { | |
| 228 | + previous = current; | |
| 229 | + current = current->next; | |
| 230 | + } | |
| 231 | + previous->next = current->next; | |
| 232 | + if (idx == list->size - 1) { | |
| 233 | + list->tail = previous; | |
| 234 | + } | |
| 235 | + } | |
| 236 | + | |
| 237 | + free(current->data); | |
| 238 | + free(current); | |
| 239 | + list->size--; | |
| 240 | +} | |
| 241 | + | |
| 242 | +void freeList(List *list) { | |
| 243 | + Node *current = list->head; | |
| 244 | + | |
| 245 | + while (current) { | |
| 246 | + Node *next = current->next; | |
| 247 | + free(current->data); | |
| 248 | + free(current); | |
| 249 | + current = next; | |
| 250 | + } | |
| 251 | + | |
| 252 | + list->head = NULL; | |
| 253 | + list->tail = NULL; | |
| 254 | + list->size = 0; | |
| 255 | +} | |
| 256 | + | |
| 257 | +void handle_sigint(int sig) { | |
| 258 | + (void)sig; | |
| 259 | + | |
| 260 | + if (getenv("WAYLAND_DISPLAY") != NULL) { | |
| 261 | + system("echo -n \"\" | wl-copy"); | |
| 262 | + } else { | |
| 263 | + system("echo -n \"\" | xclip -selection clipboard"); | |
| 264 | + } | |
| 265 | + | |
| 266 | + if (strncmp(getlang(), "en", 2) == 0) { | |
| 267 | + printf("\nClipboard cleared and program aborted.\n"); | |
| 268 | + } else { | |
| 269 | + printf("\nクリップボードをクリアし、プログラムが中止されました。\n"); | |
| 270 | + } | |
| 271 | + | |
| 272 | + exit(0); | |
| 273 | +} |
追加
src/common.h
| @@ -0,0 +1,40 @@ | ||
| 1 | +#ifndef COMMON_H | |
| 2 | +#define COMMON_H | |
| 3 | + | |
| 4 | +#include <signal.h> | |
| 5 | +#include <stdio.h> | |
| 6 | +#include <string.h> | |
| 7 | +#include <stdlib.h> | |
| 8 | +#include <stdbool.h> | |
| 9 | +#include <sys/stat.h> | |
| 10 | +#include <errno.h> | |
| 11 | + | |
| 12 | +#include <gpgme.h> | |
| 13 | + | |
| 14 | +typedef struct Node { | |
| 15 | + char *data; | |
| 16 | + struct Node *next; | |
| 17 | +} Node; | |
| 18 | + | |
| 19 | +typedef struct { | |
| 20 | + Node *head; | |
| 21 | + Node *tail; | |
| 22 | + size_t size; | |
| 23 | +} List; | |
| 24 | + | |
| 25 | +char *getbasedir(int trailing); | |
| 26 | +char *getlang(); | |
| 27 | +int mkdir_r(const char *path, mode_t mode); | |
| 28 | +int tmpcopy(const char *inpath, const char *outpath); | |
| 29 | +void scanDir(const char *dpath, const char *rpath, List *fpaths, | |
| 30 | + List *fullpaths, List *dispaths); | |
| 31 | +void handle_sigint(int sig); | |
| 32 | + | |
| 33 | +// C言語のvector | |
| 34 | +void initList(List *list); | |
| 35 | +void addElement(List *list, const char *data); | |
| 36 | +char *getElement(List *list, size_t idx); | |
| 37 | +void rmElement(List *list, size_t idx); | |
| 38 | +void freeList(List *list); | |
| 39 | + | |
| 40 | +#endif |
| @@ -0,0 +1,162 @@ | ||
| 1 | +#include <unistd.h> | |
| 2 | +#include <dirent.h> | |
| 3 | + | |
| 4 | +#include "common.h" | |
| 5 | +#include "delpass.h" | |
| 6 | + | |
| 7 | +int delcnt(const char *str, char delimiter) { | |
| 8 | + int count = 0; | |
| 9 | + while (*str) { | |
| 10 | + if (*str == delimiter) { | |
| 11 | + count++; | |
| 12 | + } | |
| 13 | + str++; | |
| 14 | + } | |
| 15 | + | |
| 16 | + return count; | |
| 17 | +} | |
| 18 | + | |
| 19 | +char **explode(const char *str, char delimiter, int *numtokens) { | |
| 20 | + int count = delcnt(str, delimiter) + 1; | |
| 21 | + *numtokens = count; | |
| 22 | + | |
| 23 | + char **tokens = malloc(count * sizeof(char *)); | |
| 24 | + if (tokens == NULL) { | |
| 25 | + perror("malloc"); | |
| 26 | + exit(EXIT_FAILURE); | |
| 27 | + } | |
| 28 | + | |
| 29 | + char *strcopy = strdup(str); | |
| 30 | + if (strcopy == NULL) { | |
| 31 | + perror("strdup"); | |
| 32 | + exit(EXIT_FAILURE); | |
| 33 | + } | |
| 34 | + | |
| 35 | + char *token = strtok(strcopy, &delimiter); | |
| 36 | + int i = 0; | |
| 37 | + while (token != NULL) { | |
| 38 | + tokens[i] = strdup(token); | |
| 39 | + if (tokens[i] == NULL) { | |
| 40 | + perror("strdup"); | |
| 41 | + exit(EXIT_FAILURE); | |
| 42 | + } | |
| 43 | + i++; | |
| 44 | + token = strtok(NULL, &delimiter); | |
| 45 | + } | |
| 46 | + | |
| 47 | + free(strcopy); | |
| 48 | + | |
| 49 | + return tokens; | |
| 50 | +} | |
| 51 | + | |
| 52 | +void freetokens(char **tokens, int numtokens) { | |
| 53 | + for (int i = 0; i < numtokens; i++) { | |
| 54 | + free(tokens[i]); | |
| 55 | + } | |
| 56 | + | |
| 57 | + free(tokens); | |
| 58 | +} | |
| 59 | + | |
| 60 | +int delpass(char *file, int force) { | |
| 61 | + char *lang = getlang(); | |
| 62 | + | |
| 63 | + // パスを準備 | |
| 64 | + char pwfile[512]; | |
| 65 | + char *basedir = getbasedir(1); | |
| 66 | + char *ext = ".gpg"; | |
| 67 | + | |
| 68 | + int alllen = snprintf(NULL, 0, "%s%s%s", basedir, file, ext) + 1; | |
| 69 | + char *gpgpathchk = malloc(alllen); | |
| 70 | + if (gpgpathchk == NULL) { | |
| 71 | + if (strncmp(lang, "en", 2) == 0) | |
| 72 | + perror("Failed to allocate memory"); | |
| 73 | + else perror("メモリを割当に失敗"); | |
| 74 | + return -1; | |
| 75 | + } | |
| 76 | + | |
| 77 | + // ファイルが既に存在するかどうか確認 | |
| 78 | + snprintf(gpgpathchk, alllen, "%s%s%s", basedir, file, ext); | |
| 79 | + if (access(gpgpathchk, F_OK) != 0) { | |
| 80 | + if (strncmp(lang, "en", 2) == 0) | |
| 81 | + perror("Password does not exist"); | |
| 82 | + else perror("パスワードが存在しません"); | |
| 83 | + free(gpgpathchk); | |
| 84 | + return -1; | |
| 85 | + } | |
| 86 | + free(gpgpathchk); | |
| 87 | + | |
| 88 | + int needed = snprintf( pwfile, sizeof(pwfile), "%s%s%s", basedir, file, ext); | |
| 89 | + if (needed >= (int)sizeof(pwfile)) { | |
| 90 | + if (strncmp(lang, "en", 2) == 0) | |
| 91 | + perror("Error: Path is too long"); | |
| 92 | + else perror("エラー:パスが長すぎる"); | |
| 93 | + return -1; | |
| 94 | + } | |
| 95 | + | |
| 96 | + // 削除を確認する | |
| 97 | + if (force == 0) { // パスワードの変更の場合、確認は不要 | |
| 98 | + if (strncmp(lang, "en", 2) == 0) | |
| 99 | + printf("Are you sure you want to delete the password '%s'? (y/N): ", file); | |
| 100 | + printf("パスワード「%s」を本当に削除する事が宜しいでしょうか? (y/N): ", file); | |
| 101 | + int confirm = getchar(); | |
| 102 | + if (confirm != 'y' && confirm != 'Y') { | |
| 103 | + if (strncmp(lang, "en", 2) == 0) puts("Not deleted"); | |
| 104 | + else puts("削除しませんでした"); | |
| 105 | + return -1; | |
| 106 | + } | |
| 107 | + | |
| 108 | + int ch; | |
| 109 | + while ((ch = getchar()) != '\n' && ch != EOF); | |
| 110 | + } | |
| 111 | + | |
| 112 | + if (unlink(pwfile) == -1) { | |
| 113 | + if (strncmp(lang, "en", 2) == 0) | |
| 114 | + perror("Password cannot be deleted"); | |
| 115 | + else perror("パスワードを削除出来ませんでした"); | |
| 116 | + return -1; | |
| 117 | + } | |
| 118 | + | |
| 119 | + // 空のディレクトリの場合 | |
| 120 | + int numt; | |
| 121 | + char **tokens = explode(file, '/', &numt); | |
| 122 | + | |
| 123 | + char passpath[1024]; | |
| 124 | + snprintf(passpath, sizeof(passpath), "%s%s", basedir, tokens[0]); | |
| 125 | + char *ls = strrchr(basedir, '/'); | |
| 126 | + if (ls != NULL) { | |
| 127 | + *ls = '\0'; | |
| 128 | + } | |
| 129 | + | |
| 130 | + for (int i = 1; i < numt; i++) { | |
| 131 | + if (i == (numt-1)) continue; | |
| 132 | + strncat(passpath, "/", sizeof(passpath) - strlen(passpath) - 1); | |
| 133 | + strncat(passpath, tokens[i], sizeof(passpath) - strlen(passpath) - 1); | |
| 134 | + } | |
| 135 | + | |
| 136 | + for (int i = numt - 1; i >= 0; i--) { | |
| 137 | + // ~/.local/share/sp を削除したら危険 | |
| 138 | + if (strncmp(passpath, basedir, sizeof(passpath)) == 0) { | |
| 139 | + break; | |
| 140 | + } | |
| 141 | + | |
| 142 | + // ディレクトリが空じゃない場合、削除を止める | |
| 143 | + if (rmdir(passpath) == -1) { | |
| 144 | + break; | |
| 145 | + } | |
| 146 | + | |
| 147 | + char *last_slash = strrchr(passpath, '/'); | |
| 148 | + if (last_slash != NULL) { | |
| 149 | + *last_slash = '\0'; | |
| 150 | + } | |
| 151 | + } | |
| 152 | + | |
| 153 | + freetokens(tokens, numt); | |
| 154 | + | |
| 155 | + // sp -e の場合、「パスワードを削除しました」って要らない | |
| 156 | + if (force == 1) return 0; | |
| 157 | + | |
| 158 | + if (strncmp(lang, "en", 2) == 0) puts("The password got deleted"); | |
| 159 | + else puts("パスワードを削除しました"); | |
| 160 | + | |
| 161 | + return 0; | |
| 162 | +} |
| @@ -0,0 +1,6 @@ | ||
| 1 | +#ifndef DELPASS_H | |
| 2 | +#define DELPASS_H | |
| 3 | + | |
| 4 | +int delpass(char *file, int force); | |
| 5 | + | |
| 6 | +#endif |
| @@ -0,0 +1,25 @@ | ||
| 1 | +#include "common.h" | |
| 2 | +#include "findpass.h" | |
| 3 | + | |
| 4 | +#include <stdio.h> | |
| 5 | +#include <stdlib.h> | |
| 6 | +#include <string.h> | |
| 7 | + | |
| 8 | +List findFullpaths; | |
| 9 | +List findDispaths; | |
| 10 | + | |
| 11 | +void findpass(const char *dpath, const char *txt) { | |
| 12 | + List fpaths; | |
| 13 | + initList(&fpaths); | |
| 14 | + initList(&findFullpaths); | |
| 15 | + initList(&findDispaths); | |
| 16 | + scanDir(dpath, dpath, &fpaths, &findFullpaths, &findDispaths); | |
| 17 | + | |
| 18 | + for (size_t i = 0; i < findDispaths.size; i++) { | |
| 19 | + if (strcasestr(getElement(&findDispaths, i), txt) != NULL) { | |
| 20 | + printf("%s\n", getElement(&findDispaths, i)); | |
| 21 | + } | |
| 22 | + } | |
| 23 | + | |
| 24 | + freeList(&fpaths); | |
| 25 | +} |
| @@ -0,0 +1,8 @@ | ||
| 1 | +#ifndef FINDPASS_H | |
| 2 | +#define FINDPASS_H | |
| 3 | + | |
| 4 | +#include "common.h" | |
| 5 | + | |
| 6 | +void findpass(const char *dpath, const char *txt); | |
| 7 | + | |
| 8 | +#endif |
| @@ -0,0 +1,32 @@ | ||
| 1 | +#include "common.h" | |
| 2 | +#include "genpass.h" | |
| 3 | + | |
| 4 | +void genpass(int count, bool issecure) { | |
| 5 | + char *lang = getlang(); | |
| 6 | + | |
| 7 | + const char *charset_risky = | |
| 8 | + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | |
| 9 | + const char *charset_secure = | |
| 10 | + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()=~-^\\|_@`[{]};:+*<>,./?"; | |
| 11 | + const char *charset = issecure ? charset_secure : charset_risky; | |
| 12 | + | |
| 13 | + FILE *fp = fopen("/dev/random", "rb"); | |
| 14 | + if (fp == NULL) { | |
| 15 | + if (strncmp(lang, "en", 2) == 0) | |
| 16 | + perror("Could not open /dev/random"); | |
| 17 | + else perror("/dev/randomを開けられませんでした"); | |
| 18 | + exit(EXIT_FAILURE); | |
| 19 | + } | |
| 20 | + | |
| 21 | + char password[count + 1]; | |
| 22 | + for (int i = 0; i < count; i++) { | |
| 23 | + unsigned char key; | |
| 24 | + fread(&key, sizeof(key), 1, fp); | |
| 25 | + password[i] = charset[key % strlen(charset)]; | |
| 26 | + } | |
| 27 | + | |
| 28 | + password[count] = '\0'; | |
| 29 | + fclose(fp); | |
| 30 | + | |
| 31 | + printf("%s\n", password); | |
| 32 | +} |
| @@ -0,0 +1,8 @@ | ||
| 1 | +#ifndef GENPASS_H | |
| 2 | +#define GENPASS_H | |
| 3 | + | |
| 4 | +#include <stdbool.h> | |
| 5 | + | |
| 6 | +void genpass(int count, bool issecure); | |
| 7 | + | |
| 8 | +#endif |
| @@ -0,0 +1,48 @@ | ||
| 1 | +#include "common.h" | |
| 2 | +#include "initpass.h" | |
| 3 | + | |
| 4 | +void initpass(char *gpgid) { | |
| 5 | + char *lang = getlang(); | |
| 6 | + | |
| 7 | + char *basedir = getbasedir(1); | |
| 8 | + | |
| 9 | + if (mkdir_r(basedir, 0755) != 0 && errno != EEXIST) { | |
| 10 | + if (strncmp(lang, "en", 2) == 0) | |
| 11 | + fprintf(stderr, "Failed to create directory.\n"); | |
| 12 | + else fprintf(stderr, "ディレクトリを作成に失敗。\n"); | |
| 13 | + return; | |
| 14 | + } | |
| 15 | + | |
| 16 | + char gpgidpath[512]; | |
| 17 | + snprintf(gpgidpath, sizeof(gpgidpath), "%s/.gpg-id", basedir); | |
| 18 | + | |
| 19 | + struct stat statbuf; | |
| 20 | + if (stat(gpgidpath, &statbuf) == 0) { | |
| 21 | + if (strncmp(lang, "en", 2) == 0) | |
| 22 | + fprintf(stderr, ".gpg-id file already exists.\n"); | |
| 23 | + else fprintf(stderr, ".gpg-idファイルは既に存在します。\n"); | |
| 24 | + return; | |
| 25 | + } | |
| 26 | + | |
| 27 | + FILE *gpgidfile = fopen(gpgidpath, "w"); | |
| 28 | + if (gpgidfile == NULL) { | |
| 29 | + if (strncmp(lang, "en", 2) == 0) | |
| 30 | + fprintf(stderr, "Failed to write .gpg-id file.\n"); | |
| 31 | + else fprintf(stderr, ".gpg-idファイルを書き込めません。\n"); | |
| 32 | + return; | |
| 33 | + } | |
| 34 | + | |
| 35 | + if (fprintf(gpgidfile, "%s\n", gpgid) < 0) { | |
| 36 | + if (strncmp(lang, "en", 2) == 0) | |
| 37 | + fprintf(stderr, "Failed to write .gpg-id file.\n"); | |
| 38 | + else fprintf(stderr, ".gpg-idファイルへの書き込みに失敗しました。\n"); | |
| 39 | + fclose(gpgidfile); | |
| 40 | + return; | |
| 41 | + } | |
| 42 | + | |
| 43 | + fclose(gpgidfile); | |
| 44 | + | |
| 45 | + if (strncmp(lang, "en", 2) == 0) | |
| 46 | + puts("Initialization completed."); | |
| 47 | + else puts("初期設定に完了しました。"); | |
| 48 | +} |
| @@ -0,0 +1,6 @@ | ||
| 1 | +#ifndef INITPASS_H | |
| 2 | +#define INITPASS_H | |
| 3 | + | |
| 4 | +void initpass(char* gpgid); | |
| 5 | + | |
| 6 | +#endif |
| @@ -0,0 +1,56 @@ | ||
| 1 | +#include <dirent.h> | |
| 2 | + | |
| 3 | +#include "common.h" | |
| 4 | +#include "listpass.h" | |
| 5 | + | |
| 6 | +void listpass(char *basePath, int level) { | |
| 7 | + char *lang = getlang(); | |
| 8 | + | |
| 9 | + struct dirent *entry; | |
| 10 | + DIR* dir = opendir(basePath); | |
| 11 | + if (!dir) { | |
| 12 | + if (strncmp(lang, "en", 2) == 0) | |
| 13 | + perror("Could not open directory"); | |
| 14 | + else perror("ディレクトリを開けられません"); | |
| 15 | + return; | |
| 16 | + } | |
| 17 | + | |
| 18 | + while ((entry = readdir(dir)) != NULL) { | |
| 19 | + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { | |
| 20 | + continue; | |
| 21 | + } | |
| 22 | + | |
| 23 | + char path[1000]; | |
| 24 | + int needed = snprintf(path, sizeof(path), "%s/%s", basePath, entry->d_name); | |
| 25 | + if (needed >= (int)sizeof(path) || needed < 0) { | |
| 26 | + if (strncmp(lang, "en", 2) == 0) | |
| 27 | + perror("Error: Path is too long, or failed to get lenth"); | |
| 28 | + else perror("エラー:パスが長すぎる、又は長さを受取に失敗"); | |
| 29 | + continue; | |
| 30 | + } | |
| 31 | + | |
| 32 | + struct stat statbuf; | |
| 33 | + if (stat(path, &statbuf) == -1) { | |
| 34 | + if (strncmp(lang, "en", 2) == 0) | |
| 35 | + perror("Failed to read file status"); | |
| 36 | + else perror("ファイル状況を読込に失敗"); | |
| 37 | + continue; | |
| 38 | + } | |
| 39 | + | |
| 40 | + for (int i = 0; i < level; i++) { | |
| 41 | + printf(" "); | |
| 42 | + } | |
| 43 | + | |
| 44 | + if (S_ISDIR(statbuf.st_mode)) { | |
| 45 | + printf("|-- %s\n", entry->d_name); | |
| 46 | + listpass(path, level + 1); | |
| 47 | + } else if (S_ISREG(statbuf.st_mode)) { | |
| 48 | + char *filename = entry->d_name; | |
| 49 | + char *ext = strstr(filename, ".gpg"); | |
| 50 | + if (ext) *ext = '\0'; | |
| 51 | + printf("|-- %s\n", filename); | |
| 52 | + } | |
| 53 | + } | |
| 54 | + | |
| 55 | + closedir(dir); | |
| 56 | +} |
| @@ -0,0 +1,6 @@ | ||
| 1 | +#ifndef LISTPASS_H | |
| 2 | +#define LISTPASS_H | |
| 3 | + | |
| 4 | +void listpass(char *basePath, int level); | |
| 5 | + | |
| 6 | +#endif |
| @@ -0,0 +1,246 @@ | ||
| 1 | +#include <openssl/hmac.h> | |
| 2 | +#include <openssl/sha.h> | |
| 3 | + | |
| 4 | +#include <unistd.h> | |
| 5 | + | |
| 6 | +#if defined(__APPLE) | |
| 7 | +#include <libkern/OSByteOrder.h> | |
| 8 | +#define htobe64(x) OSSwapHostToBigInt64(x) | |
| 9 | +#endif | |
| 10 | + | |
| 11 | +#include "base32.h" | |
| 12 | +#include "common.h" | |
| 13 | +#include "otppass.h" | |
| 14 | + | |
| 15 | +unsigned char *extract_secret(const char *otpauth_url, size_t *decoded_len) { | |
| 16 | + char *lang = getlang(); | |
| 17 | + | |
| 18 | + const char *secret_start = strstr(otpauth_url, "secret="); | |
| 19 | + if (!secret_start) { | |
| 20 | + if (strncmp(lang, "en", 2) == 0) | |
| 21 | + perror("Failed to find secret in the OTPAuth URL"); | |
| 22 | + else perror("OTPAuth URLの中に、シークレットを見つけられませんでした"); | |
| 23 | + return NULL; | |
| 24 | + } | |
| 25 | + secret_start += 7; | |
| 26 | + | |
| 27 | + const char *secret_end = strchr(secret_start, '&'); | |
| 28 | + if (!secret_end) { | |
| 29 | + if (secret_start[0] != '\0') { | |
| 30 | + secret_end = secret_start + strlen(secret_start); | |
| 31 | + } else { | |
| 32 | + secret_end = secret_start; | |
| 33 | + } | |
| 34 | + } | |
| 35 | + | |
| 36 | + if (secret_end < secret_start) { | |
| 37 | + if (strncmp(lang, "en", 2) == 0) | |
| 38 | + perror("Incorrect secret range"); | |
| 39 | + else perror("不正なシークレットの距離"); | |
| 40 | + return NULL; | |
| 41 | + } | |
| 42 | + | |
| 43 | + size_t secret_len = secret_end - secret_start; | |
| 44 | + char *secret_encoded = (char *)malloc(secret_len + 1); | |
| 45 | + | |
| 46 | + if (secret_encoded == NULL) { | |
| 47 | + if (strncmp(lang, "en", 2) == 0) | |
| 48 | + perror("Failed to allocate memory"); | |
| 49 | + else perror("メモリの役割に失敗"); | |
| 50 | + return NULL; | |
| 51 | + } | |
| 52 | + | |
| 53 | + strncpy(secret_encoded, secret_start, secret_len); | |
| 54 | + secret_encoded[secret_len] = '\0'; | |
| 55 | + | |
| 56 | + unsigned char *secret_decoded = base32_decode(secret_encoded, decoded_len); | |
| 57 | + free(secret_encoded); | |
| 58 | + | |
| 59 | + if (!secret_decoded) { | |
| 60 | + if (strncmp(lang, "en", 2) == 0) | |
| 61 | + perror("Failed to decrypt BASE32"); | |
| 62 | + else perror("BASE32の復号化に失敗"); | |
| 63 | + return NULL; | |
| 64 | + } | |
| 65 | + | |
| 66 | + return secret_decoded; | |
| 67 | +} | |
| 68 | + | |
| 69 | +#if defined(__HAIKU__) | |
| 70 | +uint64_t htobe64(uint64_t counter) { | |
| 71 | + uint64_t res = 0; | |
| 72 | + uint8_t *dest = (uint8_t *)&res; | |
| 73 | + uint8_t *src = (uint8_t *)&counter; | |
| 74 | + | |
| 75 | + dest[0] = src[7]; | |
| 76 | + dest[1] = src[6]; | |
| 77 | + dest[2] = src[5]; | |
| 78 | + dest[3] = src[4]; | |
| 79 | + dest[4] = src[3]; | |
| 80 | + dest[5] = src[2]; | |
| 81 | + dest[6] = src[1]; | |
| 82 | + dest[7] = src[0]; | |
| 83 | + | |
| 84 | + return res; | |
| 85 | +} | |
| 86 | +#endif | |
| 87 | + | |
| 88 | +uint32_t generate_totp(const char *secret, uint64_t counter) { | |
| 89 | + counter = htobe64(counter); | |
| 90 | + | |
| 91 | + unsigned char hash[SHA_DIGEST_LENGTH]; | |
| 92 | + HMAC( | |
| 93 | + EVP_sha1(), | |
| 94 | + secret, | |
| 95 | + strlen(secret), | |
| 96 | + (unsigned char *)&counter, | |
| 97 | + sizeof(counter), | |
| 98 | + hash, | |
| 99 | + NULL | |
| 100 | + ); | |
| 101 | + | |
| 102 | + int offset = hash[SHA_DIGEST_LENGTH - 1] & 0x0F; | |
| 103 | + uint32_t truncated_hash = | |
| 104 | + (hash[offset] & 0x7F) << 24 | | |
| 105 | + (hash[offset + 1] & 0xFF) << 16 | | |
| 106 | + (hash[offset + 2] & 0xFF) << 8 | | |
| 107 | + (hash[offset + 3] & 0xFF); | |
| 108 | + | |
| 109 | + return truncated_hash % 1000000; | |
| 110 | +} | |
| 111 | + | |
| 112 | +void otppass(char *file, int isCopy, int copyTimeout) { | |
| 113 | + if (isCopy == 1 && copyTimeout > 300) copyTimeout = 300; | |
| 114 | + char *lang = getlang(); | |
| 115 | + | |
| 116 | + int isGay = (getenv("WAYLAND_DISPLAY") != NULL); | |
| 117 | + | |
| 118 | + // Xセッションではない場合(例えば、SSH、TTY等)、otppass()を実行して | |
| 119 | + if (isCopy == 1 && getenv("DISPLAY") == NULL && getenv("WAYLAND_DISPLAY") == NULL) { | |
| 120 | + if (strncmp(lang, "en", 2) == 0) | |
| 121 | + puts("There is no X or Wayland session, so running 'sp -o'."); | |
| 122 | + else puts("X又はWaylandセッションではありませんので、「sp -o」を実行します。"); | |
| 123 | + otppass(file, 0, 0); | |
| 124 | + return; | |
| 125 | + } | |
| 126 | + | |
| 127 | + // CTRL + Cを押す場合 | |
| 128 | + if (isCopy) { | |
| 129 | + signal(SIGINT, handle_sigint); | |
| 130 | + } | |
| 131 | + | |
| 132 | + gpgme_ctx_t ctx; | |
| 133 | + gpgme_error_t err; | |
| 134 | + gpgme_data_t in, out; | |
| 135 | + size_t secret_len; | |
| 136 | + | |
| 137 | + gpgme_check_version(NULL); | |
| 138 | + err = gpgme_new(&ctx); | |
| 139 | + if (err) { | |
| 140 | + if (strncmp(lang, "en", 2) == 0) | |
| 141 | + perror("Failed to generate the GPG"); | |
| 142 | + else perror("GPGを創作に失敗"); | |
| 143 | + exit(1); | |
| 144 | + } | |
| 145 | + | |
| 146 | + err = gpgme_data_new_from_file(&in, file, 1); | |
| 147 | + if (err) { | |
| 148 | + if (strncmp(lang, "en", 2) == 0) | |
| 149 | + perror("Failed to read the GPG file"); | |
| 150 | + else perror("GPGファイルを読込に失敗"); | |
| 151 | + gpgme_release(ctx); | |
| 152 | + exit(1); | |
| 153 | + } | |
| 154 | + | |
| 155 | + err = gpgme_data_new(&out); | |
| 156 | + if (err) { | |
| 157 | + if (strncmp(lang, "en", 2) == 0) | |
| 158 | + perror("Failed to read the GPG data"); | |
| 159 | + else perror("GPGデータを読込に失敗"); | |
| 160 | + gpgme_release(ctx); | |
| 161 | + gpgme_data_release(in); | |
| 162 | + exit(1); | |
| 163 | + } | |
| 164 | + | |
| 165 | + err = gpgme_op_decrypt(ctx, in, out); | |
| 166 | + if (err) { | |
| 167 | + if (strncmp(lang, "en", 2) == 0) | |
| 168 | + perror("Failed to decrypt the GPG"); | |
| 169 | + else perror("GPGを復号化に失敗"); | |
| 170 | + gpgme_release(ctx); | |
| 171 | + gpgme_data_release(in); | |
| 172 | + exit(1); | |
| 173 | + } | |
| 174 | + | |
| 175 | + char *secret = gpgme_data_release_and_get_mem(out, &secret_len); | |
| 176 | + if (!secret) { | |
| 177 | + if (strncmp(lang, "en", 2) == 0) | |
| 178 | + perror("Failed to get the GPG"); | |
| 179 | + else perror("GPGを受取に失敗"); | |
| 180 | + gpgme_data_release(in); | |
| 181 | + gpgme_release(ctx); | |
| 182 | + exit(1); | |
| 183 | + } | |
| 184 | + | |
| 185 | + secret[secret_len] = '\0'; | |
| 186 | + | |
| 187 | + size_t decoded_len; | |
| 188 | + unsigned char *secret_decoded = extract_secret(secret, &decoded_len); | |
| 189 | + if (!secret_decoded) { | |
| 190 | + if (strncmp(lang, "en", 2) == 0) | |
| 191 | + perror("Failed to decode or export secret"); | |
| 192 | + else perror("シークレットの抽出又はデコードに失敗しました"); | |
| 193 | + gpgme_data_release(in); | |
| 194 | + gpgme_release(ctx); | |
| 195 | + gpgme_free(secret); | |
| 196 | + exit(1); | |
| 197 | + } | |
| 198 | + | |
| 199 | + secret_decoded[decoded_len] = '\0'; | |
| 200 | + | |
| 201 | + time_t current_time = time(NULL); | |
| 202 | + uint64_t counter = current_time / 30; | |
| 203 | + uint32_t otp = generate_totp((const char *)secret_decoded, counter); | |
| 204 | + | |
| 205 | + gpgme_data_release(in); | |
| 206 | + gpgme_release(ctx); | |
| 207 | + gpgme_free(secret); | |
| 208 | + free(secret_decoded); | |
| 209 | + | |
| 210 | + if (isCopy) { | |
| 211 | + char cmd[64]; | |
| 212 | + if (isGay) snprintf(cmd, sizeof(cmd), "echo -n %06d | wl-copy", otp); | |
| 213 | + else snprintf(cmd, sizeof(cmd), "echo -n %06d | xclip -selection clipboard", otp); | |
| 214 | + | |
| 215 | + int ret = system(cmd); | |
| 216 | + if (ret != 0) { | |
| 217 | + char *ero = (strncmp(lang, "en", 2) == 0 ? | |
| 218 | + "Failed to copy OTP." : "ワンタイムパスワードをコピーに失敗。"); | |
| 219 | + fprintf(stderr, "%s\n", ero); | |
| 220 | + } | |
| 221 | + | |
| 222 | + // 何(デフォルトは30)秒後、クリップボードから削除する | |
| 223 | + if (strncmp(lang, "en", 2) == 0) | |
| 224 | + printf( | |
| 225 | + "%s\n%s%d%s\n", | |
| 226 | + "Added the one time password to the clipboard.", | |
| 227 | + "After ", | |
| 228 | + copyTimeout, | |
| 229 | + " seconds it'll be deleted from the clipboard." | |
| 230 | + ); | |
| 231 | + else | |
| 232 | + printf( | |
| 233 | + "%s\n%d%s\n", | |
| 234 | + "ワンタイムパスワードをクリップボードに追加しました。", | |
| 235 | + copyTimeout, | |
| 236 | + "秒後はクリップボードから取り消されます。" | |
| 237 | + ); | |
| 238 | + | |
| 239 | + sleep(copyTimeout); | |
| 240 | + | |
| 241 | + if (isGay) system("echo -n \"\" | wl-copy"); | |
| 242 | + else system("echo -n \"\" | xclip -selection clipboard"); | |
| 243 | + } else { | |
| 244 | + printf("%06d\n", otp); | |
| 245 | + } | |
| 246 | +} |
| @@ -0,0 +1,6 @@ | ||
| 1 | +#ifndef OTPPASS_H | |
| 2 | +#define OTPPASS_H | |
| 3 | + | |
| 4 | +void otppass(char* file, int isCopy, int copyTimeout); | |
| 5 | + | |
| 6 | +#endif |
| @@ -0,0 +1,148 @@ | ||
| 1 | +#include <locale.h> | |
| 2 | + | |
| 3 | +#include "common.h" | |
| 4 | +#include "showpass.h" | |
| 5 | + | |
| 6 | +void clean_up( | |
| 7 | + gpgme_ctx_t ctx, | |
| 8 | + gpgme_data_t in, | |
| 9 | + gpgme_data_t out, | |
| 10 | + FILE *gpgfile, | |
| 11 | + char *gpgpath | |
| 12 | +) { | |
| 13 | + if (gpgfile) fclose(gpgfile); | |
| 14 | + if (gpgpath) free(gpgpath); | |
| 15 | + gpgme_data_release(in); | |
| 16 | + gpgme_data_release(out); | |
| 17 | + gpgme_release(ctx); | |
| 18 | +} | |
| 19 | + | |
| 20 | +const char *showpass(char *file) { | |
| 21 | + char *lang = getlang(); | |
| 22 | + | |
| 23 | + gpgme_ctx_t ctx; | |
| 24 | + gpgme_error_t err; | |
| 25 | + gpgme_data_t in = NULL, out = NULL; | |
| 26 | + char *gpgpath = NULL; | |
| 27 | + FILE *gpgfile = NULL; | |
| 28 | + | |
| 29 | + // GPGMEライブラリを設置 | |
| 30 | + setlocale(LC_ALL, ""); | |
| 31 | + gpgme_check_version(NULL); | |
| 32 | + gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL)); | |
| 33 | + | |
| 34 | + // GPGMEを創作 | |
| 35 | + err = gpgme_new(&ctx); | |
| 36 | + if (err) { | |
| 37 | + if (strncmp(lang, "en", 2) == 0) | |
| 38 | + fprintf(stderr, "Failed to generate GPGME: %s\n", gpgme_strerror(err)); | |
| 39 | + else fprintf(stderr, "GPGMEを創作に失敗:%s\n", gpgme_strerror(err)); | |
| 40 | + return NULL; | |
| 41 | + } | |
| 42 | + | |
| 43 | + // OpenPGPプロトコールを設定 | |
| 44 | + gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP); | |
| 45 | + if (err) { | |
| 46 | + if (strncmp(lang, "en", 2) == 0) | |
| 47 | + fprintf(stderr, "Failed to set OpenPGP protocol: %s\n", gpgme_strerror(err)); | |
| 48 | + else fprintf(stderr, "OpenPGP付トロコールの設置に失敗:%s\n", gpgme_strerror(err)); | |
| 49 | + clean_up(ctx, in, out, gpgfile, gpgpath); | |
| 50 | + return NULL; | |
| 51 | + } | |
| 52 | + | |
| 53 | + // 暗号化したタイルを開く | |
| 54 | + char *basedir = getbasedir(1); | |
| 55 | + char *ext = ".gpg"; | |
| 56 | + int alllen = snprintf(NULL, 0, "%s%s%s", basedir, file, ext) + 1; | |
| 57 | + gpgpath = malloc(alllen); | |
| 58 | + if (gpgpath == NULL) { | |
| 59 | + if (strncmp(lang, "en", 2) == 0) | |
| 60 | + perror("Failed to allocate memeory"); | |
| 61 | + else perror("メモリを割当に失敗"); | |
| 62 | + return NULL; | |
| 63 | + } | |
| 64 | + | |
| 65 | + snprintf(gpgpath, alllen, "%s%s%s", basedir, file, ext); | |
| 66 | + | |
| 67 | + gpgfile = fopen(gpgpath, "rb"); | |
| 68 | + if (gpgfile == NULL) { | |
| 69 | + if (strncmp(lang, "en", 2) == 0) { | |
| 70 | + perror("Failed to open file"); | |
| 71 | + } else { | |
| 72 | + perror("ファイルを開くに失敗"); | |
| 73 | + } | |
| 74 | + free(gpgpath); | |
| 75 | + return NULL; | |
| 76 | + } | |
| 77 | + | |
| 78 | + // ファイルからinデータオブジェクトを創作 | |
| 79 | + if (gpgme_data_new_from_stream(&in, gpgfile) != GPG_ERR_NO_ERROR) { | |
| 80 | + if (strncmp(lang, "en", 2) == 0) | |
| 81 | + perror("Failed to generate the GPGME data object"); | |
| 82 | + else perror("GPGMEデータオブジェクトを創作に失敗"); | |
| 83 | + clean_up(ctx, in, out, gpgfile, gpgpath); | |
| 84 | + return NULL; | |
| 85 | + } | |
| 86 | + | |
| 87 | + // outデータオブジェクトを創作 | |
| 88 | + if (gpgme_data_new(&out) != GPG_ERR_NO_ERROR) { | |
| 89 | + if (strncmp(lang, "en", 2) == 0) | |
| 90 | + perror("Failed to generate the GPGME data object"); | |
| 91 | + else perror("GPGMEデータオブジェクトを創作に失敗"); | |
| 92 | + clean_up(ctx, in, out, gpgfile, gpgpath); | |
| 93 | + return NULL; | |
| 94 | + } | |
| 95 | + | |
| 96 | + // データオブジェクトを創作 | |
| 97 | + gpgme_data_new(&out); | |
| 98 | + | |
| 99 | + // 復号化して | |
| 100 | + err = gpgme_op_decrypt(ctx, in, out); | |
| 101 | + if (err) { | |
| 102 | + if (strncmp(lang, "en", 2) == 0) | |
| 103 | + fprintf(stderr, "Failed to decrypt: %s (source: %s)\n", | |
| 104 | + gpgme_strerror(err), gpgme_strsource(err)); | |
| 105 | + else fprintf(stderr, "復号化に失敗: %s (元: %s)\n", | |
| 106 | + gpgme_strerror(err), gpgme_strsource(err)); | |
| 107 | + | |
| 108 | + // 掃除 | |
| 109 | + clean_up(ctx, in, out, gpgfile, gpgpath); | |
| 110 | + return NULL; | |
| 111 | + } | |
| 112 | + | |
| 113 | + // 復号化したパスワードを表示する | |
| 114 | + gpgme_data_seek(out, 0, SEEK_SET); | |
| 115 | + size_t bufsize = 512; | |
| 116 | + size_t totsize = 0; | |
| 117 | + char buffer[512]; | |
| 118 | + char *res = malloc(bufsize); | |
| 119 | + if (res == NULL) { | |
| 120 | + if (strncmp(lang, "en", 2) == 0) | |
| 121 | + perror("Failed to allocate memory"); | |
| 122 | + else perror("メモリを役割に失敗"); | |
| 123 | + clean_up(ctx, in, out, gpgfile, gpgpath); | |
| 124 | + return NULL; | |
| 125 | + } | |
| 126 | + | |
| 127 | + ssize_t read_bytes; | |
| 128 | + | |
| 129 | + while ((read_bytes = gpgme_data_read(out, buffer, sizeof(buffer) - 1)) > 0) { | |
| 130 | + if (totsize + read_bytes >= bufsize) { | |
| 131 | + bufsize *= 2; | |
| 132 | + res = realloc(res, bufsize); | |
| 133 | + if (res == NULL) { | |
| 134 | + perror("Failed to reallocate memory"); | |
| 135 | + clean_up(ctx, in, out, gpgfile, gpgpath); | |
| 136 | + return NULL; | |
| 137 | + } | |
| 138 | + } | |
| 139 | + memcpy(res + totsize, buffer, read_bytes); | |
| 140 | + totsize += read_bytes; | |
| 141 | + } | |
| 142 | + | |
| 143 | + res[totsize] = '\0'; | |
| 144 | + | |
| 145 | + // 掃除 | |
| 146 | + clean_up(ctx, in, out, gpgfile, gpgpath); | |
| 147 | + return res; | |
| 148 | +} |
| @@ -0,0 +1,6 @@ | ||
| 1 | +#ifndef SHOWPASS_H | |
| 2 | +#define SHOWPASS_H | |
| 3 | + | |
| 4 | +const char *showpass(char *file); | |
| 5 | + | |
| 6 | +#endif |
| @@ -0,0 +1,141 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | +#include <stdlib.h> | |
| 3 | +#include <string.h> | |
| 4 | +#include <unistd.h> | |
| 5 | +#include <arpa/inet.h> | |
| 6 | +#include <sys/types.h> | |
| 7 | +#include <sys/socket.h> | |
| 8 | +#include <netdb.h> | |
| 9 | +#include <netinet/in.h> | |
| 10 | +#include <dirent.h> | |
| 11 | + | |
| 12 | +#include "vulnpass.h" | |
| 13 | +#include "showpass.h" | |
| 14 | +#include "common.h" | |
| 15 | + | |
| 16 | +List vulnFullpaths; | |
| 17 | +List vulnDispaths; | |
| 18 | + | |
| 19 | +void vulnpass(const char *dpath) { | |
| 20 | + char *lang = getlang(); | |
| 21 | + | |
| 22 | + // pwndサーバに接続 | |
| 23 | + int sock; | |
| 24 | + struct sockaddr_in srv; | |
| 25 | + struct addrinfo hints, *addr; | |
| 26 | + | |
| 27 | + memset(&hints, 0, sizeof(hints)); | |
| 28 | + hints.ai_family = AF_INET; | |
| 29 | + hints.ai_socktype = SOCK_STREAM; | |
| 30 | + | |
| 31 | + int status = getaddrinfo("076.moe", NULL, &hints, &addr); | |
| 32 | + if (status != 0) { | |
| 33 | + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status)); | |
| 34 | + return; | |
| 35 | + } | |
| 36 | + | |
| 37 | + sock = socket(AF_INET, SOCK_STREAM, 0); | |
| 38 | + if (sock == -1) { | |
| 39 | + const char *ero = strncmp(lang, "en", 2) == 0 ? | |
| 40 | + "Failed to create socket" : "ソケットを作成に失敗"; | |
| 41 | + perror(ero); | |
| 42 | + return; | |
| 43 | + } | |
| 44 | + | |
| 45 | + srv.sin_addr = ((struct sockaddr_in *)(addr->ai_addr))->sin_addr; | |
| 46 | + srv.sin_family = AF_INET; | |
| 47 | + srv.sin_port = htons(9951); | |
| 48 | + | |
| 49 | + freeaddrinfo(addr); | |
| 50 | + | |
| 51 | + if (connect(sock, (struct sockaddr *)&srv, sizeof(srv)) < 0) { | |
| 52 | + const char *ero = strncmp(lang, "en", 2) == 0 ? "Failed to connect" : "接続に失敗"; | |
| 53 | + perror(ero); | |
| 54 | + close(sock); | |
| 55 | + return; | |
| 56 | + } | |
| 57 | + | |
| 58 | + // パスワードをスキャンして | |
| 59 | + List fpaths; | |
| 60 | + initList(&fpaths); | |
| 61 | + initList(&vulnFullpaths); | |
| 62 | + initList(&vulnDispaths); | |
| 63 | + scanDir(dpath, dpath, &fpaths, &vulnFullpaths, &vulnDispaths); | |
| 64 | + int vulncount = 0; | |
| 65 | + | |
| 66 | + puts(strncmp(lang, "en", 2) == 0 ? | |
| 67 | + "Checking, please wait for a while..." : "確認中。暫くお待ち下さい・・・"); | |
| 68 | + | |
| 69 | + for (size_t i = 0; i < vulnDispaths.size; i++) { | |
| 70 | + const char *pass = showpass(getElement(&vulnDispaths, i)); | |
| 71 | + if (!pass) continue; | |
| 72 | + char res[256]; | |
| 73 | + int reslen = 0; | |
| 74 | + | |
| 75 | + if (send(sock, pass, strlen(pass), 0) < 0) { | |
| 76 | + const char *ero = strncmp(lang, "en", 2) == 0 ? | |
| 77 | + "Failed to send" : "送信に失敗"; | |
| 78 | + perror(ero); | |
| 79 | + close(sock); | |
| 80 | + freeList(&fpaths); | |
| 81 | + return; | |
| 82 | + } | |
| 83 | + | |
| 84 | + reslen = recv(sock, res, sizeof(res) -1, 0); | |
| 85 | + if (reslen < 0) { | |
| 86 | + const char *ero = strncmp(lang, "en", 2) == 0 ? | |
| 87 | + "Failed to retreive" : "受取に失敗"; | |
| 88 | + perror(ero); | |
| 89 | + close(sock); | |
| 90 | + freeList(&fpaths); | |
| 91 | + return; | |
| 92 | + } | |
| 93 | + | |
| 94 | + res[reslen] = '\0'; | |
| 95 | + | |
| 96 | + if (strncmp(res, "0", 1) != 0) { | |
| 97 | + printf("【!】"); | |
| 98 | + if (strncmp(lang, "en", 2) == 0) { | |
| 99 | + printf("The password \"%s\" has been breached %d times.\n", | |
| 100 | + getElement(&vulnDispaths, i), atoi(res)); | |
| 101 | + } else { | |
| 102 | + printf("パスワード「%s」は%d回に漏洩されました。\n", | |
| 103 | + getElement(&vulnDispaths, i), atoi(res)); | |
| 104 | + } | |
| 105 | + | |
| 106 | + vulncount++; | |
| 107 | + } | |
| 108 | + | |
| 109 | + close(sock); | |
| 110 | + | |
| 111 | + // 再接続 | |
| 112 | + sock = socket(AF_INET, SOCK_STREAM, 0); | |
| 113 | + if (sock == -1) { | |
| 114 | + const char *ero = strncmp(lang, "en", 2) == 0 ? | |
| 115 | + "Failed to create socket" : "ソケットを作成に失敗"; | |
| 116 | + perror(ero); | |
| 117 | + return; | |
| 118 | + } | |
| 119 | + | |
| 120 | + if (connect(sock, (struct sockaddr *)&srv, sizeof(srv)) < 0) { | |
| 121 | + const char *ero = strncmp(lang, "en", 2) == 0 ? | |
| 122 | + "Failed to reconnect" : "再接続に失敗"; | |
| 123 | + perror(ero); | |
| 124 | + close(sock); | |
| 125 | + return; | |
| 126 | + } | |
| 127 | + } | |
| 128 | + | |
| 129 | + if (strncmp(lang, "en", 2) == 0) { | |
| 130 | + printf("\nTotal breached: %d\n", vulncount); | |
| 131 | + if (vulncount > 0) | |
| 132 | + printf("It's advised to change any of the breached " | |
| 133 | + "passwords as soon as possible!\n"); | |
| 134 | + } else { | |
| 135 | + printf("\n漏洩したパスワードの合計: %d\n", vulncount); | |
| 136 | + if (vulncount > 0) | |
| 137 | + printf("漏洩したパスワードは出来るだけ早く変更する事をお勧めします!\n"); | |
| 138 | + } | |
| 139 | + | |
| 140 | + freeList(&fpaths); | |
| 141 | +} |
| @@ -0,0 +1,6 @@ | ||
| 1 | +#ifndef VULNPASS_H | |
| 2 | +#define VULNPASS_H | |
| 3 | + | |
| 4 | +void vulnpass(const char *dpath); | |
| 5 | + | |
| 6 | +#endif |
| @@ -0,0 +1,154 @@ | ||
| 1 | +#include <locale.h> | |
| 2 | +#include <unistd.h> | |
| 3 | + | |
| 4 | +#include "common.h" | |
| 5 | +#include "yankpass.h" | |
| 6 | +#include "showpass.h" | |
| 7 | + | |
| 8 | +void yankpass(char *file, int copyTimeout) { | |
| 9 | + if (copyTimeout > 300) copyTimeout = 300; | |
| 10 | + char *lang = getlang(); | |
| 11 | + | |
| 12 | + int isGay = (getenv("WAYLAND_DISPLAY") != NULL); | |
| 13 | + | |
| 14 | + // Xセッションではない場合(例えば、SSH、TTY等)、showpass()を実行して | |
| 15 | + if (getenv("DISPLAY") == NULL && getenv("WAYLAND_DISPLAY") == NULL) { | |
| 16 | + if (strncmp(lang, "en", 2) == 0) | |
| 17 | + puts("There is no X or Wayland session, so running 'sp -s'."); | |
| 18 | + else puts("X又はWaylandセッションではありませんので、「sp -s」を実行します。"); | |
| 19 | + showpass(file); | |
| 20 | + return; | |
| 21 | + } | |
| 22 | + | |
| 23 | + gpgme_ctx_t ctx; | |
| 24 | + gpgme_error_t err; | |
| 25 | + gpgme_data_t in, out; | |
| 26 | + FILE *gpgfile; | |
| 27 | + | |
| 28 | + // GPGMEライブラリを設置 | |
| 29 | + setlocale(LC_ALL, ""); | |
| 30 | + gpgme_check_version(NULL); | |
| 31 | + gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL)); | |
| 32 | + | |
| 33 | + // GPGMEを創作 | |
| 34 | + err = gpgme_new(&ctx); | |
| 35 | + if (err) { | |
| 36 | + if (strncmp(lang, "en", 2) == 0) | |
| 37 | + fprintf(stderr, "Failed to generate GPGME: %s\n", gpgme_strerror(err)); | |
| 38 | + else fprintf(stderr, "GPGMEを創作に失敗:%s\n", gpgme_strerror(err)); | |
| 39 | + return; | |
| 40 | + } | |
| 41 | + | |
| 42 | + // CTRL + Cを押す場合 | |
| 43 | + signal(SIGINT, handle_sigint); | |
| 44 | + | |
| 45 | + // OpenPGPプロトコールを設定 | |
| 46 | + gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP); | |
| 47 | + | |
| 48 | + // 暗号化したタイルを開く | |
| 49 | + char *basedir = getbasedir(1); | |
| 50 | + char* ext = ".gpg"; | |
| 51 | + int alllen = snprintf(NULL, 0, "%s%s%s", basedir, file, ext) + 1; | |
| 52 | + char* gpgpath = malloc(alllen); | |
| 53 | + if (gpgpath == NULL) { | |
| 54 | + if (strncmp(lang, "en", 2) == 0) | |
| 55 | + perror("Failed to allocate memory"); | |
| 56 | + else perror("メモリを割当に失敗"); | |
| 57 | + return; | |
| 58 | + } | |
| 59 | + | |
| 60 | + snprintf(gpgpath, alllen, "%s%s%s", basedir, file, ext); | |
| 61 | + gpgfile = fopen(gpgpath, "rb"); | |
| 62 | + if (gpgfile == NULL) { | |
| 63 | + if (strncmp(lang, "en", 2) == 0) { | |
| 64 | + perror("Failed to open the file"); | |
| 65 | + fprintf(stderr, "Failed path: %s\n", gpgpath); | |
| 66 | + } else { | |
| 67 | + perror("ファイルを開くに失敗"); | |
| 68 | + fprintf(stderr, "失敗したパス: %s\n", gpgpath); | |
| 69 | + } | |
| 70 | + free(gpgpath); | |
| 71 | + return; | |
| 72 | + } | |
| 73 | + gpgme_data_new_from_stream(&in, gpgfile); | |
| 74 | + | |
| 75 | + // データオブジェクトを創作 | |
| 76 | + gpgme_data_new(&out); | |
| 77 | + | |
| 78 | + // 復号化して | |
| 79 | + err = gpgme_op_decrypt(ctx, in, out); | |
| 80 | + if (err) { | |
| 81 | + if (strncmp(lang, "en", 2) == 0) | |
| 82 | + fprintf(stderr, "Failed to decrypt: %s\n", gpgme_strerror(err)); | |
| 83 | + else fprintf(stderr, "復号化に失敗: %s\n", gpgme_strerror(err)); | |
| 84 | + | |
| 85 | + // 掃除 | |
| 86 | + fclose(gpgfile); | |
| 87 | + free(gpgpath); | |
| 88 | + gpgme_data_release(in); | |
| 89 | + gpgme_data_release(out); | |
| 90 | + gpgme_release(ctx); | |
| 91 | + return; | |
| 92 | + } | |
| 93 | + | |
| 94 | + // xclip又はwl-copyを準備して | |
| 95 | + char *cmd; | |
| 96 | + if (isGay) cmd = "wl-copy"; | |
| 97 | + else cmd = "xclip -selection clipboard"; | |
| 98 | + | |
| 99 | + FILE *pipe = popen(cmd, "w"); | |
| 100 | + if (pipe == NULL) { | |
| 101 | + // 掃除 | |
| 102 | + fclose(gpgfile); | |
| 103 | + free(gpgpath); | |
| 104 | + gpgme_data_release(in); | |
| 105 | + gpgme_data_release(out); | |
| 106 | + gpgme_release(ctx); | |
| 107 | + if (strncmp(lang, "en", 2) == 0) | |
| 108 | + perror("Could not find a clipboard"); | |
| 109 | + else perror("クリップボードを見つけられませんでした"); | |
| 110 | + return; | |
| 111 | + } | |
| 112 | + | |
| 113 | + // 復号化したパスワードを表示する | |
| 114 | + gpgme_data_seek(out, 0, SEEK_SET); | |
| 115 | + | |
| 116 | + char buffer[512]; | |
| 117 | + ssize_t read_bytes; | |
| 118 | + | |
| 119 | + while ((read_bytes = gpgme_data_read(out, buffer, sizeof(buffer))) > 0) { | |
| 120 | + if (buffer[read_bytes - 1] == '\n') { | |
| 121 | + read_bytes--; | |
| 122 | + } | |
| 123 | + fwrite(buffer, 1, read_bytes, pipe); | |
| 124 | + } | |
| 125 | + | |
| 126 | + pclose(pipe); | |
| 127 | + | |
| 128 | + // 何(デフォルトは45)秒後、クリップボードから削除する | |
| 129 | + if (strncmp(lang, "en", 2) == 0) | |
| 130 | + printf( | |
| 131 | + "%s\n%s%d%s\n", | |
| 132 | + "Added password to the clipboard.", | |
| 133 | + "After ", | |
| 134 | + copyTimeout, | |
| 135 | + " seconds it'll be deleted from the clipboard." | |
| 136 | + ); | |
| 137 | + else | |
| 138 | + printf( | |
| 139 | + "%s\n%d%s\n", | |
| 140 | + "パスワードをクリップボードに追加しました。", | |
| 141 | + copyTimeout, | |
| 142 | + "秒後はクリップボードから取り消されます。" | |
| 143 | + ); | |
| 144 | + sleep(copyTimeout); | |
| 145 | + if (isGay) system("echo -n \"\" | wl-copy"); | |
| 146 | + else system("echo -n \"\" | xclip -selection clipboard"); | |
| 147 | + | |
| 148 | + // 掃除 | |
| 149 | + fclose(gpgfile); | |
| 150 | + free(gpgpath); | |
| 151 | + gpgme_data_release(in); | |
| 152 | + gpgme_data_release(out); | |
| 153 | + gpgme_release(ctx); | |
| 154 | +} |
| @@ -0,0 +1,6 @@ | ||
| 1 | +#ifndef YANKPASS_H | |
| 2 | +#define YANKPASS_H | |
| 3 | + | |
| 4 | +void yankpass(char *file, int copyTimeout); | |
| 5 | + | |
| 6 | +#endif |