~ubuntu-branches/ubuntu/utopic/squid3/utopic-proposed

« back to all changes in this revision

Viewing changes to helpers/basic_auth/NCSA/ncsa_auth.c

  • Committer: Package Import Robot
  • Author(s): Yolanda Robla
  • Date: 2013-07-10 17:12:42 UTC
  • mfrom: (21.2.18 sid)
  • Revision ID: package-import@ubuntu.com-20130710171242-2i9v941ikbpnjyqk
Tags: 3.3.4-1ubuntu1
* Merge from Debian unstable (LP: #1199883).  Remaining changes:
  + debian/control:
    - Update maintainer.
    - Suggests apparmor (>= 2.3)
    - Depends on ssl-cert ((>= 1.0-11ubuntu1), autopkgtests
  + debian/squid3.upstart
    - Move ulimit command to script section so that it applies
      to the started squid daemon. Thanks to Timur Irmatov (LP: 986159)
    - Work around squid not handling SIGHUP by adding respawn to
      upstart job. (LP: 978356)
  + debian/NEWS.Debian: Rename NEWS.debian, add note regarding squid3
    transition in 12.04 (LP: 924739)
  + debian/rules
    - Re-enable all hardening options lost in the squid->squid3
      transition (LP: 986314)
  + squid3.resolvconf, debian/squid3.postinst, debian/squid3.postrm,
    debian/squid3.preinst, debian/squid3.prerm:
    - Convert init script to upstart
  + debian/patches/99-ubuntu-ssl-cert-snakeoil:
    - Use snakeoil certificates.
  + debian/logrotate
    - Use sar-reports rather than sarg-maint. (LP: 26616)
  + debian/patches/90-cf.data.ubuntu.dpatch:
    - Add an example refresh pattern for debs.
      (foundations-lucid-local-report spec)
  + Add disabled by default AppArmor profile (LP: 497790)
    - debian/squid3.upstart: load profile in pre-start stanza
    - add debian/usr.sbin.squid3 profile
    - debian/rules:
      + install debian/usr.sbin.squid3, etc/apparmor.d/force-complain and
        etc/apparmor.d/disable into $(INSTALLDIR)
      + use dh_apparmor
    - debian/squid3.install: install etc/apparmor.d/disable, force-complain
      and usr.sbin.squid3
    - debian/squid3.preinst: disable profile on clean install or upgrades
      from earlier than when we shipped the profile
  + debian/tests:
    - Add autopkgtests.

* Dropped:
  - debian/patches: dropped patches, superseded by new release:
    + 98-CVE-2012-5643.patch
    + 99-lp1117517_r12473.patch
  - debian/rules: fix FTBFS, removed --with-cppunit-basedir flag,
    included in Debian.
  - debian/control: Dropped transitional packages from squid, no
    longer required.

* Refreshed patches:
  - 01-cf.data.debian.patch
  - 02-makefile-defaults.patch
  - 15-cachemgr-default-config.patch

* debian/tests/test-squid.py: fixed case problem with ftp test

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * ncsa_auth.c
3
 
 *
4
 
 * AUTHOR: Arjan de Vet <Arjan.deVet@adv.iae.nl>
5
 
 *
6
 
 * Example authentication program for Squid, based on the original
7
 
 * proxy_auth code from client_side.c, written by
8
 
 * Jon Thackray <jrmt@uk.gdscorp.com>.
9
 
 *
10
 
 * Uses a NCSA httpd style password file for authentication with the
11
 
 * following improvements suggested by various people:
12
 
 *
13
 
 * - comment lines are possible and should start with a '#';
14
 
 * - empty or blank lines are possible;
15
 
 * - extra fields in the password file are ignored; this makes it
16
 
 *   possible to use a Unix password file but I do not recommend that.
17
 
 *
18
 
 *  MD5 without salt and magic strings - Added by Ramon de Carvalho and Rodrigo Rubira Branco
19
 
 */
20
 
 
21
 
#include "config.h"
22
 
#include "rfc1738.h"
23
 
 
24
 
#if HAVE_STDIO_H
25
 
#include <stdio.h>
26
 
#endif
27
 
#if HAVE_STDLIB_H
28
 
#include <stdlib.h>
29
 
#endif
30
 
#if HAVE_UNISTD_H
31
 
#include <unistd.h>
32
 
#endif
33
 
#if HAVE_STRING_H
34
 
#include <string.h>
35
 
#endif
36
 
#if HAVE_SYS_TYPES_H
37
 
#include <sys/types.h>
38
 
#endif
39
 
#if HAVE_SYS_STAT_H
40
 
#include <sys/stat.h>
41
 
#endif
42
 
#if HAVE_CRYPT_H
43
 
#include <crypt.h>
44
 
#endif
45
 
 
46
 
#include "util.h"
47
 
#include "hash.h"
48
 
#include "crypt_md5.h"
49
 
 
50
 
static hash_table *hash = NULL;
51
 
static HASHFREE my_free;
52
 
 
53
 
typedef struct _user_data {
54
 
    /* first two items must be same as hash_link */
55
 
    char *user;
56
 
    struct _user_data *next;
57
 
    char *passwd;
58
 
} user_data;
59
 
 
60
 
static void
61
 
my_free(void *p)
62
 
{
63
 
    user_data *u = p;
64
 
    xfree(u->user);
65
 
    xfree(u->passwd);
66
 
    xfree(u);
67
 
}
68
 
 
69
 
static void
70
 
read_passwd_file(const char *passwdfile)
71
 
{
72
 
    FILE *f;
73
 
    char buf[8192];
74
 
    user_data *u;
75
 
    char *user;
76
 
    char *passwd;
77
 
    if (hash != NULL) {
78
 
        hashFreeItems(hash, my_free);
79
 
        hashFreeMemory(hash);
80
 
    }
81
 
    /* initial setup */
82
 
    hash = hash_create((HASHCMP *) strcmp, 7921, hash_string);
83
 
    if (NULL == hash) {
84
 
        fprintf(stderr, "ncsa_auth: cannot create hash table\n");
85
 
        exit(1);
86
 
    }
87
 
    f = fopen(passwdfile, "r");
88
 
    if (NULL == f) {
89
 
        fprintf(stderr, "%s: %s\n", passwdfile, xstrerror());
90
 
        exit(1);
91
 
    }
92
 
    while (fgets(buf, 8192, f) != NULL) {
93
 
        if ((buf[0] == '#') || (buf[0] == ' ') || (buf[0] == '\t') ||
94
 
                (buf[0] == '\n'))
95
 
            continue;
96
 
        user = strtok(buf, ":\n\r");
97
 
        passwd = strtok(NULL, ":\n\r");
98
 
        if ((strlen(user) > 0) && passwd) {
99
 
            u = xmalloc(sizeof(*u));
100
 
            u->user = xstrdup(user);
101
 
            u->passwd = xstrdup(passwd);
102
 
            hash_join(hash, (hash_link *) u);
103
 
        }
104
 
    }
105
 
    fclose(f);
106
 
}
107
 
 
108
 
int
109
 
main(int argc, char **argv)
110
 
{
111
 
    struct stat sb;
112
 
    time_t change_time = -1;
113
 
    char buf[256];
114
 
    char *user, *passwd, *p;
115
 
    user_data *u;
116
 
    setbuf(stdout, NULL);
117
 
    if (argc != 2) {
118
 
        fprintf(stderr, "Usage: ncsa_auth <passwordfile>\n");
119
 
        exit(1);
120
 
    }
121
 
    if (stat(argv[1], &sb) != 0) {
122
 
        fprintf(stderr, "cannot stat %s\n", argv[1]);
123
 
        exit(1);
124
 
    }
125
 
    while (fgets(buf, 256, stdin) != NULL) {
126
 
        if ((p = strchr(buf, '\n')) != NULL)
127
 
            *p = '\0';          /* strip \n */
128
 
        if (stat(argv[1], &sb) == 0) {
129
 
            if (sb.st_mtime != change_time) {
130
 
                read_passwd_file(argv[1]);
131
 
                change_time = sb.st_mtime;
132
 
            }
133
 
        }
134
 
        if ((user = strtok(buf, " ")) == NULL) {
135
 
            printf("ERR\n");
136
 
            continue;
137
 
        }
138
 
        if ((passwd = strtok(NULL, "")) == NULL) {
139
 
            printf("ERR\n");
140
 
            continue;
141
 
        }
142
 
        rfc1738_unescape(user);
143
 
        rfc1738_unescape(passwd);
144
 
        u = (user_data *) hash_lookup(hash, user);
145
 
        if (u == NULL) {
146
 
            printf("ERR No such user\n");
147
 
#if HAVE_CRYPT
148
 
        } else if (strlen(passwd) <= 8 && strcmp(u->passwd, (char *) crypt(passwd, u->passwd)) == 0) {
149
 
            // Bug 3107: crypt() DES functionality silently truncates long passwords.
150
 
            printf("OK\n");
151
 
        } else if (strlen(passwd) > 8 && strcmp(u->passwd, (char *) crypt(passwd, u->passwd)) == 0) {
152
 
            // Bug 3107: crypt() DES functionality silently truncates long passwords.
153
 
            fprintf(stderr, "SECURITY ALERT: NCSA DES algorithm truncating user %s password to 8 bytes. Upgrade to MD5.", user);
154
 
            // Highly Unsafe: permit a transition period for admin to update passwords.
155
 
            printf("OK\n");
156
 
#endif
157
 
        } else if (strcmp(u->passwd, (char *) crypt_md5(passwd, u->passwd)) == 0) {
158
 
            printf("OK\n");
159
 
        } else if (strcmp(u->passwd, (char *) md5sum(passwd)) == 0) {
160
 
            printf("OK\n");
161
 
        } else {
162
 
            printf("ERR Wrong password\n");
163
 
        }
164
 
    }
165
 
    if (hash != NULL) {
166
 
        hashFreeItems(hash, my_free);
167
 
        hashFreeMemory(hash);
168
 
    }
169
 
    exit(0);
170
 
}