1 | /* |
---|
2 | * athena.js |
---|
3 | * |
---|
4 | * |
---|
5 | * |
---|
6 | * Mozilla Thunderbird MIT/Athena Autoconfig Script |
---|
7 | * Copyright © 2006 Charles Dominguez |
---|
8 | * Copyright © 2009 Evan Broder |
---|
9 | * |
---|
10 | * This program is free software; you can redistribute it and/or |
---|
11 | * modify it under the terms of the GNU General Public License |
---|
12 | * as published by the Free Software Foundation; either version 2 |
---|
13 | * of the License, or (at your option) any later version. |
---|
14 | * |
---|
15 | * This program is distributed in the hope that it will be useful, |
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | * GNU General Public License for more details. |
---|
19 | * |
---|
20 | * You should have received a copy of the GNU General Public License |
---|
21 | * along with this program; if not, write to the Free Software |
---|
22 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | // CONSTANTS ******************************************************************* |
---|
27 | |
---|
28 | // Processing settings |
---|
29 | const POBOX_SAVE_PREF = '_debathena.save.pobox'; |
---|
30 | const USERNAME_SAVE_PREF = '_debathena.save.username'; |
---|
31 | |
---|
32 | // FUNCTIONS ******************************************************************* |
---|
33 | |
---|
34 | /** Gets the current user's username. |
---|
35 | * <p>This function will retrieve the username from the environment, and |
---|
36 | * if that fails, will try to get it from a stored preference.</p> |
---|
37 | * @returns the current user's username |
---|
38 | * @throws if a valid username could not be retrieved |
---|
39 | */ |
---|
40 | function getUsername() |
---|
41 | { |
---|
42 | // Get username from environment |
---|
43 | var username = getenv("ATHENA_USER"); |
---|
44 | if(username == "") |
---|
45 | username = getenv("USER"); |
---|
46 | |
---|
47 | // Check that username is sane |
---|
48 | if(username == null || username == '' || username == 'root') |
---|
49 | { |
---|
50 | var previousUsername = getPref(USERNAME_SAVE_PREF); |
---|
51 | |
---|
52 | if(previousUsername != null) |
---|
53 | { |
---|
54 | username = previousUsername; |
---|
55 | } |
---|
56 | else |
---|
57 | { |
---|
58 | throw "Failed to retrieve user name (environment corruption)"; |
---|
59 | } |
---|
60 | } |
---|
61 | return username; |
---|
62 | } |
---|
63 | |
---|
64 | /** Gets whether the current profile is the default profile. |
---|
65 | * <p>Note: this test is simpleminded: does the profile directory end in |
---|
66 | * .default?</p> |
---|
67 | * @returns true if the current profile is the default, false otherwise |
---|
68 | */ |
---|
69 | function isCurrentProfileDefault() |
---|
70 | { |
---|
71 | var profdir = getCurrentProfilePath(); |
---|
72 | return profdir.match(/\.default$/) != null; |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | /** Gets the path of the current user profile. |
---|
77 | * @returns the path to the current profile |
---|
78 | */ |
---|
79 | function getCurrentProfilePath() |
---|
80 | { |
---|
81 | var gDirServiceProp =Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties); |
---|
82 | var gProfileDir = gDirServiceProp.get("ProfD", Components.interfaces.nsIFile); |
---|
83 | return gProfileDir.path; |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | /** Runs hesinfo to retrieve user data. |
---|
88 | * @param shell the shell to use to execute hesinfo |
---|
89 | * @param hesiodName the hesoid name to resolve |
---|
90 | * @param hesiodNameType the type of data to resolve |
---|
91 | * @return a string containing the hesiod information |
---|
92 | * @author Charles Dominguez |
---|
93 | */ |
---|
94 | function hesinfo(shell, hesiodName, hesiodNameType) |
---|
95 | { |
---|
96 | var output; // output of command |
---|
97 | |
---|
98 | output = shell.exec('/usr/bin/hesinfo' + ' ' + hesiodName + ' ' + hesiodNameType); |
---|
99 | |
---|
100 | return output.stdout; |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | // CLASSES ********************************************************************* |
---|
107 | |
---|
108 | |
---|
109 | /** Class that holds user information. |
---|
110 | * @param name the user's name |
---|
111 | */ |
---|
112 | function UserInfo(name) |
---|
113 | { |
---|
114 | /** The user's username. */ |
---|
115 | this.username = name; |
---|
116 | |
---|
117 | /** Indicates wether hesoid pobox call suceeded. */ |
---|
118 | this.poboxSuceeded = false; |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | /* Provides an interface to a shell. |
---|
123 | * |
---|
124 | * <p>This class provides a way to execute arbitrary shell commands and read |
---|
125 | * back the output they produce.</p> |
---|
126 | * |
---|
127 | * @param shellPath a string containing the full path to the shell binary. |
---|
128 | * Note that the current implementation only supports BASH. |
---|
129 | * @param tempDir a string containing the full path to the temporary |
---|
130 | * directory to use. User must have writable access to this directory. |
---|
131 | * @author Charles Dominguez |
---|
132 | */ |
---|
133 | function Shell(shellPath, tempDir) |
---|
134 | { |
---|
135 | /** The temporary directory to store the capture files in. */ |
---|
136 | this._tmpDir; |
---|
137 | |
---|
138 | /** The shell used by this object. */ |
---|
139 | this._shellPath; |
---|
140 | |
---|
141 | /** The shell binary. */ |
---|
142 | this._shellBin; |
---|
143 | |
---|
144 | /** The process used to execute the shell. */ |
---|
145 | this._process; |
---|
146 | |
---|
147 | /** Shell strings that redirect to the files. */ |
---|
148 | this._redirect; |
---|
149 | |
---|
150 | /** Object linking stdout file. */ |
---|
151 | this._stdout; |
---|
152 | |
---|
153 | /** Object linking stderr file. */ |
---|
154 | this._stderr; |
---|
155 | |
---|
156 | /** Execute the given command line in the shell. |
---|
157 | * @param commandLine a string containing the command to execute |
---|
158 | * the command MUST NOT contain any redirects |
---|
159 | * @return a ShellOutput object containing the output of the command |
---|
160 | * @throws ShellOutput if the command did not succeed succesfully |
---|
161 | */ |
---|
162 | this.exec = function(commandLine) |
---|
163 | { |
---|
164 | var args; // the arguments to the shell |
---|
165 | var output; // the output of the command |
---|
166 | |
---|
167 | output = new ShellOutput(); |
---|
168 | args = new Array('-c', commandLine + this._redirect); |
---|
169 | |
---|
170 | output.retval = this._process.run(1, args, args.length); |
---|
171 | |
---|
172 | output.stdout = this._stdout.readAll(); |
---|
173 | output.stderr = this._stderr.readAll(); |
---|
174 | |
---|
175 | // If the command did not succeed, throw an exception with the returned data |
---|
176 | if(output.retval != 0) |
---|
177 | { |
---|
178 | throw output; |
---|
179 | } |
---|
180 | |
---|
181 | return output; |
---|
182 | } |
---|
183 | |
---|
184 | /** Cleans up object. |
---|
185 | */ |
---|
186 | this.destroy = function() |
---|
187 | { |
---|
188 | // close files |
---|
189 | this._stdout.close(); |
---|
190 | this._stderr.close(); |
---|
191 | } |
---|
192 | |
---|
193 | /** Generates a path to a temporary file. |
---|
194 | * @param prefix a string containing a prefix to prepend to the file name |
---|
195 | * @return a string containing a fully qualified path to a temporary file location |
---|
196 | */ |
---|
197 | this._getTemporaryPath = function(prefix) |
---|
198 | { |
---|
199 | return this._tmpDir + '/' + prefix + '-shellCapture.txt'; |
---|
200 | } |
---|
201 | |
---|
202 | /** Initialize the object. |
---|
203 | * @param shellPath a string containing the full path to the shell binary. |
---|
204 | * Note that the current implementation only supports BASH. |
---|
205 | * @param tempDir a string containing the full path to the temporary |
---|
206 | * directory to use. User must have writable access to this directory. |
---|
207 | */ |
---|
208 | this._init = function(shellPath, tempDir) |
---|
209 | { |
---|
210 | this._tmpDir = tempDir; |
---|
211 | |
---|
212 | // Create IO File objects |
---|
213 | this._stdout = new CaptureFile(this._getTemporaryPath("stdout")); |
---|
214 | this._stderr = new CaptureFile(this._getTemporaryPath("stderr")); |
---|
215 | |
---|
216 | // connect to shell binary |
---|
217 | this._shellBin = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); |
---|
218 | this._shellBin.initWithPath(shellPath); |
---|
219 | |
---|
220 | // create a process to execute shell commands |
---|
221 | this._process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess); |
---|
222 | this._process.init(this._shellBin); |
---|
223 | |
---|
224 | // create shell redirect commands |
---|
225 | this._redirect = ' 1>| "' + this._stdout.getPath() + '" 2>| "' + this._stderr.getPath() + '"'; |
---|
226 | } |
---|
227 | |
---|
228 | // run initialization procedure |
---|
229 | this._init(shellPath, tempDir); |
---|
230 | } |
---|
231 | |
---|
232 | |
---|
233 | /* Represents a capture file. |
---|
234 | * |
---|
235 | * <p>This class initializes and contains all objects needed to interact |
---|
236 | * with command output files.</p> |
---|
237 | * |
---|
238 | * @param path a string containing the full path to the (temporary) capture file. |
---|
239 | * @author Charles Dominguez |
---|
240 | */ |
---|
241 | function CaptureFile(path) |
---|
242 | { |
---|
243 | /** Path to file that captures output. */ |
---|
244 | this._filePath; |
---|
245 | /** File that captures output. */ |
---|
246 | this._file; |
---|
247 | /** Base stream used for input. */ |
---|
248 | this._baseStream; |
---|
249 | /** Input stream for reading IO file. */ |
---|
250 | this._inputStream; |
---|
251 | /** Seekable input stream for seeking within IO file. */ |
---|
252 | this._seekableInputStream; |
---|
253 | /** Scriptable input stream for reading IO file. */ |
---|
254 | this._scriptableInputStream; |
---|
255 | |
---|
256 | /** Reads all of the data from the file and returns it as a string. |
---|
257 | * @returns A string containing the data read, or "" if nothing was read. |
---|
258 | */ |
---|
259 | this.readAll = function() |
---|
260 | { |
---|
261 | if(this._scriptableInputStream.available() > 0) |
---|
262 | { |
---|
263 | this._seekableInputStream.seek(this._seekableInputStream.seek.NS_SEEK_SET, 0); |
---|
264 | return this._scriptableInputStream.read(this._scriptableInputStream.available()-1); |
---|
265 | } |
---|
266 | else |
---|
267 | { |
---|
268 | return "" |
---|
269 | } |
---|
270 | } |
---|
271 | |
---|
272 | /** Gets the path to the capture file. |
---|
273 | * @returns a string containing the path to the capure file |
---|
274 | */ |
---|
275 | this.getPath = function() |
---|
276 | { |
---|
277 | return this._filePath; |
---|
278 | } |
---|
279 | |
---|
280 | /** Closes and cleans up files. */ |
---|
281 | this.close = function() |
---|
282 | { |
---|
283 | this._baseStream.close(); |
---|
284 | this._file.remove(false); |
---|
285 | } |
---|
286 | |
---|
287 | /** Initialize all IO objects related to the file. |
---|
288 | * @param path a string containing the full path to the (temporary) capture file. |
---|
289 | */ |
---|
290 | this._init = function(path) |
---|
291 | { |
---|
292 | // set path |
---|
293 | this._filePath = path; |
---|
294 | |
---|
295 | // create file object |
---|
296 | this._file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); |
---|
297 | this._file.initWithPath(this._filePath); |
---|
298 | |
---|
299 | // create a unique temporary file |
---|
300 | this._file.createUnique(this._file.NORMAL_FILE_TYPE, 0600); |
---|
301 | // Note: exception not caught |
---|
302 | |
---|
303 | // create base stream |
---|
304 | this._baseStream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(); |
---|
305 | |
---|
306 | // create input stream |
---|
307 | this._inputStream = this._baseStream.QueryInterface(Components.interfaces.nsIFileInputStream); |
---|
308 | this._inputStream.init(this._file, this._file.PR_RDONLY | this._file.PR_CREATE_FILE, 0600, this._file.OPEN_ON_READ | this._file.DELETE_ON_CLOSE); |
---|
309 | |
---|
310 | // create seekable input stream |
---|
311 | this._seekableInputStream = this._baseStream.QueryInterface(Components.interfaces.nsISeekableStream); |
---|
312 | |
---|
313 | // create seekable input stream |
---|
314 | this._scriptableInputStream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream); |
---|
315 | this._scriptableInputStream.init(this._inputStream); |
---|
316 | } |
---|
317 | |
---|
318 | // run initialization procedure |
---|
319 | this._init(path); |
---|
320 | } |
---|
321 | |
---|
322 | |
---|
323 | /* Contains the data output by a shell command. |
---|
324 | * @author Charles Dominguez |
---|
325 | */ |
---|
326 | function ShellOutput() |
---|
327 | { |
---|
328 | /** A string containing the data the command wrote to stdout. */ |
---|
329 | this.stdout = ""; |
---|
330 | |
---|
331 | /** A string containing the data the command wrote to stderr. */ |
---|
332 | this.stderr = ""; |
---|
333 | |
---|
334 | /** The command's return value. */ |
---|
335 | this.retval = -1; |
---|
336 | } |
---|
337 | |
---|
338 | |
---|
339 | |
---|
340 | // MAIN ************************************************************************ |
---|
341 | |
---|
342 | |
---|
343 | |
---|
344 | try |
---|
345 | { |
---|
346 | |
---|
347 | // Kerberos Authentication |
---|
348 | // TODO: Kerberos settings |
---|
349 | // You know, if MIT's IMAP server supported GSSAPI, |
---|
350 | // Thunderbird could use the user's Kerberos tokens for authentication, |
---|
351 | // allowing single signon support. |
---|
352 | // More info: |
---|
353 | // Official Site: http://www.gnu.org/software/gss/ |
---|
354 | // Basic Info: http://en.wikipedia.org/wiki/GSSAPI |
---|
355 | // lockPref("network.negotiate-auth.gsslib", "/usr/athena/lib/libgssapi_krb5.so"); |
---|
356 | // lockPref("network.negotiate-auth.trusted-uris", "imap://po*.mit.edu,imaps://po*.mit.edu"); |
---|
357 | // lockPref("network.negotiate-auth.delegation-uris", "imap://po*.mit.edu,imaps://po*.mit.edu"); |
---|
358 | // lockPref("network.negotiate-auth.using-native-gsslib", true); |
---|
359 | // lockPref("network.negotiate-auth.allow-proxies", true); |
---|
360 | // lockPref("network.auth.use-sspi", false); |
---|
361 | |
---|
362 | |
---|
363 | // We only want to apply these settings if the current profile is the default |
---|
364 | // This lets users create other accounts with their own configuration, |
---|
365 | // if they want. |
---|
366 | if(isCurrentProfileDefault()) |
---|
367 | { |
---|
368 | // Initialize object containin user information |
---|
369 | var user = new UserInfo(getUsername()); |
---|
370 | |
---|
371 | // Define Temporary directory |
---|
372 | const TMP_DIR = '/var/tmp/MozillaThunderbird-' + user.username; |
---|
373 | |
---|
374 | // Try to retrieve the user's pobox from hesoid |
---|
375 | try |
---|
376 | { |
---|
377 | var shell = new Shell('/bin/bash', TMP_DIR); |
---|
378 | |
---|
379 | var pobox = hesinfo(shell, user.username, "pobox"); |
---|
380 | user.athenaPobox = pobox.replace(/^.*? (.*?) .*?$/, '$1'); |
---|
381 | user.poboxSuceeded = true; |
---|
382 | shell.destroy(); |
---|
383 | } |
---|
384 | catch(e) |
---|
385 | { |
---|
386 | user.poboxSuceeded = false; |
---|
387 | } |
---|
388 | |
---|
389 | // Process hesoid Information |
---|
390 | if(!user.poboxSuceeded) |
---|
391 | { |
---|
392 | // Call failed, so fixup info and continue |
---|
393 | var previousPobox = getPref(POBOX_SAVE_PREF); |
---|
394 | |
---|
395 | if(previousPobox != null) |
---|
396 | { |
---|
397 | user.athenaPobox = previousPobox; |
---|
398 | } |
---|
399 | else |
---|
400 | { |
---|
401 | throw "Unable to set user's pobox (Hesinfo Failed)"; |
---|
402 | } |
---|
403 | } |
---|
404 | |
---|
405 | // save some preferences in case LDAP / hesoid / environment fails in the future |
---|
406 | lockPref(POBOX_SAVE_PREF, user.athenaPobox); |
---|
407 | lockPref(USERNAME_SAVE_PREF, user.username); |
---|
408 | |
---|
409 | // Construct additional user settings |
---|
410 | user.athenaEmail = user.username+'@mit.edu'; |
---|
411 | user.imapURI='imap://'+user.username+'@mit.edu'; |
---|
412 | user.imapRoot='imap://'+user.username+'@'+user.athenaPobox+'/INBOX'; |
---|
413 | |
---|
414 | |
---|
415 | // Register accounts |
---|
416 | defaultPref("mail.account.account1.identities", "id1"); |
---|
417 | defaultPref("mail.account.account1.server", "server1"); |
---|
418 | defaultPref("mail.account.account2.server", "server2"); |
---|
419 | defaultPref("mail.accountmanager.localfoldersserver", "server2"); |
---|
420 | defaultPref("mail.accountmanager.accounts", "account1,account2"); |
---|
421 | defaultPref("mail.accountmanager.defaultaccount", "account1"); |
---|
422 | defaultPref("mail.smtp.defaultserver", "smtp1"); |
---|
423 | defaultPref("mail.smtpservers", "smtp1"); |
---|
424 | |
---|
425 | // Setup MIT Mail Server |
---|
426 | defaultPref("mail.server.server1.hostname", user.athenaPobox); |
---|
427 | defaultPref("mail.server.server1.name", user.athenaEmail); |
---|
428 | defaultPref("mail.server.server1.port", 993); |
---|
429 | defaultPref("mail.server.server1.serverFilterName", "SpamAssassin"); |
---|
430 | defaultPref("mail.server.server1.socketType", 3); |
---|
431 | defaultPref("mail.server.server1.type", "imap"); |
---|
432 | defaultPref("mail.server.server1.userName", user.username); |
---|
433 | |
---|
434 | defaultPref("mail.server.server1.manualMark", true); |
---|
435 | defaultPref("mail.server.server1.manualMarkMode", 1); |
---|
436 | defaultPref("mail.server.server1.moveTargetMode", 1); |
---|
437 | defaultPref("mail.server.server1.spamActionTargetAccount", user.imapURI); |
---|
438 | defaultPref("mail.server.server1.spamActionTargetFolder", user.imapRoot+'/Spamscreen';); |
---|
439 | defaultPref("mail.server.server1.useServerFilter", true); |
---|
440 | |
---|
441 | // Setup Local Folders Server |
---|
442 | defaultPref("mail.server.server2.directory-rel", "[ProfD]Mail/Local Folders"); |
---|
443 | defaultPref("mail.server.server2.hostname", "Local Folders"); |
---|
444 | defaultPref("mail.server.server2.name", "Local Folders"); |
---|
445 | defaultPref("mail.server.server2.type", "none"); |
---|
446 | defaultPref("mail.server.server2.userName", "nobody"); |
---|
447 | |
---|
448 | // Setup Outgoing (SMTP) Server |
---|
449 | defaultPref("mail.smtpserver.smtp1.auth_method", 1); |
---|
450 | defaultPref("mail.smtpserver.smtp1.description", "MIT SMTP Server"); |
---|
451 | defaultPref("mail.smtpserver.smtp1.hostname", 'outgoing.mit.edu'); |
---|
452 | defaultPref("mail.smtpserver.smtp1.port", 465); |
---|
453 | defaultPref("mail.smtpserver.smtp1.try_ssl", 3); |
---|
454 | defaultPref("mail.smtpserver.smtp1.username", user.username); |
---|
455 | |
---|
456 | // Setup MIT Email Identity |
---|
457 | defaultPref("mail.identity.id1.valid", true); |
---|
458 | defaultPref("mail.identity.id1.useremail", user.athenaEmail); |
---|
459 | defaultPref("mail.identity.id1.fcc", true); |
---|
460 | defaultPref("mail.identity.id1.fcc_folder", user.imapRoot+'/sent-mail';); |
---|
461 | defaultPref("mail.identity.id1.fcc_folder_picker_mode", "0"); |
---|
462 | defaultPref("mail.identity.id1.draft_folder", "mailbox://nobody@Local%20Folders/Drafts"); |
---|
463 | defaultPref("mail.identity.id1.drafts_folder_picker_mode", "0"); |
---|
464 | defaultPref("mail.identity.id1.stationery_folder", "mailbox://nobody@Local%20Folders/Templates"); |
---|
465 | defaultPref("mail.identity.id1.tmpl_folder_picker_mode", "0"); |
---|
466 | defaultPref("mail.identity.id1.smtpServer", "smtp1"); |
---|
467 | |
---|
468 | // Setup LDAP Servers & Addressbooks |
---|
469 | defaultPref("ldap_2.servers.MITCampusDirectory.auth.savePassword", false); |
---|
470 | defaultPref("ldap_2.servers.MITCampusDirectory.description", "MIT LDAP"); |
---|
471 | defaultPref("ldap_2.servers.MITCampusDirectory.filename", "MIT_LDAP.mab"); |
---|
472 | defaultPref("ldap_2.servers.MITCampusDirectory.uri", "ldaps://ldap.mit.edu:636/dc=mit,dc=edu??sub?(objectclass=*)"); |
---|
473 | } |
---|
474 | |
---|
475 | |
---|
476 | } |
---|
477 | catch(e) |
---|
478 | { |
---|
479 | displayError("Error in Autoconfig", e); |
---|
480 | } |
---|