aboutsummaryrefslogtreecommitdiff
path: root/dicod/auth.c
blob: 32b6f6c6e25893ff00821072249a8476e91389a2 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* This file is part of GNU Dico.
   Copyright (C) 1998-2000, 2008, 2010, 2012 Sergey Poznyakoff

   GNU Dico is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GNU Dico is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Dico.  If not, see <http://www.gnu.org/licenses/>. */

#include <dicod.h>
#include <md5.h>

static int
verify_apop(const char *password, const char *user_digest)
{
    int i;
    struct md5_ctx md5context;
    unsigned char md5digest[16];
    char buf[sizeof(md5digest) * 2 + 1];
    char *p;

    md5_init_ctx(&md5context);
    md5_process_bytes(msg_id, strlen(msg_id), &md5context);
    md5_process_bytes(password, strlen(password), &md5context);
    md5_finish_ctx(&md5context, md5digest);

    for (i = 0, p = buf; i < 16; i++, p += 2)
	sprintf(p, "%02x", md5digest[i]);
    return strcmp(user_digest, buf);
}

static int
auth(const char *username, const char *authstr)
{
    int rc = 1;
    char *password;
    
    if (dico_udb_open(user_db)) {
	dico_log(L_ERR, 0, _("failed to open user database"));
	return 1;
    }
    if (dico_udb_get_password(user_db, username, &password)) {
	dico_log(L_ERR, 0,
		 _("failed to get password for `%s' from the database"),
		 username);
    } else {
	if (!password) {
	    dico_log(L_ERR, 0, _("no such user `%s'"), username);
	    rc = 1;
	} else {
	    rc = verify_apop(password, authstr);
	    if (rc) 
		dico_log(L_ERR, 0,
			 _("authentication failed for `%s'"), username);
	    else {
		user_name = xstrdup(username);
		dico_udb_get_groups(user_db, username, &user_groups);
	    }
	    free(password);
	}
    }
    dico_udb_close(user_db);
    return rc;
}

void
dicod_auth(dico_stream_t str, int argc, char **argv)
{
    if (auth(argv[1], argv[2]) == 0) {
	stream_writez(str, "230 Authentication successful");
	check_db_visibility();
    } else
	stream_writez(str,
		      "531 Access denied, "
		      "use \"SHOW INFO\" for server information");
    dico_stream_write(str, "\r\n", 2);
}

static int
auth_init(void *ptr)
{
    if (user_db == NULL) {
	dico_log(L_ERR, 0, "auth capability required but user database is "
	       "not configured");
	return 1;
    }
    return 0;
}

void
register_auth()
{
    static struct dicod_command cmd[] = {
	{ "AUTH", 3, 3, "user string", "provide authentication information",
	  dicod_auth },
	{ NULL }
    };	
    dicod_capa_register("auth", cmd, auth_init, NULL);
}


void
init_auth_data()
{
    free(user_name);
    user_name = NULL;
    free(client_id);
    client_id = NULL;
    free(identity_name);
    identity_name = NULL;
    dico_list_destroy(&user_groups);
    reset_db_visibility();
}

Return to:

Send suggestions and report system problems to the System administrator.