~ubuntu-branches/ubuntu/vivid/edbrowse/vivid

« back to all changes in this revision

Viewing changes to src/auth.c

  • Committer: Bazaar Package Importer
  • Author(s): Kapil Hari Paranjape
  • Date: 2009-03-01 16:55:12 UTC
  • mfrom: (1.1.5 upstream) (3.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090301165512-97yhte20cy3c4q2w
Tags: 3.4.1-1
* New upstream version (3.4.1).
* debian/rules:
  - add "touch build-stamp" to build-stamp target.
  - modify clean target to use clean target in src/makefile.
  - remove debian/edbrowse.1 in clean target.
  - added "-lcurl" to linker flags.
* debian/control:
  - added libcurl4-openssl-dev to Build-Depends
  - Standards Version 3.8.0. No changes required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
struct httpAuth {
19
19
    struct httpAuth *next;
20
20
    struct httpAuth *prev;
21
 
    char user[MAXUSERPASS];
22
 
    char password[MAXUSERPASS];
23
21
/* These strings are allocated. */
24
22
    char *host;
25
23
    char *directory;
26
 
    char *user_password_encoded;
 
24
    char *user_password;
27
25
    int port;
28
26
    bool proxy;
29
27
    uchar realm;
31
29
 
32
30
static struct listHead authlist = { &authlist, &authlist };
33
31
 
34
 
/* This string is included in the outgoing http header.
35
 
 * It could include both a proxy and a host authorization.
36
 
 * Not that I understand any of the proxy stuff. */
37
 
char *
38
 
getAuthString(const char *url)
 
32
bool
 
33
getUserPass(const char *url, char *creds, bool find_proxy)
39
34
{
40
35
    const char *host = getHostURL(url);
41
36
    int port = getPortURL(url);
42
37
    const char *dir, *dirend;
43
38
    struct httpAuth *a;
44
 
    char *r = NULL;
 
39
    struct httpAuth *found = NULL;
45
40
    int l, d1len, d2len;
46
41
 
47
 
    if(isProxyURL(url)) {
48
 
        foreach(a, authlist) {
49
 
            if(a->proxy && stringEqualCI(a->host, host) && a->port == port) {
50
 
                r = initString(&l);
51
 
                stringAndString(&r, &l, "Proxy-Authorization: ");
52
 
                stringAndString(&r, &l, realmDesc[a->realm]);
53
 
                stringAndString(&r, &l, a->user_password_encoded);
54
 
                stringAndString(&r, &l, eol);
55
 
            }
56
 
        }
57
 
 
58
 
/* Skip past the proxy directive */
59
 
        url = getDataURL(url);
60
 
        host = getHostURL(url);
61
 
        port = getPortURL(url);
62
 
    }
63
 
    /* proxy */
64
42
    getDirURL(url, &dir, &dirend);
65
43
    d2len = dirend - dir;
66
44
 
67
45
    foreach(a, authlist) {
68
 
        if(!a->proxy && stringEqualCI(a->host, host) && a->port == port) {
69
 
            d1len = strlen(a->directory);
70
 
            if(d1len > d2len)
71
 
                continue;
72
 
            if(memcmp(a->directory, dir, d1len))
73
 
                continue;
74
 
            if(!r)
75
 
                r = initString(&l);
76
 
            stringAndString(&r, &l, "Authorization: ");
77
 
            stringAndString(&r, &l, realmDesc[a->realm]);
78
 
            stringAndString(&r, &l, a->user_password_encoded);
79
 
            stringAndString(&r, &l, eol);
 
46
        if(found == NULL && a->proxy == find_proxy &&
 
47
           stringEqualCI(a->host, host) && a->port == port) {
 
48
            if(!a->proxy) {
 
49
/* Directory match not done for proxy records. */
 
50
                d1len = strlen(a->directory);
 
51
                if(d1len > d2len)
 
52
                    continue;
 
53
                if(memcmp(a->directory, dir, d1len))
 
54
                    continue;
 
55
                found = a;
 
56
            } else              /* not proxy */
 
57
                found = a;
80
58
        }
81
59
    }
82
60
 
83
 
    return r;
84
 
}                               /* getAuthString */
 
61
    if(found)
 
62
        strcpy(creds, found->user_password);
 
63
 
 
64
    return (found != NULL);
 
65
}                               /* getUserPass */
85
66
 
86
67
bool
87
68
addWebAuthorization(const char *url,
88
 
   int realm, const char *user, const char *password, bool proxy)
 
69
   int realm, const char *credentials, bool proxy)
89
70
{
90
71
    struct httpAuth *a;
91
72
    const char *host;
118
99
           stringEqualCI(a->host, host) &&
119
100
           (proxy ||
120
101
           dl == strlen(a->directory) && !memcmp(a->directory, dir, dl))) {
121
 
            nzFree(a->user_password_encoded);
 
102
            nzFree(a->user_password);
122
103
            break;
123
104
        }
124
105
    }
134
115
    a->port = port;
135
116
    if(!a->host)
136
117
        a->host = cloneString(host);
137
 
    strcpy(a->user, user);
138
 
    strcpy(a->password, password);
139
118
    if(dir && !a->directory)
140
119
        a->directory = pullString1(dir, dirend);
141
120
 
142
 
/* Now compute the user password encoded */
143
 
    p = allocMem(strlen(user) + strlen(password) + 2);
144
 
    strcpy(p, user);
145
 
    strcat(p, ":");
146
 
    strcat(p, password);
147
 
    a->user_password_encoded = base64Encode(p, strlen(p), false);
148
 
    free(p);
 
121
    a->user_password = cloneString(credentials);
149
122
    debugPrint(3, "%s authorization for %s%s",
150
123
       updated ? "updated" : "new", a->host, a->directory);
 
124
    return true;
151
125
}                               /* addWebAuthorization */