Auto-commit of generated files.
[bpt/emacs.git] / lib-src / update-game-score.c
CommitLineData
2f1de3dd 1/* update-game-score.c --- Update a score file
294981c7 2
95df8112 3Copyright (C) 2002-2011 Free Software Foundation, Inc.
294981c7
GM
4
5Author: Colin Walters <walters@debian.org>
2f1de3dd
CW
6
7This file is part of GNU Emacs.
8
294981c7 9GNU Emacs is free software: you can redistribute it and/or modify
2f1de3dd 10it under the terms of the GNU General Public License as published by
294981c7
GM
11the Free Software Foundation, either version 3 of the License, or
12(at your option) any later version.
2f1de3dd
CW
13
14GNU Emacs is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
294981c7
GM
20along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21
2f1de3dd 22
89cc1591 23/* This program allows a game to securely and atomically update a
8eec804f
CW
24 score file. It should be installed setuid, owned by an appropriate
25 user like `games'.
26
27 Alternatively, it can be compiled without HAVE_SHARED_GAME_DIR
28 defined, and in that case it will store scores in the user's home
29 directory (it should NOT be setuid).
2f1de3dd 30
294981c7 31 Created 2002/03/22.
2f1de3dd
CW
32*/
33
c42d6dbd
EZ
34#include <config.h>
35
2f1de3dd
CW
36#include <unistd.h>
37#include <errno.h>
5b400482 38#ifdef HAVE_STRING_H
2f1de3dd 39#include <string.h>
5b400482
DL
40#endif
41#ifdef HAVE_STDLIB_H
2f1de3dd 42#include <stdlib.h>
5b400482 43#endif
2f1de3dd
CW
44#include <stdio.h>
45#include <time.h>
cd553ffb 46#include <pwd.h>
2f1de3dd 47#include <ctype.h>
5b400482 48#ifdef HAVE_FCNTL_H
2f1de3dd 49#include <fcntl.h>
5b400482
DL
50#endif
51#ifdef STDC_HEADERS
8eec804f 52#include <stdarg.h>
5b400482 53#endif
2f1de3dd 54#include <sys/stat.h>
2f1de3dd 55
5b400482
DL
56/* Needed for SunOS4, for instance. */
57extern char *optarg;
58extern int optind, opterr;
59
b23b5a5b 60static int usage (int err) NO_RETURN;
68441b90 61
2f1de3dd 62#define MAX_ATTEMPTS 5
8eec804f
CW
63#define MAX_SCORES 200
64#define MAX_DATA_LEN 1024
5795b420 65
1d28afaf
DL
66#ifndef HAVE_DIFFTIME
67/* OK on POSIX (time_t is arithmetic type) modulo overflow in subtraction. */
68#define difftime(t1, t0) (double)((t1) - (t0))
69#endif
70
b23b5a5b 71static int
873fbd0b 72usage (int err)
2f1de3dd 73{
a5d107f3
RS
74 fprintf (stdout, "Usage: update-game-score [-m MAX ] [ -r ] game/scorefile SCORE DATA\n");
75 fprintf (stdout, " update-game-score -h\n");
76 fprintf (stdout, " -h\t\tDisplay this help.\n");
77 fprintf (stdout, " -m MAX\t\tLimit the maximum number of scores to MAX.\n");
78 fprintf (stdout, " -r\t\tSort the scores in increasing order.\n");
79 fprintf (stdout, " -d DIR\t\tStore scores in DIR (only if not setuid).\n");
80 exit (err);
2f1de3dd
CW
81}
82
b23b5a5b
PE
83static int lock_file (const char *filename, void **state);
84static int unlock_file (const char *filename, void *state);
2f1de3dd
CW
85
86struct score_entry
87{
88 long score;
cd553ffb 89 char *username;
2f1de3dd
CW
90 char *data;
91};
92
b23b5a5b
PE
93static int read_scores (const char *filename, struct score_entry **scores,
94 int *count);
95static int push_score (struct score_entry **scores, int *count,
96 int newscore, char *username, char *newdata);
97static void sort_scores (struct score_entry *scores, int count, int reverse);
98static int write_scores (const char *filename,
99 const struct score_entry *scores, int count);
cf398788 100
b23b5a5b 101static void lose (const char *msg) NO_RETURN;
cf398788 102
b23b5a5b 103static void
873fbd0b 104lose (const char *msg)
cf398788 105{
a5d107f3 106 fprintf (stderr, "%s\n", msg);
65396510 107 exit (EXIT_FAILURE);
cf398788 108}
2f1de3dd 109
b23b5a5b 110static void lose_syserr (const char *msg) NO_RETURN;
d99fabf0 111
1d28afaf
DL
112/* Taken from sysdep.c. */
113#ifndef HAVE_STRERROR
114#ifndef WINDOWSNT
115char *
116strerror (errnum)
117 int errnum;
118{
119 extern char *sys_errlist[];
120 extern int sys_nerr;
121
122 if (errnum >= 0 && errnum < sys_nerr)
123 return sys_errlist[errnum];
124 return (char *) "Unknown error";
125}
126#endif /* not WINDOWSNT */
127#endif /* ! HAVE_STRERROR */
128
b23b5a5b 129static void
873fbd0b 130lose_syserr (const char *msg)
d99fabf0 131{
a5d107f3 132 fprintf (stderr, "%s: %s\n", msg, strerror (errno));
65396510 133 exit (EXIT_FAILURE);
d99fabf0
CW
134}
135
b23b5a5b 136static char *
f57e2426 137get_user_id (void)
cd553ffb
CW
138{
139 char *name;
a5d107f3 140 struct passwd *buf = getpwuid (getuid ());
cd553ffb
CW
141 if (!buf)
142 {
143 int count = 1;
a5d107f3 144 int uid = (int) getuid ();
8eec804f
CW
145 int tuid = uid;
146 while (tuid /= 10)
cd553ffb 147 count++;
a5d107f3 148 name = malloc (count+1);
d99fabf0
CW
149 if (!name)
150 return NULL;
a5d107f3 151 sprintf (name, "%d", uid);
cd553ffb
CW
152 return name;
153 }
154 return buf->pw_name;
155}
156
b23b5a5b 157static const char *
988e88ab 158get_prefix (int running_suid, const char *user_prefix)
8eec804f 159{
d99fabf0 160 if (!running_suid && user_prefix == NULL)
a5d107f3 161 lose ("Not using a shared game directory, and no prefix given.");
d99fabf0
CW
162 if (running_suid)
163 {
164#ifdef HAVE_SHARED_GAME_DIR
165 return HAVE_SHARED_GAME_DIR;
166#else
a5d107f3 167 lose ("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.");
d99fabf0
CW
168#endif
169 }
170 return user_prefix;
8eec804f
CW
171}
172
2f1de3dd 173int
873fbd0b 174main (int argc, char **argv)
2f1de3dd 175{
d99fabf0 176 int c, running_suid;
2f1de3dd 177 void *lockstate;
988e88ab
J
178 char *user_id, *scorefile;
179 const char *prefix, *user_prefix = NULL;
2f1de3dd
CW
180 struct stat buf;
181 struct score_entry *scores;
8eec804f 182 int newscore, scorecount, reverse = 0, max = MAX_SCORES;
2f1de3dd
CW
183 char *newdata;
184
a5d107f3 185 srand (time (0));
2f1de3dd 186
a5d107f3 187 while ((c = getopt (argc, argv, "hrm:d:")) != -1)
2f1de3dd
CW
188 switch (c)
189 {
190 case 'h':
65396510 191 usage (EXIT_SUCCESS);
2f1de3dd 192 break;
d99fabf0
CW
193 case 'd':
194 user_prefix = optarg;
195 break;
2f1de3dd
CW
196 case 'r':
197 reverse = 1;
198 break;
199 case 'm':
a5d107f3 200 max = atoi (optarg);
8eec804f
CW
201 if (max > MAX_SCORES)
202 max = MAX_SCORES;
2f1de3dd
CW
203 break;
204 default:
65396510 205 usage (EXIT_FAILURE);
2f1de3dd
CW
206 }
207
208 if (optind+3 != argc)
65396510 209 usage (EXIT_FAILURE);
5795b420 210
a5d107f3 211 running_suid = (getuid () != geteuid ());
5795b420 212
a5d107f3 213 prefix = get_prefix (running_suid, user_prefix);
8eec804f 214
a5d107f3 215 scorefile = malloc (strlen (prefix) + strlen (argv[optind]) + 2);
2f1de3dd 216 if (!scorefile)
a5d107f3 217 lose_syserr ("Couldn't allocate score file");
8eec804f 218
a5d107f3
RS
219 strcpy (scorefile, prefix);
220 strcat (scorefile, "/");
221 strcat (scorefile, argv[optind]);
222 newscore = atoi (argv[optind+1]);
2f1de3dd 223 newdata = argv[optind+2];
a5d107f3 224 if (strlen (newdata) > MAX_DATA_LEN)
8eec804f 225 newdata[MAX_DATA_LEN] = '\0';
d99fabf0 226
00a7e408
RS
227 user_id = get_user_id ();
228 if (user_id == NULL)
a5d107f3 229 lose_syserr ("Couldn't determine user id");
177c0ea7 230
a5d107f3
RS
231 if (stat (scorefile, &buf) < 0)
232 lose_syserr ("Failed to access scores file");
177c0ea7 233
a5d107f3
RS
234 if (lock_file (scorefile, &lockstate) < 0)
235 lose_syserr ("Failed to lock scores file");
177c0ea7 236
a5d107f3 237 if (read_scores (scorefile, &scores, &scorecount) < 0)
2f1de3dd 238 {
a5d107f3
RS
239 unlock_file (scorefile, lockstate);
240 lose_syserr ("Failed to read scores file");
2f1de3dd 241 }
a5d107f3 242 push_score (&scores, &scorecount, newscore, user_id, newdata);
24e9e996 243 sort_scores (scores, scorecount, reverse);
8eec804f 244 /* Limit the number of scores. If we're using reverse sorting, then
f0d80d43
PE
245 also increment the beginning of the array, to skip over the
246 *smallest* scores. Otherwise, just decrementing the number of
247 scores suffices, since the smallest is at the end. */
8eec804f 248 if (scorecount > MAX_SCORES)
f0d80d43
PE
249 {
250 if (reverse)
251 scores += (scorecount - MAX_SCORES);
252 scorecount = MAX_SCORES;
253 }
a5d107f3 254 if (write_scores (scorefile, scores, scorecount) < 0)
2f1de3dd 255 {
a5d107f3
RS
256 unlock_file (scorefile, lockstate);
257 lose_syserr ("Failed to write scores file");
2f1de3dd 258 }
a5d107f3 259 unlock_file (scorefile, lockstate);
65396510 260 exit (EXIT_SUCCESS);
2f1de3dd
CW
261}
262
b23b5a5b 263static int
873fbd0b 264read_score (FILE *f, struct score_entry *score)
2f1de3dd
CW
265{
266 int c;
a5d107f3 267 if (feof (f))
2f1de3dd 268 return 1;
a5d107f3
RS
269 while ((c = getc (f)) != EOF
270 && isdigit (c))
cd553ffb
CW
271 {
272 score->score *= 10;
273 score->score += (c-48);
274 }
a5d107f3
RS
275 while ((c = getc (f)) != EOF
276 && isspace (c))
cd553ffb
CW
277 ;
278 if (c == EOF)
279 return -1;
a5d107f3 280 ungetc (c, f);
cd553ffb
CW
281#ifdef HAVE_GETDELIM
282 {
7605f1bd 283 size_t count = 0;
a5d107f3 284 if (getdelim (&score->username, &count, ' ', f) < 1
cd553ffb
CW
285 || score->username == NULL)
286 return -1;
1d4328ff 287 /* Trim the space */
a5d107f3 288 score->username[strlen (score->username)-1] = '\0';
2f1de3dd 289 }
cd553ffb
CW
290#else
291 {
292 int unameread = 0;
293 int unamelen = 30;
a5d107f3 294 char *username = malloc (unamelen);
f5bceaf8
CW
295 if (!username)
296 return -1;
177c0ea7 297
a5d107f3
RS
298 while ((c = getc (f)) != EOF
299 && !isspace (c))
cd553ffb 300 {
8eec804f 301 if (unameread >= unamelen-1)
a5d107f3 302 if (!(username = realloc (username, unamelen *= 2)))
8eec804f 303 return -1;
cd553ffb
CW
304 username[unameread] = c;
305 unameread++;
306 }
177c0ea7 307 if (c == EOF)
f5bceaf8
CW
308 return -1;
309 username[unameread] = '\0';
cd553ffb
CW
310 score->username = username;
311 }
312#endif
2f1de3dd
CW
313#ifdef HAVE_GETLINE
314 score->data = NULL;
7605f1bd 315 errno = 0;
2f1de3dd 316 {
7605f1bd 317 size_t len;
a5d107f3 318 if (getline (&score->data, &len, f) < 0)
2f1de3dd 319 return -1;
a5d107f3 320 score->data[strlen (score->data)-1] = '\0';
2f1de3dd
CW
321 }
322#else
cd553ffb
CW
323 {
324 int cur = 0;
325 int len = 16;
a5d107f3 326 char *buf = malloc (len);
cd553ffb
CW
327 if (!buf)
328 return -1;
a5d107f3 329 while ((c = getc (f)) != EOF
f5bceaf8 330 && c != '\n')
cd553ffb
CW
331 {
332 if (cur >= len-1)
333 {
a5d107f3 334 if (!(buf = realloc (buf, len *= 2)))
cd553ffb
CW
335 return -1;
336 }
337 buf[cur] = c;
338 cur++;
339 }
340 score->data = buf;
5795b420 341 score->data[cur] = '\0';
cd553ffb 342 }
2f1de3dd 343#endif
2f1de3dd
CW
344 return 0;
345}
346
b23b5a5b 347static int
873fbd0b 348read_scores (const char *filename, struct score_entry **scores, int *count)
2f1de3dd
CW
349{
350 int readval, scorecount, cursize;
351 struct score_entry *ret;
a5d107f3 352 FILE *f = fopen (filename, "r");
177c0ea7 353 if (!f)
2f1de3dd
CW
354 return -1;
355 scorecount = 0;
356 cursize = 16;
00a7e408 357 ret = (struct score_entry *) malloc (sizeof (struct score_entry) * cursize);
177c0ea7 358 if (!ret)
2f1de3dd 359 return -1;
a5d107f3 360 while ((readval = read_score (f, &ret[scorecount])) == 0)
2f1de3dd
CW
361 {
362 /* We encoutered an error */
363 if (readval < 0)
364 return -1;
365 scorecount++;
366 if (scorecount >= cursize)
367 {
ee435bae
JB
368 cursize *= 2;
369 ret = (struct score_entry *)
370 realloc (ret, (sizeof (struct score_entry) * cursize));
2f1de3dd
CW
371 if (!ret)
372 return -1;
373 }
374 }
375 *count = scorecount;
376 *scores = ret;
377 return 0;
378}
379
b23b5a5b 380static int
873fbd0b 381score_compare (const void *a, const void *b)
2f1de3dd
CW
382{
383 const struct score_entry *sa = (const struct score_entry *) a;
384 const struct score_entry *sb = (const struct score_entry *) b;
385 return (sb->score > sa->score) - (sb->score < sa->score);
386}
387
b23b5a5b 388static int
873fbd0b 389score_compare_reverse (const void *a, const void *b)
2f1de3dd
CW
390{
391 const struct score_entry *sa = (const struct score_entry *) a;
392 const struct score_entry *sb = (const struct score_entry *) b;
393 return (sa->score > sb->score) - (sa->score < sb->score);
394}
395
396int
873fbd0b 397push_score (struct score_entry **scores, int *count, int newscore, char *username, char *newdata)
2f1de3dd 398{
a5d107f3 399 struct score_entry *newscores
00a7e408
RS
400 = (struct score_entry *) realloc (*scores,
401 sizeof (struct score_entry) * ((*count) + 1));
2f1de3dd
CW
402 if (!newscores)
403 return -1;
404 newscores[*count].score = newscore;
cd553ffb 405 newscores[*count].username = username;
2f1de3dd
CW
406 newscores[*count].data = newdata;
407 (*count) += 1;
408 *scores = newscores;
409 return 0;
410}
177c0ea7 411
b23b5a5b 412static void
873fbd0b 413sort_scores (struct score_entry *scores, int count, int reverse)
2f1de3dd 414{
a5d107f3 415 qsort (scores, count, sizeof (struct score_entry),
2f1de3dd
CW
416 reverse ? score_compare_reverse : score_compare);
417}
418
b23b5a5b 419static int
873fbd0b 420write_scores (const char *filename, const struct score_entry *scores, int count)
2f1de3dd 421{
177c0ea7 422 FILE *f;
2f1de3dd 423 int i;
a5d107f3 424 char *tempfile = malloc (strlen (filename) + strlen (".tempXXXXXX") + 1);
2f1de3dd
CW
425 if (!tempfile)
426 return -1;
a5d107f3
RS
427 strcpy (tempfile, filename);
428 strcat (tempfile, ".tempXXXXXX");
8eec804f 429#ifdef HAVE_MKSTEMP
a5d107f3 430 if (mkstemp (tempfile) < 0
8eec804f 431#else
a5d107f3 432 if (mktemp (tempfile) != tempfile
8eec804f 433#endif
a5d107f3 434 || !(f = fopen (tempfile, "w")))
2f1de3dd
CW
435 return -1;
436 for (i = 0; i < count; i++)
a5d107f3 437 if (fprintf (f, "%ld %s %s\n", scores[i].score, scores[i].username,
cd553ffb 438 scores[i].data) < 0)
2f1de3dd 439 return -1;
a5d107f3
RS
440 fclose (f);
441 if (rename (tempfile, filename) < 0)
2f1de3dd 442 return -1;
a5d107f3 443 if (chmod (filename, 0644) < 0)
2f1de3dd 444 return -1;
8eec804f 445 return 0;
2f1de3dd 446}
177c0ea7 447
b23b5a5b 448static int
873fbd0b 449lock_file (const char *filename, void **state)
2f1de3dd
CW
450{
451 int fd;
7c4f6873 452 struct stat buf;
2f1de3dd 453 int attempts = 0;
988e88ab 454 const char *lockext = ".lockfile";
a5d107f3 455 char *lockpath = malloc (strlen (filename) + strlen (lockext) + 60);
2f1de3dd
CW
456 if (!lockpath)
457 return -1;
a5d107f3
RS
458 strcpy (lockpath, filename);
459 strcat (lockpath, lockext);
2f1de3dd
CW
460 *state = lockpath;
461 trylock:
462 attempts++;
7c4f6873 463 /* If the lock is over an hour old, delete it. */
a5d107f3
RS
464 if (stat (lockpath, &buf) == 0
465 && (difftime (buf.st_ctime, time (NULL) > 60*60)))
466 unlink (lockpath);
00a7e408
RS
467 fd = open (lockpath, O_CREAT | O_EXCL, 0600);
468 if (fd < 0)
2f1de3dd
CW
469 {
470 if (errno == EEXIST)
471 {
472 /* Break the lock; we won't corrupt the file, but we might
473 lose some scores. */
474 if (attempts > MAX_ATTEMPTS)
7c4f6873 475 {
a5d107f3 476 unlink (lockpath);
7c4f6873
CW
477 attempts = 0;
478 }
a5d107f3 479 sleep ((rand () % 2)+1);
2f1de3dd
CW
480 goto trylock;
481 }
482 else
483 return -1;
484 }
a5d107f3 485 close (fd);
2f1de3dd
CW
486 return 0;
487}
177c0ea7 488
b23b5a5b 489static int
873fbd0b 490unlock_file (const char *filename, void *state)
2f1de3dd
CW
491{
492 char *lockpath = (char *) state;
a5d107f3 493 int ret = unlink (lockpath);
2f1de3dd 494 int saved_errno = errno;
a5d107f3 495 free (lockpath);
2f1de3dd
CW
496 errno = saved_errno;
497 return ret;
498}
ab5796a9 499
65396510
TTN
500
501/* update-game-score.c ends here */