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