1 /* update-game-score.c --- Update a score file
3 Copyright (C) 2002-2013 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).
49 #define MAX_ATTEMPTS 5
50 #define MAX_SCORES 200
51 #define MAX_DATA_LEN 1024
54 /* OK on POSIX (time_t is arithmetic type) modulo overflow in subtraction. */
55 #define difftime(t1, t0) (double)((t1) - (t0))
61 fprintf (stdout
, "Usage: update-game-score [-m MAX] [-r] [-d DIR] game/scorefile SCORE DATA\n");
62 fprintf (stdout
, " update-game-score -h\n");
63 fprintf (stdout
, " -h\t\tDisplay this help.\n");
64 fprintf (stdout
, " -m MAX\t\tLimit the maximum number of scores to MAX.\n");
65 fprintf (stdout
, " -r\t\tSort the scores in increasing order.\n");
66 fprintf (stdout
, " -d DIR\t\tStore scores in DIR (only if not setuid).\n");
70 static int lock_file (const char *filename
, void **state
);
71 static int unlock_file (const char *filename
, void *state
);
80 static int read_scores (const char *filename
, struct score_entry
**scores
,
82 static int push_score (struct score_entry
**scores
, int *count
,
83 int newscore
, char *username
, char *newdata
);
84 static void sort_scores (struct score_entry
*scores
, int count
, int reverse
);
85 static int write_scores (const char *filename
,
86 const struct score_entry
*scores
, int count
);
89 lose (const char *msg
)
91 fprintf (stderr
, "%s\n", msg
);
96 lose_syserr (const char *msg
)
98 fprintf (stderr
, "%s: %s\n", msg
, strerror (errno
));
105 struct passwd
*buf
= getpwuid (getuid ());
108 long uid
= getuid ();
109 char *name
= malloc (sizeof uid
* CHAR_BIT
/ 3 + 1);
111 sprintf (name
, "%ld", uid
);
118 get_prefix (int running_suid
, const char *user_prefix
)
120 if (!running_suid
&& user_prefix
== NULL
)
121 lose ("Not using a shared game directory, and no prefix given.");
124 #ifdef HAVE_SHARED_GAME_DIR
125 return HAVE_SHARED_GAME_DIR
;
127 lose ("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.");
134 main (int argc
, char **argv
)
138 char *user_id
, *scorefile
;
139 const char *prefix
, *user_prefix
= NULL
;
141 struct score_entry
*scores
;
142 int newscore
, scorecount
, reverse
= 0, max
= MAX_SCORES
;
147 while ((c
= getopt (argc
, argv
, "hrm:d:")) != -1)
151 usage (EXIT_SUCCESS
);
154 user_prefix
= optarg
;
161 if (max
> MAX_SCORES
)
165 usage (EXIT_FAILURE
);
168 if (optind
+3 != argc
)
169 usage (EXIT_FAILURE
);
171 running_suid
= (getuid () != geteuid ());
173 prefix
= get_prefix (running_suid
, user_prefix
);
175 scorefile
= malloc (strlen (prefix
) + strlen (argv
[optind
]) + 2);
177 lose_syserr ("Couldn't allocate score file");
179 strcpy (scorefile
, prefix
);
180 strcat (scorefile
, "/");
181 strcat (scorefile
, argv
[optind
]);
182 newscore
= atoi (argv
[optind
+1]);
183 newdata
= argv
[optind
+2];
184 if (strlen (newdata
) > MAX_DATA_LEN
)
185 newdata
[MAX_DATA_LEN
] = '\0';
187 user_id
= get_user_id ();
189 lose_syserr ("Couldn't determine user id");
191 if (stat (scorefile
, &buf
) < 0)
192 lose_syserr ("Failed to access scores file");
194 if (lock_file (scorefile
, &lockstate
) < 0)
195 lose_syserr ("Failed to lock scores file");
197 if (read_scores (scorefile
, &scores
, &scorecount
) < 0)
199 unlock_file (scorefile
, lockstate
);
200 lose_syserr ("Failed to read scores file");
202 push_score (&scores
, &scorecount
, newscore
, user_id
, newdata
);
203 sort_scores (scores
, scorecount
, reverse
);
204 /* Limit the number of scores. If we're using reverse sorting, then
205 also increment the beginning of the array, to skip over the
206 *smallest* scores. Otherwise, just decrementing the number of
207 scores suffices, since the smallest is at the end. */
208 if (scorecount
> MAX_SCORES
)
211 scores
+= (scorecount
- MAX_SCORES
);
212 scorecount
= MAX_SCORES
;
214 if (write_scores (scorefile
, scores
, scorecount
) < 0)
216 unlock_file (scorefile
, lockstate
);
217 lose_syserr ("Failed to write scores file");
219 unlock_file (scorefile
, lockstate
);
224 read_score (FILE *f
, struct score_entry
*score
)
229 while ((c
= getc (f
)) != EOF
233 score
->score
+= (c
-48);
235 while ((c
= getc (f
)) != EOF
244 if (getdelim (&score
->username
, &count
, ' ', f
) < 1
245 || score
->username
== NULL
)
248 score
->username
[strlen (score
->username
)-1] = '\0';
254 char *username
= malloc (unamelen
);
258 while ((c
= getc (f
)) != EOF
261 if (unameread
>= unamelen
-1)
262 if (!(username
= realloc (username
, unamelen
*= 2)))
264 username
[unameread
] = c
;
269 username
[unameread
] = '\0';
270 score
->username
= username
;
278 if (getline (&score
->data
, &len
, f
) < 0)
280 score
->data
[strlen (score
->data
)-1] = '\0';
286 char *buf
= malloc (len
);
289 while ((c
= getc (f
)) != EOF
294 if (!(buf
= realloc (buf
, len
*= 2)))
301 score
->data
[cur
] = '\0';
308 read_scores (const char *filename
, struct score_entry
**scores
, int *count
)
310 int readval
, scorecount
, cursize
;
311 struct score_entry
*ret
;
312 FILE *f
= fopen (filename
, "r");
317 ret
= (struct score_entry
*) malloc (sizeof (struct score_entry
) * cursize
);
320 while ((readval
= read_score (f
, &ret
[scorecount
])) == 0)
322 /* We encountered an error. */
326 if (scorecount
>= cursize
)
329 ret
= (struct score_entry
*)
330 realloc (ret
, (sizeof (struct score_entry
) * cursize
));
341 score_compare (const void *a
, const void *b
)
343 const struct score_entry
*sa
= (const struct score_entry
*) a
;
344 const struct score_entry
*sb
= (const struct score_entry
*) b
;
345 return (sb
->score
> sa
->score
) - (sb
->score
< sa
->score
);
349 score_compare_reverse (const void *a
, const void *b
)
351 const struct score_entry
*sa
= (const struct score_entry
*) a
;
352 const struct score_entry
*sb
= (const struct score_entry
*) b
;
353 return (sa
->score
> sb
->score
) - (sa
->score
< sb
->score
);
357 push_score (struct score_entry
**scores
, int *count
, int newscore
, char *username
, char *newdata
)
359 struct score_entry
*newscores
360 = (struct score_entry
*) realloc (*scores
,
361 sizeof (struct score_entry
) * ((*count
) + 1));
364 newscores
[*count
].score
= newscore
;
365 newscores
[*count
].username
= username
;
366 newscores
[*count
].data
= newdata
;
373 sort_scores (struct score_entry
*scores
, int count
, int reverse
)
375 qsort (scores
, count
, sizeof (struct score_entry
),
376 reverse
? score_compare_reverse
: score_compare
);
380 write_scores (const char *filename
, const struct score_entry
*scores
, int count
)
384 char *tempfile
= malloc (strlen (filename
) + strlen (".tempXXXXXX") + 1);
387 strcpy (tempfile
, filename
);
388 strcat (tempfile
, ".tempXXXXXX");
390 if (mkstemp (tempfile
) < 0
392 if (mktemp (tempfile
) != tempfile
394 || !(f
= fopen (tempfile
, "w")))
396 for (i
= 0; i
< count
; i
++)
397 if (fprintf (f
, "%ld %s %s\n", scores
[i
].score
, scores
[i
].username
,
401 if (rename (tempfile
, filename
) < 0)
403 if (chmod (filename
, 0644) < 0)
409 lock_file (const char *filename
, void **state
)
414 const char *lockext
= ".lockfile";
415 char *lockpath
= malloc (strlen (filename
) + strlen (lockext
) + 60);
418 strcpy (lockpath
, filename
);
419 strcat (lockpath
, lockext
);
423 /* If the lock is over an hour old, delete it. */
424 if (stat (lockpath
, &buf
) == 0
425 && (difftime (buf
.st_ctime
, time (NULL
) > 60*60)))
427 fd
= open (lockpath
, O_CREAT
| O_EXCL
, 0600);
432 /* Break the lock; we won't corrupt the file, but we might
434 if (attempts
> MAX_ATTEMPTS
)
439 sleep ((rand () % 2)+1);
450 unlock_file (const char *filename
, void *state
)
452 char *lockpath
= (char *) state
;
453 int ret
= unlink (lockpath
);
454 int saved_errno
= errno
;
461 /* update-game-score.c ends here */