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

@ -16,6 +16,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <limits.h> #include <limits.h>
#include <signal.h> #include <signal.h>
@ -42,6 +43,10 @@ static char *funfruit[] = {
}; };
#define NFRUIT (sizeof(funfruit) / sizeof (char *)) #define NFRUIT (sizeof(funfruit) / sizeof (char *))
int scorefd = -1;
FILE *logfile = NULL;
void open_records(void);
main(argc, argv, envp) main(argc, argv, envp)
char **argv; char **argv;
char **envp; char **envp;
@ -102,6 +107,10 @@ char **envp;
strcpy(fruit, funfruit[rnd(NFRUIT)]); strcpy(fruit, funfruit[rnd(NFRUIT)]);
} }
open_records();
if (!use_savedir)
md_normaluser();
/* /*
* check for print-score option * check for print-score option
*/ */
@ -242,6 +251,18 @@ char **envp;
playit(); playit();
} }
void
open_records(void)
{
if (scorefd == -1)
scorefd = open(score_file, O_RDWR | O_CREAT, 0666);
#ifdef LOGFILE
if (logfile == NULL)
logfile = fopen(LOGFILE, "a");
#endif
return;
}
/* /*
* endit: * endit:
* Exit the program abnormally. * Exit the program abnormally.

View file

@ -36,6 +36,9 @@
* scoreout() and scorein() to reflect the change. Also update SCORELEN. * scoreout() and scorein() to reflect the change. Also update SCORELEN.
*/ */
extern int scorefd;
extern FILE *logfile;
struct sc_ent { struct sc_ent {
unsigned long sc_score; unsigned long sc_score;
char sc_name[LINELEN]; char sc_name[LINELEN];
@ -201,7 +204,7 @@ short monst;
* Open file and read list * Open file and read list
*/ */
if ((fd = open(score_file, O_RDWR | O_CREAT, 0666)) < 0) if ((fd = scorefd) < 0)
{ {
printf("\nCannot open score_file.\n"); printf("\nCannot open score_file.\n");
return; return;
@ -576,7 +579,6 @@ short monst;
void writelog(unsigned long amount, int flags, short monst) { void writelog(unsigned long amount, int flags, short monst) {
#ifdef LOGFILE #ifdef LOGFILE
FILE *logfi;
char fate[100]; char fate[100];
char *class; char *class;
struct linked_list *item; struct linked_list *item;
@ -585,6 +587,8 @@ void writelog(unsigned long amount, int flags, short monst) {
if (waswizard) if (waswizard)
return; return;
if (logfile == NULL)
return;
switch (player.t_ctype) { switch (player.t_ctype) {
case C_FIGHTER: class = "Fighter"; case C_FIGHTER: class = "Fighter";
when C_MAGICIAN: class = "Magician"; when C_MAGICIAN: class = "Magician";
@ -609,15 +613,10 @@ void writelog(unsigned long amount, int flags, short monst) {
else else
return; return;
logfi = fopen(LOGFILE, "a"); fprintf(logfile, "%d %d %s %d %s %d %d %d %c %s\n", time(NULL), amount,
if (logfi == NULL) {
perror(LOGFILE);
return;
}
fprintf(logfi, "%d %d %s %d %s %d %d %d %c %s\n", time(NULL), amount,
whoami, pstats.s_lvl, class, level, max_level, quest_item, had_quest, whoami, pstats.s_lvl, class, level, max_level, quest_item, had_quest,
fate); fate);
fclose(logfi); fclose(logfile);
#endif #endif
return; return;
} }

View file

@ -44,6 +44,15 @@
#define SCOREFILE "arogue7.scr" #define SCOREFILE "arogue7.scr"
#endif #endif
#ifndef LOGFILE
#define LOGFILE "arogue7.log"
#endif
#ifndef SAVEDIR
#define SAVEDIR "."
#endif
/* /*
* Variables for checking to make sure the system isn't too loaded * Variables for checking to make sure the system isn't too loaded
* for people to play * for people to play

View file

@ -16,6 +16,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <signal.h> #include <signal.h>
#include <sys/types.h>
#include <fcntl.h>
#ifdef BSD #ifdef BSD
#include <sys/time.h> #include <sys/time.h>
#else #else
@ -31,7 +33,9 @@ extern struct uwdata wdata, oldwin;
extern char oldtext[WTXTNUM][WTXTLEN]; extern char oldtext[WTXTNUM][WTXTLEN];
#endif #endif
#define SAVEDIR "." int scorefd = -1;
FILE *logfile = NULL;
void open_records(void);
main(argc, argv, envp) main(argc, argv, envp)
char **argv; char **argv;
@ -91,6 +95,8 @@ char **envp;
if (whoami[0] == '\0') if (whoami[0] == '\0')
strucpy(whoami, md_getusername(), strlen(md_getusername())); strucpy(whoami, md_getusername(), strlen(md_getusername()));
open_records();
/* /*
* check for print-score option * check for print-score option
*/ */
@ -161,6 +167,10 @@ char **envp;
if (!restore(file_name, envp)) if (!restore(file_name, envp))
exit(1); exit(1);
} }
else
{
md_normaluser();
}
if (argc == 2) if (argc == 2)
if (!restore(argv[1], envp)) /* Note: restore will never return */ if (!restore(argv[1], envp)) /* Note: restore will never return */
exit(1); exit(1);
@ -539,6 +549,18 @@ setup()
nonl(); nonl();
} }
void
open_records(void)
{
if (scorefd == -1)
scorefd = open(score_file, O_RDWR | O_CREAT, 0666);
#ifdef LOGFILE
if (logfile == NULL)
logfile = fopen(LOGFILE, "a");
#endif
return;
}
/* /*
* playit: * playit:
* The main loop of the program. Loop until the game is over, * The main loop of the program. Loop until the game is over,

View file

@ -17,8 +17,6 @@
#define EDITSCORE 2 /* Edit the current score file */ #define EDITSCORE 2 /* Edit the current score file */
#define ADDSCORE 3 /* Add a new score */ #define ADDSCORE 3 /* Add a new score */
#define LOGFILE "arogue7.log"
#define NAMELEN 80 #define NAMELEN 80
/* /*
@ -48,6 +46,9 @@ extern struct uwdata wdata, oldwin;
extern char oldtext[WTXTNUM][WTXTLEN]; extern char oldtext[WTXTNUM][WTXTLEN];
#endif #endif
extern int scorefd;
extern FILE *logfile;
#ifdef NUMNET #ifdef NUMNET
/* Network machines (for mutual score keeping) */ /* Network machines (for mutual score keeping) */
static struct network Network[NUMNET] = { static struct network Network[NUMNET] = {
@ -207,7 +208,6 @@ register short monst;
void void
writelog(unsigned long amount, int flags, short monst) writelog(unsigned long amount, int flags, short monst)
{ {
FILE *logwriter;
char had_quest = '0'; char had_quest = '0';
char fate[LINELEN]; char fate[LINELEN];
struct linked_list *item; struct linked_list *item;
@ -215,6 +215,8 @@ writelog(unsigned long amount, int flags, short monst)
#ifdef LOGFILE #ifdef LOGFILE
if (waswizard) if (waswizard)
return; return;
if (logfile == NULL)
return;
/* Check for quest item */ /* Check for quest item */
for (item = pack; item != NULL; item = next(item)) { for (item = pack; item != NULL; item = next(item)) {
obj = OBJPTR(item); obj = OBJPTR(item);
@ -233,14 +235,11 @@ writelog(unsigned long amount, int flags, short monst)
} }
else else
return; return;
/* Open and write */ /* Write */
logwriter = fopen(LOGFILE, "a"); fprintf(logfile, "%d %d %s %d %s %d %d %d %c %s\n", time(NULL), amount,
if (logwriter == NULL)
return;
fprintf(logwriter, "%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, whoami, pstats.s_lvl, char_class[char_type].name, level, max_level,
quest_item, had_quest, fate); quest_item, had_quest, fate);
fclose(logwriter); fclose(logfile);
#endif #endif
return; return;
} }
@ -295,7 +294,7 @@ short monst;
* Open file and read list * Open file and read list
*/ */
if ((fd = open(score_file, O_RDWR | O_CREAT, 0666)) < 0) return; if ((fd = scorefd) < 0) return;
outfd = fd; outfd = fd;
#ifndef SYSTEM #ifndef SYSTEM

View file

@ -57,3 +57,8 @@ extern char *xcrypt();
*/ */
#define FUDGE_TIME 200 #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 "network.h"
#include "rogue.h" #include "rogue.h"
#define SCOREFILE "xrogue.scr" FILE *scorefi = NULL;
#define SAVEDIR "." FILE *logfile = NULL;
void open_records(void);
main(argc, argv, envp) main(argc, argv, envp)
char **argv; char **argv;
@ -83,6 +84,9 @@ char **envp;
if (whoami[0] == '\0') if (whoami[0] == '\0')
strucpy(whoami, md_getusername(), strlen(md_getusername())); strucpy(whoami, md_getusername(), strlen(md_getusername()));
open_records();
if (!use_savedir)
md_normaluser();
/* /*
* check for print-score option * check for print-score option
*/ */
@ -470,3 +474,18 @@ int flag;
exit(0); 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 EDITSCORE 2 /* Edit the current score file */
#define ADDSCORE 3 /* Add a new score */ #define ADDSCORE 3 /* Add a new score */
#define LOGFILE "xrogue.log"
#include <curses.h> #include <curses.h>
#include <time.h> #include <time.h>
#include <signal.h> #include <signal.h>
@ -58,6 +56,8 @@ NULL
char *killname(); char *killname();
extern FILE *scorefi, *logfile;
/*UNUSED*/ /*UNUSED*/
void void
byebye(sig) byebye(sig)
@ -138,7 +138,6 @@ register short monst;
void void
writelog(unsigned long amount, int flags, short monst) writelog(unsigned long amount, int flags, short monst)
{ {
FILE *logwriter;
char had_quest = '0'; char had_quest = '0';
char fate[LINELEN]; char fate[LINELEN];
struct linked_list *item; struct linked_list *item;
@ -146,6 +145,11 @@ writelog(unsigned long amount, int flags, short monst)
#ifdef LOGFILE #ifdef LOGFILE
if (waswizard) if (waswizard)
return; return;
if (logfile == NULL)
{
/* Error message? */
return;
}
/* Adjustments to the score */ /* Adjustments to the score */
if (level == 0 && max_level == 0) if (level == 0 && max_level == 0)
amount = 0; amount = 0;
@ -169,14 +173,11 @@ writelog(unsigned long amount, int flags, short monst)
} }
else else
return; return;
/* Open and write */ /* Write the line */
logwriter = fopen(LOGFILE, "a"); fprintf(logfile, "%d %d %s %d %s %d %d %d %c %s\n", time(NULL), amount,
if (logwriter == NULL)
return;
fprintf(logwriter, "%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, whoami, pstats.s_lvl, char_class[char_type].name, level, max_level,
quest_item, had_quest, fate); quest_item, had_quest, fate);
fclose(logwriter); fclose(logfile);
#endif #endif
return; return;
} }
@ -234,14 +235,15 @@ short monst;
* Open file and read list * 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); mvprintw(lines - 1, 0, "Unable to open or create score file: %s",score_file);
refresh(); refresh();
return; return;
} }
else
{
outf = scorefi;
} }
thissys = md_gethostname(); thissys = md_gethostname();

View file

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