Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for htdigest authentication #101

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Run flags
```
Usage: ./ympd [OPTION]...

-d, --digest <htdigest> path to htdigest file for authorization
(realm ympd) [no authorization]
-h, --host <host> connect to mpd at host [localhost]
-p, --port <port> connect to mpd at port [6600]
-w, --webport [ip:]<port> listen interface/port for webserver [8080]
Expand Down
3 changes: 2 additions & 1 deletion contrib/init.debian
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ YMPD_USER=nobody
MPD_HOST=localhost
MPD_PORT=6600
WEB_PORT=8080
#DIGEST=--digest /path/to/htdigest


# Exit if the package is not installed
Expand All @@ -35,7 +36,7 @@ WEB_PORT=8080
# Load the VERBOSE setting and other rcS variables
[ -f /etc/default/rcS ] && . /etc/default/rcS

DAEMON_OPT="--user $YMPD_USER --webport $WEB_PORT --host $MPD_HOST --port $MPD_PORT"
DAEMON_OPT="--user $YMPD_USER --webport $WEB_PORT --host $MPD_HOST --port $MPD_PORT $DIGEST"

do_start()
{
Expand Down
1 change: 1 addition & 0 deletions contrib/ympd.default
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ MPD_HOST=localhost
MPD_PORT=6600
WEB_PORT=8080
YMPD_USER=nobody
#DIGEST=--digest /path/to/htdigest
3 changes: 2 additions & 1 deletion contrib/ympd.service
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ Environment=MPD_HOST=localhost
Environment=MPD_PORT=6600
Environment=WEB_PORT=8080
Environment=YMPD_USER=nobody
Environment=DIGEST=
EnvironmentFile=/etc/default/ympd
ExecStart=/usr/bin/ympd --user $YMPD_USER --webport $WEB_PORT --host $MPD_HOST --port $MPD_PORT
ExecStart=/usr/bin/ympd --user $YMPD_USER --webport $WEB_PORT --host $MPD_HOST --port $MPD_PORT $DIGEST
Type=simple

[Install]
Expand Down
25 changes: 23 additions & 2 deletions src/ympd.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ void bye()
force_exit = 1;
}

char *gpass = NULL;

static int server_callback(struct mg_connection *c, enum mg_event ev) {
int result = MG_FALSE;
FILE *fp = NULL;

switch(ev) {
case MG_CLOSE:
mpd_close_handler(c);
Expand All @@ -57,7 +62,16 @@ static int server_callback(struct mg_connection *c, enum mg_event ev) {
return callback_http(c);
#endif
case MG_AUTH:
return MG_TRUE;
// no auth for websockets since mobile safari does not support it
if ( (gpass == NULL) || (c->is_websocket) )
return MG_TRUE;
else {
if ( (fp = fopen(gpass, "r")) != NULL ) {
result = mg_authorize_digest(c, fp);
fclose(fp);
}
}
return result;
default:
return MG_FALSE;
}
Expand All @@ -77,10 +91,12 @@ int main(int argc, char **argv)
mg_set_option(server, "document_root", SRC_PATH);
#endif

mg_set_option(server, "auth_domain", "ympd");
mpd.port = 6600;
strcpy(mpd.host, "127.0.0.1");

static struct option long_options[] = {
{"digest", required_argument, 0, 'd'},
{"host", required_argument, 0, 'h'},
{"port", required_argument, 0, 'p'},
{"webport", required_argument, 0, 'w'},
Expand All @@ -90,9 +106,12 @@ int main(int argc, char **argv)
{0, 0, 0, 0 }
};

while((n = getopt_long(argc, argv, "h:p:w:u:v",
while((n = getopt_long(argc, argv, "d:h:p:w:u:v",
long_options, &option_index)) != -1) {
switch (n) {
case 'd':
gpass = strdup(optarg);
break;
case 'h':
strncpy(mpd.host, optarg, sizeof(mpd.host));
break;
Expand All @@ -114,6 +133,8 @@ int main(int argc, char **argv)
break;
default:
fprintf(stderr, "Usage: %s [OPTION]...\n\n"
" -d, --digest <htdigest>\tpath to htdigest file for authorization\n"
" \t(realm ympd) [no authorization]\n"
" -h, --host <host>\t\tconnect to mpd at host [localhost]\n"
" -p, --port <port>\t\tconnect to mpd at port [6600]\n"
" -w, --webport [ip:]<port>\tlisten interface/port for webserver [8080]\n"
Expand Down