owntone-server/src/main.c

358 lines
8.5 KiB
C
Raw Normal View History

2003-10-13 11:03:14 -04:00
/*
* $Id$
* Driver for multi-threaded daap server
*
* Copyright (C) 2003 Ron Pedde (ron@corbey.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
2003-11-03 14:02:00 -05:00
#include <errno.h>
#include <fcntl.h>
2003-11-03 14:02:00 -05:00
#include <limits.h>
2003-10-13 11:03:14 -04:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2003-10-13 11:03:14 -04:00
#include <pthread.h>
2003-11-03 14:02:00 -05:00
#include <unistd.h>
2003-10-13 11:03:14 -04:00
#include <sys/stat.h>
#include <sys/types.h>
#include "configfile.h"
2003-11-04 01:11:00 -05:00
#include "db-memory.h"
2003-10-30 17:42:11 -05:00
#include "daap.h"
#include "daap-proto.h"
2003-10-13 11:03:14 -04:00
#include "err.h"
2003-11-04 01:11:00 -05:00
#include "mp3-scanner.h"
2003-10-23 17:43:01 -04:00
#include "rend.h"
2003-10-13 11:03:14 -04:00
#include "webserver.h"
// 3689
/*
* Globals
*/
CONFIG config;
2003-10-30 17:42:11 -05:00
/*
* daap_handler
*
* Handle daap-related web pages
*/
void daap_handler(WS_CONNINFO *pwsc) {
int len;
int close;
DAAP_BLOCK *root,*error;
int compress=0;
2003-11-04 01:11:00 -05:00
int clientrev;
2003-10-30 17:42:11 -05:00
close=pwsc->close;
pwsc->close=1;
ws_addresponseheader(pwsc,"Accept-Ranges","bytes");
ws_addresponseheader(pwsc,"DAAP-Server","iTunes/4.1 (Mac OS X)");
ws_addresponseheader(pwsc,"Content-Type","application/x-dmap-tagged");
if(!strcasecmp(pwsc->uri,"/server-info")) {
root=daap_response_server_info();
} else if (!strcasecmp(pwsc->uri,"/content-codes")) {
root=daap_response_content_codes();
} else if (!strcasecmp(pwsc->uri,"/login")) {
root=daap_response_login();
} else if (!strcasecmp(pwsc->uri,"/update")) {
if(!ws_getvar(pwsc,"delta")) { /* first check */
2003-11-04 01:11:00 -05:00
clientrev=db_version() - 1;
} else {
clientrev=atoi(ws_getvar(pwsc,"delta"));
2003-11-04 01:11:00 -05:00
}
root=daap_response_update(clientrev);
2003-10-30 17:42:11 -05:00
} else if (!strcasecmp(pwsc->uri,"/databases")) {
2003-11-03 15:34:18 -05:00
root=daap_response_databases(pwsc->uri);
2003-10-30 17:42:11 -05:00
} else if (!strcasecmp(pwsc->uri,"/logout")) {
ws_returnerror(pwsc,204,"Logout Successful");
return;
2003-11-03 15:34:18 -05:00
} else if (!strncasecmp(pwsc->uri,"/databases/",11)) {
root=daap_response_databases(pwsc->uri);
2003-10-30 17:42:11 -05:00
} else {
DPRINTF(ERR_WARN,"Bad handler! Can't find uri handler for %s\n",
pwsc->uri);
return;
}
2003-11-03 15:34:18 -05:00
if(!root) {
ws_returnerror(pwsc,400,"Invalid Request");
2003-10-30 17:42:11 -05:00
return;
2003-11-03 15:34:18 -05:00
}
2003-10-30 17:42:11 -05:00
pwsc->close=close;
ws_addresponseheader(pwsc,"Content-Length","%d",root->reported_size + 8);
ws_writefd(pwsc,"HTTP/1.1 200 OK\r\n");
ws_emitheaders(pwsc);
/*
if(ws_testrequestheader(pwsc,"Accept-Encoding","gzip")) {
ws_addresponseheader(pwsc,"Content-Encoding","gzip");
compress=1;
}
*/
daap_serialize(root,pwsc->fd,0);
daap_free(root);
return;
}
/*
* config_handler
*
* Handle config web pages
*/
void config_handler(WS_CONNINFO *pwsc) {
char path[PATH_MAX];
char resolved_path[PATH_MAX];
int file_fd;
struct stat sb;
char argbuffer[30];
int in_arg;
char *argptr;
char next;
pwsc->close=1;
ws_addresponseheader(pwsc,"Connection","close");
snprintf(path,PATH_MAX,"%s/%s",config.web_root,pwsc->uri);
if(!realpath(path,resolved_path)) {
pwsc->error=errno;
DPRINTF(ERR_WARN,"Cannot resolve %s\n",path);
ws_returnerror(pwsc,404,"Not found");
return;
}
/* this should really return a 302:Found */
stat(resolved_path,&sb);
if(sb.st_mode & S_IFDIR)
strcat(resolved_path,"/index.html");
DPRINTF(ERR_DEBUG,"Thread %d: Preparing to serve %s\n",
pwsc->threadno, resolved_path);
if(strncmp(resolved_path,config.web_root,
strlen(config.web_root))) {
pwsc->error=EINVAL;
DPRINTF(ERR_WARN,"Thread %d: Requested file %s out of root\n",
pwsc->threadno,resolved_path);
ws_returnerror(pwsc,403,"Forbidden");
return;
}
file_fd=open(resolved_path,O_RDONLY);
if(file_fd == -1) {
pwsc->error=errno;
DPRINTF(ERR_WARN,"Thread %d: Error opening %s: %s\n",
pwsc->threadno,resolved_path,strerror(errno));
ws_returnerror(pwsc,404,"Not found");
return;
}
if(strcasecmp(pwsc->uri,"/config-update.html")==0) {
/* we need to update stuff */
argptr=ws_getvar(pwsc,"adminpw");
if(argptr) {
2003-11-03 15:34:18 -05:00
if(config.adminpassword)
free(config.adminpassword);
config.adminpassword=strdup(argptr);
}
}
ws_writefd(pwsc,"HTTP/1.1 200 OK\r\n");
ws_emitheaders(pwsc);
/* now throw out the file, with replacements */
in_arg=0;
argptr=argbuffer;
while(1) {
if(r_read(file_fd,&next,1) <= 0)
break;
if(in_arg) {
if(next == '@') {
in_arg=0;
if(strcasecmp(argbuffer,"WEB_ROOT") == 0) {
ws_writefd(pwsc,"%s",config.web_root);
} else if (strcasecmp(argbuffer,"PORT") == 0) {
ws_writefd(pwsc,"%d",config.port);
} else if (strcasecmp(argbuffer,"ADMINPW") == 0) {
ws_writefd(pwsc,"%s",config.adminpassword);
} else if (strcasecmp(argbuffer,"RELEASE") == 0) {
ws_writefd(pwsc,"mt-daapd %s\n",VERSION);
2003-11-03 15:34:18 -05:00
} else if (strcasecmp(argbuffer,"MP3DIR") == 0) {
ws_writefd(pwsc,"%s",config.mp3dir);
} else {
ws_writefd(pwsc,"@ERR@");
}
} else {
if((argptr - argbuffer) < (sizeof(argbuffer)-1))
*argptr++ = next;
}
} else {
if(next == '@') {
argptr=argbuffer;
memset(argbuffer,0,sizeof(argbuffer));
in_arg=1;
} else {
if(r_write(pwsc->fd,&next,1) == -1)
break;
}
}
}
close(file_fd);
DPRINTF(ERR_DEBUG,"Thread %d: Served successfully\n",pwsc->threadno);
return;
}
int config_auth(char *user, char *password) {
return !strcmp(password,config.adminpassword);
}
void usage(char *program) {
printf("Usage: %s [options]\n\n",program);
printf("Options:\n");
#ifdef DEBUG
printf(" -d <number> Debuglevel (0-9)\n");
#endif
2003-10-30 17:42:11 -05:00
printf(" -m Use mDNS\n");
printf(" -c <file> Use configfile specified");
printf("\n\n");
}
2003-10-13 11:03:14 -04:00
int main(int argc, char *argv[]) {
int option;
char *configfile=NULL;
2003-10-13 11:03:14 -04:00
WSCONFIG ws_config;
WSHANDLE server;
2003-10-23 17:43:01 -04:00
pid_t rendezvous_pid;
2003-10-30 17:42:11 -05:00
int use_mdns=0;
2003-11-04 01:11:00 -05:00
ENUMHANDLE handle;
MP3FILE *pmp3;
2003-10-23 17:43:01 -04:00
#ifdef DEBUG
2003-10-30 17:42:11 -05:00
char *optval="d:c:m";
#else
2003-10-30 17:42:11 -05:00
char *optval="c:m";
#endif /* DEBUG */
2003-10-13 11:03:14 -04:00
printf("mt-daapd: version $Revision$\n");
printf("Copyright (c) 2003 Ron Pedde. All rights reserved\n");
printf("Portions Copyright (c) 1999-2001 Apple Computer, Inc. All rights Reserved.\n\n");
while((option=getopt(argc,argv,optval)) != -1) {
switch(option) {
#ifdef DEBUG
case 'd':
err_debuglevel=atoi(optarg);
break;
#endif
case 'c':
configfile=optarg;
break;
2003-10-30 17:42:11 -05:00
case 'm':
use_mdns=1;
break;
default:
usage(argv[0]);
exit(EXIT_FAILURE);
break;
}
}
/* read the configfile, if specified, otherwise
* try defaults */
if(!configfile) {
if(config_read("/etc/mt-daapd.conf",&config))
if(config_read("./mt-daapd.conf",&config)) {
perror("configfile_read");
exit(EXIT_FAILURE);
}
} else {
if(config_read(configfile,&config)) {
perror("config_read");
exit(EXIT_FAILURE);
}
}
DPRINTF(ERR_DEBUG,"Initializing database\n");
2003-11-04 01:11:00 -05:00
/* Initialize the database before starting */
if(db_init("none")) {
perror("db_init");
exit(EXIT_FAILURE);
}
2003-11-09 23:00:13 -05:00
printf("Scanning MP3s\n");
2003-11-04 01:11:00 -05:00
if(scan_init(config.mp3dir)) {
perror("scan_init");
exit(EXIT_FAILURE);
}
2003-11-09 23:00:13 -05:00
printf("Done... starting server\n");
/* start up the web server */
ws_config.web_root=config.web_root;
ws_config.port=config.port;
2003-10-13 11:03:14 -04:00
server=ws_start(&ws_config);
if(!server) {
perror("ws_start");
return EXIT_FAILURE;
}
2003-10-30 17:42:11 -05:00
ws_registerhandler(server, "^.*$",config_handler,config_auth,1);
ws_registerhandler(server, "^/server-info$",daap_handler,NULL,0);
ws_registerhandler(server, "^/content-codes$",daap_handler,NULL,0);
ws_registerhandler(server,"^/login$",daap_handler,NULL,0);
ws_registerhandler(server,"^/update$",daap_handler,NULL,0);
ws_registerhandler(server,"^/databases$",daap_handler,NULL,0);
ws_registerhandler(server,"^/logout$",daap_handler,NULL,0);
2003-11-03 15:34:18 -05:00
ws_registerhandler(server,"^/databases/.*",daap_handler,NULL,0);
2003-10-30 17:42:11 -05:00
if(use_mdns)
rend_init(&rendezvous_pid);
2003-10-23 17:43:01 -04:00
2003-10-13 11:03:14 -04:00
while(1) {
sleep(20);
}
return EXIT_SUCCESS;
}