From 583f31cd2d52d8fee94109bde6b4c03b71472387 Mon Sep 17 00:00:00 2001 From: mogami Date: Wed, 12 Feb 2003 12:00:58 +0000 Subject: [PATCH] =?utf8?q?cmd4.c=E3=81=AE=20auto=5Fdump=20=E3=81=AE?= =?utf8?q?=E3=82=B3=E3=83=BC=E3=83=89=E3=82=92=E6=95=B4=E7=90=86=E3=80=81?= =?utf8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88=E4=BB=98=E5=8A=A0=E3=80=82S?= =?utf8?q?angband=E3=81=B8=E3=81=AE=E7=A7=BB=E6=A4=8D=E4=BD=9C=E6=A5=AD=20?= =?utf8?q?=E3=81=AB=E3=81=8A=E3=81=84=E3=81=A6=E3=82=82=E3=81=86=E3=81=A1?= =?utf8?q?=E3=82=87=E3=81=A3=E3=81=A8=E7=B6=BA=E9=BA=97=E3=81=AB=E3=81=97?= =?utf8?q?=E3=81=9F=E6=96=B9=E3=81=8C=E8=89=AF=E3=81=84=E3=81=A0=E3=82=8D?= =?utf8?q?=E3=81=86=E3=81=A8=E3=81=84=E3=81=86=E4=BA=8B=E3=81=AB=E3=81=AA?= =?utf8?q?=E3=81=A3=E3=81=9F=E7=82=BA=E3=80=82=20=E9=96=A2=E9=80=A3?= =?utf8?q?=E3=81=97=E3=81=A6safe=5Fsetuid=5F*()=E3=81=AE=E4=BD=BF=E3=81=84?= =?utf8?q?=E6=96=B9=E3=82=92=E5=A4=89=E6=9B=B4=E3=80=82Vanilla=E3=81=A8?= =?utf8?q?=E5=90=8C=E6=A7=98=E3=81=AB=E3=81=97=E3=81=9F=E3=80=82=E5=BE=AE?= =?utf8?q?=E5=A6=99=E3=81=ABsecurity=E3=81=8C=E5=90=91=E4=B8=8A=E3=81=99?= =?utf8?q?=E3=82=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/autopick.c | 11 -- src/cmd4.c | 381 ++++++++++++++++++++++++++++++++------------------------- src/files.c | 63 ++++++---- src/floors.c | 24 ++++ src/init2.c | 16 ++- src/load.c | 18 +++ src/main.c | 20 ++- src/save.c | 36 ++++++ src/scores.c | 22 +++- src/wizard1.c | 14 --- 10 files changed, 369 insertions(+), 236 deletions(-) diff --git a/src/autopick.c b/src/autopick.c index 348975b8e..80f1aa6bd 100644 --- a/src/autopick.c +++ b/src/autopick.c @@ -1831,8 +1831,6 @@ static cptr *read_text_lines(cptr filename, bool user) if (user) { - /* Hack -- drop permissions */ - safe_setuid_drop(); path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename); } else @@ -1860,9 +1858,6 @@ static cptr *read_text_lines(cptr filename, bool user) my_fclose(fff); } - /* Grab priv's */ - safe_setuid_grab(); - if (!fff) return NULL; return lines_list; } @@ -1923,9 +1918,6 @@ static bool write_text_lines(cptr filename, cptr *lines_list) int lines = 0; char buf[1024]; - /* Hack -- drop permissions */ - safe_setuid_drop(); - /* Build the filename */ path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename); @@ -1939,9 +1931,6 @@ static bool write_text_lines(cptr filename, cptr *lines_list) my_fclose(fff); } - /* Grab priv's */ - safe_setuid_grab(); - if (!fff) return FALSE; return TRUE; } diff --git a/src/cmd4.c b/src/cmd4.c index 15cc366b5..777b0fbba 100644 --- a/src/cmd4.c +++ b/src/cmd4.c @@ -13,44 +13,86 @@ #include "angband.h" + +/* + * A set of functions to maintain automatic dumps of various kinds. + * -Mogami- + * + * remove_auto_dump(orig_file, mark) + * Remove the old automatic dump of type "mark". + * auto_dump_printf(fmt, ...) + * Dump a formatted string using fprintf(). + * open_auto_dump(buf, mark, &line_num) + * Open a file, remove old dump, and add new header. + * close_auto_dump(fff, mark, &line_num) + * Add a footer, and close the file. + * + * The dump commands of original Angband simply add new lines to + * existing files; these files will become bigger and bigger unless + * an user deletes some or all of these files by hand at some + * point. + * + * These three functions automatically delete old dumped lines + * before adding new ones. Since there are various kinds of automatic + * dumps in a single file, we add a header and a footer with a type + * name for every automatic dump, and kill old lines only when the + * lines have the correct type of header and footer. + * + * We need to be quite paranoid about correctness; the user might + * (mistakenly) edit the file by hand, and see all their work come + * to nothing on the next auto dump otherwise. The current code only + * detects changes by noting inconsistencies between the actual number + * of lines and the number written in the footer. Note that this will + * not catch single-line edits. + */ + /* - * mark strings for auto dump + * Mark strings for auto dump */ static char auto_dump_header[] = "# vvvvvvv== %s ==vvvvvvv"; static char auto_dump_footer[] = "# ^^^^^^^== %s ==^^^^^^^"; /* + * Variables for auto dump + */ +static FILE *auto_dump_stream; +static cptr auto_dump_mark; +static int auto_dump_line_num; + +/* * Remove old lines automatically generated before. */ -static void remove_auto_dump(cptr orig_file, cptr mark) +static void remove_auto_dump(cptr orig_file) { FILE *tmp_fff, *orig_fff; char tmp_file[1024]; char buf[1024]; bool between_mark = FALSE; - bool success = FALSE; + bool changed = FALSE; int line_num = 0; long header_location = 0; char header_mark_str[80]; char footer_mark_str[80]; size_t mark_len; - sprintf(header_mark_str, auto_dump_header, mark); - sprintf(footer_mark_str, auto_dump_footer, mark); + /* Prepare a header/footer mark string */ + sprintf(header_mark_str, auto_dump_header, auto_dump_mark); + sprintf(footer_mark_str, auto_dump_footer, auto_dump_mark); mark_len = strlen(footer_mark_str); - /* If original file is not exist, nothing to do */ + /* Open an old dump file in read-only mode */ orig_fff = my_fopen(orig_file, "r"); - if (!orig_fff) - { - return; - } - /* Open a new file */ + /* If original file does not exist, nothing to do */ + if (!orig_fff) return; + + /* Open a new (temporary) file */ tmp_fff = my_fopen_temp(tmp_file, 1024); - if (!tmp_fff) { + + if (!tmp_fff) + { #ifdef JP msg_format("°ì»þ¥Õ¥¡¥¤¥ë %s ¤òºîÀ®¤Ç¤­¤Þ¤»¤ó¤Ç¤·¤¿¡£", tmp_file); #else @@ -59,100 +101,180 @@ static void remove_auto_dump(cptr orig_file, cptr mark) msg_print(NULL); return; } - - while (1) + + /* Loop for every line */ + while (TRUE) { + /* Read a line */ if (my_fgets(orig_fff, buf, sizeof(buf))) { + /* Read error: Assume End of File */ + + /* + * Was looking for the footer, but not found. + * + * Since automatic dump might be edited by hand, + * it's dangerous to kill these lines. + * Seek back to the next line of the (pseudo) header, + * and read again. + */ if (between_mark) { fseek(orig_fff, header_location, SEEK_SET); between_mark = FALSE; continue; } + + /* Success -- End the loop */ else { break; } } + /* We are looking for the header mark of automatic dump */ if (!between_mark) { + /* Is this line a header? */ if (!strcmp(buf, header_mark_str)) { + /* Memorise seek point of this line */ header_location = ftell(orig_fff); + + /* Initialize counter for number of lines */ line_num = 0; + + /* Look for the footer from now */ between_mark = TRUE; - success = TRUE; + + /* There are some changes */ + changed = TRUE; } + + /* Not a header */ else { + /* Copy orginally lines */ fprintf(tmp_fff, "%s\n", buf); } } + + /* We are looking for the footer mark of automatic dump */ else { + /* Is this line a footer? */ if (!strncmp(buf, footer_mark_str, mark_len)) { int tmp; + /* + * Compare the number of lines + * + * If there is an inconsistency between + * actual number of lines and the + * number here, the automatic dump + * might be edited by hand. So it's + * dangerous to kill these lines. + * Seek back to the next line of the + * (pseudo) header, and read again. + */ if (!sscanf(buf + mark_len, " (%d)", &tmp) || tmp != line_num) { fseek(orig_fff, header_location, SEEK_SET); } + /* Look for another header */ between_mark = FALSE; } + + /* Not a footer */ else { + /* Ignore old line, and count number of lines */ line_num++; } } } + + /* Close files */ my_fclose(orig_fff); my_fclose(tmp_fff); - if (success) + /* If there are some changes, overwrite the original file with new one */ + if (changed) { - /* copy contents of temporally file */ + /* Copy contents of temporary file */ tmp_fff = my_fopen(tmp_file, "r"); orig_fff = my_fopen(orig_file, "w"); - + while (!my_fgets(tmp_fff, buf, sizeof(buf))) fprintf(orig_fff, "%s\n", buf); - + my_fclose(orig_fff); my_fclose(tmp_fff); } + + /* Kill the temporary file */ fd_kill(tmp_file); return; } + +/* + * Dump a formatted line, using "vstrnfmt()". + */ +static void auto_dump_printf(cptr fmt, ...) +{ + cptr p; + va_list vp; + + char buf[1024]; + + /* Begin the Varargs Stuff */ + va_start(vp, fmt); + + /* Format the args, save the length */ + (void)vstrnfmt(buf, sizeof(buf), fmt, vp); + + /* End the Varargs Stuff */ + va_end(vp); + + /* Count number of lines */ + for (p = buf; *p; p++) + { + if (*p == '\n') auto_dump_line_num++; + } + + /* Dump it */ + fprintf(auto_dump_stream, buf); +} + + /* * Open file to append auto dump. */ -static FILE *open_auto_dump(cptr buf, cptr mark, int *line) +static bool open_auto_dump(cptr buf, cptr mark) { - FILE *fff; char header_mark_str[80]; - /* Drop priv's */ - safe_setuid_drop(); + /* Save the mark string */ + auto_dump_mark = mark; - sprintf(header_mark_str, auto_dump_header, mark); + /* Prepare a header mark string */ + sprintf(header_mark_str, auto_dump_header, auto_dump_mark); /* Remove old macro dumps */ - remove_auto_dump(buf, mark); + remove_auto_dump(buf); /* Append to the file */ - fff = my_fopen(buf, "a"); + auto_dump_stream = my_fopen(buf, "a"); /* Failure */ - if (!fff) { + if (!auto_dump_stream) { #ifdef JP msg_format("%s ¤ò³«¤¯¤³¤È¤¬¤Ç¤­¤Þ¤»¤ó¤Ç¤·¤¿¡£", buf); #else @@ -160,53 +282,52 @@ static FILE *open_auto_dump(cptr buf, cptr mark, int *line) #endif msg_print(NULL); - /* Grab priv's */ - safe_setuid_grab(); - - return NULL; + /* Failed */ + return FALSE; } /* Start dumping */ - fprintf(fff, "%s\n", header_mark_str); + fprintf(auto_dump_stream, "%s\n", header_mark_str); + + /* Initialize counter */ + auto_dump_line_num = 0; #ifdef JP - fprintf(fff, "# *·Ù¹ð!!* °Ê¹ß¤Î¹Ô¤Ï¼«Æ°À¸À®¤µ¤ì¤¿¤â¤Î¤Ç¤¹¡£\n"); - fprintf(fff, "# *·Ù¹ð!!* ¸å¤Ç¼«Æ°Åª¤Ëºï½ü¤µ¤ì¤ë¤Î¤ÇÊÔ½¸¤·¤Ê¤¤¤Ç¤¯¤À¤µ¤¤¡£\n"); + auto_dump_printf("# *·Ù¹ð!!* °Ê¹ß¤Î¹Ô¤Ï¼«Æ°À¸À®¤µ¤ì¤¿¤â¤Î¤Ç¤¹¡£\n"); + auto_dump_printf("# *·Ù¹ð!!* ¸å¤Ç¼«Æ°Åª¤Ëºï½ü¤µ¤ì¤ë¤Î¤ÇÊÔ½¸¤·¤Ê¤¤¤Ç¤¯¤À¤µ¤¤¡£\n"); #else - fprintf(fff, "# *Warning!!* The lines below are automatic dump.\n"); - fprintf(fff, "# *Warning!!* Don't edit these! These lines will be deleted automaticaly.\n"); + auto_dump_printf("# *Warning!* The lines below are an automatic dump.\n"); + auto_dump_printf("# Don't edit them; changes will be deleted and replaced automatically.\n"); #endif - *line = 2; - return fff; + /* Success */ + return TRUE; } /* * Append foot part and close auto dump. */ -static void close_auto_dump(FILE *fff, cptr mark, int line_num) +static void close_auto_dump(void) { char footer_mark_str[80]; - sprintf(footer_mark_str, auto_dump_footer, mark); + /* Prepare a footer mark string */ + sprintf(footer_mark_str, auto_dump_footer, auto_dump_mark); - /* End of dumping */ #ifdef JP - fprintf(fff, "# *·Ù¹ð!!* °Ê¾å¤Î¹Ô¤Ï¼«Æ°À¸À®¤µ¤ì¤¿¤â¤Î¤Ç¤¹¡£\n"); - fprintf(fff, "# *·Ù¹ð!!* ¸å¤Ç¼«Æ°Åª¤Ëºï½ü¤µ¤ì¤ë¤Î¤ÇÊÔ½¸¤·¤Ê¤¤¤Ç¤¯¤À¤µ¤¤¡£\n"); + auto_dump_printf("# *·Ù¹ð!!* °Ê¾å¤Î¹Ô¤Ï¼«Æ°À¸À®¤µ¤ì¤¿¤â¤Î¤Ç¤¹¡£\n"); + auto_dump_printf("# *·Ù¹ð!!* ¸å¤Ç¼«Æ°Åª¤Ëºï½ü¤µ¤ì¤ë¤Î¤ÇÊÔ½¸¤·¤Ê¤¤¤Ç¤¯¤À¤µ¤¤¡£\n"); #else - fprintf(fff, "# *Warning!!* The lines above are automatic dump.\n"); - fprintf(fff, "# *Warning!!* Don't edit these! These lines will be deleted automaticaly.\n"); + auto_dump_printf("# *Warning!* The lines above are an automatic dump.\n"); + auto_dump_printf("# Don't edit them; changes will be deleted and replaced automatically.\n"); #endif - line_num += 2; - fprintf(fff, "%s (%d)\n", footer_mark_str, line_num); + /* End of dump */ + fprintf(auto_dump_stream, "%s (%d)\n", footer_mark_str, auto_dump_line_num); - my_fclose(fff); + /* Close */ + my_fclose(auto_dump_stream); - /* Grab priv's */ - safe_setuid_grab(); - return; } @@ -257,9 +378,6 @@ errr do_cmd_write_nikki(int type, int num, cptr note) sprintf(file_name,"playrec-%s.txt",savefile_base); #endif - /* Hack -- drop permissions */ - safe_setuid_drop(); - /* Build the filename */ path_build(buf, sizeof(buf), ANGBAND_DIR_USER, file_name); @@ -271,8 +389,6 @@ errr do_cmd_write_nikki(int type, int num, cptr note) /* Failure */ if (!fff) { - /* Hack -- grab permissions */ - safe_setuid_grab(); #ifdef JP msg_format("%s ¤ò³«¤¯¤³¤È¤¬¤Ç¤­¤Þ¤»¤ó¤Ç¤·¤¿¡£¥×¥ì¥¤µ­Ï¿¤ò°ì»þÄä»ß¤·¤Þ¤¹¡£", buf); #else @@ -672,9 +788,6 @@ errr do_cmd_write_nikki(int type, int num, cptr note) my_fclose(fff); - /* Hack -- grab permissions */ - safe_setuid_grab(); - if (do_level) write_level = FALSE; return (0); @@ -761,9 +874,6 @@ static void do_cmd_disp_nikki(void) sprintf(file_name,"playrec-%s.txt",savefile_base); #endif - /* Hack -- drop permissions */ - safe_setuid_drop(); - /* Build the filename */ path_build(buf, sizeof(buf), ANGBAND_DIR_USER, file_name); @@ -783,9 +893,6 @@ static void do_cmd_disp_nikki(void) /* Display the file contents */ show_file(FALSE, buf, nikki_title, -1, 0); - - /* Hack -- grab permissions */ - safe_setuid_grab(); } static void do_cmd_bunshou(void) @@ -848,9 +955,6 @@ static void do_cmd_erase_nikki(void) sprintf(file_name,"playrec-%s.txt",savefile_base); #endif - /* Hack -- drop permissions */ - safe_setuid_drop(); - /* Build the filename */ path_build(buf, sizeof(buf), ANGBAND_DIR_USER, file_name); @@ -873,9 +977,6 @@ static void do_cmd_erase_nikki(void) #endif } msg_print(NULL); - - /* Hack -- grab permissions */ - safe_setuid_grab(); } @@ -2573,9 +2674,7 @@ static errr macro_dump(cptr fname) { static cptr mark = "Macro Dump"; - int i, line_num; - - FILE *fff; + int i; char buf[1024]; @@ -2586,16 +2685,14 @@ static errr macro_dump(cptr fname) FILE_TYPE(FILE_TYPE_TEXT); /* Append to the file */ - fff = open_auto_dump(buf, mark, &line_num); - if (!fff) return (-1); + if (!open_auto_dump(buf, mark)) return (-1); /* Start dumping */ #ifdef JP - fprintf(fff, "\n# ¼«Æ°¥Þ¥¯¥í¥»¡¼¥Ö\n\n"); + auto_dump_printf("\n# ¼«Æ°¥Þ¥¯¥í¥»¡¼¥Ö\n\n"); #else - fprintf(fff, "\n# Automatic macro dump\n\n"); + auto_dump_printf("\n# Automatic macro dump\n\n"); #endif - line_num += 3; /* Dump them */ for (i = 0; i < macro__num; i++) @@ -2604,23 +2701,20 @@ static errr macro_dump(cptr fname) ascii_to_text(buf, macro__act[i]); /* Dump the macro */ - fprintf(fff, "A:%s\n", buf); + auto_dump_printf("A:%s\n", buf); /* Extract the action */ ascii_to_text(buf, macro__pat[i]); /* Dump normal macros */ - fprintf(fff, "P:%s\n", buf); + auto_dump_printf("P:%s\n", buf); /* End the macro */ - fprintf(fff, "\n"); - - /* count number of lines */ - line_num += 3; + auto_dump_printf("\n"); } /* Close */ - close_auto_dump(fff, mark, line_num); + close_auto_dump(); /* Success */ return (0); @@ -2721,11 +2815,8 @@ static void do_cmd_macro_aux_keymap(char *buf) static errr keymap_dump(cptr fname) { static cptr mark = "Keymap Dump"; - int line_num; int i; - FILE *fff; - char key[1024]; char buf[1024]; @@ -2751,16 +2842,14 @@ static errr keymap_dump(cptr fname) FILE_TYPE(FILE_TYPE_TEXT); /* Append to the file */ - fff = open_auto_dump(buf, mark, &line_num); - if (!fff) return -1; + if (!open_auto_dump(buf, mark)) return -1; /* Start dumping */ #ifdef JP - fprintf(fff, "\n# ¼«Æ°¥­¡¼ÇÛÃÖ¥»¡¼¥Ö\n\n"); + auto_dump_printf("\n# ¼«Æ°¥­¡¼ÇÛÃÖ¥»¡¼¥Ö\n\n"); #else - fprintf(fff, "\n# Automatic keymap dump\n\n"); + auto_dump_printf("\n# Automatic keymap dump\n\n"); #endif - line_num += 3; /* Dump them */ for (i = 0; i < 256; i++) @@ -2782,13 +2871,12 @@ static errr keymap_dump(cptr fname) ascii_to_text(buf, act); /* Dump the macro */ - fprintf(fff, "A:%s\n", buf); - fprintf(fff, "C:%d:%s\n", mode, key); - line_num += 2; + auto_dump_printf("A:%s\n", buf); + auto_dump_printf("C:%d:%s\n", mode, key); } /* Close */ - close_auto_dump(fff, mark, line_num); + close_auto_dump(); /* Success */ return (0); @@ -3422,8 +3510,6 @@ void do_cmd_visuals(void) { int i; - FILE *fff; - char tmp[160]; char buf[1024]; @@ -3539,7 +3625,6 @@ void do_cmd_visuals(void) else if (i == '2') { static cptr mark = "Monster attr/chars"; - int line_num; /* Prompt */ #ifdef JP @@ -3567,16 +3652,14 @@ void do_cmd_visuals(void) path_build(buf, sizeof(buf), ANGBAND_DIR_USER, tmp); /* Append to the file */ - fff = open_auto_dump(buf, mark, &line_num); - if (!fff) continue; + if (!open_auto_dump(buf, mark)) continue; /* Start dumping */ #ifdef JP - fprintf(fff, "\n# ¥â¥ó¥¹¥¿¡¼¤Î[¿§/ʸ»ú]¤ÎÀßÄê\n\n"); + auto_dump_printf("\n# ¥â¥ó¥¹¥¿¡¼¤Î[¿§/ʸ»ú]¤ÎÀßÄê\n\n"); #else - fprintf(fff, "\n# Monster attr/char definitions\n\n"); + auto_dump_printf("\n# Monster attr/char definitions\n\n"); #endif - line_num += 3; /* Dump monsters */ for (i = 1; i < max_r_idx; i++) @@ -3587,17 +3670,15 @@ void do_cmd_visuals(void) if (!r_ptr->name) continue; /* Dump a comment */ - fprintf(fff, "# %s\n", (r_name + r_ptr->name)); - line_num++; + auto_dump_printf("# %s\n", (r_name + r_ptr->name)); /* Dump the monster attr/char info */ - fprintf(fff, "R:%d:0x%02X/0x%02X\n\n", i, + auto_dump_printf("R:%d:0x%02X/0x%02X\n\n", i, (byte)(r_ptr->x_attr), (byte)(r_ptr->x_char)); - line_num += 2; } /* Close */ - close_auto_dump(fff, mark, line_num); + close_auto_dump(); /* Message */ #ifdef JP @@ -3612,7 +3693,6 @@ void do_cmd_visuals(void) else if (i == '3') { static cptr mark = "Object attr/chars"; - int line_num; /* Prompt */ #ifdef JP @@ -3640,16 +3720,14 @@ void do_cmd_visuals(void) path_build(buf, sizeof(buf), ANGBAND_DIR_USER, tmp); /* Append to the file */ - fff = open_auto_dump(buf, mark, &line_num); - if (!fff) continue; + if (!open_auto_dump(buf, mark)) continue; /* Start dumping */ #ifdef JP - fprintf(fff, "\n# ¥¢¥¤¥Æ¥à¤Î[¿§/ʸ»ú]¤ÎÀßÄê\n\n"); + auto_dump_printf("\n# ¥¢¥¤¥Æ¥à¤Î[¿§/ʸ»ú]¤ÎÀßÄê\n\n"); #else - fprintf(fff, "\n# Object attr/char definitions\n\n"); + auto_dump_printf("\n# Object attr/char definitions\n\n"); #endif - line_num += 3; /* Dump objects */ for (i = 1; i < max_k_idx; i++) @@ -3667,17 +3745,15 @@ void do_cmd_visuals(void) strip_name(o_name, i); /* Dump a comment */ - fprintf(fff, "# %s\n", o_name); - line_num++; + auto_dump_printf("# %s\n", o_name); /* Dump the object attr/char info */ - fprintf(fff, "K:%d:0x%02X/0x%02X\n\n", i, + auto_dump_printf("K:%d:0x%02X/0x%02X\n\n", i, (byte)(k_ptr->x_attr), (byte)(k_ptr->x_char)); - line_num += 2; } /* Close */ - close_auto_dump(fff, mark, line_num); + close_auto_dump(); /* Message */ #ifdef JP @@ -3692,7 +3768,6 @@ void do_cmd_visuals(void) else if (i == '4') { static cptr mark = "Feature attr/chars"; - int line_num; /* Prompt */ #ifdef JP @@ -3720,16 +3795,14 @@ void do_cmd_visuals(void) path_build(buf, sizeof(buf), ANGBAND_DIR_USER, tmp); /* Append to the file */ - fff = open_auto_dump(buf, mark, &line_num); - if (!fff) continue; + if (!open_auto_dump(buf, mark)) continue; /* Start dumping */ #ifdef JP - fprintf(fff, "\n# ÃÏ·Á¤Î[¿§/ʸ»ú]¤ÎÀßÄê\n\n"); + auto_dump_printf("\n# ÃÏ·Á¤Î[¿§/ʸ»ú]¤ÎÀßÄê\n\n"); #else - fprintf(fff, "\n# Feature attr/char definitions\n\n"); + auto_dump_printf("\n# Feature attr/char definitions\n\n"); #endif - line_num += 3; /* Dump features */ for (i = 1; i < max_f_idx; i++) @@ -3743,17 +3816,15 @@ void do_cmd_visuals(void) if (f_ptr->mimic != i) continue; /* Dump a comment */ - fprintf(fff, "# %s\n", (f_name + f_ptr->name)); - line_num++; + auto_dump_printf("# %s\n", (f_name + f_ptr->name)); /* Dump the feature attr/char info */ - fprintf(fff, "F:%d:0x%02X/0x%02X\n\n", i, + auto_dump_printf("F:%d:0x%02X/0x%02X\n\n", i, (byte)(f_ptr->x_attr), (byte)(f_ptr->x_char)); - line_num += 2; } /* Close */ - close_auto_dump(fff, mark, line_num); + close_auto_dump(); /* Message */ #ifdef JP @@ -4141,8 +4212,6 @@ void do_cmd_colors(void) { int i; - FILE *fff; - char tmp[160]; char buf[1024]; @@ -4243,7 +4312,6 @@ void do_cmd_colors(void) else if (i == '2') { static cptr mark = "Colors"; - int line_num; /* Prompt */ #ifdef JP @@ -4271,16 +4339,14 @@ void do_cmd_colors(void) path_build(buf, sizeof(buf), ANGBAND_DIR_USER, tmp); /* Append to the file */ - fff = open_auto_dump(buf, mark, &line_num); - if (!fff) continue; + if (!open_auto_dump(buf, mark)) continue; /* Start dumping */ #ifdef JP - fprintf(fff, "\n# ¥«¥é¡¼¤ÎÀßÄê\n\n"); + auto_dump_printf("\n# ¥«¥é¡¼¤ÎÀßÄê\n\n"); #else - fprintf(fff, "\n# Color redefinitions\n\n"); + auto_dump_printf("\n# Color redefinitions\n\n"); #endif - line_num += 3; /* Dump colors */ for (i = 0; i < 256; i++) @@ -4305,20 +4371,18 @@ void do_cmd_colors(void) /* Dump a comment */ #ifdef JP - fprintf(fff, "# ¥«¥é¡¼ '%s'\n", name); + auto_dump_printf("# ¥«¥é¡¼ '%s'\n", name); #else - fprintf(fff, "# Color '%s'\n", name); + auto_dump_printf("# Color '%s'\n", name); #endif - line_num++; /* Dump the monster attr/char info */ - fprintf(fff, "V:%d:0x%02X:0x%02X:0x%02X:0x%02X\n\n", + auto_dump_printf("V:%d:0x%02X:0x%02X:0x%02X:0x%02X\n\n", i, kv, rv, gv, bv); - line_num += 2; } /* Close */ - close_auto_dump(fff, mark, line_num); + close_auto_dump(); /* Message */ #ifdef JP @@ -5388,9 +5452,6 @@ void do_cmd_load_screen(void) Term_get_size(&wid, &hgt); - /* Hack -- drop permissions */ - safe_setuid_drop(); - /* Build the filename */ path_build(buf, sizeof(buf), ANGBAND_DIR_USER, "dump.txt"); @@ -5466,9 +5527,6 @@ void do_cmd_load_screen(void) /* Close it */ my_fclose(fff); - /* Hack -- grab permissions */ - safe_setuid_grab(); - /* Message */ #ifdef JP @@ -5731,7 +5789,7 @@ static void do_cmd_knowledge_inven(void) #ifdef JP msg_format("°ì»þ¥Õ¥¡¥¤¥ë %s ¤òºîÀ®¤Ç¤­¤Þ¤»¤ó¤Ç¤·¤¿¡£", file_name); #else - msg_format("Failed to create temporally file %s.", file_name); + msg_format("Failed to create temporary file %s.", file_name); #endif msg_print(NULL); return; @@ -5983,13 +6041,7 @@ static void do_cmd_save_screen_html(void) msg_print(NULL); - /* Hack -- drop permissions */ - safe_setuid_drop(); - do_cmd_save_screen_html_aux(buf, 1); - - /* Hack -- grab permissions */ - safe_setuid_grab(); } @@ -6068,10 +6120,6 @@ void do_cmd_save_screen(void) char buf[1024]; - - /* Hack -- drop permissions */ - safe_setuid_drop(); - /* Build the filename */ path_build(buf, sizeof(buf), ANGBAND_DIR_USER, "dump.txt"); @@ -6084,8 +6132,6 @@ void do_cmd_save_screen(void) /* Oops */ if (!fff) { - /* Hack -- grab permissions */ - safe_setuid_grab(); #ifdef JP msg_format("¥Õ¥¡¥¤¥ë %s ¤ò³«¤±¤Þ¤»¤ó¤Ç¤·¤¿¡£", buf); #else @@ -6151,9 +6197,6 @@ void do_cmd_save_screen(void) /* Close it */ my_fclose(fff); - /* Hack -- grab permissions */ - safe_setuid_grab(); - /* Message */ #ifdef JP msg_print("²èÌÌ(µ­Ç°»£±Æ)¤ò¥Õ¥¡¥¤¥ë¤Ë½ñ¤­½Ð¤·¤Þ¤·¤¿¡£"); diff --git a/src/files.c b/src/files.c index f805a556a..81563cef2 100644 --- a/src/files.c +++ b/src/files.c @@ -1203,18 +1203,12 @@ errr process_pref_file(cptr name) if (err1 > 0) return err1; - /* Drop priv's */ - safe_setuid_drop(); - /* Build the filename */ path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name); /* Process the user pref file */ err2 = process_pref_file_aux(buf, FALSE); - /* Grab priv's */ - safe_setuid_grab(); - /* User file does not exist, but read system pref file */ if (err2 < 0 && !err1) @@ -4831,9 +4825,6 @@ errr file_character(cptr name) FILE *fff = NULL; char buf[1024]; - /* Drop priv's */ - safe_setuid_drop(); - /* Build the filename */ path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name); @@ -4887,8 +4878,6 @@ errr file_character(cptr name) /* Close it */ my_fclose(fff); - /* Grab priv's */ - safe_setuid_grab(); /* Message */ #ifdef JP @@ -5524,9 +5513,6 @@ strcpy(tmp, "jhelp.hlp"); /* Close it */ my_fclose(fff); - /* Drop priv's */ - safe_setuid_drop(); - /* Build the filename */ path_build(buff, sizeof(buff), ANGBAND_DIR_USER, xtmp); @@ -5559,9 +5545,6 @@ msg_print(" my_fclose(fff); my_fclose(ffp); - /* Grab priv's */ - safe_setuid_grab(); - /* Hack -- Re-Open the file */ fff = my_fopen(path, "r"); } @@ -6135,9 +6118,15 @@ static void make_bones(void) /* File type is "TEXT" */ FILE_TYPE(FILE_TYPE_TEXT); + /* Grab permissions */ + safe_setuid_grab(); + /* Try to write a new "Bones File" */ fp = my_fopen(str, "w"); + /* Drop permissions */ + safe_setuid_drop(); + /* Not allowed to write it? Weird. */ if (!fp) return; @@ -6697,9 +6686,14 @@ void close_game(void) /* Build the filename */ path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw"); + /* Grab permissions */ + safe_setuid_grab(); + /* Open the high score file, for reading/writing */ highscore_fd = fd_open(buf, O_RDWR); + /* Drop permissions */ + safe_setuid_drop(); /* Handle death */ if (p_ptr->is_dead) @@ -7054,17 +7048,11 @@ errr process_pickpref_file(cptr name) errr err = 0; - /* Drop priv's */ - safe_setuid_drop(); - /* Build the filename */ path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name); err = process_pref_file_aux(buf, TRUE); - /* Grab priv's */ - safe_setuid_grab(); - /* Result */ return (err); } @@ -7135,13 +7123,22 @@ errr counts_write(int where, u32b count) { int fd; char buf[1024]; + errr err; #ifdef JP path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, "z_info_j.raw"); #else path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, "z_info.raw"); #endif + + /* Grab permissions */ + safe_setuid_grab(); + fd = fd_open(buf, O_RDWR); + + /* Drop permissions */ + safe_setuid_drop(); + if (fd < 0) { /* File type is "DATA" */ @@ -7151,12 +7148,28 @@ errr counts_write(int where, u32b count) fd = fd_make(buf, 0644); } - if (fd_lock(fd, F_WRLCK)) return 1; + /* Grab permissions */ + safe_setuid_grab(); + + err = fd_lock(fd, F_WRLCK); + + /* Drop permissions */ + safe_setuid_drop(); + + if (err) return 1; counts_seek(fd, where, TRUE); fd_write(fd, (char*)(&count), sizeof(u32b)); - if (fd_lock(fd, F_UNLCK)) return 1; + /* Grab permissions */ + safe_setuid_grab(); + + err = fd_lock(fd, F_UNLCK); + + /* Drop permissions */ + safe_setuid_drop(); + + if (err) return 1; (void)fd_close(fd); diff --git a/src/floors.c b/src/floors.c index 243b5e809..b7e8de029 100644 --- a/src/floors.c +++ b/src/floors.c @@ -45,9 +45,15 @@ void init_saved_floors(void) /* File name */ sprintf(floor_savefile, "%s.F%02d", savefile, i); + /* Grab permissions */ + safe_setuid_grab(); + /* Try to create the file */ fd = fd_make(floor_savefile, mode); + /* Drop permissions */ + safe_setuid_drop(); + /* Failed! */ if (fd < 0) { @@ -75,9 +81,15 @@ void init_saved_floors(void) (void)fd_close(fd); } + /* Grab permissions */ + safe_setuid_grab(); + /* Simply kill the temporal file */ (void)fd_kill(floor_savefile); + /* Drop permissions */ + safe_setuid_drop(); + sf_ptr->floor_id = 0; } @@ -133,8 +145,14 @@ void clear_saved_floor_files(void) /* File name */ sprintf(floor_savefile, "%s.F%02d", savefile, i); + /* Grab permissions */ + safe_setuid_grab(); + /* Simply kill the temporal file */ (void)fd_kill(floor_savefile); + + /* Drop permissions */ + safe_setuid_drop(); } #ifdef SET_UID @@ -190,8 +208,14 @@ static void kill_saved_floor(saved_floor_type *sf_ptr) /* File name */ sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id); + /* Grab permissions */ + safe_setuid_grab(); + /* Simply kill the temporal file */ (void)fd_kill(floor_savefile); + + /* Drop permissions */ + safe_setuid_drop(); } /* No longer exists */ diff --git a/src/init2.c b/src/init2.c index 36501c9c6..252625e52 100644 --- a/src/init2.c +++ b/src/init2.c @@ -551,19 +551,25 @@ static errr init_info(cptr filename, header *head, #endif + /* Grab permissions */ + safe_setuid_grab(); + /* Kill the old file */ (void)fd_kill(buf); /* Attempt to create the raw file */ fd = fd_make(buf, mode); + /* Drop permissions */ + safe_setuid_drop(); + /* Dump to the file */ if (fd >= 0) { /* Dump it */ fd_write(fd, (cptr)(head), head->head_size); - /* Dump the "*_info" array */ + /* Dump the "*_info" array */ fd_write(fd, head->info_ptr, head->info_size); /* Dump the "*_name" array */ @@ -1975,7 +1981,7 @@ void init_angband(void) { int fd = -1; - int mode = 0644; + int mode = 0664; FILE *fp; @@ -2066,9 +2072,15 @@ void init_angband(void) /* File type is "DATA" */ FILE_TYPE(FILE_TYPE_DATA); + /* Grab permissions */ + safe_setuid_grab(); + /* Create a new high score file */ fd = fd_make(buf, mode); + /* Drop permissions */ + safe_setuid_drop(); + /* Failure */ if (fd < 0) { diff --git a/src/load.c b/src/load.c index b72310560..2027c64c0 100644 --- a/src/load.c +++ b/src/load.c @@ -3632,9 +3632,15 @@ errr rd_savefile_new(void) { errr err; + /* Grab permissions */ + safe_setuid_grab(); + /* The savefile is a binary file */ fff = my_fopen(savefile, "rb"); + /* Drop permissions */ + safe_setuid_drop(); + /* Paranoia */ if (!fff) return (-1); @@ -3747,9 +3753,15 @@ bool load_floor(saved_floor_type *sf_ptr, u32b mode) /* floor savefile */ sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id); + /* Grab permissions */ + safe_setuid_grab(); + /* The savefile is a binary file */ fff = my_fopen(floor_savefile, "rb"); + /* Drop permissions */ + safe_setuid_drop(); + /* Couldn't read */ if (!fff) ok = FALSE; @@ -3765,8 +3777,14 @@ bool load_floor(saved_floor_type *sf_ptr, u32b mode) /* Close the file */ my_fclose(fff); + /* Grab permissions */ + safe_setuid_grab(); + /* Delete the file */ if (!(mode & SLF_NO_KILL)) (void)fd_kill(floor_savefile); + + /* Drop permissions */ + safe_setuid_drop(); } /* We have one file already opened */ diff --git a/src/main.c b/src/main.c index c9bf698aa..3ea3b3ab7 100644 --- a/src/main.c +++ b/src/main.c @@ -29,6 +29,9 @@ static void quit_hook(cptr s) { int j; + /* Unused */ + (void)s; + /* Scan windows */ for (j = 8 - 1; j >= 0; j--) { @@ -73,9 +76,6 @@ static void create_user_dir(void) char dirpath[1024]; char subdirpath[1024]; - /* Drop privs */ - safe_setuid_drop(); - /* Get an absolute path from the filename */ path_parse(dirpath, 1024, PRIVATE_USER_PATH); @@ -87,9 +87,6 @@ static void create_user_dir(void) /* Create the directory */ mkdir(subdirpath, 0700); - - /* Grab privs */ - safe_setuid_grab(); } #endif /* PRIVATE_USER_PATH */ @@ -364,6 +361,10 @@ int main(int argc, char *argv[]) #endif + /* Drop permissions */ + safe_setuid_drop(); + + #ifdef SET_UID /* Initialize the "time" checker */ @@ -615,9 +616,6 @@ int main(int argc, char *argv[]) quit_aux = quit_hook; - /* Drop privs (so X11 will work correctly) */ - safe_setuid_drop(); - #ifdef USE_XAW /* Attempt to use the "main-xaw.c" support */ @@ -769,10 +767,6 @@ int main(int argc, char *argv[]) #endif - /* Grab privs (dropped above for X11) */ - safe_setuid_grab(); - - /* Make sure we have a display! */ if (!done) quit("Unable to prepare any 'display module'!"); diff --git a/src/save.c b/src/save.c index f8e9c3007..eaa51462c 100644 --- a/src/save.c +++ b/src/save.c @@ -1454,18 +1454,30 @@ static bool save_player_aux(char *name) FILE_TYPE(FILE_TYPE_SAVE); + /* Grab permissions */ + safe_setuid_grab(); + /* Create the savefile */ fd = fd_make(name, mode); + /* Drop permissions */ + safe_setuid_drop(); + /* File is okay */ if (fd >= 0) { /* Close the "fd" */ (void)fd_close(fd); + /* Grab permissions */ + safe_setuid_grab(); + /* Open the savefile */ fff = my_fopen(name, "wb"); + /* Drop permissions */ + safe_setuid_drop(); + /* Successful open */ if (fff) { @@ -1476,8 +1488,14 @@ static bool save_player_aux(char *name) if (my_fclose(fff)) ok = FALSE; } + /* Grab permissions */ + safe_setuid_grab(); + /* Remove "broken" files */ if (!ok) (void)fd_kill(name); + + /* Drop permissions */ + safe_setuid_drop(); } @@ -1527,9 +1545,15 @@ bool save_player(void) strcat(safe, "n"); #endif /* VM */ + /* Grab permissions */ + safe_setuid_grab(); + /* Remove it */ fd_kill(safe); + /* Drop permissions */ + safe_setuid_drop(); + update_playtime(); /* Attempt to save the player */ @@ -1547,6 +1571,9 @@ bool save_player(void) strcat(temp, "o"); #endif /* VM */ + /* Grab permissions */ + safe_setuid_grab(); + /* Remove it */ fd_kill(temp); @@ -1559,6 +1586,9 @@ bool save_player(void) /* Remove preserved savefile */ fd_kill(temp); + /* Drop permissions */ + safe_setuid_drop(); + /* Hack -- Pretend the character was loaded */ character_loaded = TRUE; @@ -1568,9 +1598,15 @@ bool save_player(void) strcpy(temp, savefile); strcat(temp, ".lok"); + /* Grab permissions */ + safe_setuid_grab(); + /* Remove lock file */ fd_kill(temp); + /* Drop permissions */ + safe_setuid_drop(); + #endif /* Success */ diff --git a/src/scores.c b/src/scores.c index 908cef1d6..3fcbfc7a5 100644 --- a/src/scores.c +++ b/src/scores.c @@ -480,6 +480,8 @@ errr top_twenty(void) time_t ct = time((time_t*)0); + errr err; + /* Clear the record */ (void)WIPE(&the_score, high_score); @@ -541,14 +543,30 @@ errr top_twenty(void) strcpy(the_score.how, p_ptr->died_from); } + /* Grab permissions */ + safe_setuid_grab(); + /* Lock (for writing) the highscore file, or fail */ - if (fd_lock(highscore_fd, F_WRLCK)) return (1); + err = fd_lock(highscore_fd, F_WRLCK); + + /* Drop permissions */ + safe_setuid_drop(); + + if (err) return (1); /* Add a new entry to the score list, see where it went */ j = highscore_add(&the_score); + /* Grab permissions */ + safe_setuid_grab(); + /* Unlock the highscore file, or fail */ - if (fd_lock(highscore_fd, F_UNLCK)) return (1); + err = fd_lock(highscore_fd, F_UNLCK); + + /* Drop permissions */ + safe_setuid_drop(); + + if (err) return (1); /* Hack -- Display the top fifteen scores */ diff --git a/src/wizard1.c b/src/wizard1.c index 046d1ca63..f37958737 100644 --- a/src/wizard1.c +++ b/src/wizard1.c @@ -2129,10 +2129,6 @@ void do_cmd_spoilers(void) screen_save(); - /* Drop priv's */ - safe_setuid_drop(); - - /* Interact */ while (1) { @@ -2200,10 +2196,6 @@ prt(" } - /* Grab priv's */ - safe_setuid_grab(); - - /* Restore the screen */ screen_load(); } @@ -2325,9 +2317,6 @@ void spoil_random_artifact(cptr fname) char buf[1024]; - /* Drop priv's */ - safe_setuid_drop(); - /* Build the filename */ path_build(buf, sizeof(buf), ANGBAND_DIR_USER, fname); @@ -2389,9 +2378,6 @@ void spoil_random_artifact(cptr fname) return; } - /* Grab priv's */ - safe_setuid_grab(); - /* Message */ msg_print("Successfully created a list file."); } -- 2.11.0