1 /* update-game-score.c --- Update a score file
3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 Author: Colin Walters <walters@debian.org>
8 This file is part of GNU Emacs.
10 GNU Emacs is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 GNU Emacs is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
24 /* This program allows a game to securely and atomically update a
25 score file. It should be installed setuid, owned by an appropriate
28 Alternatively, it can be compiled without HAVE_SHARED_GAME_DIR
29 defined, and in that case it will store scores in the user's home
30 directory (it should NOT be setuid).
59 /* Needed for SunOS4, for instance. */
61 extern int optind
, opterr
;
63 #define MAX_ATTEMPTS 5
64 #define MAX_SCORES 200
65 #define MAX_DATA_LEN 1024
67 /* Declare the prototype for a general external function. */
68 #if defined (PROTOTYPES) || defined (WINDOWSNT)
69 #define P_(proto) proto
75 /* OK on POSIX (time_t is arithmetic type) modulo overflow in subtraction. */
76 #define difftime(t1, t0) (double)((t1) - (t0))
82 fprintf (stdout
, "Usage: update-game-score [-m MAX ] [ -r ] game/scorefile SCORE DATA\n");
83 fprintf (stdout
, " update-game-score -h\n");
84 fprintf (stdout
, " -h\t\tDisplay this help.\n");
85 fprintf (stdout
, " -m MAX\t\tLimit the maximum number of scores to MAX.\n");
86 fprintf (stdout
, " -r\t\tSort the scores in increasing order.\n");
87 fprintf (stdout
, " -d DIR\t\tStore scores in DIR (only if not setuid).\n");
91 int lock_file (const char *filename
, void **state
);
92 int unlock_file (const char *filename
, void *state
);
101 int read_scores (const char *filename
, struct score_entry
**scores
,
103 int push_score (struct score_entry
**scores
, int *count
,
104 int newscore
, char *username
, char *newdata
);
105 void sort_scores (struct score_entry
*scores
, int count
, int reverse
);
106 int write_scores (const char *filename
, const struct score_entry
*scores
,
109 void lose (const char *msg
) NO_RETURN
;
112 lose (const char *msg
)
114 fprintf (stderr
, "%s\n", msg
);
118 void lose_syserr (const char *msg
) NO_RETURN
;
120 /* Taken from sysdep.c. */
121 #ifndef HAVE_STRERROR
127 extern char *sys_errlist
[];
130 if (errnum
>= 0 && errnum
< sys_nerr
)
131 return sys_errlist
[errnum
];
132 return (char *) "Unknown error";
134 #endif /* not WINDOWSNT */
135 #endif /* ! HAVE_STRERROR */
138 lose_syserr (const char *msg
)
140 fprintf (stderr
, "%s: %s\n", msg
, strerror (errno
));
148 struct passwd
*buf
= getpwuid (getuid ());
152 int uid
= (int) getuid ();
156 name
= malloc (count
+1);
159 sprintf (name
, "%d", uid
);
166 get_prefix (int running_suid
, char *user_prefix
)
168 if (!running_suid
&& user_prefix
== NULL
)
169 lose ("Not using a shared game directory, and no prefix given.");
172 #ifdef HAVE_SHARED_GAME_DIR
173 return HAVE_SHARED_GAME_DIR
;
175 lose ("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.");
182 main (int argc
, char **argv
)
186 char *user_id
, *scorefile
, *prefix
, *user_prefix
= NULL
;
188 struct score_entry
*scores
;
189 int newscore
, scorecount
, reverse
= 0, max
= MAX_SCORES
;
194 while ((c
= getopt (argc
, argv
, "hrm:d:")) != -1)
198 usage (EXIT_SUCCESS
);
201 user_prefix
= optarg
;
208 if (max
> MAX_SCORES
)
212 usage (EXIT_FAILURE
);
215 if (optind
+3 != argc
)
216 usage (EXIT_FAILURE
);
218 running_suid
= (getuid () != geteuid ());
220 prefix
= get_prefix (running_suid
, user_prefix
);
222 scorefile
= malloc (strlen (prefix
) + strlen (argv
[optind
]) + 2);
224 lose_syserr ("Couldn't allocate score file");
226 strcpy (scorefile
, prefix
);
227 strcat (scorefile
, "/");
228 strcat (scorefile
, argv
[optind
]);
229 newscore
= atoi (argv
[optind
+1]);
230 newdata
= argv
[optind
+2];
231 if (strlen (newdata
) > MAX_DATA_LEN
)
232 newdata
[MAX_DATA_LEN
] = '\0';
234 user_id
= get_user_id ();
236 lose_syserr ("Couldn't determine user id");
238 if (stat (scorefile
, &buf
) < 0)
239 lose_syserr ("Failed to access scores file");
241 if (lock_file (scorefile
, &lockstate
) < 0)
242 lose_syserr ("Failed to lock scores file");
244 if (read_scores (scorefile
, &scores
, &scorecount
) < 0)
246 unlock_file (scorefile
, lockstate
);
247 lose_syserr ("Failed to read scores file");
249 push_score (&scores
, &scorecount
, newscore
, user_id
, newdata
);
250 sort_scores (scores
, scorecount
, reverse
);
251 /* Limit the number of scores. If we're using reverse sorting, then
252 we should increment the beginning of the array, to skip over the
253 *smallest* scores. Otherwise, we just decrement the number of
254 scores, since the smallest will be at the end. */
255 if (scorecount
> MAX_SCORES
)
256 scorecount
-= (scorecount
- MAX_SCORES
);
258 scores
+= (scorecount
- MAX_SCORES
);
259 if (write_scores (scorefile
, scores
, scorecount
) < 0)
261 unlock_file (scorefile
, lockstate
);
262 lose_syserr ("Failed to write scores file");
264 unlock_file (scorefile
, lockstate
);
269 read_score (FILE *f
, struct score_entry
*score
)
274 while ((c
= getc (f
)) != EOF
278 score
->score
+= (c
-48);
280 while ((c
= getc (f
)) != EOF
289 if (getdelim (&score
->username
, &count
, ' ', f
) < 1
290 || score
->username
== NULL
)
293 score
->username
[strlen (score
->username
)-1] = '\0';
299 char *username
= malloc (unamelen
);
303 while ((c
= getc (f
)) != EOF
306 if (unameread
>= unamelen
-1)
307 if (!(username
= realloc (username
, unamelen
*= 2)))
309 username
[unameread
] = c
;
314 username
[unameread
] = '\0';
315 score
->username
= username
;
323 if (getline (&score
->data
, &len
, f
) < 0)
325 score
->data
[strlen (score
->data
)-1] = '\0';
331 char *buf
= malloc (len
);
334 while ((c
= getc (f
)) != EOF
339 if (!(buf
= realloc (buf
, len
*= 2)))
346 score
->data
[cur
] = '\0';
353 read_scores (const char *filename
, struct score_entry
**scores
, int *count
)
355 int readval
, scorecount
, cursize
;
356 struct score_entry
*ret
;
357 FILE *f
= fopen (filename
, "r");
362 ret
= (struct score_entry
*) malloc (sizeof (struct score_entry
) * cursize
);
365 while ((readval
= read_score (f
, &ret
[scorecount
])) == 0)
367 /* We encoutered an error */
371 if (scorecount
>= cursize
)
374 ret
= (struct score_entry
*)
375 realloc (ret
, (sizeof (struct score_entry
) * cursize
));
386 score_compare (const void *a
, const void *b
)
388 const struct score_entry
*sa
= (const struct score_entry
*) a
;
389 const struct score_entry
*sb
= (const struct score_entry
*) b
;
390 return (sb
->score
> sa
->score
) - (sb
->score
< sa
->score
);
394 score_compare_reverse (const void *a
, const void *b
)
396 const struct score_entry
*sa
= (const struct score_entry
*) a
;
397 const struct score_entry
*sb
= (const struct score_entry
*) b
;
398 return (sa
->score
> sb
->score
) - (sa
->score
< sb
->score
);
402 push_score (struct score_entry
**scores
, int *count
, int newscore
, char *username
, char *newdata
)
404 struct score_entry
*newscores
405 = (struct score_entry
*) realloc (*scores
,
406 sizeof (struct score_entry
) * ((*count
) + 1));
409 newscores
[*count
].score
= newscore
;
410 newscores
[*count
].username
= username
;
411 newscores
[*count
].data
= newdata
;
418 sort_scores (struct score_entry
*scores
, int count
, int reverse
)
420 qsort (scores
, count
, sizeof (struct score_entry
),
421 reverse
? score_compare_reverse
: score_compare
);
425 write_scores (const char *filename
, const struct score_entry
*scores
, int count
)
429 char *tempfile
= malloc (strlen (filename
) + strlen (".tempXXXXXX") + 1);
432 strcpy (tempfile
, filename
);
433 strcat (tempfile
, ".tempXXXXXX");
435 if (mkstemp (tempfile
) < 0
437 if (mktemp (tempfile
) != tempfile
439 || !(f
= fopen (tempfile
, "w")))
441 for (i
= 0; i
< count
; i
++)
442 if (fprintf (f
, "%ld %s %s\n", scores
[i
].score
, scores
[i
].username
,
446 if (rename (tempfile
, filename
) < 0)
448 if (chmod (filename
, 0644) < 0)
454 lock_file (const char *filename
, void **state
)
459 char *lockext
= ".lockfile";
460 char *lockpath
= malloc (strlen (filename
) + strlen (lockext
) + 60);
463 strcpy (lockpath
, filename
);
464 strcat (lockpath
, lockext
);
468 /* If the lock is over an hour old, delete it. */
469 if (stat (lockpath
, &buf
) == 0
470 && (difftime (buf
.st_ctime
, time (NULL
) > 60*60)))
472 fd
= open (lockpath
, O_CREAT
| O_EXCL
, 0600);
477 /* Break the lock; we won't corrupt the file, but we might
479 if (attempts
> MAX_ATTEMPTS
)
484 sleep ((rand () % 2)+1);
495 unlock_file (const char *filename
, void *state
)
497 char *lockpath
= (char *) state
;
498 int ret
= unlink (lockpath
);
499 int saved_errno
= errno
;
505 /* arch-tag: 2bf5c52e-4beb-463a-954e-c58b9c64736b
506 (do not change this comment) */
508 /* update-game-score.c ends here */