2021-11-30 Fred Gleason <fredg@paravelsystems.com>

* Added 'RDCoreApplication::ExitSyscallError' to the
	'RDCoreApplication::ExitCode' enumeration.
	* Added 'RDCoreApplication::ExitNoConfig' to the
	'RDCoreApplication::ExitCode' enumeration.

Signed-off-by: Fred Gleason <fredg@paravelsystems.com>
This commit is contained in:
Fred Gleason
2021-11-30 18:24:33 -05:00
parent 519a09fcee
commit 94ffbbaf38
12 changed files with 554 additions and 1 deletions

1
.gitignore vendored
View File

@@ -137,6 +137,7 @@ tests/mcast_recv_test
tests/metadata_wildcard_test
tests/meterstrip_test
tests/notification_test
tests/rdfiler_test
tests/rdwavefile_test
tests/rdxml_parse_test
tests/readcd_test

View File

@@ -22576,3 +22576,8 @@
audio recordings manually.
2021-11-24 Fred Gleason <fredg@paravelsystems.com>
* Added a '--check-strings' switch to rddbmgr(8).
2021-11-30 Fred Gleason <fredg@paravelsystems.com>
* Added 'RDCoreApplication::ExitSyscallError' to the
'RDCoreApplication::ExitCode' enumeration.
* Added 'RDCoreApplication::ExitNoConfig' to the
'RDCoreApplication::ExitCode' enumeration.

View File

@@ -135,6 +135,7 @@ dist_librd_la_SOURCES = dbversion.h\
rdexport_settings_dialog.cpp rdexport_settings_dialog.h\
rdfeed.cpp rdfeed.h\
rdfeedlistmodel.cpp rdfeedlistmodel.h\
rdfiler.cpp rdfiler.h\
rdfontengine.cpp rdfontengine.h\
rdformpost.cpp rdformpost.h\
rdflacdecode.cpp rdflacdecode.h\

View File

@@ -103,6 +103,7 @@ SOURCES += rdevent_line.cpp
SOURCES += rdeventimportlist.cpp
SOURCES += rdexport_settings_dialog.cpp
SOURCES += rdfeedlistmodel.cpp
SOURCES += rdfiler.cpp
SOURCES += rdfontengine.cpp
SOURCES += rdframe.cpp
SOURCES += rdget_ath.cpp
@@ -288,6 +289,7 @@ HEADERS += rdevent_line.h
HEADERS += rdeventimportlist.h
HEADERS += rdexport_settings_dialog.h
HEADERS += rdfeedlistmodel.h
HEADERS += rdfiler.h
HEADERS += rdfontengine.h
HEADERS += rdframe.h
HEADERS += rdget_ath.h

View File

@@ -550,6 +550,10 @@ QString RDCoreApplication::exitCodeText(RDCoreApplication::ExitCode code)
ret=tr("bad ticket");
break;
case RDCoreApplication::ExitSyscallError:
ret=tr("syscall error");
break;
case RDCoreApplication::ExitLast:
break;
}

View File

@@ -53,7 +53,8 @@ class RDCoreApplication : public QObject
ExitLogLinkFailed=10,ExitNoPerms=11,ExitReportFailed=12,
ExitImportFailed=13,ExitNoDropbox=14,ExitNoGroup=15,
ExitInvalidCart=16,ExitNoSchedCode=17,
ExitBadTicket=18,ExitLast=19};
ExitBadTicket=18,ExitSyscallError=19,ExitNoConfig=20,
ExitLast=21};
RDCoreApplication(const QString &module_name,const QString &cmdname,
const QString &usage,QObject *parent=0);
~RDCoreApplication();

269
lib/rdfiler.cpp Normal file
View File

@@ -0,0 +1,269 @@
// rdfiler.cpp
//
// Delegate for opening/opening/deleting files
//
// (C) Copyright 2021 Fred Gleason <fredg@paravelsystems.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
//
#include <unistd.h>
#include <sys/socket.h>
#include <QStringList>
#include "rdfiler.h"
RDFiler::RDFiler(uid_t uid,gid_t gid,const QString &root_dir)
{
d_uid=uid;
d_gid=gid;
d_root_directory=root_dir;
d_service_pid=-1;
d_unix_socket=-1;
}
RDFiler::~RDFiler()
{
if(d_unix_socket>=0) {
close(d_unix_socket);
}
}
bool RDFiler::start()
{
int socks[2];
if(socketpair(AF_UNIX,SOCK_DGRAM,0,socks)<0) {
return false;
}
d_service_pid=fork();
if(d_service_pid<0) {
return false;
}
if(d_service_pid==0) {
//
// We are the child
//
close(socks[0]);
d_unix_socket=socks[1];
RDApplication::ExitCode exit_code=ServiceLoop();
close(d_unix_socket);
exit(exit_code);
}
//
// We are the parent
//
close(socks[1]);
d_unix_socket=socks[0];
return true;
}
int RDFiler::open(const QString &pathname,int flags,mode_t mode) const
{
int fd=-1;
ssize_t n;
char resp[1501];
QByteArray cmd=(QString("OPEN ")+
pathname+" "+
QString::asprintf("%u ",flags)+
QString::asprintf("%u",mode)).toUtf8();
if(send(d_unix_socket,cmd,cmd.size(),0)<0) {
return -1;
}
printf("HERE A\n");
struct msghdr msg;
struct iovec iov[1];
union {
struct cmsghdr cm;
char control[CMSG_SPACE(sizeof(int))];
} control_un;
struct cmsghdr *cmptr=NULL;
msg.msg_control=control_un.control;
msg.msg_controllen=sizeof(control_un.control);
msg.msg_name=NULL;
msg.msg_namelen=0;
iov[0].iov_base=resp;
iov[0].iov_len=1500;
msg.msg_iov=iov;
msg.msg_iovlen=1;
printf("HERE B\n");
if((n=recvmsg(d_unix_socket,&msg,0))<0) {
printf("HERE C\n");
return -1;
}
printf("n: %d\n",n);
printf("HERE D\n");
if(((cmptr=CMSG_FIRSTHDR(&msg))!=NULL)&&
cmptr->cmsg_len==CMSG_LEN(sizeof(int))) {
if((cmptr->cmsg_level==SOL_SOCKET)&&(cmptr->cmsg_type==SCM_RIGHTS)) {
fd=*((int *) CMSG_DATA(cmptr));
}
}
/*
if((n=recv(d_unix_socket,resp,1500,0))<0) {
return -1;
}
resp[n]=0;
if(QString(resp)=="OK") {
}
*/
return fd;
}
bool RDFiler::unlink(const QString &pathname) const
{
ssize_t n;
char resp[1501];
QByteArray msg=(QString("UNLINK ")+pathname).toUtf8();
if(send(d_unix_socket,msg,msg.size(),0)<0) {
return false;
}
if((n=recv(d_unix_socket,resp,1500,0))<0) {
return false;
}
resp[n]=0;
return QString(resp)=="OK";
}
RDApplication::ExitCode RDFiler::ServiceLoop()
{
char msg[1501];
char resp[256];
ssize_t n=0;
bool ok;
//
// Set filesystem context
//
if(chdir(d_root_directory.toUtf8())<0) {
close(d_unix_socket);
return RDApplication::ExitSyscallError;
}
if(chroot(d_root_directory.toUtf8())<0) {
close(d_unix_socket);
return RDApplication::ExitSyscallError;
}
//
// Drop root permissions
//
if(setgid(d_gid)<0) {
close(d_unix_socket);
return RDApplication::ExitSyscallError;
}
if(setuid(d_uid)<0) {
close(d_unix_socket);
return RDApplication::ExitSyscallError;
}
//
// Main Loop
//
while((n=read(d_unix_socket,msg,1500))>0) {
strncpy(resp,"ERROR",255);
ok=false;
msg[n]=0;
QStringList f0=QString(msg).split(" ",QString::SkipEmptyParts);
if(f0.at(0)=="OPEN") {
printf("HERE1\n");
int flags=0;
mode_t mode=0;
int fd=-1;
printf("HERE2\n");
if(f0.size()==4) {
printf("HERE3\n");
flags=f0.at(2).toInt(&ok);
if(ok) {
printf("HERE4\n");
mode=f0.at(3).toInt(&ok);
if(ok) {
printf("HERE5\n");
if((fd=open(f0.at(1).toUtf8(),flags,mode))>=0) {
printf("HERE6\n");
strncpy(resp,"OK",255);
}
printf("HERE7\n");
}
}
}
printf("\nsend_fd: %d\n",fd);
/*
if(send(d_unix_socket,resp,resp.size(),0)<0) {
break;
}
*/
//
// Transfer file descriptor
//
struct msghdr msg;
struct iovec iov[1];
union {
struct cmsghdr cm;
char control[CMSG_SPACE(sizeof(int))];
} control_un;
struct cmsghdr *cmptr=NULL;
msg.msg_control=control_un.control;
msg.msg_controllen=sizeof(control_un.control);
cmptr=CMSG_FIRSTHDR(&msg);
cmptr->cmsg_len=CMSG_LEN(sizeof(int));
cmptr->cmsg_level=SOL_SOCKET;
cmptr->cmsg_type=SCM_RIGHTS;
*((int *) CMSG_DATA(cmptr))=fd;
msg.msg_name=NULL;
msg.msg_namelen=0;
iov[0].iov_base=resp;
iov[0].iov_len=strlen(resp);
msg.msg_iov=iov;
msg.msg_iovlen=1;
if(sendmsg(d_unix_socket,&msg,0)<0) {
break;
}
}
if(f0.at(0)=="UNLINK") {
if(f0.size()==2) {
if((::unlink(f0.at(1).toUtf8())==0)||(errno=ENOENT)) {
strncpy(resp,"OK",255);
}
}
if(send(d_unix_socket,resp,strlen(resp),0)<0) {
break;
}
}
}
close(d_unix_socket);
return RDApplication::ExitOk;
}

52
lib/rdfiler.h Normal file
View File

@@ -0,0 +1,52 @@
// rdfiler.h
//
// Delegate for opening/opening/deleting files
//
// (C) Copyright 2021 Fred Gleason <fredg@paravelsystems.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
//
#ifndef RDFILER_H
#define RDFILER_H
//#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <QString>
#include <rdapplication.h>
class RDFiler
{
public:
RDFiler(uid_t uid,gid_t gid,const QString &root_dir);
~RDFiler();
bool start();
int open(const QString &pathname,int flags,
mode_t mode=S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) const;
bool unlink(const QString &pathname) const;
private:
RDApplication::ExitCode ServiceLoop();
uid_t d_uid;
gid_t d_gid;
QString d_root_directory;
int d_unix_socket;
pid_t d_service_pid;
};
#endif // RDFILER_H

View File

@@ -44,6 +44,7 @@ noinst_PROGRAMS = audio_convert_test\
metadata_wildcard_test\
meterstrip_test\
notification_test\
rdfiler_test\
rdwavefile_test\
rdxml_parse_test\
readcd_test\
@@ -119,6 +120,10 @@ dist_notification_test_SOURCES = notification_test.cpp notification_test.h
nodist_notification_test_SOURCES = moc_notification_test.cpp
notification_test_LDADD = @LIB_RDLIBS@ @LIBVORBIS@ @QT5_LIBS@ @MUSICBRAINZ_LIBS@
dist_rdfiler_test_SOURCES = rdfiler_test.cpp rdfiler_test.h
nodist_rdfiler_test_SOURCES = moc_rdfiler_test.cpp
rdfiler_test_LDADD = @LIB_RDLIBS@ @LIBVORBIS@ @QT5_LIBS@ @MUSICBRAINZ_LIBS@
dist_rdwavefile_test_SOURCES = rdwavefile_test.cpp rdwavefile_test.h
nodist_rdwavefile_test_SOURCES = moc_rdwavefile_test.cpp
rdwavefile_test_LDADD = @LIB_RDLIBS@ @LIBVORBIS@ @QT5_LIBS@ @MUSICBRAINZ_LIBS@

176
tests/rdfiler_test.cpp Normal file
View File

@@ -0,0 +1,176 @@
// rdfiler_test.cpp
//
// Test the Rivendell RDFiler class.
//
// (C) Copyright 2021 Fred Gleason <fredg@paravelsystems.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
//
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <QCoreApplication>
#include <QDir>
#include <rdapplication.h>
#include <rdcmd_switch.h>
#include <rdconfig.h>
#include <rdfiler.h>
#include "rdfiler_test.h"
MainObject::MainObject()
: QObject()
{
QString user;
QString root_directory;
QString operation;
QString filename;
//
// Check that we are root
//
if(geteuid()!=0) {
fprintf(stderr,"this program must be run as root\n");
exit(RDApplication::ExitNoPerms);
}
//
// Process command line switches
//
RDCmdSwitch *cmd=new RDCmdSwitch("rdfiler_test",RDFILER_TEST_USAGE);
for(unsigned i=0;i<cmd->keys();i++) {
if(cmd->key(i)=="--filename") {
filename=cmd->value(i);
cmd->setProcessed(i,true);
}
if(cmd->key(i)=="--operation") {
if((cmd->value(i)!="create")&&
(cmd->value(i)!="read")&&
(cmd->value(i)!="unlink")&&
(cmd->value(i)!="write")) {
fprintf(stderr,"rdfiler_test: unknown operation \"%s\"\n",
cmd->value(i).toUtf8().constData());
exit(RDApplication::ExitInvalidOption);
}
operation=cmd->value(i);
cmd->setProcessed(i,true);
}
if(cmd->key(i)=="--root-dir") {
QDir dir(cmd->value(i));
if(!dir.exists()) {
fprintf(stderr,"rdfiler_test: no such root directory\n");
exit(RDApplication::ExitInvalidOption);
}
root_directory=cmd->value(i);
cmd->setProcessed(i,true);
}
if(cmd->key(i)=="--user") {
user=cmd->value(i);
cmd->setProcessed(i,true);
}
if(!cmd->processed(i)) {
fprintf(stderr,"unrecognized switch \"%s\"\n",
cmd->key(i).toUtf8().constData());
exit(RDApplication::ExitInvalidOption);
}
}
if(filename.isEmpty()) {
fprintf(stderr,"rdfiler_test: you must specify \"--filename\"\n");
exit(RDApplication::ExitInvalidOption);
}
if(operation.isEmpty()) {
fprintf(stderr,"rdfiler_test: you must specify \"--operation\"\n");
exit(RDApplication::ExitInvalidOption);
}
if(root_directory.isEmpty()) {
fprintf(stderr,"rdfiler_test: you must specify \"--root-dir\"\n");
exit(RDApplication::ExitInvalidOption);
}
if(user.isEmpty()) {
fprintf(stderr,"rdfiler_test: you must specify \"--user\"\n");
exit(RDApplication::ExitInvalidOption);
}
//
// Get configuration
//
RDConfig *config=new RDConfig();
if(!config->load()) {
fprintf(stderr,"rdfiler_test: unable to open Rivendell configuration\n");
exit(RDApplication::ExitNoConfig);
}
//
// Create filer object
//
RDFiler *filer=new RDFiler(config->uid(),config->gid(),root_directory);
if(!filer->start()) {
fprintf(stderr,"rdfiler_test: unable to start filer object\n");
delete filer;
exit(RDApplication::ExitSyscallError);
}
//
// Run the operation
//
if(operation=="read") {
int fd=-1;
printf("\n");
printf("attempting to open \"%s/%s\": ",
root_directory.toUtf8().constData(),
filename.toUtf8().constData());
fflush(stdout);
if((fd=filer->open(filename,O_RDONLY))>=0) {
printf("success\n");
}
else {
printf("failure\n");
}
}
if(operation=="unlink") {
printf("\n");
printf("attempting to unlink \"%s/%s\": ",
root_directory.toUtf8().constData(),
filename.toUtf8().constData());
fflush(stdout);
if(filer->unlink(filename)) {
printf("success\n");
}
else {
printf("failure\n");
}
}
//
// Wait for exit
//
printf("\n");
printf("Hit CTRL-C to quit\n");
}
int main(int argc,char *argv[])
{
QCoreApplication a(argc,argv);
new MainObject();
return a.exec();
}

36
tests/rdfiler_test.h Normal file
View File

@@ -0,0 +1,36 @@
// rdfiler_test.h
//
// Test the Rivendell RDFiler class.
//
// (C) Copyright 2021 Fred Gleason <fredg@paravelsystems.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
//
#ifndef RDFILER_TEST_H
#define RDFILER_TEST_H
#include <QObject>
#define RDFILER_TEST_USAGE "--user=<username> --root-dir=<pathname> --operation=create|read|write|unlink --filename=<filename>\n"
class MainObject : public QObject
{
Q_OBJECT;
public:
MainObject();
};
#endif // RDFILER_TEST_H

View File

@@ -518,6 +518,7 @@ void MainObject::PutAudio()
case RDApplication::ExitInvalidCart:
case RDApplication::ExitNoSchedCode:
case RDApplication::ExitBadTicket:
case RDApplication::ExitSyscallError:
rda->syslog(LOG_WARNING,
"importer process returned exit code %d [cmdline: %s, client addr: %s]",
proc->exitCode(),