1 /* update-game-score.c --- Update a score file
3 Copyright (C) 2002-2011 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).
56 /* Needed for SunOS4, for instance. */
58 extern int optind
, opterr
;
60 static int usage (int err
) NO_RETURN
;
62 #define MAX_ATTEMPTS 5
63 #define MAX_SCORES 200
64 #define MAX_DATA_LEN 1024
67 /* OK on POSIX (time_t is arithmetic type) modulo overflow in subtraction. */
68 #define difftime(t1, t0) (double)((t1) - (t0))
74 fprintf (stdout
, "Usage: update-game-score [-m MAX] [-r] [-d DIR] 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");
83 static int lock_file (const char *filename
, void **state
);
84 static int unlock_file (const char *filename
, void *state
);
93 static int read_scores (const char *filename
, struct score_entry
**scores
,
95 static int push_score (struct score_entry
**scores
, int *count
,
96 int newscore
, char *username
, char *newdata
);
97 static void sort_scores (struct score_entry
*scores
, int count
, int reverse
);
98 static int write_scores (const char *filename
,
99 const struct score_entry
*scores
, int count
);
101 static void lose (const char *msg
) NO_RETURN
;
104 lose (const char *msg
)
106 fprintf (stderr
, "%s\n", msg
);
110 static void lose_syserr (const char *msg
) NO_RETURN
;
112 /* Taken from sysdep.c. */
113 #ifndef HAVE_STRERROR
116 strerror (int errnum
)
118 extern char *sys_errlist
[];
121 if (errnum
>= 0 && errnum
< sys_nerr
)
122 return sys_errlist
[errnum
];
123 return (char *) "Unknown error";
125 #endif /* not WINDOWSNT */
126 #endif /* ! HAVE_STRERROR */
129 lose_syserr (const char *msg
)
131 fprintf (stderr
, "%s: %s\n", msg
, strerror (errno
));
139 struct passwd
*buf
= getpwuid (getuid ());
143 int uid
= (int) getuid ();
147 name
= malloc (count
+1);
150 sprintf (name
, "%d", uid
);
157 get_prefix (int running_suid
, const char *user_prefix
)
159 if (!running_suid
&& user_prefix
== NULL
)
160 lose ("Not using a shared game directory, and no prefix given.");
163 #ifdef HAVE_SHARED_GAME_DIR
164 return HAVE_SHARED_GAME_DIR
;
166 lose ("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.");
173 main (int argc
, char **argv
)
177 char *user_id
, *scorefile
;
178 const char *prefix
, *user_prefix
= NULL
;
180 struct score_entry
*scores
;
181 int newscore
, scorecount
, reverse
= 0, max
= MAX_SCORES
;
186 while ((c
= getopt (argc
, argv
, "hrm:d:")) != -1)
190 usage (EXIT_SUCCESS
);
193 user_prefix
= optarg
;
200 if (max
> MAX_SCORES
)
204 usage (EXIT_FAILURE
);
207 if (optind
+3 != argc
)
208 usage (EXIT_FAILURE
);
210 running_suid
= (getuid () != geteuid ());
212 prefix
= get_prefix (running_suid
, user_prefix
);
214 scorefile
= malloc (strlen (prefix
) + strlen (argv
[optind
]) + 2);
216 lose_syserr ("Couldn't allocate score file");
218 strcpy (scorefile
, prefix
);
219 strcat (scorefile
, "/");
220 strcat (scorefile
, argv
[optind
]);
221 newscore
= atoi (argv
[optind
+1]);
222 newdata
= argv
[optind
+2];
223 if (strlen (newdata
) > MAX_DATA_LEN
)
224 newdata
[MAX_DATA_LEN
] = '\0';
226 user_id
= get_user_id ();
228 lose_syserr ("Couldn't determine user id");
230 if (stat (scorefile
, &buf
) < 0)
231 lose_syserr ("Failed to access scores file");
233 if (lock_file (scorefile
, &lockstate
) < 0)
234 lose_syserr ("Failed to lock scores file");
236 if (read_scores (scorefile
, &scores
, &scorecount
) < 0)
238 unlock_file (scorefile
, lockstate
);
239 lose_syserr ("Failed to read scores file");
241 push_score (&scores
, &scorecount
, newscore
, user_id
, newdata
);
242 sort_scores (scores
, scorecount
, reverse
);
243 /* Limit the number of scores. If we're using reverse sorting, then
244 also increment the beginning of the array, to skip over the
245 *smallest* scores. Otherwise, just decrementing the number of
246 scores suffices, since the smallest is at the end. */
247 if (scorecount
> MAX_SCORES
)
250 scores
+= (scorecount
- MAX_SCORES
);
251 scorecount
= MAX_SCORES
;
253 if (write_scores (scorefile
, scores
, scorecount
) < 0)
255 unlock_file (scorefile
, lockstate
);
256 lose_syserr ("Failed to write scores file");
258 unlock_file (scorefile
, lockstate
);
263 read_score (FILE *f
, struct score_entry
*score
)
268 while ((c
= getc (f
)) != EOF
272 score
->score
+= (c
-48);
274 while ((c
= getc (f
)) != EOF
283 if (getdelim (&score
->username
, &count
, ' ', f
) < 1
284 || score
->username
== NULL
)
287 score
->username
[strlen (score
->username
)-1] = '\0';
293 char *username
= malloc (unamelen
);
297 while ((c
= getc (f
)) != EOF
300 if (unameread
>= unamelen
-1)
301 if (!(username
= realloc (username
, unamelen
*= 2)))
303 username
[unameread
] = c
;
308 username
[unameread
] = '\0';
309 score
->username
= username
;
317 if (getline (&score
->data
, &len
, f
) < 0)
319 score
->data
[strlen (score
->data
)-1] = '\0';
325 char *buf
= malloc (len
);
328 while ((c
= getc (f
)) != EOF
333 if (!(buf
= realloc (buf
, len
*= 2)))
340 score
->data
[cur
] = '\0';
347 read_scores (const char *filename
, struct score_entry
**scores
, int *count
)
349 int readval
, scorecount
, cursize
;
350 struct score_entry
*ret
;
351 FILE *f
= fopen (filename
, "r");
356 ret
= (struct score_entry
*) malloc (sizeof (struct score_entry
) * cursize
);
359 while ((readval
= read_score (f
, &ret
[scorecount
])) == 0)
361 /* We encoutered an error */
365 if (scorecount
>= cursize
)
368 ret
= (struct score_entry
*)
369 realloc (ret
, (sizeof (struct score_entry
) * cursize
));
380 score_compare (const void *a
, const void *b
)
382 const struct score_entry
*sa
= (const struct score_entry
*) a
;
383 const struct score_entry
*sb
= (const struct score_entry
*) b
;
384 return (sb
->score
> sa
->score
) - (sb
->score
< sa
->score
);
388 score_compare_reverse (const void *a
, const void *b
)
390 const struct score_entry
*sa
= (const struct score_entry
*) a
;
391 const struct score_entry
*sb
= (const struct score_entry
*) b
;
392 return (sa
->score
> sb
->score
) - (sa
->score
< sb
->score
);
396 push_score (struct score_entry
**scores
, int *count
, int newscore
, char *username
, char *newdata
)
398 struct score_entry
*newscores
399 = (struct score_entry
*) realloc (*scores
,
400 sizeof (struct score_entry
) * ((*count
) + 1));
403 newscores
[*count
].score
= newscore
;
404 newscores
[*count
].username
= username
;
405 newscores
[*count
].data
= newdata
;
412 sort_scores (struct score_entry
*scores
, int count
, int reverse
)
414 qsort (scores
, count
, sizeof (struct score_entry
),
415 reverse
? score_compare_reverse
: score_compare
);
419 write_scores (const char *filename
, const struct score_entry
*scores
, int count
)
423 char *tempfile
= malloc (strlen (filename
) + strlen (".tempXXXXXX") + 1);
426 strcpy (tempfile
, filename
);
427 strcat (tempfile
, ".tempXXXXXX");
429 if (mkstemp (tempfile
) < 0
431 if (mktemp (tempfile
) != tempfile
433 || !(f
= fopen (tempfile
, "w")))
435 for (i
= 0; i
< count
; i
++)
436 if (fprintf (f
, "%ld %s %s\n", scores
[i
].score
, scores
[i
].username
,
440 if (rename (tempfile
, filename
) < 0)
442 if (chmod (filename
, 0644) < 0)
448 lock_file (const char *filename
, void **state
)
453 const char *lockext
= ".lockfile";
454 char *lockpath
= malloc (strlen (filename
) + strlen (lockext
) + 60);
457 strcpy (lockpath
, filename
);
458 strcat (lockpath
, lockext
);
462 /* If the lock is over an hour old, delete it. */
463 if (stat (lockpath
, &buf
) == 0
464 && (difftime (buf
.st_ctime
, time (NULL
) > 60*60)))
466 fd
= open (lockpath
, O_CREAT
| O_EXCL
, 0600);
471 /* Break the lock; we won't corrupt the file, but we might
473 if (attempts
> MAX_ATTEMPTS
)
478 sleep ((rand () % 2)+1);
489 unlock_file (const char *filename
, void *state
)
491 char *lockpath
= (char *) state
;
492 int ret
= unlink (lockpath
);
493 int saved_errno
= errno
;
500 /* update-game-score.c ends here */