added option for htdigest authentication

This commit is contained in:
SuperBFG7 2015-10-06 20:17:28 +02:00
parent 4295d15874
commit bde33d5aa7
2 changed files with 24 additions and 2 deletions

View File

@ -28,6 +28,8 @@ Run flags
``` ```
Usage: ./ympd [OPTION]... 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] -h, --host <host> connect to mpd at host [localhost]
-p, --port <port> connect to mpd at port [6600] -p, --port <port> connect to mpd at port [6600]
-w, --webport [ip:]<port> listen interface/port for webserver [8080] -w, --webport [ip:]<port> listen interface/port for webserver [8080]

View File

@ -38,7 +38,12 @@ void bye()
force_exit = 1; force_exit = 1;
} }
char *gpass = NULL;
static int server_callback(struct mg_connection *c, enum mg_event ev) { static int server_callback(struct mg_connection *c, enum mg_event ev) {
int result = MG_FALSE;
FILE *fp = NULL;
switch(ev) { switch(ev) {
case MG_CLOSE: case MG_CLOSE:
mpd_close_handler(c); mpd_close_handler(c);
@ -57,7 +62,15 @@ static int server_callback(struct mg_connection *c, enum mg_event ev) {
return callback_http(c); return callback_http(c);
#endif #endif
case MG_AUTH: case MG_AUTH:
return MG_TRUE; if ( gpass == NULL )
return MG_TRUE;
else {
if ( (fp = fopen(gpass, "r")) != NULL ) {
result = mg_authorize_digest(c, fp);
fclose(fp);
}
}
return result;
default: default:
return MG_FALSE; return MG_FALSE;
} }
@ -77,10 +90,12 @@ int main(int argc, char **argv)
mg_set_option(server, "document_root", SRC_PATH); mg_set_option(server, "document_root", SRC_PATH);
#endif #endif
mg_set_option(server, "auth_domain", "ympd");
mpd.port = 6600; mpd.port = 6600;
strcpy(mpd.host, "127.0.0.1"); strcpy(mpd.host, "127.0.0.1");
static struct option long_options[] = { static struct option long_options[] = {
{"digest", optional_argument, 0, 'd'},
{"host", required_argument, 0, 'h'}, {"host", required_argument, 0, 'h'},
{"port", required_argument, 0, 'p'}, {"port", required_argument, 0, 'p'},
{"webport", required_argument, 0, 'w'}, {"webport", required_argument, 0, 'w'},
@ -90,9 +105,12 @@ int main(int argc, char **argv)
{0, 0, 0, 0 } {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) { long_options, &option_index)) != -1) {
switch (n) { switch (n) {
case 'd':
gpass = strdup(optarg);
break;
case 'h': case 'h':
strncpy(mpd.host, optarg, sizeof(mpd.host)); strncpy(mpd.host, optarg, sizeof(mpd.host));
break; break;
@ -114,6 +132,8 @@ int main(int argc, char **argv)
break; break;
default: default:
fprintf(stderr, "Usage: %s [OPTION]...\n\n" 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" " -h, --host <host>\t\tconnect to mpd at host [localhost]\n"
" -p, --port <port>\t\tconnect to mpd at port [6600]\n" " -p, --port <port>\t\tconnect to mpd at port [6600]\n"
" -w, --webport [ip:]<port>\tlisten interface/port for webserver [8080]\n" " -w, --webport [ip:]<port>\tlisten interface/port for webserver [8080]\n"