~clint-fewbar/ubuntu/precise/squid3/ignore-sighup-early

« back to all changes in this revision

Viewing changes to helpers/digest_auth/password/text_backend.c

  • Committer: Bazaar Package Importer
  • Author(s): Luigi Gangitano
  • Date: 2010-05-04 11:15:49 UTC
  • mfrom: (1.3.1 upstream)
  • mto: (20.3.1 squeeze) (21.2.1 sid)
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: james.westby@ubuntu.com-20100504111549-1apjh2g5sndki4te
Tags: upstream-3.1.3
ImportĀ upstreamĀ versionĀ 3.1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 * - comment lines are possible and should start with a '#';
12
12
 * - empty or blank lines are possible;
13
13
 * - file format is username:plaintext or username:realm:HA1
14
 
 * 
 
14
 *
15
15
 * To build a directory integrated backend, you need to be able to
16
16
 * calculate the HA1 returned to squid. To avoid storing a plaintext
17
17
 * password you can calculate MD5(username:realm:password) when the
53
53
}
54
54
 
55
55
static void
56
 
read_passwd_file(const char *passwdfile, int ha1mode)
 
56
read_passwd_file(const char *passwordFile, int isHa1Mode)
57
57
{
58
58
    FILE *f;
59
59
    char buf[8192];
64
64
    char *realm;
65
65
 
66
66
    if (hash != NULL) {
67
 
        hashFreeItems(hash, my_free);
 
67
        hashFreeItems(hash, my_free);
68
68
    }
69
69
    /* initial setup */
70
70
    hash = hash_create((HASHCMP *) strcmp, 7921, hash_string);
71
71
    if (NULL == hash) {
72
 
        fprintf(stderr, "digest_pw_auth: cannot create hash table\n");
73
 
        exit(1);
 
72
        fprintf(stderr, "digest_pw_auth: cannot create hash table\n");
 
73
        exit(1);
74
74
    }
75
 
    f = fopen(passwdfile, "r");
 
75
    f = fopen(passwordFile, "r");
76
76
    while (fgets(buf, 8192, f) != NULL) {
77
 
        if ((buf[0] == '#') || (buf[0] == ' ') || (buf[0] == '\t') ||
78
 
            (buf[0] == '\n'))
79
 
            continue;
80
 
        user = strtok(buf, ":\n");
81
 
        realm = strtok(NULL, ":\n");
82
 
        passwd = strtok(NULL, ":\n");
83
 
        if (!passwd) {
84
 
            passwd = realm;
85
 
            realm = NULL;
86
 
        }
87
 
        if ((strlen(user) > 0) && passwd) {
88
 
            if (strncmp(passwd, "{HHA1}", 6) == 0) {
89
 
                ha1 = passwd + 6;
90
 
                passwd = NULL;
91
 
            } else if (ha1mode) {
92
 
                ha1 = passwd;
93
 
                passwd = NULL;
94
 
            }
95
 
            if (ha1 && strlen(ha1) != 32) {
96
 
                /* We cannot accept plaintext passwords when using HA1 encoding,
97
 
                 * as the passwords may be output to cache.log if debugging is on.
98
 
                 */
99
 
                fprintf(stderr, "digest_pw_auth: ignoring invalid password for %s\n", user);
100
 
                continue;
101
 
            }
102
 
            u = xcalloc(1, sizeof(*u));
103
 
            if (realm) {
104
 
                int len = strlen(user) + strlen(realm) + 2;
105
 
                u->hash.key = malloc(len);
106
 
                snprintf(u->hash.key, len, "%s:%s", user, realm);
107
 
            } else {
108
 
                u->hash.key = xstrdup(user);
109
 
            }
110
 
            if (ha1)
111
 
                u->ha1 = xstrdup(ha1);
112
 
            else
113
 
                u->passwd = xstrdup(passwd);
114
 
            hash_join(hash, &u->hash);
115
 
        }
 
77
        if ((buf[0] == '#') || (buf[0] == ' ') || (buf[0] == '\t') ||
 
78
                (buf[0] == '\n'))
 
79
            continue;
 
80
        user = strtok(buf, ":\n");
 
81
        realm = strtok(NULL, ":\n");
 
82
        passwd = strtok(NULL, ":\n");
 
83
        if (!passwd) {
 
84
            passwd = realm;
 
85
            realm = NULL;
 
86
        }
 
87
        if ((strlen(user) > 0) && passwd) {
 
88
            if (strncmp(passwd, "{HHA1}", 6) == 0) {
 
89
                ha1 = passwd + 6;
 
90
                passwd = NULL;
 
91
            } else if (isHa1Mode) {
 
92
                ha1 = passwd;
 
93
                passwd = NULL;
 
94
            }
 
95
            if (ha1 && strlen(ha1) != 32) {
 
96
                /* We cannot accept plaintext passwords when using HA1 encoding,
 
97
                 * as the passwords may be output to cache.log if debugging is on.
 
98
                 */
 
99
                fprintf(stderr, "digest_pw_auth: ignoring invalid password for %s\n", user);
 
100
                continue;
 
101
            }
 
102
            u = xcalloc(1, sizeof(*u));
 
103
            if (realm) {
 
104
                int len = strlen(user) + strlen(realm) + 2;
 
105
                u->hash.key = malloc(len);
 
106
                snprintf(u->hash.key, len, "%s:%s", user, realm);
 
107
            } else {
 
108
                u->hash.key = xstrdup(user);
 
109
            }
 
110
            if (ha1)
 
111
                u->ha1 = xstrdup(ha1);
 
112
            else
 
113
                u->passwd = xstrdup(passwd);
 
114
            hash_join(hash, &u->hash);
 
115
        }
116
116
    }
117
117
    fclose(f);
118
118
}
123
123
{
124
124
    struct stat sb;
125
125
    if (argc == 2)
126
 
        passwdfile = argv[1];
 
126
        passwdfile = argv[1];
127
127
    if ((argc == 3) && !strcmp("-c", argv[1])) {
128
 
        ha1mode = 1;
129
 
        passwdfile = argv[2];
 
128
        ha1mode = 1;
 
129
        passwdfile = argv[2];
130
130
    }
131
131
    if (!passwdfile) {
132
 
        fprintf(stderr, "Usage: digest_pw_auth [OPTIONS] <passwordfile>\n");
133
 
        fprintf(stderr, "  -c   accept digest hashed passwords rather than plaintext in passwordfile\n");
134
 
        exit(1);
 
132
        fprintf(stderr, "Usage: digest_pw_auth [OPTIONS] <passwordfile>\n");
 
133
        fprintf(stderr, "  -c   accept digest hashed passwords rather than plaintext in passwordfile\n");
 
134
        exit(1);
135
135
    }
136
136
    if (stat(passwdfile, &sb) != 0) {
137
 
        fprintf(stderr, "cannot stat %s\n", passwdfile);
138
 
        exit(1);
 
137
        fprintf(stderr, "cannot stat %s\n", passwdfile);
 
138
        exit(1);
139
139
    }
140
140
}
141
141
 
147
147
    char buf[256];
148
148
    int len;
149
149
    if (stat(passwdfile, &sb) == 0) {
150
 
        if (sb.st_mtime != change_time) {
151
 
            read_passwd_file(passwdfile, ha1mode);
152
 
            change_time = sb.st_mtime;
153
 
        }
 
150
        if (sb.st_mtime != change_time) {
 
151
            read_passwd_file(passwdfile, ha1mode);
 
152
            change_time = sb.st_mtime;
 
153
        }
154
154
    }
155
155
    if (!hash)
156
 
        return NULL;
 
156
        return NULL;
157
157
    len = snprintf(buf, sizeof(buf), "%s:%s", requestData->user, requestData->realm);
158
158
    if (len >= sizeof(buf))
159
 
        return NULL;
 
159
        return NULL;
160
160
    u = (user_data *) hash_lookup(hash, buf);
161
161
    if (u)
162
 
        return u;
 
162
        return u;
163
163
    u = (user_data *) hash_lookup(hash, requestData->user);
164
164
    return u;
165
165
}
169
169
{
170
170
    const user_data *u = GetPassword(requestData);
171
171
    if (!u) {
172
 
        requestData->error = -1;
173
 
        return;
 
172
        requestData->error = -1;
 
173
        return;
174
174
    }
175
175
    if (u->ha1) {
176
 
        xstrncpy(requestData->HHA1, u->ha1, sizeof(requestData->HHA1));
 
176
        xstrncpy(requestData->HHA1, u->ha1, sizeof(requestData->HHA1));
177
177
    } else {
178
 
        HASH HA1;
179
 
        DigestCalcHA1("md5", requestData->user, requestData->realm, u->passwd, NULL, NULL, HA1, requestData->HHA1);
 
178
        HASH HA1;
 
179
        DigestCalcHA1("md5", requestData->user, requestData->realm, u->passwd, NULL, NULL, HA1, requestData->HHA1);
180
180
    }
181
181
}