44184
|
1 /* update-game-score.c --- Update a score file
|
94828
|
2
|
106815
|
3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
|
94828
|
4 Free Software Foundation, Inc.
|
|
5
|
|
6 Author: Colin Walters <walters@debian.org>
|
44184
|
7
|
|
8 This file is part of GNU Emacs.
|
|
9
|
94828
|
10 GNU Emacs is free software: you can redistribute it and/or modify
|
44184
|
11 it under the terms of the GNU General Public License as published by
|
94828
|
12 the Free Software Foundation, either version 3 of the License, or
|
|
13 (at your option) any later version.
|
44184
|
14
|
|
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.
|
|
19
|
|
20 You should have received a copy of the GNU General Public License
|
94828
|
21 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
|
|
22
|
44184
|
23
|
105171
|
24 /* This program allows a game to securely and atomically update a
|
44481
|
25 score file. It should be installed setuid, owned by an appropriate
|
|
26 user like `games'.
|
|
27
|
|
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).
|
44184
|
31
|
94828
|
32 Created 2002/03/22.
|
44184
|
33 */
|
|
34
|
44639
44995332ed1b
Move config.h before the other headers, to prevent compiler warnings
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
35 #include <config.h>
|
44995332ed1b
Move config.h before the other headers, to prevent compiler warnings
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
36
|
48422
|
37 #ifdef HAVE_UNISTD_H
|
44184
|
38 #include <unistd.h>
|
48422
|
39 #endif
|
44184
|
40 #include <errno.h>
|
48422
|
41 #ifdef HAVE_STRING_H
|
44184
|
42 #include <string.h>
|
48422
|
43 #endif
|
|
44 #ifdef HAVE_STDLIB_H
|
44184
|
45 #include <stdlib.h>
|
48422
|
46 #endif
|
44184
|
47 #include <stdio.h>
|
|
48 #include <time.h>
|
44402
|
49 #include <pwd.h>
|
44184
|
50 #include <ctype.h>
|
48422
|
51 #ifdef HAVE_FCNTL_H
|
44184
|
52 #include <fcntl.h>
|
48422
|
53 #endif
|
|
54 #ifdef STDC_HEADERS
|
44481
|
55 #include <stdarg.h>
|
48422
|
56 #endif
|
44184
|
57 #include <sys/stat.h>
|
|
58
|
48422
|
59 /* Needed for SunOS4, for instance. */
|
|
60 extern char *optarg;
|
|
61 extern int optind, opterr;
|
|
62
|
109511
|
63 int usage (int err) NO_RETURN;
|
|
64
|
44184
|
65 #define MAX_ATTEMPTS 5
|
44481
|
66 #define MAX_SCORES 200
|
|
67 #define MAX_DATA_LEN 1024
|
44419
|
68
|
51146
|
69 #ifndef HAVE_DIFFTIME
|
|
70 /* OK on POSIX (time_t is arithmetic type) modulo overflow in subtraction. */
|
|
71 #define difftime(t1, t0) (double)((t1) - (t0))
|
|
72 #endif
|
|
73
|
44184
|
74 int
|
109111
|
75 usage (int err)
|
44184
|
76 {
|
49594
|
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");
|
|
83 exit (err);
|
44184
|
84 }
|
|
85
|
109100
|
86 int lock_file (const char *filename, void **state);
|
|
87 int unlock_file (const char *filename, void *state);
|
44184
|
88
|
|
89 struct score_entry
|
|
90 {
|
|
91 long score;
|
44402
|
92 char *username;
|
44184
|
93 char *data;
|
|
94 };
|
|
95
|
109100
|
96 int read_scores (const char *filename, struct score_entry **scores,
|
|
97 int *count);
|
|
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,
|
|
102 int count);
|
49594
|
103
|
109100
|
104 void lose (const char *msg) NO_RETURN;
|
49594
|
105
|
44184
|
106 void
|
109111
|
107 lose (const char *msg)
|
44481
|
108 {
|
49594
|
109 fprintf (stderr, "%s\n", msg);
|
55442
a47704955f8d
Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
diff
changeset
|
110 exit (EXIT_FAILURE);
|
46774
|
111 }
|
|
112
|
109100
|
113 void lose_syserr (const char *msg) NO_RETURN;
|
46774
|
114
|
51146
|
115 /* Taken from sysdep.c. */
|
|
116 #ifndef HAVE_STRERROR
|
|
117 #ifndef WINDOWSNT
|
|
118 char *
|
|
119 strerror (errnum)
|
|
120 int errnum;
|
|
121 {
|
|
122 extern char *sys_errlist[];
|
|
123 extern int sys_nerr;
|
|
124
|
|
125 if (errnum >= 0 && errnum < sys_nerr)
|
|
126 return sys_errlist[errnum];
|
|
127 return (char *) "Unknown error";
|
|
128 }
|
|
129 #endif /* not WINDOWSNT */
|
|
130 #endif /* ! HAVE_STRERROR */
|
|
131
|
49594
|
132 void
|
109111
|
133 lose_syserr (const char *msg)
|
46774
|
134 {
|
49594
|
135 fprintf (stderr, "%s: %s\n", msg, strerror (errno));
|
55442
a47704955f8d
Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
diff
changeset
|
136 exit (EXIT_FAILURE);
|
44481
|
137 }
|
|
138
|
44987
|
139 char *
|
109100
|
140 get_user_id (void)
|
44987
|
141 {
|
|
142 char *name;
|
49594
|
143 struct passwd *buf = getpwuid (getuid ());
|
44987
|
144 if (!buf)
|
|
145 {
|
|
146 int count = 1;
|
49594
|
147 int uid = (int) getuid ();
|
44987
|
148 int tuid = uid;
|
|
149 while (tuid /= 10)
|
|
150 count++;
|
49594
|
151 name = malloc (count+1);
|
44987
|
152 if (!name)
|
|
153 return NULL;
|
49594
|
154 sprintf (name, "%d", uid);
|
44987
|
155 return name;
|
|
156 }
|
|
157 return buf->pw_name;
|
|
158 }
|
|
159
|
|
160 char *
|
109111
|
161 get_prefix (int running_suid, char *user_prefix)
|
44987
|
162 {
|
|
163 if (!running_suid && user_prefix == NULL)
|
49594
|
164 lose ("Not using a shared game directory, and no prefix given.");
|
44987
|
165 if (running_suid)
|
|
166 {
|
|
167 #ifdef HAVE_SHARED_GAME_DIR
|
|
168 return HAVE_SHARED_GAME_DIR;
|
|
169 #else
|
49594
|
170 lose ("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.");
|
44987
|
171 #endif
|
|
172 }
|
|
173 return user_prefix;
|
|
174 }
|
|
175
|
44184
|
176 int
|
109111
|
177 main (int argc, char **argv)
|
44184
|
178 {
|
44987
|
179 int c, running_suid;
|
44184
|
180 void *lockstate;
|
44987
|
181 char *user_id, *scorefile, *prefix, *user_prefix = NULL;
|
44184
|
182 struct stat buf;
|
|
183 struct score_entry *scores;
|
44481
|
184 int newscore, scorecount, reverse = 0, max = MAX_SCORES;
|
44184
|
185 char *newdata;
|
|
186
|
49594
|
187 srand (time (0));
|
44184
|
188
|
49594
|
189 while ((c = getopt (argc, argv, "hrm:d:")) != -1)
|
44184
|
190 switch (c)
|
|
191 {
|
|
192 case 'h':
|
55442
a47704955f8d
Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
diff
changeset
|
193 usage (EXIT_SUCCESS);
|
44184
|
194 break;
|
44987
|
195 case 'd':
|
|
196 user_prefix = optarg;
|
|
197 break;
|
44184
|
198 case 'r':
|
|
199 reverse = 1;
|
|
200 break;
|
|
201 case 'm':
|
49594
|
202 max = atoi (optarg);
|
44481
|
203 if (max > MAX_SCORES)
|
|
204 max = MAX_SCORES;
|
44184
|
205 break;
|
|
206 default:
|
55442
a47704955f8d
Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
diff
changeset
|
207 usage (EXIT_FAILURE);
|
44184
|
208 }
|
|
209
|
|
210 if (optind+3 != argc)
|
55442
a47704955f8d
Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
diff
changeset
|
211 usage (EXIT_FAILURE);
|
44419
|
212
|
49594
|
213 running_suid = (getuid () != geteuid ());
|
44419
|
214
|
49594
|
215 prefix = get_prefix (running_suid, user_prefix);
|
44481
|
216
|
49594
|
217 scorefile = malloc (strlen (prefix) + strlen (argv[optind]) + 2);
|
44184
|
218 if (!scorefile)
|
49594
|
219 lose_syserr ("Couldn't allocate score file");
|
44481
|
220
|
49594
|
221 strcpy (scorefile, prefix);
|
|
222 strcat (scorefile, "/");
|
|
223 strcat (scorefile, argv[optind]);
|
|
224 newscore = atoi (argv[optind+1]);
|
44184
|
225 newdata = argv[optind+2];
|
49594
|
226 if (strlen (newdata) > MAX_DATA_LEN)
|
44481
|
227 newdata[MAX_DATA_LEN] = '\0';
|
44987
|
228
|
49595
|
229 user_id = get_user_id ();
|
|
230 if (user_id == NULL)
|
49594
|
231 lose_syserr ("Couldn't determine user id");
|
49600
|
232
|
49594
|
233 if (stat (scorefile, &buf) < 0)
|
|
234 lose_syserr ("Failed to access scores file");
|
49600
|
235
|
49594
|
236 if (lock_file (scorefile, &lockstate) < 0)
|
|
237 lose_syserr ("Failed to lock scores file");
|
49600
|
238
|
49594
|
239 if (read_scores (scorefile, &scores, &scorecount) < 0)
|
44184
|
240 {
|
49594
|
241 unlock_file (scorefile, lockstate);
|
|
242 lose_syserr ("Failed to read scores file");
|
44184
|
243 }
|
49594
|
244 push_score (&scores, &scorecount, newscore, user_id, newdata);
|
104955
|
245 sort_scores (scores, scorecount, reverse);
|
44481
|
246 /* Limit the number of scores. If we're using reverse sorting, then
|
|
247 we should increment the beginning of the array, to skip over the
|
|
248 *smallest* scores. Otherwise, we just decrement the number of
|
|
249 scores, since the smallest will be at the end. */
|
|
250 if (scorecount > MAX_SCORES)
|
|
251 scorecount -= (scorecount - MAX_SCORES);
|
104955
|
252 if (reverse)
|
|
253 scores += (scorecount - MAX_SCORES);
|
49594
|
254 if (write_scores (scorefile, scores, scorecount) < 0)
|
44184
|
255 {
|
49594
|
256 unlock_file (scorefile, lockstate);
|
|
257 lose_syserr ("Failed to write scores file");
|
44184
|
258 }
|
49594
|
259 unlock_file (scorefile, lockstate);
|
55442
a47704955f8d
Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
diff
changeset
|
260 exit (EXIT_SUCCESS);
|
44184
|
261 }
|
|
262
|
|
263 int
|
109111
|
264 read_score (FILE *f, struct score_entry *score)
|
44184
|
265 {
|
|
266 int c;
|
49594
|
267 if (feof (f))
|
44184
|
268 return 1;
|
49594
|
269 while ((c = getc (f)) != EOF
|
|
270 && isdigit (c))
|
44402
|
271 {
|
|
272 score->score *= 10;
|
|
273 score->score += (c-48);
|
|
274 }
|
49594
|
275 while ((c = getc (f)) != EOF
|
|
276 && isspace (c))
|
44402
|
277 ;
|
|
278 if (c == EOF)
|
|
279 return -1;
|
49594
|
280 ungetc (c, f);
|
44402
|
281 #ifdef HAVE_GETDELIM
|
|
282 {
|
44566
|
283 size_t count = 0;
|
49594
|
284 if (getdelim (&score->username, &count, ' ', f) < 1
|
44402
|
285 || score->username == NULL)
|
|
286 return -1;
|
44789
|
287 /* Trim the space */
|
49594
|
288 score->username[strlen (score->username)-1] = '\0';
|
44184
|
289 }
|
44402
|
290 #else
|
|
291 {
|
|
292 int unameread = 0;
|
|
293 int unamelen = 30;
|
49594
|
294 char *username = malloc (unamelen);
|
44404
|
295 if (!username)
|
|
296 return -1;
|
49600
|
297
|
49594
|
298 while ((c = getc (f)) != EOF
|
|
299 && !isspace (c))
|
44402
|
300 {
|
44481
|
301 if (unameread >= unamelen-1)
|
49594
|
302 if (!(username = realloc (username, unamelen *= 2)))
|
44481
|
303 return -1;
|
44402
|
304 username[unameread] = c;
|
|
305 unameread++;
|
|
306 }
|
49600
|
307 if (c == EOF)
|
44404
|
308 return -1;
|
|
309 username[unameread] = '\0';
|
44402
|
310 score->username = username;
|
|
311 }
|
|
312 #endif
|
44184
|
313 #ifdef HAVE_GETLINE
|
|
314 score->data = NULL;
|
44566
|
315 errno = 0;
|
44184
|
316 {
|
44566
|
317 size_t len;
|
49594
|
318 if (getline (&score->data, &len, f) < 0)
|
44184
|
319 return -1;
|
49594
|
320 score->data[strlen (score->data)-1] = '\0';
|
44184
|
321 }
|
|
322 #else
|
44402
|
323 {
|
|
324 int cur = 0;
|
|
325 int len = 16;
|
49594
|
326 char *buf = malloc (len);
|
44402
|
327 if (!buf)
|
|
328 return -1;
|
49594
|
329 while ((c = getc (f)) != EOF
|
44404
|
330 && c != '\n')
|
44402
|
331 {
|
|
332 if (cur >= len-1)
|
|
333 {
|
49594
|
334 if (!(buf = realloc (buf, len *= 2)))
|
44402
|
335 return -1;
|
|
336 }
|
|
337 buf[cur] = c;
|
|
338 cur++;
|
|
339 }
|
|
340 score->data = buf;
|
44419
|
341 score->data[cur] = '\0';
|
44402
|
342 }
|
44184
|
343 #endif
|
|
344 return 0;
|
|
345 }
|
|
346
|
|
347 int
|
109111
|
348 read_scores (const char *filename, struct score_entry **scores, int *count)
|
44184
|
349 {
|
|
350 int readval, scorecount, cursize;
|
|
351 struct score_entry *ret;
|
49594
|
352 FILE *f = fopen (filename, "r");
|
49600
|
353 if (!f)
|
44184
|
354 return -1;
|
|
355 scorecount = 0;
|
|
356 cursize = 16;
|
49595
|
357 ret = (struct score_entry *) malloc (sizeof (struct score_entry) * cursize);
|
49600
|
358 if (!ret)
|
44184
|
359 return -1;
|
49594
|
360 while ((readval = read_score (f, &ret[scorecount])) == 0)
|
44184
|
361 {
|
|
362 /* We encoutered an error */
|
|
363 if (readval < 0)
|
|
364 return -1;
|
|
365 scorecount++;
|
|
366 if (scorecount >= cursize)
|
|
367 {
|
50718
|
368 cursize *= 2;
|
|
369 ret = (struct score_entry *)
|
|
370 realloc (ret, (sizeof (struct score_entry) * cursize));
|
44184
|
371 if (!ret)
|
|
372 return -1;
|
|
373 }
|
|
374 }
|
|
375 *count = scorecount;
|
|
376 *scores = ret;
|
|
377 return 0;
|
|
378 }
|
|
379
|
|
380 int
|
109111
|
381 score_compare (const void *a, const void *b)
|
44184
|
382 {
|
|
383 const struct score_entry *sa = (const struct score_entry *) a;
|
|
384 const struct score_entry *sb = (const struct score_entry *) b;
|
|
385 return (sb->score > sa->score) - (sb->score < sa->score);
|
|
386 }
|
|
387
|
|
388 int
|
109111
|
389 score_compare_reverse (const void *a, const void *b)
|
44184
|
390 {
|
|
391 const struct score_entry *sa = (const struct score_entry *) a;
|
|
392 const struct score_entry *sb = (const struct score_entry *) b;
|
|
393 return (sa->score > sb->score) - (sa->score < sb->score);
|
|
394 }
|
|
395
|
|
396 int
|
109111
|
397 push_score (struct score_entry **scores, int *count, int newscore, char *username, char *newdata)
|
44184
|
398 {
|
49594
|
399 struct score_entry *newscores
|
49595
|
400 = (struct score_entry *) realloc (*scores,
|
|
401 sizeof (struct score_entry) * ((*count) + 1));
|
44184
|
402 if (!newscores)
|
|
403 return -1;
|
|
404 newscores[*count].score = newscore;
|
44402
|
405 newscores[*count].username = username;
|
44184
|
406 newscores[*count].data = newdata;
|
|
407 (*count) += 1;
|
|
408 *scores = newscores;
|
|
409 return 0;
|
|
410 }
|
49600
|
411
|
44184
|
412 void
|
109111
|
413 sort_scores (struct score_entry *scores, int count, int reverse)
|
44184
|
414 {
|
49594
|
415 qsort (scores, count, sizeof (struct score_entry),
|
44184
|
416 reverse ? score_compare_reverse : score_compare);
|
|
417 }
|
|
418
|
|
419 int
|
109111
|
420 write_scores (const char *filename, const struct score_entry *scores, int count)
|
44184
|
421 {
|
49600
|
422 FILE *f;
|
44184
|
423 int i;
|
49594
|
424 char *tempfile = malloc (strlen (filename) + strlen (".tempXXXXXX") + 1);
|
44184
|
425 if (!tempfile)
|
|
426 return -1;
|
49594
|
427 strcpy (tempfile, filename);
|
|
428 strcat (tempfile, ".tempXXXXXX");
|
44481
|
429 #ifdef HAVE_MKSTEMP
|
49594
|
430 if (mkstemp (tempfile) < 0
|
44481
|
431 #else
|
49594
|
432 if (mktemp (tempfile) != tempfile
|
44481
|
433 #endif
|
49594
|
434 || !(f = fopen (tempfile, "w")))
|
44184
|
435 return -1;
|
|
436 for (i = 0; i < count; i++)
|
49594
|
437 if (fprintf (f, "%ld %s %s\n", scores[i].score, scores[i].username,
|
44402
|
438 scores[i].data) < 0)
|
44184
|
439 return -1;
|
49594
|
440 fclose (f);
|
|
441 if (rename (tempfile, filename) < 0)
|
44481
|
442 return -1;
|
49594
|
443 if (chmod (filename, 0644) < 0)
|
44481
|
444 return -1;
|
44184
|
445 return 0;
|
|
446 }
|
49600
|
447
|
44184
|
448 int
|
109111
|
449 lock_file (const char *filename, void **state)
|
44184
|
450 {
|
|
451 int fd;
|
44576
90866353c7bd
(lock_file): If the lock file is older than an hour, delete it. Reset
Colin Walters <walters@gnu.org>
diff
changeset
|
452 struct stat buf;
|
44184
|
453 int attempts = 0;
|
|
454 char *lockext = ".lockfile";
|
49594
|
455 char *lockpath = malloc (strlen (filename) + strlen (lockext) + 60);
|
44184
|
456 if (!lockpath)
|
|
457 return -1;
|
49594
|
458 strcpy (lockpath, filename);
|
|
459 strcat (lockpath, lockext);
|
44184
|
460 *state = lockpath;
|
|
461 trylock:
|
|
462 attempts++;
|
44576
90866353c7bd
(lock_file): If the lock file is older than an hour, delete it. Reset
Colin Walters <walters@gnu.org>
diff
changeset
|
463 /* If the lock is over an hour old, delete it. */
|
49594
|
464 if (stat (lockpath, &buf) == 0
|
|
465 && (difftime (buf.st_ctime, time (NULL) > 60*60)))
|
|
466 unlink (lockpath);
|
49595
|
467 fd = open (lockpath, O_CREAT | O_EXCL, 0600);
|
|
468 if (fd < 0)
|
44184
|
469 {
|
|
470 if (errno == EEXIST)
|
|
471 {
|
|
472 /* Break the lock; we won't corrupt the file, but we might
|
|
473 lose some scores. */
|
|
474 if (attempts > MAX_ATTEMPTS)
|
44576
90866353c7bd
(lock_file): If the lock file is older than an hour, delete it. Reset
Colin Walters <walters@gnu.org>
diff
changeset
|
475 {
|
49594
|
476 unlink (lockpath);
|
44576
90866353c7bd
(lock_file): If the lock file is older than an hour, delete it. Reset
Colin Walters <walters@gnu.org>
diff
changeset
|
477 attempts = 0;
|
90866353c7bd
(lock_file): If the lock file is older than an hour, delete it. Reset
Colin Walters <walters@gnu.org>
diff
changeset
|
478 }
|
49594
|
479 sleep ((rand () % 2)+1);
|
44184
|
480 goto trylock;
|
|
481 }
|
|
482 else
|
|
483 return -1;
|
|
484 }
|
49594
|
485 close (fd);
|
44184
|
486 return 0;
|
|
487 }
|
49600
|
488
|
44184
|
489 int
|
109111
|
490 unlock_file (const char *filename, void *state)
|
44184
|
491 {
|
|
492 char *lockpath = (char *) state;
|
49594
|
493 int ret = unlink (lockpath);
|
44184
|
494 int saved_errno = errno;
|
49594
|
495 free (lockpath);
|
44184
|
496 errno = saved_errno;
|
|
497 return ret;
|
|
498 }
|
52401
|
499
|
|
500 /* arch-tag: 2bf5c52e-4beb-463a-954e-c58b9c64736b
|
|
501 (do not change this comment) */
|
55442
a47704955f8d
Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
diff
changeset
|
502
|
a47704955f8d
Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
diff
changeset
|
503 /* update-game-score.c ends here */
|