Mercurial > ~mikael > mcabber > hg
comparison mcabber/mcabber/main.c @ 2173:cf08c3b630af
Tweak the previous change...
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sun, 28 Jun 2015 15:39:24 +0200 |
parents | 06669fc1810c |
children | e62319868844 |
comparison
equal
deleted
inserted
replaced
2172:06669fc1810c | 2173:cf08c3b630af |
---|---|
164 if (*p == '\n' || *p == '\r') *p = 0; | 164 if (*p == '\n' || *p == '\r') *p = 0; |
165 | 165 |
166 return password; | 166 return password; |
167 } | 167 } |
168 | 168 |
169 // password_eval(command, *status) | |
170 // Get password from a system command. | |
171 // The string must be freed after use. | |
172 static char *password_eval(const char *command, int *status) | |
173 { | |
174 #define MAX_PWD 100 | |
175 char *pwd; | |
176 FILE *outfp = popen(command, "r"); | |
177 if (outfp == NULL) { | |
178 scr_log_print(LPRINT_NORMAL, "** ERROR: Failed to execute password_eval command."); | |
179 *status = -1; | |
180 return NULL; | |
181 } | |
182 | |
183 pwd = g_new0(char, MAX_PWD); | |
184 if (fgets(pwd, MAX_PWD, outfp) == NULL) { | |
185 scr_log_print(LPRINT_NORMAL, "** ERROR: Failed to read from password_eval command."); | |
186 g_free(pwd); | |
187 *status = -1; | |
188 return NULL; | |
189 } | |
190 | |
191 int res = pclose(outfp); | |
192 if (res != 0) { | |
193 scr_log_print(LPRINT_NORMAL, "** ERROR: Password evaluation command exited with error %d.", res); | |
194 g_free(pwd); | |
195 *status = res; | |
196 return NULL; | |
197 } | |
198 | |
199 // Strip trailing whitespaces and newlines | |
200 size_t i = strlen(pwd); | |
201 while (i && isspace(pwd[i-1])) { | |
202 i--; | |
203 } | |
204 pwd[i] = '\0'; | |
205 return pwd; | |
206 } | |
207 | |
169 static void credits(void) | 208 static void credits(void) |
170 { | 209 { |
171 const char *v_fmt = "MCabber %s -- Email: mcabber [at] lilotux [dot] net\n"; | 210 const char *v_fmt = "MCabber %s -- Email: mcabber [at] lilotux [dot] net\n"; |
172 char *v = mcabber_version(); | 211 char *v = mcabber_version(); |
173 printf(v_fmt, v); | 212 printf(v_fmt, v); |
401 } | 440 } |
402 | 441 |
403 /* If no password is stored, we ask for it before entering | 442 /* If no password is stored, we ask for it before entering |
404 ncurses mode -- unless the username is unknown. */ | 443 ncurses mode -- unless the username is unknown. */ |
405 if (settings_opt_get("jid") && !settings_opt_get("password")) { | 444 if (settings_opt_get("jid") && !settings_opt_get("password")) { |
406 if (settings_opt_get("password_eval")) { | 445 const char *pass_eval = settings_opt_get("password_eval"); |
407 FILE *outfp = popen(settings_opt_get("password_eval"), "r"); | 446 if (pass_eval) { |
408 if (outfp == NULL) { | 447 int status = 0; |
409 scr_log_print(LPRINT_NORMAL, "** ERROR: Failed to execute password_eval command."); | 448 char *pwd = password_eval(pass_eval, &status); |
410 exit(EXIT_FAILURE); | 449 if (status == 0 && pwd) { |
450 settings_set(SETTINGS_TYPE_OPTION, "password", pwd); | |
411 } | 451 } |
412 #define MAX_PWD 100 | 452 g_free(pwd); |
413 char pwd[MAX_PWD+1]; | 453 } |
414 char *read_pwd = fgets(pwd, MAX_PWD, outfp); | 454 // If the password is still unset, ask the user... |
415 if (read_pwd == NULL) { | 455 if (!settings_opt_get("password")) { |
416 scr_log_print(LPRINT_NORMAL, "** ERROR: Failed to read from password_eval command."); | |
417 exit(EXIT_FAILURE); | |
418 } | |
419 int res = pclose(outfp); | |
420 if (res != 0) { | |
421 scr_log_print(LPRINT_NORMAL, "** ERROR: Password evaluation command exited with error %d.", res); | |
422 exit(EXIT_FAILURE); | |
423 } | |
424 | |
425 // strip trailing whitespaces and newlines | |
426 size_t i = 0; | |
427 while (i < MAX_PWD && !isspace(pwd[i])) { | |
428 i++; | |
429 } | |
430 pwd[i] = '\0'; | |
431 | |
432 settings_set(SETTINGS_TYPE_OPTION, "password", pwd); | |
433 } else { | |
434 char *pwd = ask_password("your Jabber password"); | 456 char *pwd = ask_password("your Jabber password"); |
435 settings_set(SETTINGS_TYPE_OPTION, "password", pwd); | 457 settings_set(SETTINGS_TYPE_OPTION, "password", pwd); |
436 g_free(pwd); | 458 g_free(pwd); |
437 } | 459 } |
438 } | 460 } |