suwako / sp アーカイブ

このリポジトリはアーカイブされています。読むだけで、プッシュやイシューの変更は出来ません。

master
ブランチ master
sp / src / chkpass.c
6.3 KB、184行 生 Blame 履歴
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
12List chkFullpaths;
13List chkDispaths;
14List dupPasswd;
15List dupPath;
16
17int vulncount = 0;
18int weaklencount = 0;
19int weakcharcount = 0;
20int duppasscount = 0;
21
22size_t minimumlen = 12;
23size_t recommendlen = 64;
24
25const char *spchar = "!@#$%^&*()-_=+[]{}|;:'\",.<>?/\\`~";
26
27void 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
56void 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
86void 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
109void 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}