1 | /* $Id: mail.c 3956 2010-01-05 20:56:56Z zacheiss $ |
---|
2 | * |
---|
3 | * Library-internal routines for categorizing machines in terms |
---|
4 | * of email. |
---|
5 | * |
---|
6 | * Copyright (C) 1999 by the Massachusetts Institute of Technology |
---|
7 | * For copying and distribution information, please see the file |
---|
8 | * <mit-copyright.h>. |
---|
9 | */ |
---|
10 | |
---|
11 | #include <mit-copyright.h> |
---|
12 | #include <moira.h> |
---|
13 | #include <mrclient.h> |
---|
14 | #include "mrclient-internal.h" |
---|
15 | |
---|
16 | #include <stdio.h> |
---|
17 | #include <stdlib.h> |
---|
18 | #include <string.h> |
---|
19 | |
---|
20 | RCSID("$HeadURL: svn+ssh://svn.mit.edu/moira/trunk/moira/clients/lib/mail.c $ $Id: mail.c 3956 2010-01-05 20:56:56Z zacheiss $"); |
---|
21 | |
---|
22 | static int save_sloc_machine(int argc, char **argv, void *sq); |
---|
23 | static int save_alias_value(int argc, char **argv, void *sq); |
---|
24 | |
---|
25 | /* Given a canonicalized machine name, ask the Moira server if it is of type |
---|
26 | * POP, LOCAL, or MAILHUB -- if none of those, we assume it's FOREIGN. |
---|
27 | */ |
---|
28 | int mailtype(char *machine) |
---|
29 | { |
---|
30 | char *name; |
---|
31 | int status, match = 0; |
---|
32 | static struct save_queue *pop = NULL, *local = NULL; |
---|
33 | static struct save_queue *mailhub = NULL, *mailhub_name = NULL; |
---|
34 | static struct save_queue *imap = NULL, *exchange = NULL; |
---|
35 | |
---|
36 | mrcl_clear_message(); |
---|
37 | |
---|
38 | /* 1. Check if the machine is an IMAP server. */ |
---|
39 | if (!imap) |
---|
40 | { |
---|
41 | char *service = "POSTOFFICE"; |
---|
42 | imap = sq_create(); |
---|
43 | status = mr_query("get_server_locations", 1, &service, |
---|
44 | save_sloc_machine, imap); |
---|
45 | if (status && (status != MR_NO_MATCH)) |
---|
46 | { |
---|
47 | mrcl_set_message("Could not read list of IMAP servers: %s", |
---|
48 | error_message(status)); |
---|
49 | return MAILTYPE_ERROR; |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | /* Because of how sq_get_data works, we need to go through the entire |
---|
54 | * queue even if we find a match, so that it gets reset for the next |
---|
55 | * call. |
---|
56 | */ |
---|
57 | while (sq_get_data(imap, &name)) |
---|
58 | { |
---|
59 | if (!match && !strcasecmp(name, machine)) |
---|
60 | match = 1; |
---|
61 | } |
---|
62 | |
---|
63 | if (match) |
---|
64 | return MAILTYPE_IMAP; |
---|
65 | |
---|
66 | |
---|
67 | /* 2. Check if the machine is a POP server. */ |
---|
68 | if (!pop) |
---|
69 | { |
---|
70 | char *service = "POP"; |
---|
71 | pop = sq_create(); |
---|
72 | status = mr_query("get_server_locations", 1, &service, |
---|
73 | save_sloc_machine, pop); |
---|
74 | if (status && (status != MR_NO_MATCH)) |
---|
75 | { |
---|
76 | mrcl_set_message("Could not read list of POP servers: %s", |
---|
77 | error_message(status)); |
---|
78 | return MAILTYPE_ERROR; |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | /* Because of how sq_get_data works, we need to go through the entire |
---|
83 | * queue even if we find a match, so that it gets reset for the next |
---|
84 | * call. |
---|
85 | */ |
---|
86 | while (sq_get_data(pop, &name)) |
---|
87 | { |
---|
88 | if (!match && !strcasecmp(name, machine)) |
---|
89 | match = 1; |
---|
90 | } |
---|
91 | if (match) |
---|
92 | return MAILTYPE_POP; |
---|
93 | |
---|
94 | |
---|
95 | /* 3. Check if the machine is "LOCAL". */ |
---|
96 | if (!local) |
---|
97 | { |
---|
98 | char *service = "LOCAL"; |
---|
99 | local = sq_create(); |
---|
100 | status = mr_query("get_server_locations", 1, &service, |
---|
101 | save_sloc_machine, local); |
---|
102 | if (status && (status != MR_NO_MATCH)) |
---|
103 | { |
---|
104 | mrcl_set_message("Could not read list of LOCAL servers: %s", |
---|
105 | error_message(status)); |
---|
106 | return MAILTYPE_ERROR; |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | while (sq_get_data(local, &name)) |
---|
111 | { |
---|
112 | if (!match && !strcasecmp(name, machine)) |
---|
113 | match = 1; |
---|
114 | } |
---|
115 | if (match) |
---|
116 | return MAILTYPE_LOCAL; |
---|
117 | |
---|
118 | |
---|
119 | /* 4. Check if the machine is one of the mailhubs. */ |
---|
120 | if (!mailhub) |
---|
121 | { |
---|
122 | char *service = "MAILHUB"; |
---|
123 | mailhub = sq_create(); |
---|
124 | status = mr_query("get_server_locations", 1, &service, |
---|
125 | save_sloc_machine, mailhub); |
---|
126 | if (!status || status == MR_NO_MATCH) |
---|
127 | { |
---|
128 | service = "NMAILHUB"; |
---|
129 | status = mr_query("get_server_locations", 1, &service, |
---|
130 | save_sloc_machine, mailhub); |
---|
131 | } |
---|
132 | |
---|
133 | if (status && (status != MR_NO_MATCH)) |
---|
134 | { |
---|
135 | mrcl_set_message("Could not read list of MAILHUB servers: %s", |
---|
136 | error_message(status)); |
---|
137 | return MAILTYPE_ERROR; |
---|
138 | } |
---|
139 | |
---|
140 | } |
---|
141 | |
---|
142 | while (sq_get_data(mailhub, &name)) |
---|
143 | { |
---|
144 | if (!match && !strcasecmp(name, machine)) |
---|
145 | match = 1; |
---|
146 | } |
---|
147 | if (match) |
---|
148 | return MAILTYPE_MAILHUB; |
---|
149 | |
---|
150 | |
---|
151 | /* 5. Check if the machine is one of the external names of the mailhubs. */ |
---|
152 | if (!mailhub_name) |
---|
153 | { |
---|
154 | char *argv[3]; |
---|
155 | mailhub_name = sq_create(); |
---|
156 | argv[0] = "mailhub"; |
---|
157 | argv[1] = "TYPE"; |
---|
158 | argv[2] = "*"; |
---|
159 | status = mr_query("get_alias", 3, argv, save_alias_value, mailhub_name); |
---|
160 | if (status && (status != MR_NO_MATCH)) |
---|
161 | { |
---|
162 | mrcl_set_message("Could not read list of mailhub names: %s", |
---|
163 | error_message(status)); |
---|
164 | return MAILTYPE_ERROR; |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | while (sq_get_data(mailhub_name, &name)) |
---|
169 | { |
---|
170 | if (!match && !strcasecmp(name, machine)) |
---|
171 | match = 1; |
---|
172 | } |
---|
173 | if (match) |
---|
174 | return MAILTYPE_MAILHUB; |
---|
175 | |
---|
176 | /* 6. Check for EXCHANGE service. */ |
---|
177 | if (!exchange) |
---|
178 | { |
---|
179 | char *service = "EXCHANGE"; |
---|
180 | exchange = sq_create(); |
---|
181 | status = mr_query("get_server_locations", 1, &service, |
---|
182 | save_sloc_machine, exchange); |
---|
183 | if (status && (status != MR_NO_MATCH)) |
---|
184 | { |
---|
185 | mrcl_set_message("Could not read list of EXCHANGE servers: %s", |
---|
186 | error_message(status)); |
---|
187 | return MAILTYPE_ERROR; |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | while (sq_get_data(exchange, &name)) |
---|
192 | { |
---|
193 | if (!match && !strcasecmp(name, machine)) |
---|
194 | match = 1; |
---|
195 | } |
---|
196 | if (match) |
---|
197 | return MAILTYPE_EXCHANGE; |
---|
198 | |
---|
199 | return MAILTYPE_SMTP; |
---|
200 | } |
---|
201 | |
---|
202 | static int save_sloc_machine(int argc, char **argv, void *sq) |
---|
203 | { |
---|
204 | sq_save_data(sq, strdup(argv[1])); |
---|
205 | return MR_CONT; |
---|
206 | } |
---|
207 | |
---|
208 | static int save_alias_value(int argc, char **argv, void *sq) |
---|
209 | { |
---|
210 | sq_save_data(sq, canonicalize_hostname(strdup(argv[2]))); |
---|
211 | return MR_CONT; |
---|
212 | } |
---|