| 1 | /* |
| 2 | * Copyright (c) 2024 Red Hat, Inc. |
| 3 | * Author: Sergio Arroutbi <[email protected]> |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | #include <arpa/inet.h> |
| 19 | #include <netinet/in.h> |
| 20 | #include <pthread.h> |
| 21 | #include <signal.h> |
| 22 | #include <stddef.h> |
| 23 | #include <stdint.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <sys/un.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/socket.h> |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | #ifdef GIT_VERSIONcxx . . $ `<built-in>:418:9`! |
| 33 | const char* VERSIONcxx . . $ VERSION. = GIT_VERSIONcxx . . $ `<built-in>:418:9`!; |
| 34 | #else |
| 35 | const char* VERSION = "v0.0.1"; |
| 36 | #endif |
| 37 | |
| 38 | #define MAX_DEVICEcxx . . $ `src/pins/pkcs11/clevis-pkcs11-afunix-socket-unlock.c:38:9`! 1024 |
| 39 | #define MAX_ENTRIEScxx . . $ `src/pins/pkcs11/clevis-pkcs11-afunix-socket-unlock.c:39:9`! 1024 |
| 40 | #define MAX_KEYcxx . . $ `src/pins/pkcs11/clevis-pkcs11-afunix-socket-unlock.c:40:9`! 1024 |
| 41 | |
| 42 | const uint16_tcxx . . $ uint16_t# DEFAULT_MAX_ITERATIONScxx . . $ DEFAULT_MAX_ITERATIONS. = 3; |
| 43 | const uint16_tcxx . . $ uint16_t# MAX_PATHcxx . . $ MAX_PATH. = 1024; |
| 44 | const uint16_tcxx . . $ uint16_t# MAX_CONTROL_MSGcxx . . $ MAX_CONTROL_MSG. = 1024; |
| 45 | const uint8_tcxx . . $ uint8_t# WAIT_CONTROL_THREAD_TIMERcxx . . $ WAIT_CONTROL_THREAD_TIMER. = 1; |
| 46 | |
| 47 | // Time to wait before trying to write key |
| 48 | const uint16_tcxx . . $ uint16_t# DEFAULT_START_DELAYcxx . . $ DEFAULT_START_DELAY. = 0; |
| 49 | |
| 50 | typedef structcxx . . $ $anonymous_type_3d16bdea7ee1d7b6_0# { |
| 51 | char devcxx . . $ $anonymous_type_3d16bdea7ee1d7b6_0#dev.[MAX_DEVICEcxx . . $ `src/pins/pkcs11/clevis-pkcs11-afunix-socket-unlock.c:38:9`!+1]; |
| 52 | char keycxx . . $ $anonymous_type_3d16bdea7ee1d7b6_0#key.[MAX_KEYcxx . . $ `src/pins/pkcs11/clevis-pkcs11-afunix-socket-unlock.c:40:9`!+1]; |
| 53 | } key_entry_tcxx . . $ key_entry_t#; |
| 54 | key_entry_tcxx . . $ key_entry_t# keyscxx . . $ keys.[MAX_ENTRIEScxx . . $ `src/pins/pkcs11/clevis-pkcs11-afunix-socket-unlock.c:39:9`!]; |
| 55 | uint16_tcxx . . $ uint16_t# entry_countercxx . . $ entry_counter. = 0; |
| 56 | uint8_tcxx . . $ uint8_t# thread_loopcxx . . $ thread_loop. = 1; |
| 57 | uint8_tcxx . . $ uint8_t# control_thread_infocxx . . $ control_thread_info. = 0; |
| 58 | pthread_mutex_tcxx . . $ pthread_mutex_t# mutexcxx . . $ mutex.; |
| 59 | FILEcxx . . $ FILE#* logfilecxx . . $ logfile. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 60 | |
| 61 | static void |
| 62 | get_control_socket_namecxx . . $ get_control_socket_name(c194a158e5e34a1c).(const char* file_sock, char* control_sock, uint32_tcxx . . $ uint32_t# control_sock_len) { |
| 63 | char *p = strstrcxx . . $ strstr(a713fe7b9a57cf96).(file_sock, ".sock"); |
| 64 | size_t prefix_length = strlencxx . . $ strlen(751346f8406fd082).(file_sock) - strlencxx . . $ strlen(751346f8406fd082).(p); |
| 65 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(control_sock, 0, control_sock_len); |
| 66 | memcpycxx . . $ memcpy(cc817cb175f282d).(control_sock, file_sock, prefix_length); |
| 67 | if (prefix_length + strlencxx . . $ strlen(751346f8406fd082).(".control.sock") < control_sock_len) { |
| 68 | strcatcxx . . $ strcat(b7b3a83273496486).(control_sock + prefix_length, ".control.sock"); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | static void insert_devicecxx . . $ insert_device(85c52e162fed56f9).(const char* dev) { |
| 73 | pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&mutexcxx . . $ mutex.); |
| 74 | if(MAX_ENTRIEScxx . . $ `src/pins/pkcs11/clevis-pkcs11-afunix-socket-unlock.c:39:9`! == entry_countercxx . . $ entry_counter.) { |
| 75 | pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&mutexcxx . . $ mutex.); |
| 76 | perrorcxx . . $ perror(85c52e162fed56f9).("No more entries accepted\n"); |
| 77 | return; |
| 78 | } |
| 79 | strncpycxx . . $ strncpy(255158a415b673b0).(keyscxx . . $ keys.[entry_countercxx . . $ entry_counter.].devcxx . . $ $anonymous_type_3d16bdea7ee1d7b6_0#dev., dev, MAX_DEVICEcxx . . $ `src/pins/pkcs11/clevis-pkcs11-afunix-socket-unlock.c:38:9`!); |
| 80 | pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&mutexcxx . . $ mutex.); |
| 81 | } |
| 82 | |
| 83 | static void insert_keycxx . . $ insert_key(85c52e162fed56f9).(const char* key) { |
| 84 | pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&mutexcxx . . $ mutex.); |
| 85 | if(MAX_ENTRIEScxx . . $ `src/pins/pkcs11/clevis-pkcs11-afunix-socket-unlock.c:39:9`! == entry_countercxx . . $ entry_counter.) { |
| 86 | pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&mutexcxx . . $ mutex.); |
| 87 | perrorcxx . . $ perror(85c52e162fed56f9).("No more entries accepted\n"); |
| 88 | return; |
| 89 | } |
| 90 | strncpycxx . . $ strncpy(255158a415b673b0).(keyscxx . . $ keys.[entry_countercxx . . $ entry_counter.++].keycxx . . $ $anonymous_type_3d16bdea7ee1d7b6_0#key., key, MAX_KEYcxx . . $ `src/pins/pkcs11/clevis-pkcs11-afunix-socket-unlock.c:40:9`!); |
| 91 | pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&mutexcxx . . $ mutex.); |
| 92 | } |
| 93 | |
| 94 | static const char* get_keycxx . . $ get_key(fb58d2e09a2a1ce6).(const char* dev) { |
| 95 | for(int e = 0; e < entry_countercxx . . $ entry_counter.; e++) { |
| 96 | pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&mutexcxx . . $ mutex.); |
| 97 | if(0 == strcmpcxx . . $ strcmp(5ae29d726ae57f88).(keyscxx . . $ keys.[e].devcxx . . $ $anonymous_type_3d16bdea7ee1d7b6_0#dev., dev)) { |
| 98 | pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&mutexcxx . . $ mutex.); |
| 99 | return keyscxx . . $ keys.[e].keycxx . . $ $anonymous_type_3d16bdea7ee1d7b6_0#key.; |
| 100 | } |
| 101 | pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&mutexcxx . . $ mutex.); |
| 102 | } |
| 103 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 104 | } |
| 105 | |
| 106 | static void* control_threadcxx . . $ control_thread(9f162e79bfc6c74).(void *targ) { |
| 107 | // Create a socket to listen on control socket |
| 108 | struct sockaddr_uncxx . . $ sockaddr_un# control_addr, accept_addr; |
| 109 | int s = 0, a = 0, r = 0; |
| 110 | char control_msg[MAX_CONTROL_MSGcxx . . $ MAX_CONTROL_MSG.+1]; |
| 111 | const char* control_sock = (const char*)targ; |
| 112 | socklen_tcxx . . $ socklen_t# len = 0; |
| 113 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(&control_addr, 0, sizeof(control_addr)); |
| 114 | control_addr.sun_familycxx . . $ sockaddr_un#sun_family. = AF_UNIXcxx . . $ `<external>/8962547315054503424/socket.h:95:9`!; |
| 115 | strncpycxx . . $ strncpy(255158a415b673b0).(control_addr.sun_pathcxx . . $ sockaddr_un#sun_path., control_sock, sizeof(control_addr.sun_pathcxx . . $ sockaddr_un#sun_path.)-1); |
| 116 | unlinkcxx . . $ unlink(5c4306d8553b3696).(control_sock); |
| 117 | s = socketcxx . . $ socket(2326284602b0bb2d).(AF_UNIXcxx . . $ `<external>/8962547315054503424/socket.h:95:9`!, SOCK_STREAMcxx . . $ `<external>/5956532346368192945/socket_type.h:28:9`!, 0); |
| 118 | if (s == -1) { |
| 119 | perrorcxx . . $ perror(85c52e162fed56f9).("control socket"); |
| 120 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(logfilecxx . . $ logfile., "Control socket error\n"); |
| 121 | pthread_exitcxx . . $ pthread_exit(b8a01b603c304b90).("control socket"); |
| 122 | } |
| 123 | if (bindcxx . . $ bind(1ce9971e209c3c95).(s, (struct sockaddrcxx . . $ sockaddr# *)&control_addr, sizeof(control_addr)) == -1) { |
| 124 | perrorcxx . . $ perror(85c52e162fed56f9).("control bind"); |
| 125 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(logfilecxx . . $ logfile., "Control bind error\n"); |
| 126 | pthread_exitcxx . . $ pthread_exit(b8a01b603c304b90).("control bind"); |
| 127 | } |
| 128 | if (listencxx . . $ listen(9b79fb6aee4c0440).(s, SOMAXCONNcxx . . $ `<external>/8962547315054503424/socket.h:178:9`!) == -1) { |
| 129 | perrorcxx . . $ perror(85c52e162fed56f9).("control listen"); |
| 130 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(logfilecxx . . $ logfile., "Control listen error\n"); |
| 131 | pthread_exitcxx . . $ pthread_exit(b8a01b603c304b90).("control listen"); |
| 132 | } |
| 133 | while (thread_loopcxx . . $ thread_loop.) { |
| 134 | a = acceptcxx . . $ accept(46fcd054f4a4d98b).(s, (struct sockaddrcxx . . $ sockaddr# *)&accept_addr, &len); |
| 135 | if (a == -1) { |
| 136 | perrorcxx . . $ perror(85c52e162fed56f9).("control accept"); |
| 137 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(logfilecxx . . $ logfile., "Control accept\n"); |
| 138 | pthread_exitcxx . . $ pthread_exit(b8a01b603c304b90).("control accept"); |
| 139 | } |
| 140 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(control_msg, 0, MAX_CONTROL_MSGcxx . . $ MAX_CONTROL_MSG.); |
| 141 | if((r = recvcxx . . $ recv(4136b1fcb15a75da).(a, control_msg, MAX_CONTROL_MSGcxx . . $ MAX_CONTROL_MSG., 0)) < 0) { |
| 142 | perrorcxx . . $ perror(85c52e162fed56f9).("recv error"); |
| 143 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(logfilecxx . . $ logfile., "Error on reception\n"); |
| 144 | closecxx . . $ close(7864480464b09eea).(a); |
| 145 | pthread_exitcxx . . $ pthread_exit(b8a01b603c304b90).("control recv"); |
| 146 | } else { |
| 147 | control_msg[r] = '\0'; |
| 148 | } |
| 149 | char* t = control_msg; |
| 150 | int is_device = 1; |
| 151 | while((t = strtokcxx . . $ strtok(b7b3a83273496486).(t, ","))) { |
| 152 | if (is_device) { |
| 153 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(logfilecxx . . $ logfile., "Adding device:%s\n", t); |
| 154 | insert_devicecxx . . $ insert_device(85c52e162fed56f9).(t); |
| 155 | is_device = 0; |
| 156 | } else { |
| 157 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(logfilecxx . . $ logfile., "Adding key:%s\n", t); |
| 158 | insert_keycxx . . $ insert_key(85c52e162fed56f9).(t); |
| 159 | // As long as some key is inserted, we store it |
| 160 | // in the control_thread_info variable |
| 161 | control_thread_infocxx . . $ control_thread_info. = 1; |
| 162 | } |
| 163 | t = strtokcxx . . $ strtok(b7b3a83273496486).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, ","); |
| 164 | } |
| 165 | closecxx . . $ close(7864480464b09eea).(a); |
| 166 | } |
| 167 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 168 | } |
| 169 | |
| 170 | static int usagecxx . . $ usage(1df18506ef0a631b).(const char* name, uint32_tcxx . . $ uint32_t# ecode) { |
| 171 | printfcxx . . $ printf(581fddd72a8db8fc).("\nUsage:\n\t%s -f socket_file [-c control_socket] [-k key] " |
| 172 | "[-l logfile] [-t iterations, 3 by default]" |
| 173 | "[-s start delay, 0s by default] [-v(version)] [-h(help)]\n\n", name); |
| 174 | exitcxx . . $ exit(3ca292c338df9bb3).(ecode); |
| 175 | } |
| 176 | |
| 177 | static void dump_versioncxx . . $ dump_version(49f6e7a06ebc5aa8).(void) { |
| 178 | printfcxx . . $ printf(581fddd72a8db8fc).("VERSION: [%s]\n", VERSIONcxx . . $ VERSION.); |
| 179 | } |
| 180 | |
| 181 | static void dump_wide_versioncxx . . $ dump_wide_version(49f6e7a06ebc5aa8).(void) { |
| 182 | printfcxx . . $ printf(581fddd72a8db8fc).("\n"); |
| 183 | dump_versioncxx . . $ dump_version(49f6e7a06ebc5aa8).(); |
| 184 | printfcxx . . $ printf(581fddd72a8db8fc).("\n"); |
| 185 | } |
| 186 | |
| 187 | static void int_handlercxx . . $ int_handler(d4f767463ce0a6b3).(int s) { |
| 188 | if(logfilecxx . . $ logfile.) { |
| 189 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(logfilecxx . . $ logfile., "Closing, received signal:[%d]\n", s); |
| 190 | fclosecxx . . $ fclose(89bc1efcdac0ca22).(logfilecxx . . $ logfile.); |
| 191 | } |
| 192 | exitcxx . . $ exit(3ca292c338df9bb3).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!); |
| 193 | } |
| 194 | |
| 195 | int maincxx . . $ main(afcd3168fc1188a4).(int argc, char* argv[]) { |
| 196 | int s, a, opt; |
| 197 | struct sockaddr_uncxx . . $ sockaddr_un# sock_addr, accept_addr, peer_addr; |
| 198 | socklen_tcxx . . $ socklen_t# pathlen; |
| 199 | char key[MAX_KEYcxx . . $ `src/pins/pkcs11/clevis-pkcs11-afunix-socket-unlock.c:40:9`!]; |
| 200 | char lfile[MAX_PATHcxx . . $ MAX_PATH.]; |
| 201 | char sock_file[MAX_PATHcxx . . $ MAX_PATH.]; |
| 202 | char sock_control_file[MAX_PATHcxx . . $ MAX_PATH.]; |
| 203 | socklen_tcxx . . $ socklen_t# len = sizeof(accept_addr); |
| 204 | uint8_tcxx . . $ uint8_t# wait_control_thread = 1; |
| 205 | uint32_tcxx . . $ uint32_t# iterations = DEFAULT_MAX_ITERATIONScxx . . $ DEFAULT_MAX_ITERATIONS.; |
| 206 | uint32_tcxx . . $ uint32_t# startdelay = DEFAULT_START_DELAYcxx . . $ DEFAULT_START_DELAY.; |
| 207 | uint32_tcxx . . $ uint32_t# ic = 0; |
| 208 | uint32_tcxx . . $ uint32_t# time = 0; |
| 209 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(lfile, 0, MAX_PATHcxx . . $ MAX_PATH.); |
| 210 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(sock_file, 0, MAX_PATHcxx . . $ MAX_PATH.); |
| 211 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(sock_control_file, 0, MAX_PATHcxx . . $ MAX_PATH.); |
| 212 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(key, 0, MAX_KEYcxx . . $ `src/pins/pkcs11/clevis-pkcs11-afunix-socket-unlock.c:40:9`!); |
| 213 | |
| 214 | signalcxx . . $ signal(c2d516773f91faf8).(SIGTERMcxx . . $ `<external>/11773118241360591791/signum-generic.h:53:9`! | SIGKILLcxx . . $ `<external>/11773118241360591791/signum-generic.h:59:9`!, int_handlercxx . . $ int_handler(d4f767463ce0a6b3).); |
| 215 | for (uint16_tcxx . . $ uint16_t# e = 0; e < MAX_ENTRIEScxx . . $ `src/pins/pkcs11/clevis-pkcs11-afunix-socket-unlock.c:39:9`!; e++) { |
| 216 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(&keyscxx . . $ keys.[e], 0, sizeof(key_entry_tcxx . . $ key_entry_t#)); |
| 217 | } |
| 218 | while ((opt = getoptcxx . . $ getopt(2483c31ff9bab884).(argc, argv, "c:f:k:i:l:s:t:hv")) != -1) { |
| 219 | int ret_code = EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!; |
| 220 | switch (opt) { |
| 221 | case 'c': |
| 222 | strncpycxx . . $ strncpy(255158a415b673b0).(sock_control_file, optargcxx . . $ optarg., MAX_PATHcxx . . $ MAX_PATH. - 1); |
| 223 | break; |
| 224 | case 'f': |
| 225 | strncpycxx . . $ strncpy(255158a415b673b0).(sock_file, optargcxx . . $ optarg., MAX_PATHcxx . . $ MAX_PATH. - 1); |
| 226 | unlinkcxx . . $ unlink(5c4306d8553b3696).(sock_file); |
| 227 | break; |
| 228 | case 'k': |
| 229 | strncpycxx . . $ strncpy(255158a415b673b0).(key, optargcxx . . $ optarg., MAX_KEYcxx . . $ `src/pins/pkcs11/clevis-pkcs11-afunix-socket-unlock.c:40:9`! - 1); |
| 230 | break; |
| 231 | case 'l': |
| 232 | strncpycxx . . $ strncpy(255158a415b673b0).(lfile, optargcxx . . $ optarg., MAX_PATHcxx . . $ MAX_PATH. - 1); |
| 233 | logfilecxx . . $ logfile. = fopencxx . . $ fopen(82ec0787cd37e0a3).(lfile, "w+"); |
| 234 | break; |
| 235 | case 't': |
| 236 | iterations = strtoulcxx . . $ strtoul(31746e8d6ec49860).(optargcxx . . $ optarg., 0, 10); |
| 237 | break; |
| 238 | case 's': |
| 239 | startdelay = strtoulcxx . . $ strtoul(31746e8d6ec49860).(optargcxx . . $ optarg., 0, 10); |
| 240 | break; |
| 241 | case 'v': |
| 242 | dump_wide_versioncxx . . $ dump_wide_version(49f6e7a06ebc5aa8).(); |
| 243 | exitcxx . . $ exit(3ca292c338df9bb3).(EXIT_SUCCESScxx . . $ `<external>/10696550936717951914/stdlib.h:93:9`!); |
| 244 | break; |
| 245 | case 'h': |
| 246 | ret_code = EXIT_SUCCESScxx . . $ `<external>/10696550936717951914/stdlib.h:93:9`!; |
| 247 | __attribute__ ((fallthrough)); |
| 248 | default: |
| 249 | usagecxx . . $ usage(1df18506ef0a631b).(argv[0], ret_code); |
| 250 | } |
| 251 | } |
| 252 | if(!logfilecxx . . $ logfile.) { |
| 253 | logfilecxx . . $ logfile. = stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!; |
| 254 | strncpycxx . . $ strncpy(255158a415b673b0).(lfile, "stdout", MAX_PATHcxx . . $ MAX_PATH. - 1); |
| 255 | } |
| 256 | if(0 == strlencxx . . $ strlen(751346f8406fd082).(sock_file)) { |
| 257 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(logfilecxx . . $ logfile., "\nSocket file name must be provided\n"); |
| 258 | usagecxx . . $ usage(1df18506ef0a631b).(argv[0], EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!); |
| 259 | } |
| 260 | if(0 == strlencxx . . $ strlen(751346f8406fd082).(sock_control_file) ) { |
| 261 | get_control_socket_namecxx . . $ get_control_socket_name(c194a158e5e34a1c).(sock_file, sock_control_file, MAX_PATHcxx . . $ MAX_PATH.); |
| 262 | } |
| 263 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(logfilecxx . . $ logfile., "LOG FILE: [%s]\n", lfile); |
| 264 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(logfilecxx . . $ logfile., "FILE: [%s]\n", sock_file); |
| 265 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(logfilecxx . . $ logfile., "KEY: [%s]\n", key); |
| 266 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(logfilecxx . . $ logfile., "START DELAY: [%u] seconds\n", startdelay); |
| 267 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(logfilecxx . . $ logfile., "TRY ITERATIONS: [%u]\n", iterations); |
| 268 | dump_versioncxx . . $ dump_version(49f6e7a06ebc5aa8).(); |
| 269 | |
| 270 | pthread_tcxx . . $ pthread_t# thid; |
| 271 | void* tret; |
| 272 | if (pthread_createcxx . . $ pthread_create(2141a84bcac899e5).(&thid, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, control_threadcxx . . $ control_thread(9f162e79bfc6c74)., sock_control_file) != 0) { |
| 273 | perrorcxx . . $ perror(85c52e162fed56f9).("pthread_create() error"); |
| 274 | goto efailure; |
| 275 | } |
| 276 | |
| 277 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(&sock_addr, 0, sizeof(sock_addr)); |
| 278 | sock_addr.sun_familycxx . . $ sockaddr_un#sun_family. = AF_UNIXcxx . . $ `<external>/8962547315054503424/socket.h:95:9`!; |
| 279 | strncpycxx . . $ strncpy(255158a415b673b0).(sock_addr.sun_pathcxx . . $ sockaddr_un#sun_path., sock_file, sizeof(sock_addr.sun_pathcxx . . $ sockaddr_un#sun_path.)-1); |
| 280 | s = socketcxx . . $ socket(2326284602b0bb2d).(AF_UNIXcxx . . $ `<external>/8962547315054503424/socket.h:95:9`!, SOCK_STREAMcxx . . $ `<external>/5956532346368192945/socket_type.h:28:9`!, 0); |
| 281 | if (s == -1) { |
| 282 | perrorcxx . . $ perror(85c52e162fed56f9).("socket"); |
| 283 | goto efailure; |
| 284 | } |
| 285 | if (bindcxx . . $ bind(1ce9971e209c3c95).(s, (struct sockaddrcxx . . $ sockaddr# *)&sock_addr, sizeof(sock_addr)) == -1) { |
| 286 | perrorcxx . . $ perror(85c52e162fed56f9).("bind"); |
| 287 | goto efailure; |
| 288 | } |
| 289 | if (listencxx . . $ listen(9b79fb6aee4c0440).(s, SOMAXCONNcxx . . $ `<external>/8962547315054503424/socket.h:178:9`!) == -1) { |
| 290 | perrorcxx . . $ perror(85c52e162fed56f9).("listen"); |
| 291 | goto efailure; |
| 292 | } |
| 293 | while (ic < iterations) { |
| 294 | if (time++ < startdelay && !control_thread_infocxx . . $ control_thread_info.) { |
| 295 | sleepcxx . . $ sleep(2141b656ce2cc063).(1); |
| 296 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(logfilecxx . . $ logfile., "Start time elapsed: [%u/%u] seconds\n", |
| 297 | time, startdelay); |
| 298 | continue; |
| 299 | } |
| 300 | if (control_thread_infocxx . . $ control_thread_info. && wait_control_thread) { |
| 301 | sleepcxx . . $ sleep(2141b656ce2cc063).(WAIT_CONTROL_THREAD_TIMERcxx . . $ WAIT_CONTROL_THREAD_TIMER.); |
| 302 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(logfilecxx . . $ logfile., "Waiting %d second for control thread " |
| 303 | "to receive complete information\n", |
| 304 | WAIT_CONTROL_THREAD_TIMERcxx . . $ WAIT_CONTROL_THREAD_TIMER.); |
| 305 | wait_control_thread = 0; |
| 306 | } |
| 307 | a = acceptcxx . . $ accept(46fcd054f4a4d98b).(s, (struct sockaddrcxx . . $ sockaddr# *)&accept_addr, &len); |
| 308 | if (a == -1) { |
| 309 | perrorcxx . . $ perror(85c52e162fed56f9).("accept"); |
| 310 | goto efailure; |
| 311 | } |
| 312 | pathlen = len - offsetofcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:431:9`!(struct sockaddr_un, sun_path); |
| 313 | len = sizeof(peer_addr); |
| 314 | if (getpeernamecxx . . $ getpeername(46fcd054f4a4d98b).(a, (struct sockaddrcxx . . $ sockaddr# *)&peer_addr, &len)== -1) { |
| 315 | perrorcxx . . $ perror(85c52e162fed56f9).("getpeername"); |
| 316 | goto efailure; |
| 317 | } |
| 318 | pathlen = len - offsetofcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:431:9`!(struct sockaddr_un, sun_path); |
| 319 | char peer[pathlen]; |
| 320 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(peer, 0, pathlen); |
| 321 | strncpycxx . . $ strncpy(255158a415b673b0).(peer, peer_addr.sun_pathcxx . . $ sockaddr_un#sun_path.+1, pathlen-1); |
| 322 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(logfilecxx . . $ logfile., "Try: [%u/%u]\n", ic, iterations); |
| 323 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(logfilecxx . . $ logfile., "getpeername sun_path(peer): [%s]\n", peer); |
| 324 | char* t = peer; |
| 325 | const char* unlocking_device = ""; |
| 326 | while((t = strtokcxx . . $ strtok(b7b3a83273496486).(t, "/"))) { |
| 327 | if(t) { |
| 328 | unlocking_device = t; |
| 329 | } |
| 330 | t = strtokcxx . . $ strtok(b7b3a83273496486).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, ","); |
| 331 | } |
| 332 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(logfilecxx . . $ logfile., "Trying to unlock device:[%s]\n", unlocking_device); |
| 333 | // Now we have all the information in peer, something like: |
| 334 | // \099226072855ae2d8/cryptsetup/luks-6e38d5e1-7f83-43cc-819a-7416bcbf9f84 |
| 335 | // NUL random /cryptsetup/ DEVICE |
| 336 | // If we need to unencrypt device, pick it from peer information |
| 337 | // To return the key, just respond to socket returned by accept |
| 338 | if(strlencxx . . $ strlen(751346f8406fd082).(key)) { |
| 339 | if (sendcxx . . $ send(4e364047dcdcb5ca).(a, key, strlencxx . . $ strlen(751346f8406fd082).(key), 0) < 0) { |
| 340 | perrorcxx . . $ perror(85c52e162fed56f9).("key send error"); |
| 341 | goto efailure; |
| 342 | } |
| 343 | } else { |
| 344 | const char* entry_key; |
| 345 | if((entry_key = get_keycxx . . $ get_key(fb58d2e09a2a1ce6).(unlocking_device))) { |
| 346 | if (sendcxx . . $ send(4e364047dcdcb5ca).(a, entry_key, strlencxx . . $ strlen(751346f8406fd082).(entry_key), 0)< 0) { |
| 347 | perrorcxx . . $ perror(85c52e162fed56f9).("key entry send error"); |
| 348 | goto efailure; |
| 349 | } |
| 350 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(logfilecxx . . $ logfile., "Sending passphrase to device:[%s]\n", |
| 351 | unlocking_device); |
| 352 | } else { |
| 353 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(logfilecxx . . $ logfile., "Device not found: [%s]\n", unlocking_device); |
| 354 | } |
| 355 | } |
| 356 | closecxx . . $ close(7864480464b09eea).(a); |
| 357 | ic++; |
| 358 | } |
| 359 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(logfilecxx . . $ logfile., "Closing (max tries reached)\n"); |
| 360 | pthread_killcxx . . $ pthread_kill(4292610292d2867f).(thid, SIGKILLcxx . . $ `<external>/11773118241360591791/signum-generic.h:59:9`!); |
| 361 | thread_loopcxx . . $ thread_loop. = 0; |
| 362 | if (pthread_joincxx . . $ pthread_join(269902cf949f12c5).(thid, &tret) != 0) { |
| 363 | perrorcxx . . $ perror(85c52e162fed56f9).("pthread_join error"); |
| 364 | goto efailure; |
| 365 | } |
| 366 | return EXIT_SUCCESScxx . . $ `<external>/10696550936717951914/stdlib.h:93:9`!; |
| 367 | efailure: |
| 368 | if(logfilecxx . . $ logfile.) { |
| 369 | fclosecxx . . $ fclose(89bc1efcdac0ca22).(logfilecxx . . $ logfile.); |
| 370 | logfilecxx . . $ logfile. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 371 | } |
| 372 | exitcxx . . $ exit(3ca292c338df9bb3).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!); |
| 373 | } |