annotate src/auth/userdb-pgsql.c @ 1296:a9ac5865daf8 HEAD

Whops, forgot to add.
author Timo Sirainen <tss@iki.fi>
date Fri, 14 Mar 2003 21:28:59 +0200
parents
children 7cde19dbe754
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1296
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1 /* Copyright (C) 2003 Alex Howansky, Timo Sirainen */
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3 #include "config.h"
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
4 #undef HAVE_CONFIG_H
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
6 #ifdef USERDB_PGSQL
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
7
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
8 #include "common.h"
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
9 #include "str.h"
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
10 #include "var-expand.h"
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11 #include "db-pgsql.h"
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
12 #include "userdb.h"
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
13
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
14 #include <libpq-fe.h>
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
15 #include <stdlib.h>
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
16 #include <string.h>
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
17
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
18 struct userdb_pgsql_connection {
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
19 struct pgsql_connection *conn;
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
20 };
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
21
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
22 struct userdb_pgsql_request {
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
23 struct pgsql_request request;
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
24 userdb_callback_t *userdb_callback;
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
25 };
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
26
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
27 static struct userdb_pgsql_connection *userdb_pgsql_conn;
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
28
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
29 static int is_result_valid(PGresult *res)
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
30 {
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
31 if (PQresultStatus(res) != PGRES_TUPLES_OK) {
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
32 i_error("PGSQL: Query failed");
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
33 return FALSE;
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
34 }
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
35
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
36 if (PQntuples(res) == 0) {
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
37 if (verbose)
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
38 i_error("PGSQL: Authenticated user not found");
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
39 return FALSE;
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
40 }
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
41
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
42 if (PQfnumber(res, "home") == -1) {
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
43 i_error("PGSQL: User query did not return 'home' field");
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
44 return FALSE;
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
45 }
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
46
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
47 if (PQfnumber(res, "uid") == -1) {
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
48 i_error("PGSQL: User query did not return 'uid' field");
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
49 return FALSE;
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
50 }
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
51
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
52 if (PQfnumber(res, "gid") == -1) {
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
53 i_error("PGSQL: User query did not return 'gid' field");
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
54 return FALSE;
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
55 }
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
56
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
57 return TRUE;
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
58 }
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
59
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
60 static void pgsql_handle_request(struct pgsql_connection *conn __attr_unused__,
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
61 struct pgsql_request *request, PGresult *res)
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
62 {
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
63 struct userdb_pgsql_request *urequest =
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
64 (struct userdb_pgsql_request *) request;
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
65 struct user_data user;
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
66
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
67 if (res != NULL && is_result_valid(res)) {
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
68 memset(&user, 0, sizeof(user));
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
69 user.home = PQgetvalue(res, 0, PQfnumber(res, "home"));
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
70 user.uid = atoi(PQgetvalue(res, 0, PQfnumber(res, "uid")));
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
71 user.gid = atoi(PQgetvalue(res, 0, PQfnumber(res, "gid")));
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
72 urequest->userdb_callback(&user, request->context);
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
73 } else {
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
74 urequest->userdb_callback(NULL, request->context);
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
75 }
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
76 }
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
77
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
78 static void userdb_pgsql_lookup(const char *user, userdb_callback_t *callback,
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
79 void *context)
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
80 {
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
81 struct pgsql_connection *conn = userdb_pgsql_conn->conn;
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
82 struct userdb_pgsql_request *request;
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
83 const char *query;
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
84 string_t *str;
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
85
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
86 str = t_str_new(512);
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
87 var_expand(str, conn->set.user_query, user, NULL);
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
88 query = str_c(str);
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
89
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
90 request = i_new(struct userdb_pgsql_request, 1);
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
91 request->request.callback = pgsql_handle_request;
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
92 request->request.context = context;
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
93 request->userdb_callback = callback;
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
94
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
95 if (db_pgsql_is_valid_username(conn, user))
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
96 db_pgsql_query(conn, query, &request->request);
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
97 else {
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
98 if (verbose)
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
99 i_info("pgsql(%s): Invalid username", user);
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
100 pgsql_handle_request(conn, &request->request, NULL);
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
101 }
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
102 }
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
103
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
104 static void userdb_pgsql_init(const char *args)
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
105 {
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
106 struct pgsql_connection *conn;
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
107
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
108 userdb_pgsql_conn = i_new(struct userdb_pgsql_connection, 1);
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
109 userdb_pgsql_conn->conn = conn = db_pgsql_init(args);
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
110 }
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
111
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
112 static void userdb_pgsql_deinit(void)
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
113 {
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
114 db_pgsql_unref(userdb_pgsql_conn->conn);
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
115 i_free(userdb_pgsql_conn);
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
116 }
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
117
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
118 struct userdb_module userdb_pgsql = {
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
119 userdb_pgsql_init,
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
120 userdb_pgsql_deinit,
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
121
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
122 userdb_pgsql_lookup
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
123 };
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
124
a9ac5865daf8 Whops, forgot to add.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
125 #endif