~ubuntu-branches/ubuntu/natty/edbrowse/natty

« back to all changes in this revision

Viewing changes to auth.c

  • Committer: Bazaar Package Importer
  • Author(s): Kapil Hari Paranjape
  • Date: 2006-10-20 10:47:30 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061020104730-o7vxbrypwaz932dt
Tags: 3.1.2-1
* New upstream version (3.1.2). Closes: #306486.
  - programs now written in C
  - support for javascript.
* debian/control:
  - added Kapil Hari Paranjape to Uploaders.
  - Build-depends on "libssl-dev", "libmozjs-dev", "libpcre3-dev".
  - Standards-Version to 3.7.2. No changes required.
* debian/rules:
  - add "noopt" feature.
  - set CFLAGS and LIBS.
  - Put $(MAKE) into the build rules.
* debian/copyright: Edited to add the current copyright which
  is GPL with the exception for linking with OpenSSL.
* debian/docs: added "README".
* debian/examples: added "jsrt".

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* auth.c
 
2
 * user password authorization for web access
 
3
 * (c) 2002 Mikulas Patocka
 
4
 * This file is part of the Links project, released under GPL.
 
5
 *
 
6
 * Modified by Karl Dahlke for integration with edbrowse.
 
7
 */
 
8
 
 
9
#include "eb.h"
 
10
 
 
11
#define REALM_BASIC 1
 
12
#define REALM_MD5 2
 
13
 
 
14
static const char *const realmDesc[] = {
 
15
    "", "Basic ", "Digest "
 
16
};
 
17
 
 
18
struct httpAuth {
 
19
    struct httpAuth *next;
 
20
    struct httpAuth *prev;
 
21
    char user[MAXUSERPASS];
 
22
    char password[MAXUSERPASS];
 
23
/* These strings are allocated. */
 
24
    char *host;
 
25
    char *directory;
 
26
    char *user_password_encoded;
 
27
    int port;
 
28
    bool proxy;
 
29
    uchar realm;
 
30
};
 
31
 
 
32
static struct listHead authlist = { &authlist, &authlist };
 
33
 
 
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)
 
39
{
 
40
    const char *host = getHostURL(url);
 
41
    int port = getPortURL(url);
 
42
    const char *dir, *dirend;
 
43
    struct httpAuth *a;
 
44
    char *r = NULL;
 
45
    int l, d1len, d2len;
 
46
 
 
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
    getDirURL(url, &dir, &dirend);
 
65
    d2len = dirend - dir;
 
66
 
 
67
    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);
 
80
        }
 
81
    }
 
82
 
 
83
    return r;
 
84
}                               /* getAuthString */
 
85
 
 
86
bool
 
87
addWebAuthorization(const char *url,
 
88
   int realm, const char *user, const char *password, bool proxy)
 
89
{
 
90
    struct httpAuth *a;
 
91
    const char *host;
 
92
    const char *dir = 0, *dirend;
 
93
    int port, dl;
 
94
    bool urlProx = isProxyURL(url);
 
95
    bool updated = true;
 
96
    char *p;
 
97
 
 
98
    if(proxy) {
 
99
        if(!urlProx) {
 
100
            setError("proxy authentication for a non-proxy url");
 
101
            return false;
 
102
        }
 
103
    } else if(urlProx)
 
104
        url = getDataURL(url);
 
105
 
 
106
    host = getHostURL(url);
 
107
    port = getPortURL(url);
 
108
    if(!proxy) {
 
109
        getDirURL(url, &dir, &dirend);
 
110
        dl = dirend - dir;
 
111
    }
 
112
 
 
113
/* See if we've done this one before. */
 
114
    foreach(a, authlist) {
 
115
        if(a->proxy == proxy &&
 
116
           a->port == port &&
 
117
           a->realm == realm &&
 
118
           stringEqualCI(a->host, host) &&
 
119
           (proxy ||
 
120
           dl == strlen(a->directory) && !memcmp(a->directory, dir, dl))) {
 
121
            nzFree(a->user_password_encoded);
 
122
            break;
 
123
        }
 
124
    }
 
125
 
 
126
    if(a == (struct httpAuth *)&authlist) {
 
127
        updated = false;
 
128
        a = allocZeroMem(sizeof (struct httpAuth));
 
129
        addToListFront(&authlist, a);
 
130
    }
 
131
 
 
132
    a->proxy = proxy;
 
133
    a->realm = realm;
 
134
    a->port = port;
 
135
    if(!a->host)
 
136
        a->host = cloneString(host);
 
137
    strcpy(a->user, user);
 
138
    strcpy(a->password, password);
 
139
    if(dir && !a->directory)
 
140
        a->directory = pullString1(dir, dirend);
 
141
 
 
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);
 
149
    debugPrint(3, "%s authorization for %s%s",
 
150
       updated ? "updated" : "new", a->host, a->directory);
 
151
}                               /* addWebAuthorization */