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