SQL-specific auth functions.
Showing
1 changed file
with
194 additions
and
0 deletions
auth/sql.c
0 → 100644
1 | /* GNU mailutils - a suite of utilities for electronic mail | ||
2 | Copyright (C) 2002 Free Software Foundation, Inc. | ||
3 | |||
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program; if not, write to the Free Software | ||
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | ||
17 | |||
18 | |||
19 | #ifdef HAVE_CONFIG_H | ||
20 | # include <config.h> | ||
21 | #endif | ||
22 | |||
23 | #include <unistd.h> | ||
24 | #include <sys/types.h> | ||
25 | #include <pwd.h> | ||
26 | #ifdef HAVE_SHADOW_H | ||
27 | # include <shadow.h> | ||
28 | #endif | ||
29 | #include <errno.h> | ||
30 | #include <stdio.h> | ||
31 | #include <stdlib.h> | ||
32 | #include <string.h> | ||
33 | #ifdef HAVE_STRINGS_H | ||
34 | # include <strings.h> | ||
35 | #endif | ||
36 | |||
37 | #include <mailutils/list.h> | ||
38 | #include <mailutils/iterator.h> | ||
39 | #include <mailutils/mailbox.h> | ||
40 | #include <mailutils/argp.h> | ||
41 | #include <mailutils/mu_auth.h> | ||
42 | |||
43 | #ifdef HAVE_MYSQL | ||
44 | # include "../MySql/MySql.h" | ||
45 | #endif | ||
46 | |||
47 | /* SQL */ | ||
48 | /* FIXME: Underlying library needs rewriting */ | ||
49 | |||
50 | int | ||
51 | mu_auth_sql_by_name (void *return_data, void *key, | ||
52 | void *unused_func_data, void *unused_call_data) | ||
53 | { | ||
54 | if (!key) | ||
55 | { | ||
56 | errno = EINVAL; | ||
57 | return 1; | ||
58 | } | ||
59 | #ifdef HAVE_MYSQL | ||
60 | return mu_auth_system (return_data, getMpwnam (key)); | ||
61 | #else | ||
62 | errno = ENOSYS; | ||
63 | return 1; | ||
64 | #endif | ||
65 | } | ||
66 | |||
67 | int | ||
68 | mu_auth_sql_by_uid (void *return_data, void *key, | ||
69 | void *unused_func_data, void *unused_call_data) | ||
70 | { | ||
71 | if (!key) | ||
72 | { | ||
73 | errno = EINVAL; | ||
74 | return 1; | ||
75 | } | ||
76 | |||
77 | #ifdef HAVE_MYSQL | ||
78 | return mu_auth_system (return_data, getMpwuid (key)); | ||
79 | #else | ||
80 | errno = ENOSYS; | ||
81 | return 1; | ||
82 | #endif | ||
83 | } | ||
84 | |||
85 | #ifdef HAVE_MYSQL | ||
86 | # define ARG_SQL_GETPWNAM 1 | ||
87 | # define ARG_SQL_GETPWUID 2 | ||
88 | # define ARG_SQL_GETPASS 3 | ||
89 | # define ARG_SQL_HOST 4 | ||
90 | # define ARG_SQL_USER 5 | ||
91 | # define ARG_SQL_PASSWD 6 | ||
92 | # define ARG_SQL_DB 7 | ||
93 | # define ARG_SQL_PORT 8 | ||
94 | |||
95 | static struct argp_option mu_sql_argp_option[] = { | ||
96 | {"sql-getpwnam", ARG_SQL_GETPWNAM, "QUERY", 0, | ||
97 | "SQL query to retrieve a passwd entry based on username", 0}, | ||
98 | {"sql-getpwuid", ARG_SQL_GETPWUID, "QUERY", 0, | ||
99 | "SQL query to retrieve a passwd entry based on UID", 0}, | ||
100 | {"sql-getpass", ARG_SQL_GETPASS, "QUERY", 0, | ||
101 | "SQL query to retrieve a password from the database", 0}, | ||
102 | {"sql-host", ARG_SQL_HOST, "HOSTNAME", 0, | ||
103 | "Name or IP of MySQL server to connect to", 0}, | ||
104 | {"sql-user", ARG_SQL_USER, "NAME", 0, | ||
105 | "SQL user name", 0}, | ||
106 | {"sql-passwd", ARG_SQL_PASSWD, "STRING", 0, | ||
107 | "SQL connection password", 0}, | ||
108 | {"sql-db", ARG_SQL_DB, "STRING", 0, | ||
109 | "Name of the database to connect to", 0}, | ||
110 | {"sql-port", ARG_SQL_PORT, "NUMBER", 0, | ||
111 | "Port to use", 0}, | ||
112 | { NULL, 0, NULL, 0, NULL, 0 } | ||
113 | }; | ||
114 | |||
115 | char *sql_getpwnam_query; | ||
116 | char *sql_getpass_query; | ||
117 | char *sql_getpwuid_query; | ||
118 | char *sql_host = MHOST; | ||
119 | char *sql_user = MUSER; | ||
120 | char *sql_passwd = MPASS; | ||
121 | char *sql_db = MDB; | ||
122 | char *sql_socket = MSOCKET; | ||
123 | int sql_port = MPORT; | ||
124 | |||
125 | static error_t | ||
126 | mu_sql_argp_parser (int key, char *arg, struct argp_state *state) | ||
127 | { | ||
128 | switch (key) | ||
129 | { | ||
130 | case ARG_SQL_GETPWNAM: | ||
131 | sql_getpwnam_query = arg; | ||
132 | break; | ||
133 | |||
134 | case ARG_SQL_GETPWUID: | ||
135 | sql_getpwuid_query = arg; | ||
136 | break; | ||
137 | |||
138 | case ARG_SQL_GETPASS: | ||
139 | sql_getpass_query = arg; | ||
140 | break; | ||
141 | |||
142 | case ARG_SQL_HOST: | ||
143 | sql_host = arg; | ||
144 | break; | ||
145 | |||
146 | case ARG_SQL_USER: | ||
147 | sql_user = arg; | ||
148 | break; | ||
149 | |||
150 | case ARG_SQL_PASSWD: | ||
151 | sql_passwd = arg; | ||
152 | break; | ||
153 | |||
154 | case ARG_SQL_DB: | ||
155 | sql_db = arg; | ||
156 | break; | ||
157 | |||
158 | case ARG_SQL_PORT: | ||
159 | sql_port = strtoul (arg, NULL, 0); | ||
160 | if (sql_port == 0) | ||
161 | { | ||
162 | sql_host = NULL; | ||
163 | sql_socket = arg; | ||
164 | } | ||
165 | break; | ||
166 | |||
167 | default: | ||
168 | return ARGP_ERR_UNKNOWN; | ||
169 | } | ||
170 | return 0; | ||
171 | } | ||
172 | |||
173 | struct argp mu_sql_argp = { | ||
174 | mu_sql_argp_option, | ||
175 | mu_sql_argp_parser, | ||
176 | }; | ||
177 | |||
178 | #endif | ||
179 | |||
180 | struct mu_auth_module mu_auth_sql_module = { | ||
181 | "sql", | ||
182 | #ifdef HAVE_MYSQL | ||
183 | &mu_sql_argp, | ||
184 | #else | ||
185 | NULL, | ||
186 | #endif | ||
187 | mu_auth_nosupport, | ||
188 | NULL, | ||
189 | mu_auth_sql_by_name, | ||
190 | NULL, | ||
191 | mu_auth_sql_by_uid, | ||
192 | NULL | ||
193 | }; | ||
194 |
-
Please register or sign in to post a comment