summaryrefslogtreecommitdiffstats
path: root/util/tunepasswd.c
blob: b617fb773ed3b7c6878c4f17de626dbede6dcc4c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* $Id$ */
#include "bbs.h"

int tune(int num) {
    int i, j, fin, fout;
    userec_t u;
    
    if((fin = open(FN_PASSWD, O_RDONLY)) == -1) {
    perror(FN_PASSWD);
    return 1;
    }
    if(flock(fin, LOCK_EX)) {
    printf("Lock failed!\n");
    return 1;
    }
    if((fout = open(FN_PASSWD ".tune" , O_WRONLY | O_CREAT, 0600)) == -1) {
    perror(FN_PASSWD ".tune");
    flock(fin, LOCK_UN);
    close(fin);
    return 1;
    }
    
    for(i = j = 0; i < num; i++) {
    read(fin, &u, sizeof(u));
    if(u.userid[0]) {
        if(j == MAX_USERS) {
        printf("MAX_USERS is too small!\n");
        close(fout);
        unlink(FN_PASSWD ".tune");
        flock(fin, LOCK_UN);
        close(fin);
        return 1;
        }
        write(fout, &u, sizeof(u));
        j++;
    }
    }
    for(memset(&u, 0, sizeof(u)); j < MAX_USERS; j++) {
    write(fout, &u, sizeof(u));
    }
    close(fout);
    
    /* backup */
    unlink(FN_PASSWD "~");
    link(FN_PASSWD, FN_PASSWD "~");
    unlink(FN_PASSWD);
    link(FN_PASSWD ".tune", FN_PASSWD);
    unlink(FN_PASSWD ".tune");
    
    flock(fin, LOCK_UN);
    close(fin);
    return 0;
}

int main() {
    struct stat sb;
    
    if(stat(FN_PASSWD, &sb)) {
    perror("stat");
    return 1;
    }
    if(sb.st_size != sizeof(userec_t) * MAX_USERS) {
    printf("size and MAX_USERS do not match!\n");
    if(tune(sb.st_size / sizeof(userec_t)) == 0)
        printf(FN_PASSWD " has been tuned successfully!\n");
    } else
    printf("Nothing to do.\n");
    return 0;
}