root / auth_mysql.c

View | Annotate | Download

1 1.1 tommy
/*
2 1.1 tommy
 * NNTPSwitch MySQL Authenticator
3 1.6 tommy
 * $Id: auth_mysql.c,v 1.5 2006/03/15 10:12:15 tommy Exp $
4 1.1 tommy
 */
5 1.1 tommy
6 1.1 tommy
#include <stdio.h>
7 1.1 tommy
#include <stdlib.h>
8 1.1 tommy
#include <string.h>
9 1.1 tommy
10 1.1 tommy
#include <mysql/mysql.h>
11 1.1 tommy
12 1.1 tommy
#include "nntpd.h"
13 1.1 tommy
#include "aconfig.h"
14 1.1 tommy
#include "aprotos.h"
15 1.1 tommy
16 1.6 tommy
PROTO void auth_mysql(AUTHRESULT *authres, CONFIG *cf, char *args)
17 1.4 tommy
{
18 1.1 tommy
        MYSQL demo_db;
19 1.1 tommy
        MYSQL_RES *res;
20 1.1 tommy
        char *p, *ap;
21 1.1 tommy
22 1.1 tommy
        char query[256];
23 1.1 tommy
        char dbname[32] = "\0";
24 1.1 tommy
        char hostname[128] = "\0";
25 1.1 tommy
        char username[32] = "\0";
26 1.1 tommy
        char password[32] = "\0";
27 1.1 tommy
28 1.1 tommy
        ap = strdup(args);
29 1.1 tommy
        p = strtok(ap, ",: ");
30 1.4 tommy
        do {
31 1.1 tommy
                if ( strncasecmp(p, "dbname", 6) == 0 )
32 1.1 tommy
                        strncpy(dbname, p+7, 32);
33 1.1 tommy
                else
34 1.1 tommy
                if ( strncasecmp(p, "hostname", 8) == 0)
35 1.1 tommy
                        strncpy(hostname, p+9, 128);
36 1.1 tommy
                else
37 1.1 tommy
                if ( strncasecmp(p, "username", 8) == 0)
38 1.1 tommy
                        strncpy(username, p+9, 32);
39 1.1 tommy
                else
40 1.1 tommy
                if ( strncasecmp(p, "password", 8) == 0)
41 1.1 tommy
                        strncpy(password, p+9, 32);
42 1.1 tommy
                else
43 1.2 tommy
                        syslog(LOG_ERR, "auth_mysql: unknown connect string keyword %s", p);
44 1.1 tommy
        } while( (p=strtok(NULL,",: ")) );
45 1.1 tommy
46 1.4 tommy
        if ( dbname[0] == 0 || hostname[0] == 0 )
47 1.4 tommy
        {
48 1.1 tommy
                syslog(LOG_ERR, "auth_mysql: Invalid argument syntax, atleast hostname and dbname are required");
49 1.4 tommy
                authres->message = strdup(MSG_AUTH_ERR);
50 1.4 tommy
                return;
51 1.1 tommy
        }
52 1.1 tommy
53 1.1 tommy
        mysql_init(&demo_db);
54 1.1 tommy
55 1.4 tommy
        if( ! mysql_real_connect(&demo_db, hostname, username, password, dbname, 0, NULL, CLIENT_FOUND_ROWS) )
56 1.4 tommy
        {
57 1.1 tommy
                syslog(LOG_ERR, "auth_mysql: could not connect to mysql %s", mysql_error(&demo_db));
58 1.4 tommy
                authres->message = strdup(MSG_AUTH_ERR);
59 1.4 tommy
                return;
60 1.1 tommy
        }
61 1.1 tommy
62 1.1 tommy
        sprintf(query, "SELECT login FROM users WHERE login = '%s' AND password = '%s'",
63 1.4 tommy
                        authres->username,
64 1.4 tommy
                        authres->password);
65 1.1 tommy
66 1.4 tommy
        if( mysql_real_query(&demo_db, query, 255))
67 1.4 tommy
        {
68 1.1 tommy
                syslog(LOG_ERR, "auth_mysql: could not select query: %s", mysql_error(&demo_db));
69 1.4 tommy
                authres->message = strdup(MSG_AUTH_ERR);
70 1.4 tommy
                return;
71 1.1 tommy
        }
72 1.1 tommy
73 1.1 tommy
        res = mysql_store_result(&demo_db);
74 1.4 tommy
        if ( mysql_num_rows(res) == 0 )
75 1.4 tommy
        {
76 1.1 tommy
                mysql_free_result(res);
77 1.1 tommy
                mysql_close(&demo_db);
78 1.4 tommy
                authres->message = strdup(MSG_AUTH_REJ);
79 1.4 tommy
                return;
80 1.1 tommy
        }
81 1.1 tommy
82 1.1 tommy
        mysql_free_result(res);
83 1.1 tommy
        mysql_close(&demo_db);
84 1.1 tommy
85 1.4 tommy
        authres->authenticated = true;
86 1.4 tommy
        authres->message = strdup(MSG_AUTH_OK);
87 1.4 tommy
        return;
88 1.1 tommy
}