1 /* update-game-score.c --- Update a score file
2 Copyright (C) 2002 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* This program is allows a game to securely and atomically update a
22 score file. It should be installed setuid, owned by an appropriate
25 Alternatively, it can be compiled without HAVE_SHARED_GAME_DIR
26 defined, and in that case it will store scores in the user's home
27 directory (it should NOT be setuid).
29 Created 2002/03/22, by Colin Walters <walters@debian.org>
56 /* Needed for SunOS4, for instance. */
58 extern int optind
, opterr
;
60 #define MAX_ATTEMPTS 5
61 #define MAX_SCORES 200
62 #define MAX_DATA_LEN 1024
64 /* Declare the prototype for a general external function. */
65 #if defined (PROTOTYPES) || defined (WINDOWSNT)
66 #define P_(proto) proto
75 fprintf (stdout
, "Usage: update-game-score [-m MAX ] [ -r ] game/scorefile SCORE DATA\n");
76 fprintf (stdout
, " update-game-score -h\n");
77 fprintf (stdout
, " -h\t\tDisplay this help.\n");
78 fprintf (stdout
, " -m MAX\t\tLimit the maximum number of scores to MAX.\n");
79 fprintf (stdout
, " -r\t\tSort the scores in increasing order.\n");
80 fprintf (stdout
, " -d DIR\t\tStore scores in DIR (only if not setuid).\n");
84 int lock_file
P_ ((const char *filename
, void **state
));
85 int unlock_file
P_ ((const char *filename
, void *state
));
94 int read_scores
P_ ((const char *filename
, struct score_entry
**scores
,
96 int push_score
P_ ((struct score_entry
**scores
, int *count
,
97 int newscore
, char *username
, char *newdata
));
98 void sort_scores
P_ ((struct score_entry
*scores
, int count
, int reverse
));
99 int write_scores
P_ ((const char *filename
, const struct score_entry
*scores
,
102 void lose
P_ ((const char *msg
)) NO_RETURN
;
108 fprintf (stderr
, "%s\n", msg
);
112 void lose_syserr
P_ ((const char *msg
)) NO_RETURN
;
118 fprintf (stderr
, "%s: %s\n", msg
, strerror (errno
));
123 get_user_id
P_ ((void))
126 struct passwd
*buf
= getpwuid (getuid ());
130 int uid
= (int) getuid ();
134 name
= malloc (count
+1);
137 sprintf (name
, "%d", uid
);
144 get_prefix (running_suid
, user_prefix
)
148 if (!running_suid
&& user_prefix
== NULL
)
149 lose ("Not using a shared game directory, and no prefix given.");
152 #ifdef HAVE_SHARED_GAME_DIR
153 return HAVE_SHARED_GAME_DIR
;
155 lose ("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.");
168 char *user_id
, *scorefile
, *prefix
, *user_prefix
= NULL
;
170 struct score_entry
*scores
;
171 int newscore
, scorecount
, reverse
= 0, max
= MAX_SCORES
;
176 while ((c
= getopt (argc
, argv
, "hrm:d:")) != -1)
183 user_prefix
= optarg
;
190 if (max
> MAX_SCORES
)
197 if (optind
+3 != argc
)
200 running_suid
= (getuid () != geteuid ());
202 prefix
= get_prefix (running_suid
, user_prefix
);
204 scorefile
= malloc (strlen (prefix
) + strlen (argv
[optind
]) + 2);
206 lose_syserr ("Couldn't allocate score file");
208 strcpy (scorefile
, prefix
);
209 strcat (scorefile
, "/");
210 strcat (scorefile
, argv
[optind
]);
211 newscore
= atoi (argv
[optind
+1]);
212 newdata
= argv
[optind
+2];
213 if (strlen (newdata
) > MAX_DATA_LEN
)
214 newdata
[MAX_DATA_LEN
] = '\0';
216 user_id
= get_user_id ();
218 lose_syserr ("Couldn't determine user id");
220 if (stat (scorefile
, &buf
) < 0)
221 lose_syserr ("Failed to access scores file");
223 if (lock_file (scorefile
, &lockstate
) < 0)
224 lose_syserr ("Failed to lock scores file");
226 if (read_scores (scorefile
, &scores
, &scorecount
) < 0)
228 unlock_file (scorefile
, lockstate
);
229 lose_syserr ("Failed to read scores file");
231 push_score (&scores
, &scorecount
, newscore
, user_id
, newdata
);
232 /* Limit the number of scores. If we're using reverse sorting, then
233 we should increment the beginning of the array, to skip over the
234 *smallest* scores. Otherwise, we just decrement the number of
235 scores, since the smallest will be at the end. */
236 if (scorecount
> MAX_SCORES
)
237 scorecount
-= (scorecount
- MAX_SCORES
);
239 scores
+= (scorecount
- MAX_SCORES
);
240 sort_scores (scores
, scorecount
, reverse
);
241 if (write_scores (scorefile
, scores
, scorecount
) < 0)
243 unlock_file (scorefile
, lockstate
);
244 lose_syserr ("Failed to write scores file");
246 unlock_file (scorefile
, lockstate
);
251 read_score (f
, score
)
253 struct score_entry
*score
;
258 while ((c
= getc (f
)) != EOF
262 score
->score
+= (c
-48);
264 while ((c
= getc (f
)) != EOF
273 if (getdelim (&score
->username
, &count
, ' ', f
) < 1
274 || score
->username
== NULL
)
277 score
->username
[strlen (score
->username
)-1] = '\0';
283 char *username
= malloc (unamelen
);
287 while ((c
= getc (f
)) != EOF
290 if (unameread
>= unamelen
-1)
291 if (!(username
= realloc (username
, unamelen
*= 2)))
293 username
[unameread
] = c
;
298 username
[unameread
] = '\0';
299 score
->username
= username
;
307 if (getline (&score
->data
, &len
, f
) < 0)
309 score
->data
[strlen (score
->data
)-1] = '\0';
315 char *buf
= malloc (len
);
318 while ((c
= getc (f
)) != EOF
323 if (!(buf
= realloc (buf
, len
*= 2)))
330 score
->data
[cur
] = '\0';
337 read_scores (filename
, scores
, count
)
338 const char *filename
;
339 struct score_entry
**scores
;
342 int readval
, scorecount
, cursize
;
343 struct score_entry
*ret
;
344 FILE *f
= fopen (filename
, "r");
349 ret
= (struct score_entry
*) malloc (sizeof (struct score_entry
) * cursize
);
352 while ((readval
= read_score (f
, &ret
[scorecount
])) == 0)
354 /* We encoutered an error */
358 if (scorecount
>= cursize
)
360 ret
= (struct score_entry
*) realloc (ret
, cursize
*= 2);
375 const struct score_entry
*sa
= (const struct score_entry
*) a
;
376 const struct score_entry
*sb
= (const struct score_entry
*) b
;
377 return (sb
->score
> sa
->score
) - (sb
->score
< sa
->score
);
381 score_compare_reverse (a
, b
)
385 const struct score_entry
*sa
= (const struct score_entry
*) a
;
386 const struct score_entry
*sb
= (const struct score_entry
*) b
;
387 return (sa
->score
> sb
->score
) - (sa
->score
< sb
->score
);
391 push_score (scores
, count
, newscore
, username
, newdata
)
392 struct score_entry
**scores
;
393 int *count
; int newscore
;
397 struct score_entry
*newscores
398 = (struct score_entry
*) realloc (*scores
,
399 sizeof (struct score_entry
) * ((*count
) + 1));
402 newscores
[*count
].score
= newscore
;
403 newscores
[*count
].username
= username
;
404 newscores
[*count
].data
= newdata
;
411 sort_scores (scores
, count
, reverse
)
412 struct score_entry
*scores
;
416 qsort (scores
, count
, sizeof (struct score_entry
),
417 reverse
? score_compare_reverse
: score_compare
);
421 write_scores (filename
, scores
, count
)
422 const char *filename
;
423 const struct score_entry
* scores
;
428 char *tempfile
= malloc (strlen (filename
) + strlen (".tempXXXXXX") + 1);
431 strcpy (tempfile
, filename
);
432 strcat (tempfile
, ".tempXXXXXX");
434 if (mkstemp (tempfile
) < 0
436 if (mktemp (tempfile
) != tempfile
438 || !(f
= fopen (tempfile
, "w")))
440 for (i
= 0; i
< count
; i
++)
441 if (fprintf (f
, "%ld %s %s\n", scores
[i
].score
, scores
[i
].username
,
445 if (rename (tempfile
, filename
) < 0)
447 if (chmod (filename
, 0644) < 0)
453 lock_file (filename
, state
)
454 const char *filename
;
460 char *lockext
= ".lockfile";
461 char *lockpath
= malloc (strlen (filename
) + strlen (lockext
) + 60);
464 strcpy (lockpath
, filename
);
465 strcat (lockpath
, lockext
);
469 /* If the lock is over an hour old, delete it. */
470 if (stat (lockpath
, &buf
) == 0
471 && (difftime (buf
.st_ctime
, time (NULL
) > 60*60)))
473 fd
= open (lockpath
, O_CREAT
| O_EXCL
, 0600);
478 /* Break the lock; we won't corrupt the file, but we might
480 if (attempts
> MAX_ATTEMPTS
)
485 sleep ((rand () % 2)+1);
496 unlock_file (filename
, state
)
497 const char *filename
;
500 char *lockpath
= (char *) state
;
501 int ret
= unlink (lockpath
);
502 int saved_errno
= errno
;