summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorLars-Dominik Braun <lars@6xq.net>2012-10-11 18:09:46 +0200
committerLars-Dominik Braun <lars@6xq.net>2012-10-21 20:55:29 +0200
commit962dee5cdd3b468318916d9c8862bc4c3653c402 (patch)
tree310896a514914d98e9bf2226aae8fe40f40c3050 /src/main.c
parent5fd56271bfb4a407fffcd8633b54501e42415ead (diff)
downloadpianobar-962dee5cdd3b468318916d9c8862bc4c3653c402.tar.gz
pianobar-962dee5cdd3b468318916d9c8862bc4c3653c402.tar.bz2
pianobar-962dee5cdd3b468318916d9c8862bc4c3653c402.zip
New setting: Read password from external command
Closes #314.
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c77
1 files changed, 70 insertions, 7 deletions
diff --git a/src/main.c b/src/main.c
index 1d24167..2465334 100644
--- a/src/main.c
+++ b/src/main.c
@@ -31,6 +31,7 @@ THE SOFTWARE.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
+/* fork () */
#include <unistd.h>
#include <sys/select.h>
#include <time.h>
@@ -46,6 +47,10 @@ THE SOFTWARE.
#include <stdbool.h>
#include <limits.h>
#include <signal.h>
+/* waitpid () */
+#include <sys/types.h>
+#include <sys/wait.h>
+
/* pandora.com library */
#include <piano.h>
@@ -92,22 +97,78 @@ static bool BarMainLoginUser (BarApp_t *app) {
/* ask for username/password if none were provided in settings
*/
-static void BarMainGetLoginCredentials (BarSettings_t *settings,
+static bool BarMainGetLoginCredentials (BarSettings_t *settings,
BarReadlineFds_t *input) {
if (settings->username == NULL) {
char nameBuf[100];
+
BarUiMsg (settings, MSG_QUESTION, "Email: ");
BarReadlineStr (nameBuf, sizeof (nameBuf), input, BAR_RL_DEFAULT);
settings->username = strdup (nameBuf);
}
+
if (settings->password == NULL) {
char passBuf[100];
- BarUiMsg (settings, MSG_QUESTION, "Password: ");
- BarReadlineStr (passBuf, sizeof (passBuf), input, BAR_RL_NOECHO);
- /* write missing newline */
- puts ("");
- settings->password = strdup (passBuf);
+
+ if (settings->passwordCmd == NULL) {
+ BarUiMsg (settings, MSG_QUESTION, "Password: ");
+ BarReadlineStr (passBuf, sizeof (passBuf), input, BAR_RL_NOECHO);
+ /* write missing newline */
+ puts ("");
+ settings->password = strdup (passBuf);
+ } else {
+ pid_t chld;
+ int pipeFd[2];
+
+ BarUiMsg (settings, MSG_INFO, "Requesting password from external helper... ");
+
+ if (pipe (pipeFd) == -1) {
+ BarUiMsg (settings, MSG_NONE, "Error: %s\n", strerror (errno));
+ return false;
+ }
+
+ chld = fork ();
+ if (chld == 0) {
+ /* child */
+ close (pipeFd[0]);
+ dup2 (pipeFd[1], fileno (stdout));
+ execl ("/bin/sh", "/bin/sh", "-c", settings->passwordCmd, (char *) NULL);
+ BarUiMsg (settings, MSG_NONE, "Error: %s\n", strerror (errno));
+ close (pipeFd[1]);
+ exit (1);
+ } else if (chld == -1) {
+ BarUiMsg (settings, MSG_NONE, "Error: %s\n", strerror (errno));
+ return false;
+ } else {
+ /* parent */
+ int status;
+
+ close (pipeFd[1]);
+ memset (passBuf, 0, sizeof (passBuf));
+ read (pipeFd[0], passBuf, sizeof (passBuf)-1);
+ close (pipeFd[0]);
+
+ /* drop trailing newlines */
+ ssize_t len = strlen (passBuf)-1;
+ while (len >= 0 && passBuf[len] == '\n') {
+ passBuf[len] = '\0';
+ --len;
+ }
+
+ waitpid (chld, &status, 0);
+ if (WEXITSTATUS (status) == 0) {
+ settings->password = strdup (passBuf);
+ BarUiMsg (settings, MSG_NONE, "Ok.\n");
+ } else {
+ BarUiMsg (settings, MSG_NONE, "Error: Exit status %i.\n", WEXITSTATUS (status));
+ return false;
+ }
+ }
+
+ }
}
+
+ return true;
}
/* get station list
@@ -270,7 +331,9 @@ static void BarMainPrintTime (BarApp_t *app) {
static void BarMainLoop (BarApp_t *app) {
pthread_t playerThread;
- BarMainGetLoginCredentials (&app->settings, &app->input);
+ if (!BarMainGetLoginCredentials (&app->settings, &app->input)) {
+ return;
+ }
BarMainLoadProxy (&app->settings, &app->waith);