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>
48 #define MAX_ATTEMPTS 5
49 #define MAX_SCORES 200
50 #define MAX_DATA_LEN 1024
52 #if !defined (__GNUC__) || __GNUC__ < 2
53 #define __attribute__(x)
59 fprintf(stdout
, "Usage: update-game-score [-m MAX ] [ -r ] game/scorefile SCORE DATA\n");
60 fprintf(stdout
, " update-game-score -h\n");
61 fprintf(stdout
, " -h\t\tDisplay this help.\n");
62 fprintf(stdout
, " -m MAX\t\tLimit the maximum number of scores to MAX.\n");
63 fprintf(stdout
, " -r\t\tSort the scores in increasing order.\n");
64 fprintf(stdout
, " -d DIR\t\tStore scores in DIR (only if not setuid).\n");
69 lock_file(const char *filename
, void **state
);
71 unlock_file(const char *filename
, void *state
);
81 read_scores(const char *filename
, struct score_entry
**scores
,
84 push_score(struct score_entry
**scores
, int *count
,
85 int newscore
, char *username
, char *newdata
);
87 sort_scores(struct score_entry
*scores
, int count
, int reverse
);
89 write_scores(const char *filename
, const struct score_entry
*scores
,
92 void lose(const char *msg
, ...)
93 __attribute__ ((format (printf
,1,0), noreturn
));
95 void lose(const char *msg
, ...)
99 vfprintf(stderr
, msg
, ap
);
108 struct passwd
*buf
= getpwuid(getuid());
112 int uid
= (int) getuid();
116 name
= malloc(count
+1);
119 sprintf(name
, "%d", uid
);
126 get_prefix(int running_suid
, char *user_prefix
)
128 if (!running_suid
&& user_prefix
== NULL
)
129 lose("Not using a shared game directory, and no prefix given.\n");
132 #ifdef HAVE_SHARED_GAME_DIR
133 return HAVE_SHARED_GAME_DIR
;
135 lose("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.\n");
142 main(int argc
, char **argv
)
146 char *user_id
, *scorefile
, *prefix
, *user_prefix
= NULL
;
148 struct score_entry
*scores
;
149 int newscore
, scorecount
, reverse
= 0, max
= MAX_SCORES
;
154 while ((c
= getopt(argc
, argv
, "hrm:d:")) != -1)
161 user_prefix
= optarg
;
168 if (max
> MAX_SCORES
)
175 if (optind
+3 != argc
)
178 running_suid
= (getuid() != geteuid());
180 prefix
= get_prefix(running_suid
, user_prefix
);
182 scorefile
= malloc(strlen(prefix
) + strlen(argv
[optind
]) + 2);
184 lose("Couldn't create score file name: %s\n", strerror(errno
));
186 strcpy(scorefile
, prefix
);
187 strcat(scorefile
, "/");
188 strcat(scorefile
, argv
[optind
]);
189 newscore
= atoi(argv
[optind
+1]);
190 newdata
= argv
[optind
+2];
191 if (strlen(newdata
) > MAX_DATA_LEN
)
192 newdata
[MAX_DATA_LEN
] = '\0';
194 if ((user_id
= get_user_id()) == NULL
)
195 lose("Couldn't determine user id: %s\n", strerror(errno
));
197 if (stat(scorefile
, &buf
) < 0)
198 lose("Failed to access scores file \"%s\": %s\n", scorefile
,
200 if (lock_file(scorefile
, &lockstate
) < 0)
201 lose("Failed to lock scores file \"%s\": %s\n",
202 scorefile
, strerror(errno
));
203 if (read_scores(scorefile
, &scores
, &scorecount
) < 0)
205 unlock_file(scorefile
, lockstate
);
206 lose("Failed to read scores file \"%s\": %s\n", scorefile
,
209 push_score(&scores
, &scorecount
, newscore
, user_id
, newdata
);
210 /* Limit the number of scores. If we're using reverse sorting, then
211 we should increment the beginning of the array, to skip over the
212 *smallest* scores. Otherwise, we just decrement the number of
213 scores, since the smallest will be at the end. */
214 if (scorecount
> MAX_SCORES
)
215 scorecount
-= (scorecount
- MAX_SCORES
);
217 scores
+= (scorecount
- MAX_SCORES
);
218 sort_scores(scores
, scorecount
, reverse
);
219 if (write_scores(scorefile
, scores
, scorecount
) < 0)
221 unlock_file(scorefile
, lockstate
);
222 lose("Failed to write scores file \"%s\": %s\n", scorefile
,
225 unlock_file(scorefile
, lockstate
);
230 read_score(FILE *f
, struct score_entry
*score
)
235 while ((c
= getc(f
)) != EOF
239 score
->score
+= (c
-48);
241 while ((c
= getc(f
)) != EOF
250 if (getdelim(&score
->username
, &count
, ' ', f
) < 1
251 || score
->username
== NULL
)
254 score
->username
[strlen(score
->username
)-1] = '\0';
260 char *username
= malloc(unamelen
);
264 while ((c
= getc(f
)) != EOF
267 if (unameread
>= unamelen
-1)
268 if (!(username
= realloc(username
, unamelen
*= 2)))
270 username
[unameread
] = c
;
275 username
[unameread
] = '\0';
276 score
->username
= username
;
284 if (getline(&score
->data
, &len
, f
) < 0)
286 score
->data
[strlen(score
->data
)-1] = '\0';
292 char *buf
= malloc(len
);
295 while ((c
= getc(f
)) != EOF
300 if (!(buf
= realloc(buf
, len
*= 2)))
307 score
->data
[cur
] = '\0';
314 read_scores(const char *filename
, struct score_entry
**scores
,
317 int readval
, scorecount
, cursize
;
318 struct score_entry
*ret
;
319 FILE *f
= fopen(filename
, "r");
324 ret
= malloc(sizeof(struct score_entry
) * cursize
);
327 while ((readval
= read_score(f
, &ret
[scorecount
])) == 0)
329 /* We encoutered an error */
333 if (scorecount
>= cursize
)
335 ret
= realloc(ret
, cursize
*= 2);
346 score_compare(const void *a
, const void *b
)
348 const struct score_entry
*sa
= (const struct score_entry
*) a
;
349 const struct score_entry
*sb
= (const struct score_entry
*) b
;
350 return (sb
->score
> sa
->score
) - (sb
->score
< sa
->score
);
354 score_compare_reverse(const void *a
, const void *b
)
356 const struct score_entry
*sa
= (const struct score_entry
*) a
;
357 const struct score_entry
*sb
= (const struct score_entry
*) b
;
358 return (sa
->score
> sb
->score
) - (sa
->score
< sb
->score
);
362 push_score(struct score_entry
**scores
, int *count
,
363 int newscore
, char *username
, char *newdata
)
365 struct score_entry
*newscores
= realloc(*scores
,
366 sizeof(struct score_entry
) * ((*count
) + 1));
369 newscores
[*count
].score
= newscore
;
370 newscores
[*count
].username
= username
;
371 newscores
[*count
].data
= newdata
;
378 sort_scores(struct score_entry
*scores
, int count
, int reverse
)
380 qsort(scores
, count
, sizeof(struct score_entry
),
381 reverse
? score_compare_reverse
: score_compare
);
385 write_scores(const char *filename
, const struct score_entry
*scores
,
390 char *tempfile
= malloc(strlen(filename
) + strlen(".tempXXXXXX") + 1);
393 strcpy(tempfile
, filename
);
394 strcat(tempfile
, ".tempXXXXXX");
396 if (mkstemp(tempfile
) < 0
398 if (mktemp(tempfile
) != tempfile
400 || !(f
= fopen(tempfile
, "w")))
402 for (i
= 0; i
< count
; i
++)
403 if (fprintf(f
, "%ld %s %s\n", scores
[i
].score
, scores
[i
].username
,
407 if (rename(tempfile
, filename
) < 0)
409 if (chmod(filename
, 0644) < 0)
415 lock_file(const char *filename
, void **state
)
420 char *lockext
= ".lockfile";
421 char *lockpath
= malloc(strlen(filename
) + strlen(lockext
) + 60);
424 strcpy(lockpath
, filename
);
425 strcat(lockpath
, lockext
);
429 /* If the lock is over an hour old, delete it. */
430 if (stat(lockpath
, &buf
) == 0
431 && (difftime(buf
.st_ctime
, time(NULL
) > 60*60)))
433 if ((fd
= open(lockpath
, O_CREAT
| O_EXCL
, 0600)) < 0)
437 /* Break the lock; we won't corrupt the file, but we might
439 if (attempts
> MAX_ATTEMPTS
)
444 sleep((rand() % 2)+1);
455 unlock_file(const char *filename
, void *state
)
457 char *lockpath
= (char *) state
;
458 int ret
= unlink(lockpath
);
459 int saved_errno
= errno
;