comparison srogue/save.c @ 174:1863409c44cf

srogue: improve privilege handling. Setgid privileges are now dropped when not required. restore() is now less complex.
author John "Elwin" Edwards
date Sat, 01 Aug 2015 16:23:24 -0400
parents e6c8652473fe
children 85bd398cb96d
comparison
equal deleted inserted replaced
173:78fa1d0e5d25 174:1863409c44cf
129 byebye(3); 129 byebye(3);
130 } 130 }
131 131
132 /* 132 /*
133 * dosave: 133 * dosave:
134 * Set UID back to user and save the game 134 * Save the game. UID/GID no longer get reset here.
135 */ 135 */
136 dosave() 136 dosave()
137 { 137 {
138 FILE *savef; 138 FILE *savef;
139 139
140 ignore(); 140 ignore();
141 if (!use_savedir)
142 {
143 md_resetpriv();
144 }
145 umask(022); 141 umask(022);
146 142
147 if (file_name[0] != '\0') { 143 if (file_name[0] != '\0') {
148 if ((savef = fopen(file_name,"w")) != NULL) 144 if ((savef = fopen(file_name,"w")) != NULL)
149 { 145 {
285 #if defined(__CYGWIN__) || defined(__DJGPP__) 281 #if defined(__CYGWIN__) || defined(__DJGPP__)
286 close(inf); 282 close(inf);
287 #endif 283 #endif
288 if (!wizard) 284 if (!wizard)
289 { 285 {
290 #if defined(HAVE_WORKING_FORK)
291 endwin();
292 if (!use_savedir)
293 {
294 while((pid = fork()) < 0)
295 sleep(1);
296
297 /* set id to unlink file */
298 if(pid == 0)
299 {
300 md_resetpriv();
301 unlink(file);
302 exit(0);
303 }
304 /* wait for unlink to finish */
305 else
306 {
307 while(wait(&ret_status) != pid)
308 continue;
309 if (ret_status < 0)
310 {
311 printf("Cannot unlink file\n");
312 return FALSE;
313 }
314 }
315 }
316 else
317 {
318 /* Don't drop privileges, they're needed
319 * for the unlink. */
320 if (unlink(file) < 0)
321 {
322 printf("Cannot unlink file\n");
323 return FALSE;
324 }
325 }
326 #else
327 if (unlink(file) < 0) 286 if (unlink(file) < 0)
328 { 287 {
288 endwin();
329 printf("Cannot unlink file\n"); 289 printf("Cannot unlink file\n");
330 return FALSE; 290 return FALSE;
331 } 291 }
332 #endif
333
334 } 292 }
335 293
336 if (him->s_hpt <= 0) { 294 if (him->s_hpt <= 0) {
337 endwin(); 295 endwin();
338 printf("This character is already dead.\n"); 296 printf("This character is already dead.\n");