1 /* update-game-score.c --- Update a score file
3 Copyright (C) 2002-2012 Free Software Foundation, Inc.
5 Author: Colin Walters <walters@debian.org>
7 This file is part of GNU Emacs.
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
23 /* This program allows a game to securely and atomically update a
24 score file. It should be installed setuid, owned by an appropriate
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).
51 #define MAX_ATTEMPTS 5
52 #define MAX_SCORES 200
53 #define MAX_DATA_LEN 1024
56 /* OK on POSIX (time_t is arithmetic type) modulo overflow in subtraction. */
57 #define difftime(t1, t0) (double)((t1) - (t0))
63 fprintf (stdout
, "Usage: update-game-score [-m MAX] [-r] [-d DIR] game/scorefile SCORE DATA\n");
64 fprintf (stdout
, " update-game-score -h\n");
65 fprintf (stdout
, " -h\t\tDisplay this help.\n");
66 fprintf (stdout
, " -m MAX\t\tLimit the maximum number of scores to MAX.\n");
67 fprintf (stdout
, " -r\t\tSort the scores in increasing order.\n");
68 fprintf (stdout
, " -d DIR\t\tStore scores in DIR (only if not setuid).\n");
72 static int lock_file (const char *filename
, void **state
);
73 static int unlock_file (const char *filename
, void *state
);
82 static int read_scores (const char *filename
, struct score_entry
**scores
,
84 static int push_score (struct score_entry
**scores
, int *count
,
85 int newscore
, char *username
, char *newdata
);
86 static void sort_scores (struct score_entry
*scores
, int count
, int reverse
);
87 static int write_scores (const char *filename
,
88 const struct score_entry
*scores
, int count
);
91 lose (const char *msg
)
93 fprintf (stderr
, "%s\n", msg
);
98 lose_syserr (const char *msg
)
100 fprintf (stderr
, "%s: %s\n", msg
, strerror (errno
));
107 struct passwd
*buf
= getpwuid (getuid ());
110 long uid
= getuid ();
111 char *name
= malloc (sizeof uid
* CHAR_BIT
/ 3 + 1);
113 sprintf (name
, "%ld", uid
);
120 get_prefix (int running_suid
, const char *user_prefix
)
122 if (!running_suid
&& user_prefix
== NULL
)
123 lose ("Not using a shared game directory, and no prefix given.");
126 #ifdef HAVE_SHARED_GAME_DIR
127 return HAVE_SHARED_GAME_DIR
;
129 lose ("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.");
136 main (int argc
, char **argv
)
140 char *user_id
, *scorefile
;
141 const char *prefix
, *user_prefix
= NULL
;
143 struct score_entry
*scores
;
144 int newscore
, scorecount
, reverse
= 0, max
= MAX_SCORES
;
149 while ((c
= getopt (argc
, argv
, "hrm:d:")) != -1)
153 usage (EXIT_SUCCESS
);
156 user_prefix
= optarg
;
163 if (max
> MAX_SCORES
)
167 usage (EXIT_FAILURE
);
170 if (optind
+3 != argc
)
171 usage (EXIT_FAILURE
);
173 running_suid
= (getuid () != geteuid ());
175 prefix
= get_prefix (running_suid
, user_prefix
);
177 scorefile
= malloc (strlen (prefix
) + strlen (argv
[optind
]) + 2);
179 lose_syserr ("Couldn't allocate score file");
181 strcpy (scorefile
, prefix
);
182 strcat (scorefile
, "/");
183 strcat (scorefile
, argv
[optind
]);
184 newscore
= atoi (argv
[optind
+1]);
185 newdata
= argv
[optind
+2];
186 if (strlen (newdata
) > MAX_DATA_LEN
)
187 newdata
[MAX_DATA_LEN
] = '\0';
189 user_id
= get_user_id ();
191 lose_syserr ("Couldn't determine user id");
193 if (stat (scorefile
, &buf
) < 0)
194 lose_syserr ("Failed to access scores file");
196 if (lock_file (scorefile
, &lockstate
) < 0)
197 lose_syserr ("Failed to lock scores file");
199 if (read_scores (scorefile
, &scores
, &scorecount
) < 0)
201 unlock_file (scorefile
, lockstate
);
202 lose_syserr ("Failed to read scores file");
204 push_score (&scores
, &scorecount
, newscore
, user_id
, newdata
);
205 sort_scores (scores
, scorecount
, reverse
);
206 /* Limit the number of scores. If we're using reverse sorting, then
207 also increment the beginning of the array, to skip over the
208 *smallest* scores. Otherwise, just decrementing the number of
209 scores suffices, since the smallest is at the end. */
210 if (scorecount
> MAX_SCORES
)
213 scores
+= (scorecount
- MAX_SCORES
);
214 scorecount
= MAX_SCORES
;
216 if (write_scores (scorefile
, scores
, scorecount
) < 0)
218 unlock_file (scorefile
, lockstate
);
219 lose_syserr ("Failed to write scores file");
221 unlock_file (scorefile
, lockstate
);
226 read_score (FILE *f
, struct score_entry
*score
)
231 while ((c
= getc (f
)) != EOF
235 score
->score
+= (c
-48);
237 while ((c
= getc (f
)) != EOF
246 if (getdelim (&score
->username
, &count
, ' ', f
) < 1
247 || score
->username
== NULL
)
250 score
->username
[strlen (score
->username
)-1] = '\0';
256 char *username
= malloc (unamelen
);
260 while ((c
= getc (f
)) != EOF
263 if (unameread
>= unamelen
-1)
264 if (!(username
= realloc (username
, unamelen
*= 2)))
266 username
[unameread
] = c
;
271 username
[unameread
] = '\0';
272 score
->username
= username
;
280 if (getline (&score
->data
, &len
, f
) < 0)
282 score
->data
[strlen (score
->data
)-1] = '\0';
288 char *buf
= malloc (len
);
291 while ((c
= getc (f
)) != EOF
296 if (!(buf
= realloc (buf
, len
*= 2)))
303 score
->data
[cur
] = '\0';
310 read_scores (const char *filename
, struct score_entry
**scores
, int *count
)
312 int readval
, scorecount
, cursize
;
313 struct score_entry
*ret
;
314 FILE *f
= fopen (filename
, "r");
319 ret
= (struct score_entry
*) malloc (sizeof (struct score_entry
) * cursize
);
322 while ((readval
= read_score (f
, &ret
[scorecount
])) == 0)
324 /* We encountered an error. */
328 if (scorecount
>= cursize
)
331 ret
= (struct score_entry
*)
332 realloc (ret
, (sizeof (struct score_entry
) * cursize
));
343 score_compare (const void *a
, const void *b
)
345 const struct score_entry
*sa
= (const struct score_entry
*) a
;
346 const struct score_entry
*sb
= (const struct score_entry
*) b
;
347 return (sb
->score
> sa
->score
) - (sb
->score
< sa
->score
);
351 score_compare_reverse (const void *a
, const void *b
)
353 const struct score_entry
*sa
= (const struct score_entry
*) a
;
354 const struct score_entry
*sb
= (const struct score_entry
*) b
;
355 return (sa
->score
> sb
->score
) - (sa
->score
< sb
->score
);
359 push_score (struct score_entry
**scores
, int *count
, int newscore
, char *username
, char *newdata
)
361 struct score_entry
*newscores
362 = (struct score_entry
*) realloc (*scores
,
363 sizeof (struct score_entry
) * ((*count
) + 1));
366 newscores
[*count
].score
= newscore
;
367 newscores
[*count
].username
= username
;
368 newscores
[*count
].data
= newdata
;
375 sort_scores (struct score_entry
*scores
, int count
, int reverse
)
377 qsort (scores
, count
, sizeof (struct score_entry
),
378 reverse
? score_compare_reverse
: score_compare
);
382 write_scores (const char *filename
, const struct score_entry
*scores
, int count
)
386 char *tempfile
= malloc (strlen (filename
) + strlen (".tempXXXXXX") + 1);
389 strcpy (tempfile
, filename
);
390 strcat (tempfile
, ".tempXXXXXX");
392 if (mkstemp (tempfile
) < 0
394 if (mktemp (tempfile
) != tempfile
396 || !(f
= fopen (tempfile
, "w")))
398 for (i
= 0; i
< count
; i
++)
399 if (fprintf (f
, "%ld %s %s\n", scores
[i
].score
, scores
[i
].username
,
403 if (rename (tempfile
, filename
) < 0)
405 if (chmod (filename
, 0644) < 0)
411 lock_file (const char *filename
, void **state
)
416 const char *lockext
= ".lockfile";
417 char *lockpath
= malloc (strlen (filename
) + strlen (lockext
) + 60);
420 strcpy (lockpath
, filename
);
421 strcat (lockpath
, lockext
);
425 /* If the lock is over an hour old, delete it. */
426 if (stat (lockpath
, &buf
) == 0
427 && (difftime (buf
.st_ctime
, time (NULL
) > 60*60)))
429 fd
= open (lockpath
, O_CREAT
| O_EXCL
, 0600);
434 /* Break the lock; we won't corrupt the file, but we might
436 if (attempts
> MAX_ATTEMPTS
)
441 sleep ((rand () % 2)+1);
452 unlock_file (const char *filename
, void *state
)
454 char *lockpath
= (char *) state
;
455 int ret
= unlink (lockpath
);
456 int saved_errno
= errno
;
463 /* update-game-score.c ends here */