Advanced Rogue family: overhaul privilege handling.

Advanced Rogue 5 and 7, and XRogue, now open the scorefile and logfile
at startup and then drop any set[ug]id privileges if the savedir is not
being used.
This commit is contained in:
John "Elwin" Edwards 2015-05-16 13:39:26 -04:00
parent 1a5e14ad9b
commit 3554339257
9 changed files with 123 additions and 38 deletions

View file

@ -57,3 +57,8 @@ extern char *xcrypt();
*/
#define FUDGE_TIME 200
/* file locations */
#define SCOREFILE "xrogue.scr"
#define LOGFILE "xrogue.log"
#define SAVEDIR "."

View file

@ -26,8 +26,9 @@
#include "network.h"
#include "rogue.h"
#define SCOREFILE "xrogue.scr"
#define SAVEDIR "."
FILE *scorefi = NULL;
FILE *logfile = NULL;
void open_records(void);
main(argc, argv, envp)
char **argv;
@ -83,6 +84,9 @@ char **envp;
if (whoami[0] == '\0')
strucpy(whoami, md_getusername(), strlen(md_getusername()));
open_records();
if (!use_savedir)
md_normaluser();
/*
* check for print-score option
*/
@ -470,3 +474,18 @@ int flag;
exit(0);
}
void
open_records(void)
{
if (scorefi == NULL)
scorefi = fopen(score_file, "rb+");
if (scorefi == NULL)
scorefi = fopen(score_file, "wb+");
/* If opening fails, that will be handled when trying to write. */
#ifdef LOGFILE
if (logfile == NULL)
logfile = fopen(LOGFILE, "a");
#endif
return;
}

View file

@ -20,8 +20,6 @@
#define EDITSCORE 2 /* Edit the current score file */
#define ADDSCORE 3 /* Add a new score */
#define LOGFILE "xrogue.log"
#include <curses.h>
#include <time.h>
#include <signal.h>
@ -58,6 +56,8 @@ NULL
char *killname();
extern FILE *scorefi, *logfile;
/*UNUSED*/
void
byebye(sig)
@ -138,7 +138,6 @@ register short monst;
void
writelog(unsigned long amount, int flags, short monst)
{
FILE *logwriter;
char had_quest = '0';
char fate[LINELEN];
struct linked_list *item;
@ -146,6 +145,11 @@ writelog(unsigned long amount, int flags, short monst)
#ifdef LOGFILE
if (waswizard)
return;
if (logfile == NULL)
{
/* Error message? */
return;
}
/* Adjustments to the score */
if (level == 0 && max_level == 0)
amount = 0;
@ -169,14 +173,11 @@ writelog(unsigned long amount, int flags, short monst)
}
else
return;
/* Open and write */
logwriter = fopen(LOGFILE, "a");
if (logwriter == NULL)
return;
fprintf(logwriter, "%d %d %s %d %s %d %d %d %c %s\n", time(NULL), amount,
/* Write the line */
fprintf(logfile, "%d %d %s %d %s %d %d %d %c %s\n", time(NULL), amount,
whoami, pstats.s_lvl, char_class[char_type].name, level, max_level,
quest_item, had_quest, fate);
fclose(logwriter);
fclose(logfile);
#endif
return;
}
@ -234,14 +235,15 @@ short monst;
* Open file and read list
*/
if ((outf = fopen(score_file, "rb+")) == NULL)
if (scorefi == NULL)
{
if ((outf = fopen(score_file, "wb+")) == NULL)
{
mvprintw(lines - 1, 0, "Unable to open or create score file: %s",score_file);
refresh();
return;
}
mvprintw(lines - 1, 0, "Unable to open or create score file: %s",score_file);
refresh();
return;
}
else
{
outf = scorefi;
}
thissys = md_gethostname();

View file

@ -3356,3 +3356,12 @@ md_setup()
crmode(); /* Cbreak mode */
noecho(); /* Echo off */
}
int
md_normaluser(void)
{
#ifndef _WIN32
setuid(getuid());
setgid(getgid());
#endif
}