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 int usage (int err
) NO_RETURN
;
65 #define MAX_ATTEMPTS 5
66 #define MAX_SCORES 200
67 #define MAX_DATA_LEN 1024
70 /* OK on POSIX (time_t is arithmetic type) modulo overflow in subtraction. */
71 #define difftime(t1, t0) (double)((t1) - (t0))
77 fprintf (stdout
, "Usage: update-game-score [-m MAX ] [ -r ] game/scorefile SCORE DATA\n");
78 fprintf (stdout
, " update-game-score -h\n");
79 fprintf (stdout
, " -h\t\tDisplay this help.\n");
80 fprintf (stdout
, " -m MAX\t\tLimit the maximum number of scores to MAX.\n");
81 fprintf (stdout
, " -r\t\tSort the scores in increasing order.\n");
82 fprintf (stdout
, " -d DIR\t\tStore scores in DIR (only if not setuid).\n");
86 int lock_file (const char *filename
, void **state
);
87 int unlock_file (const char *filename
, void *state
);
96 int read_scores (const char *filename
, struct score_entry
**scores
,
98 int push_score (struct score_entry
**scores
, int *count
,
99 int newscore
, char *username
, char *newdata
);
100 void sort_scores (struct score_entry
*scores
, int count
, int reverse
);
101 int write_scores (const char *filename
, const struct score_entry
*scores
,
104 void lose (const char *msg
) NO_RETURN
;
107 lose (const char *msg
)
109 fprintf (stderr
, "%s\n", msg
);
113 void lose_syserr (const char *msg
) NO_RETURN
;
115 /* Taken from sysdep.c. */
116 #ifndef HAVE_STRERROR
122 extern char *sys_errlist
[];
125 if (errnum
>= 0 && errnum
< sys_nerr
)
126 return sys_errlist
[errnum
];
127 return (char *) "Unknown error";
129 #endif /* not WINDOWSNT */
130 #endif /* ! HAVE_STRERROR */
133 lose_syserr (const char *msg
)
135 fprintf (stderr
, "%s: %s\n", msg
, strerror (errno
));
143 struct passwd
*buf
= getpwuid (getuid ());
147 int uid
= (int) getuid ();
151 name
= malloc (count
+1);
154 sprintf (name
, "%d", uid
);
161 get_prefix (int running_suid
, const char *user_prefix
)
163 if (!running_suid
&& user_prefix
== NULL
)
164 lose ("Not using a shared game directory, and no prefix given.");
167 #ifdef HAVE_SHARED_GAME_DIR
168 return HAVE_SHARED_GAME_DIR
;
170 lose ("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.");
177 main (int argc
, char **argv
)
181 char *user_id
, *scorefile
;
182 const char *prefix
, *user_prefix
= NULL
;
184 struct score_entry
*scores
;
185 int newscore
, scorecount
, reverse
= 0, max
= MAX_SCORES
;
190 while ((c
= getopt (argc
, argv
, "hrm:d:")) != -1)
194 usage (EXIT_SUCCESS
);
197 user_prefix
= optarg
;
204 if (max
> MAX_SCORES
)
208 usage (EXIT_FAILURE
);
211 if (optind
+3 != argc
)
212 usage (EXIT_FAILURE
);
214 running_suid
= (getuid () != geteuid ());
216 prefix
= get_prefix (running_suid
, user_prefix
);
218 scorefile
= malloc (strlen (prefix
) + strlen (argv
[optind
]) + 2);
220 lose_syserr ("Couldn't allocate score file");
222 strcpy (scorefile
, prefix
);
223 strcat (scorefile
, "/");
224 strcat (scorefile
, argv
[optind
]);
225 newscore
= atoi (argv
[optind
+1]);
226 newdata
= argv
[optind
+2];
227 if (strlen (newdata
) > MAX_DATA_LEN
)
228 newdata
[MAX_DATA_LEN
] = '\0';
230 user_id
= get_user_id ();
232 lose_syserr ("Couldn't determine user id");
234 if (stat (scorefile
, &buf
) < 0)
235 lose_syserr ("Failed to access scores file");
237 if (lock_file (scorefile
, &lockstate
) < 0)
238 lose_syserr ("Failed to lock scores file");
240 if (read_scores (scorefile
, &scores
, &scorecount
) < 0)
242 unlock_file (scorefile
, lockstate
);
243 lose_syserr ("Failed to read scores file");
245 push_score (&scores
, &scorecount
, newscore
, user_id
, newdata
);
246 sort_scores (scores
, scorecount
, reverse
);
247 /* Limit the number of scores. If we're using reverse sorting, then
248 we should increment the beginning of the array, to skip over the
249 *smallest* scores. Otherwise, we just decrement the number of
250 scores, since the smallest will be at the end. */
251 if (scorecount
> MAX_SCORES
)
252 scorecount
-= (scorecount
- MAX_SCORES
);
254 scores
+= (scorecount
- MAX_SCORES
);
255 if (write_scores (scorefile
, scores
, scorecount
) < 0)
257 unlock_file (scorefile
, lockstate
);
258 lose_syserr ("Failed to write scores file");
260 unlock_file (scorefile
, lockstate
);
265 read_score (FILE *f
, struct score_entry
*score
)
270 while ((c
= getc (f
)) != EOF
274 score
->score
+= (c
-48);
276 while ((c
= getc (f
)) != EOF
285 if (getdelim (&score
->username
, &count
, ' ', f
) < 1
286 || score
->username
== NULL
)
289 score
->username
[strlen (score
->username
)-1] = '\0';
295 char *username
= malloc (unamelen
);
299 while ((c
= getc (f
)) != EOF
302 if (unameread
>= unamelen
-1)
303 if (!(username
= realloc (username
, unamelen
*= 2)))
305 username
[unameread
] = c
;
310 username
[unameread
] = '\0';
311 score
->username
= username
;
319 if (getline (&score
->data
, &len
, f
) < 0)
321 score
->data
[strlen (score
->data
)-1] = '\0';
327 char *buf
= malloc (len
);
330 while ((c
= getc (f
)) != EOF
335 if (!(buf
= realloc (buf
, len
*= 2)))
342 score
->data
[cur
] = '\0';
349 read_scores (const char *filename
, struct score_entry
**scores
, int *count
)
351 int readval
, scorecount
, cursize
;
352 struct score_entry
*ret
;
353 FILE *f
= fopen (filename
, "r");
358 ret
= (struct score_entry
*) malloc (sizeof (struct score_entry
) * cursize
);
361 while ((readval
= read_score (f
, &ret
[scorecount
])) == 0)
363 /* We encoutered an error */
367 if (scorecount
>= cursize
)
370 ret
= (struct score_entry
*)
371 realloc (ret
, (sizeof (struct score_entry
) * cursize
));
382 score_compare (const void *a
, const void *b
)
384 const struct score_entry
*sa
= (const struct score_entry
*) a
;
385 const struct score_entry
*sb
= (const struct score_entry
*) b
;
386 return (sb
->score
> sa
->score
) - (sb
->score
< sa
->score
);
390 score_compare_reverse (const void *a
, const void *b
)
392 const struct score_entry
*sa
= (const struct score_entry
*) a
;
393 const struct score_entry
*sb
= (const struct score_entry
*) b
;
394 return (sa
->score
> sb
->score
) - (sa
->score
< sb
->score
);
398 push_score (struct score_entry
**scores
, int *count
, int newscore
, char *username
, char *newdata
)
400 struct score_entry
*newscores
401 = (struct score_entry
*) realloc (*scores
,
402 sizeof (struct score_entry
) * ((*count
) + 1));
405 newscores
[*count
].score
= newscore
;
406 newscores
[*count
].username
= username
;
407 newscores
[*count
].data
= newdata
;
414 sort_scores (struct score_entry
*scores
, int count
, int reverse
)
416 qsort (scores
, count
, sizeof (struct score_entry
),
417 reverse
? score_compare_reverse
: score_compare
);
421 write_scores (const char *filename
, const struct score_entry
*scores
, int count
)
425 char *tempfile
= malloc (strlen (filename
) + strlen (".tempXXXXXX") + 1);
428 strcpy (tempfile
, filename
);
429 strcat (tempfile
, ".tempXXXXXX");
431 if (mkstemp (tempfile
) < 0
433 if (mktemp (tempfile
) != tempfile
435 || !(f
= fopen (tempfile
, "w")))
437 for (i
= 0; i
< count
; i
++)
438 if (fprintf (f
, "%ld %s %s\n", scores
[i
].score
, scores
[i
].username
,
442 if (rename (tempfile
, filename
) < 0)
444 if (chmod (filename
, 0644) < 0)
450 lock_file (const char *filename
, void **state
)
455 const char *lockext
= ".lockfile";
456 char *lockpath
= malloc (strlen (filename
) + strlen (lockext
) + 60);
459 strcpy (lockpath
, filename
);
460 strcat (lockpath
, lockext
);
464 /* If the lock is over an hour old, delete it. */
465 if (stat (lockpath
, &buf
) == 0
466 && (difftime (buf
.st_ctime
, time (NULL
) > 60*60)))
468 fd
= open (lockpath
, O_CREAT
| O_EXCL
, 0600);
473 /* Break the lock; we won't corrupt the file, but we might
475 if (attempts
> MAX_ATTEMPTS
)
480 sleep ((rand () % 2)+1);
491 unlock_file (const char *filename
, void *state
)
493 char *lockpath
= (char *) state
;
494 int ret
= unlink (lockpath
);
495 int saved_errno
= errno
;
501 /* arch-tag: 2bf5c52e-4beb-463a-954e-c58b9c64736b
502 (do not change this comment) */
504 /* update-game-score.c ends here */