Commit | Line | Data |
---|---|---|
2f1de3dd CW |
1 | /* update-game-score.c --- Update a score file |
2 | Copyright (C) 2002 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GNU Emacs. | |
5 | ||
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) | |
9 | any later version. | |
10 | ||
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. | |
15 | ||
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. */ | |
20 | ||
21 | /* This program is allows a game to securely and atomically update a | |
8eec804f CW |
22 | score file. It should be installed setuid, owned by an appropriate |
23 | user like `games'. | |
24 | ||
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). | |
2f1de3dd CW |
28 | |
29 | Created 2002/03/22, by Colin Walters <walters@debian.org> | |
30 | */ | |
31 | ||
32 | #define _GNU_SOURCE | |
33 | ||
c42d6dbd EZ |
34 | #include <config.h> |
35 | ||
2f1de3dd CW |
36 | #include <unistd.h> |
37 | #include <errno.h> | |
38 | #include <string.h> | |
39 | #include <stdlib.h> | |
40 | #include <stdio.h> | |
41 | #include <time.h> | |
cd553ffb | 42 | #include <pwd.h> |
2f1de3dd CW |
43 | #include <ctype.h> |
44 | #include <fcntl.h> | |
8eec804f | 45 | #include <stdarg.h> |
2f1de3dd | 46 | #include <sys/stat.h> |
2f1de3dd CW |
47 | |
48 | #define MAX_ATTEMPTS 5 | |
8eec804f CW |
49 | #define MAX_SCORES 200 |
50 | #define MAX_DATA_LEN 1024 | |
5795b420 CW |
51 | |
52 | #ifdef HAVE_SHARED_GAME_DIR | |
53 | #define SCORE_FILE_PREFIX HAVE_SHARED_GAME_DIR | |
54 | #else | |
8eec804f CW |
55 | #define SCORE_FILE_PREFIX "~/.emacs.d/games" |
56 | #endif | |
57 | ||
58 | #if !defined (__GNUC__) || __GNUC__ < 2 | |
59 | #define __attribute__(x) | |
5795b420 | 60 | #endif |
2f1de3dd CW |
61 | |
62 | int | |
63 | usage(int err) | |
64 | { | |
65 | fprintf(stdout, "Usage: update-game-score [-m MAX ] [ -r ] game/scorefile SCORE DATA\n"); | |
66 | fprintf(stdout, " update-game-score -h\n"); | |
67 | fprintf(stdout, " -h\t\tDisplay this help.\n"); | |
68 | fprintf(stdout, " -m MAX\t\tLimit the maximum number of scores to MAX.\n"); | |
69 | fprintf(stdout, " -r\t\tSort the scores in increasing order.\n"); | |
70 | exit(err); | |
71 | } | |
72 | ||
73 | int | |
74 | lock_file(const char *filename, void **state); | |
75 | int | |
76 | unlock_file(const char *filename, void *state); | |
77 | ||
78 | struct score_entry | |
79 | { | |
80 | long score; | |
cd553ffb | 81 | char *username; |
2f1de3dd CW |
82 | char *data; |
83 | }; | |
84 | ||
85 | int | |
86 | read_scores(const char *filename, struct score_entry **scores, | |
87 | int *count); | |
88 | int | |
89 | push_score(struct score_entry **scores, int *count, | |
cd553ffb | 90 | int newscore, char *username, char *newdata); |
2f1de3dd CW |
91 | void |
92 | sort_scores(struct score_entry *scores, int count, int reverse); | |
93 | int | |
94 | write_scores(const char *filename, const struct score_entry *scores, | |
95 | int count); | |
96 | ||
cd553ffb | 97 | char * |
5795b420 | 98 | get_user_id(struct passwd *buf) |
cd553ffb CW |
99 | { |
100 | char *name; | |
cd553ffb CW |
101 | if (!buf) |
102 | { | |
103 | int count = 1; | |
104 | int uid = (int) getuid(); | |
8eec804f CW |
105 | int tuid = uid; |
106 | while (tuid /= 10) | |
cd553ffb CW |
107 | count++; |
108 | name = malloc(count+1); | |
109 | sprintf(name, "%d", uid); | |
110 | return name; | |
111 | } | |
112 | return buf->pw_name; | |
113 | } | |
114 | ||
5795b420 CW |
115 | char * |
116 | get_home_dir(struct passwd *buf) | |
117 | { | |
118 | if (!buf) | |
119 | return NULL; | |
120 | return buf->pw_dir; | |
121 | } | |
122 | ||
8eec804f CW |
123 | void lose(const char *msg, ...) |
124 | __attribute__ ((format (printf,1,0), noreturn)); | |
125 | ||
126 | void lose(const char *msg, ...) | |
127 | { | |
128 | va_list ap; | |
129 | va_start(ap, msg); | |
130 | vfprintf(stderr, msg, ap); | |
131 | va_end(ap); | |
132 | exit(1); | |
133 | } | |
134 | ||
2f1de3dd CW |
135 | int |
136 | main(int argc, char **argv) | |
137 | { | |
138 | int c; | |
139 | void *lockstate; | |
5795b420 | 140 | char *scorefile, *prefix; |
2f1de3dd CW |
141 | struct stat buf; |
142 | struct score_entry *scores; | |
8eec804f | 143 | int newscore, scorecount, reverse = 0, max = MAX_SCORES; |
2f1de3dd | 144 | char *newdata; |
5795b420 | 145 | struct passwd *passwdbuf; |
2f1de3dd CW |
146 | |
147 | srand(time(0)); | |
148 | ||
149 | while ((c = getopt(argc, argv, "hrm:")) != -1) | |
150 | switch (c) | |
151 | { | |
152 | case 'h': | |
153 | usage(0); | |
154 | break; | |
155 | case 'r': | |
156 | reverse = 1; | |
157 | break; | |
158 | case 'm': | |
159 | max = atoi(optarg); | |
8eec804f CW |
160 | if (max > MAX_SCORES) |
161 | max = MAX_SCORES; | |
2f1de3dd CW |
162 | break; |
163 | default: | |
164 | usage(1); | |
165 | } | |
166 | ||
167 | if (optind+3 != argc) | |
168 | usage(1); | |
5795b420 CW |
169 | |
170 | passwdbuf = getpwuid(getuid()); | |
171 | ||
8eec804f | 172 | if (!strncmp(SCORE_FILE_PREFIX, "~", 1)) |
5795b420 | 173 | { |
8eec804f CW |
174 | char *homedir = get_home_dir(passwdbuf); |
175 | if (!homedir) | |
176 | lose("Unable to determine home directory\n"); | |
177 | prefix = malloc(strlen(homedir) + strlen(SCORE_FILE_PREFIX) + 1); | |
178 | strcpy(prefix, homedir); | |
179 | /* Skip over the '~'. */ | |
180 | strcat(prefix, SCORE_FILE_PREFIX+1); | |
5795b420 CW |
181 | } |
182 | else | |
8eec804f CW |
183 | prefix = strdup(SCORE_FILE_PREFIX); |
184 | ||
185 | if (!prefix) | |
186 | lose("Couldn't create score file name: %s\n", strerror(errno)); | |
187 | ||
188 | scorefile = malloc(strlen(prefix) + strlen(argv[optind]) + 2); | |
2f1de3dd | 189 | if (!scorefile) |
8eec804f CW |
190 | lose("Couldn't create score file name: %s\n", strerror(errno)); |
191 | ||
5795b420 | 192 | strcpy(scorefile, prefix); |
8eec804f CW |
193 | free(prefix); |
194 | strcat(scorefile, "/"); | |
2f1de3dd CW |
195 | strcat(scorefile, argv[optind]); |
196 | newscore = atoi(argv[optind+1]); | |
197 | newdata = argv[optind+2]; | |
8eec804f CW |
198 | if (strlen(newdata) > MAX_DATA_LEN) |
199 | newdata[MAX_DATA_LEN] = '\0'; | |
2f1de3dd CW |
200 | |
201 | if (stat(scorefile, &buf) < 0) | |
8eec804f CW |
202 | lose("Failed to access scores file \"%s\": %s\n", scorefile, |
203 | strerror(errno)); | |
2f1de3dd | 204 | if (lock_file(scorefile, &lockstate) < 0) |
8eec804f CW |
205 | lose("Failed to lock scores file \"%s\": %s\n", |
206 | scorefile, strerror(errno)); | |
2f1de3dd CW |
207 | if (read_scores(scorefile, &scores, &scorecount) < 0) |
208 | { | |
8eec804f CW |
209 | unlock_file(scorefile, lockstate); |
210 | lose("Failed to read scores file \"%s\": %s\n", scorefile, | |
211 | strerror(errno)); | |
2f1de3dd | 212 | } |
5795b420 | 213 | push_score(&scores, &scorecount, newscore, get_user_id(passwdbuf), newdata); |
8eec804f CW |
214 | /* Limit the number of scores. If we're using reverse sorting, then |
215 | we should increment the beginning of the array, to skip over the | |
216 | *smallest* scores. Otherwise, we just decrement the number of | |
217 | scores, since the smallest will be at the end. */ | |
218 | if (scorecount > MAX_SCORES) | |
219 | scorecount -= (scorecount - MAX_SCORES); | |
220 | if (reverse) | |
221 | scores += (scorecount - MAX_SCORES); | |
2f1de3dd CW |
222 | sort_scores(scores, scorecount, reverse); |
223 | if (write_scores(scorefile, scores, scorecount) < 0) | |
224 | { | |
8eec804f CW |
225 | unlock_file(scorefile, lockstate); |
226 | lose("Failed to write scores file \"%s\": %s\n", scorefile, | |
227 | strerror(errno)); | |
2f1de3dd CW |
228 | } |
229 | unlock_file(scorefile, lockstate); | |
230 | exit(0); | |
2f1de3dd CW |
231 | } |
232 | ||
233 | int | |
234 | read_score(FILE *f, struct score_entry *score) | |
235 | { | |
236 | int c; | |
237 | if (feof(f)) | |
238 | return 1; | |
239 | while ((c = getc(f)) != EOF | |
cd553ffb CW |
240 | && isdigit(c)) |
241 | { | |
242 | score->score *= 10; | |
243 | score->score += (c-48); | |
244 | } | |
245 | while ((c = getc(f)) != EOF | |
246 | && isspace(c)) | |
247 | ; | |
248 | if (c == EOF) | |
249 | return -1; | |
f5bceaf8 | 250 | ungetc(c, f); |
cd553ffb CW |
251 | #ifdef HAVE_GETDELIM |
252 | { | |
7605f1bd | 253 | size_t count = 0; |
cd553ffb CW |
254 | if (getdelim(&score->username, &count, ' ', f) < 1 |
255 | || score->username == NULL) | |
256 | return -1; | |
2f1de3dd | 257 | } |
cd553ffb CW |
258 | #else |
259 | { | |
260 | int unameread = 0; | |
261 | int unamelen = 30; | |
f5bceaf8 CW |
262 | char *username = malloc(unamelen); |
263 | if (!username) | |
264 | return -1; | |
cd553ffb CW |
265 | |
266 | while ((c = getc(f)) != EOF | |
267 | && !isspace(c)) | |
268 | { | |
8eec804f CW |
269 | if (unameread >= unamelen-1) |
270 | if (!(username = realloc(username, unamelen *= 2))) | |
271 | return -1; | |
cd553ffb CW |
272 | username[unameread] = c; |
273 | unameread++; | |
274 | } | |
f5bceaf8 CW |
275 | if (c == EOF) |
276 | return -1; | |
277 | username[unameread] = '\0'; | |
cd553ffb CW |
278 | score->username = username; |
279 | } | |
280 | #endif | |
2f1de3dd CW |
281 | #ifdef HAVE_GETLINE |
282 | score->data = NULL; | |
7605f1bd | 283 | errno = 0; |
2f1de3dd | 284 | { |
7605f1bd | 285 | size_t len; |
2f1de3dd CW |
286 | if (getline(&score->data, &len, f) < 0) |
287 | return -1; | |
5795b420 | 288 | score->data[strlen(score->data)-1] = '\0'; |
2f1de3dd CW |
289 | } |
290 | #else | |
cd553ffb CW |
291 | { |
292 | int cur = 0; | |
293 | int len = 16; | |
294 | char *buf = malloc(len); | |
295 | if (!buf) | |
296 | return -1; | |
f5bceaf8 CW |
297 | while ((c = getc(f)) != EOF |
298 | && c != '\n') | |
cd553ffb CW |
299 | { |
300 | if (cur >= len-1) | |
301 | { | |
302 | if (!(buf = realloc(buf, len *= 2))) | |
303 | return -1; | |
304 | } | |
305 | buf[cur] = c; | |
306 | cur++; | |
307 | } | |
308 | score->data = buf; | |
5795b420 | 309 | score->data[cur] = '\0'; |
cd553ffb | 310 | } |
2f1de3dd | 311 | #endif |
2f1de3dd CW |
312 | return 0; |
313 | } | |
314 | ||
315 | int | |
316 | read_scores(const char *filename, struct score_entry **scores, | |
317 | int *count) | |
318 | { | |
319 | int readval, scorecount, cursize; | |
320 | struct score_entry *ret; | |
321 | FILE *f = fopen(filename, "r"); | |
322 | if (!f) | |
323 | return -1; | |
324 | scorecount = 0; | |
325 | cursize = 16; | |
326 | ret = malloc(sizeof(struct score_entry) * cursize); | |
327 | if (!ret) | |
328 | return -1; | |
329 | while ((readval = read_score(f, &ret[scorecount])) == 0) | |
330 | { | |
331 | /* We encoutered an error */ | |
332 | if (readval < 0) | |
333 | return -1; | |
334 | scorecount++; | |
335 | if (scorecount >= cursize) | |
336 | { | |
337 | ret = realloc(ret, cursize *= 2); | |
338 | if (!ret) | |
339 | return -1; | |
340 | } | |
341 | } | |
342 | *count = scorecount; | |
343 | *scores = ret; | |
344 | return 0; | |
345 | } | |
346 | ||
347 | int | |
348 | score_compare(const void *a, const void *b) | |
349 | { | |
350 | const struct score_entry *sa = (const struct score_entry *) a; | |
351 | const struct score_entry *sb = (const struct score_entry *) b; | |
352 | return (sb->score > sa->score) - (sb->score < sa->score); | |
353 | } | |
354 | ||
355 | int | |
356 | score_compare_reverse(const void *a, const void *b) | |
357 | { | |
358 | const struct score_entry *sa = (const struct score_entry *) a; | |
359 | const struct score_entry *sb = (const struct score_entry *) b; | |
360 | return (sa->score > sb->score) - (sa->score < sb->score); | |
361 | } | |
362 | ||
363 | int | |
364 | push_score(struct score_entry **scores, int *count, | |
cd553ffb | 365 | int newscore, char *username, char *newdata) |
2f1de3dd CW |
366 | { |
367 | struct score_entry *newscores = realloc(*scores, | |
368 | sizeof(struct score_entry) * ((*count) + 1)); | |
369 | if (!newscores) | |
370 | return -1; | |
371 | newscores[*count].score = newscore; | |
cd553ffb | 372 | newscores[*count].username = username; |
2f1de3dd CW |
373 | newscores[*count].data = newdata; |
374 | (*count) += 1; | |
375 | *scores = newscores; | |
376 | return 0; | |
377 | } | |
378 | ||
379 | void | |
380 | sort_scores(struct score_entry *scores, int count, int reverse) | |
381 | { | |
382 | qsort(scores, count, sizeof(struct score_entry), | |
383 | reverse ? score_compare_reverse : score_compare); | |
384 | } | |
385 | ||
386 | int | |
387 | write_scores(const char *filename, const struct score_entry *scores, | |
388 | int count) | |
389 | { | |
390 | FILE *f; | |
391 | int i; | |
392 | char *tempfile = malloc(strlen(filename) + strlen(".tempXXXXXX") + 1); | |
393 | if (!tempfile) | |
394 | return -1; | |
395 | strcpy(tempfile, filename); | |
396 | strcat(tempfile, ".tempXXXXXX"); | |
8eec804f | 397 | #ifdef HAVE_MKSTEMP |
2f1de3dd | 398 | if (mkstemp(tempfile) < 0 |
8eec804f CW |
399 | #else |
400 | if (mktemp(tempfile) != tempfile | |
401 | #endif | |
2f1de3dd CW |
402 | || !(f = fopen(tempfile, "w"))) |
403 | return -1; | |
404 | for (i = 0; i < count; i++) | |
cd553ffb CW |
405 | if (fprintf(f, "%ld %s %s\n", scores[i].score, scores[i].username, |
406 | scores[i].data) < 0) | |
2f1de3dd CW |
407 | return -1; |
408 | fclose(f); | |
8eec804f | 409 | if (rename(tempfile, filename) < 0) |
2f1de3dd | 410 | return -1; |
8eec804f | 411 | if (chmod(filename, 0644) < 0) |
2f1de3dd | 412 | return -1; |
8eec804f | 413 | return 0; |
2f1de3dd CW |
414 | } |
415 | ||
2f1de3dd CW |
416 | int |
417 | lock_file(const char *filename, void **state) | |
418 | { | |
419 | int fd; | |
7c4f6873 | 420 | struct stat buf; |
2f1de3dd CW |
421 | int attempts = 0; |
422 | char *lockext = ".lockfile"; | |
423 | char *lockpath = malloc(strlen(filename) + strlen(lockext) + 60); | |
424 | if (!lockpath) | |
425 | return -1; | |
426 | strcpy(lockpath, filename); | |
427 | strcat(lockpath, lockext); | |
428 | *state = lockpath; | |
429 | trylock: | |
430 | attempts++; | |
7c4f6873 CW |
431 | /* If the lock is over an hour old, delete it. */ |
432 | if (stat(lockpath, &buf) == 0 | |
433 | && (difftime(buf.st_ctime, time(NULL) > 60*60))) | |
434 | unlink(lockpath); | |
2f1de3dd CW |
435 | if ((fd = open(lockpath, O_CREAT | O_EXCL, 0600)) < 0) |
436 | { | |
437 | if (errno == EEXIST) | |
438 | { | |
439 | /* Break the lock; we won't corrupt the file, but we might | |
440 | lose some scores. */ | |
441 | if (attempts > MAX_ATTEMPTS) | |
7c4f6873 CW |
442 | { |
443 | unlink(lockpath); | |
444 | attempts = 0; | |
445 | } | |
8eec804f | 446 | sleep((rand() % 2)+1); |
2f1de3dd CW |
447 | goto trylock; |
448 | } | |
449 | else | |
450 | return -1; | |
451 | } | |
452 | close(fd); | |
453 | return 0; | |
454 | } | |
455 | ||
456 | int | |
457 | unlock_file(const char *filename, void *state) | |
458 | { | |
459 | char *lockpath = (char *) state; | |
460 | int ret = unlink(lockpath); | |
461 | int saved_errno = errno; | |
462 | free(lockpath); | |
463 | errno = saved_errno; | |
464 | return ret; | |
465 | } |