New file. System-specific (passwd/shadow) auth functions.
Showing
1 changed file
with
157 additions
and
0 deletions
auth/system.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 | /* System database */ | ||
44 | int | ||
45 | mu_auth_system (void *return_data, struct passwd *pw) | ||
46 | { | ||
47 | char *mailbox_name; | ||
48 | int rc; | ||
49 | |||
50 | if (!pw) | ||
51 | return 1; | ||
52 | |||
53 | mailbox_name = malloc (strlen (mu_path_maildir) + | ||
54 | strlen (pw->pw_name) + 1); | ||
55 | if (!mailbox_name) | ||
56 | return 1; | ||
57 | |||
58 | sprintf (mailbox_name, "%s%s", mu_path_maildir, pw->pw_name); | ||
59 | |||
60 | rc = mu_auth_data_alloc ((struct mu_auth_data **) return_data, | ||
61 | pw->pw_name, | ||
62 | pw->pw_passwd, | ||
63 | pw->pw_uid, | ||
64 | pw->pw_gid, | ||
65 | pw->pw_gecos, | ||
66 | pw->pw_dir, | ||
67 | pw->pw_shell, | ||
68 | mailbox_name, | ||
69 | 1); | ||
70 | free (mailbox_name); | ||
71 | return rc; | ||
72 | } | ||
73 | |||
74 | int | ||
75 | mu_auth_system_by_name (void *return_data, void *key, | ||
76 | void *unused_func_data, void *unused_call_data) | ||
77 | { | ||
78 | if (!key) | ||
79 | { | ||
80 | errno = EINVAL; | ||
81 | return 1; | ||
82 | } | ||
83 | return mu_auth_system (return_data, getpwnam (key)); | ||
84 | } | ||
85 | |||
86 | int | ||
87 | mu_auth_system_by_uid (void *return_data, void *key, | ||
88 | void *unused_func_data, void *unused_call_data) | ||
89 | { | ||
90 | if (!key) | ||
91 | { | ||
92 | errno = EINVAL; | ||
93 | return 1; | ||
94 | } | ||
95 | return mu_auth_system (return_data, getpwuid (*(uid_t*) key)); | ||
96 | } | ||
97 | |||
98 | int | ||
99 | mu_authenticate_generic (void *ignored_return_data, | ||
100 | void *key, | ||
101 | void *ignored_func_data, | ||
102 | void *call_data) | ||
103 | { | ||
104 | struct mu_auth_data *auth_data = key; | ||
105 | char *pass = call_data; | ||
106 | |||
107 | return !auth_data | ||
108 | || !auth_data->passwd | ||
109 | || strcmp (auth_data->passwd, crypt (pass, auth_data->passwd)); | ||
110 | } | ||
111 | |||
112 | /* Called only if generic fails */ | ||
113 | int | ||
114 | mu_authenticate_system (void *ignored_return_data, | ||
115 | void *key, | ||
116 | void *ignored_func_data, | ||
117 | void *call_data) | ||
118 | { | ||
119 | struct mu_auth_data *auth_data = key; | ||
120 | char *pass = call_data; | ||
121 | |||
122 | #ifdef HAVE_SHADOW_H | ||
123 | if (auth_data) | ||
124 | { | ||
125 | struct spwd *spw; | ||
126 | spw = getspnam (auth_data->name); | ||
127 | if (spw) | ||
128 | return strcmp (spw->sp_pwdp, crypt (pass, spw->sp_pwdp)); | ||
129 | } | ||
130 | #endif | ||
131 | return 1; | ||
132 | } | ||
133 | |||
134 | |||
135 | struct mu_auth_module mu_auth_system_module = { | ||
136 | "system", | ||
137 | NULL, | ||
138 | mu_authenticate_system, | ||
139 | NULL, | ||
140 | mu_auth_system_by_name, | ||
141 | NULL, | ||
142 | mu_auth_system_by_uid, | ||
143 | NULL | ||
144 | }; | ||
145 | |||
146 | |||
147 | struct mu_auth_module mu_auth_generic_module = { | ||
148 | "generic", | ||
149 | NULL, | ||
150 | mu_authenticate_generic, | ||
151 | NULL, | ||
152 | mu_auth_nosupport, | ||
153 | NULL, | ||
154 | mu_auth_nosupport, | ||
155 | NULL | ||
156 | }; | ||
157 |
-
Please register or sign in to post a comment