source: contrib/MailArchiver/sources/src/serpro/mailarchiver/util/SystemAccount.java @ 6785

Revision 6785, 19.4 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

Line 
1/**
2 * MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface.
3 * Copyright (C) 2012  Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/******************************************************************************\
20*
21*  This product was developed by
22*
23*        SERVIÇO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO),
24*
25*  a government company established under Brazilian law (5.615/70),
26*  at Department of Development of Porto Alegre.
27*
28\******************************************************************************/
29
30package serpro.mailarchiver.util;
31
32import java.io.FileReader;
33import java.io.FilterReader;
34import java.io.IOException;
35import java.nio.file.FileSystems;
36import java.nio.file.Files;
37import java.nio.file.Path;
38import java.nio.file.Paths;
39import java.util.ArrayList;
40import java.util.Collections;
41import java.util.List;
42import java.util.Map;
43
44import org.springframework.beans.factory.annotation.Autowired;
45import org.springframework.beans.factory.annotation.Configurable;
46
47import com.sun.jna.Native;
48import com.sun.jna.platform.win32.Advapi32Util;
49import com.sun.jna.platform.win32.Advapi32Util.Account;
50import com.sun.jna.platform.win32.Shell32;
51import com.sun.jna.platform.win32.ShlObj;
52import com.sun.jna.platform.win32.W32Errors;
53import com.sun.jna.platform.win32.WinDef;
54import com.sun.jna.platform.win32.WinNT.HANDLE;
55import com.sun.jna.platform.win32.WinNT.HRESULT;
56
57import org.jvnet.libpam.UnixUser;
58
59@Configurable
60public class SystemAccount {
61
62    private static final Logger log = Logger.getLocalLogger();
63
64    @Autowired
65    private Environment env;
66
67    private String username;
68    private Path homePath;
69
70    //-- Windows --
71    private String sid;
72    private String domain;
73    private Path appDataPath;
74    private Path localAppDataPath;
75    //-------------
76
77    //--- Unix ----
78    private int uid;
79    private int gid;
80    //-------------
81
82
83    public SystemAccount(HANDLE token) {
84
85        Account account = Advapi32Util.getTokenAccount(token);
86
87        username = account.name;
88        sid = account.sidString;
89        domain = account.domain;
90
91        homePath = getKnownFolderPath(token, ShlObj.CSIDL_PROFILE);
92        appDataPath = getKnownFolderPath(token, ShlObj.CSIDL_APPDATA);
93        localAppDataPath = getKnownFolderPath(token, ShlObj.CSIDL_LOCAL_APPDATA);
94
95        System.out.printf(
96                "Windows account\n"
97                + "\tsid: %s\n"
98                + "\tusername: %s\n"
99                + "\tdomain: %s\n"
100                + "\thome path: %s\n"
101                + "\tappData path: %s\n"
102                + "\tlocalAppData path: %s\n",
103                sid, username, domain, homePath, appDataPath, localAppDataPath);
104    }
105
106    public SystemAccount(UnixUser unixUser) {
107
108        username = unixUser.getUserName();
109        uid = unixUser.getUID();
110        gid = unixUser.getGID();
111
112        homePath = FileSystems.getDefault().getPath("/home").resolve(username);
113
114        System.out.printf(
115                "Unix account\n"
116                + "\tuid: %d\n"
117                + "\tgid: %d\n"
118                + "\tusername: %s\n"
119                + "\thome path: %s\n",
120                uid, gid, username, homePath);
121    }
122
123    private Path getKnownFolderPath(HANDLE token, int folder) {
124        char[] path = new char[WinDef.MAX_PATH];
125        HRESULT hr = Shell32.INSTANCE.SHGetFolderPath(null, folder, token, ShlObj.SHGFP_TYPE_CURRENT, path);
126        if(hr.equals(W32Errors.S_OK)) {
127            return Paths.get(Native.toString(path));
128        }
129        else {
130            log.error("getKnownFolderPath {username=%s, csidl=%s} error: %d", username, folder, hr.intValue());
131        }
132        return null;
133    }
134
135    public Path getFirefoxProfilesIniPath() {
136
137        switch(env.getOperatingSystem()) {
138
139            case WindowsXP:
140            case WindowsVista:
141            case Windows7:
142                return appDataPath
143                        .resolve("Mozilla")
144                        .resolve("Firefox")
145                        .resolve("profiles.ini");
146
147            case MacOSX:
148                Path path = homePath
149                        .resolve("Library")
150                        .resolve("Application Support")
151                        .resolve("Firefox")
152                        .resolve("profiles.ini");
153                if(Files.exists(path)) {
154                    return path;
155                }
156                return homePath
157                        .resolve("Library")
158                        .resolve("Mozilla")
159                        .resolve("Firefox")
160                        .resolve("profiles.ini");
161
162            case Linux:
163                return homePath
164                        .resolve(".mozilla")
165                        .resolve("firefox")
166                        .resolve("profiles.ini");
167
168            default:
169                return null;
170        }
171    }
172
173
174    public Path getThunderbirdProfilesIniPath() {
175
176        Path path;
177
178        switch(env.getOperatingSystem()) {
179
180            case WindowsXP:
181            case WindowsVista:
182            case Windows7:
183                return appDataPath
184                        .resolve("Thunderbird")
185                        .resolve("profiles.ini");
186
187            case MacOSX:
188                path = homePath
189                        .resolve("Library")
190                        .resolve("Application Support")
191                        .resolve("Thunderbird")
192                        .resolve("profiles.ini");
193                if(Files.exists(path)) {
194                    return path;
195                }
196                return homePath
197                        .resolve("Library")
198                        .resolve("Thunderbird")
199                        .resolve("profiles.ini");
200
201            case Linux:
202                path = homePath
203                        .resolve(".thunderbird")
204                        .resolve("profiles.ini");
205                if(Files.exists(path)) {
206                    return path;
207                }
208                return homePath
209                        .resolve(".mozilla-thunderbird")
210                        .resolve("profiles.ini");
211
212            default:
213                return null;
214        }
215    }
216
217
218    public Path getThunderbirdLocalFoldersPath() {
219
220        MozillaProfile profile = getDefaultThunderbirdProfile();
221        Path path;
222
223        switch(env.getOperatingSystem()) {
224
225            case WindowsXP:
226            case WindowsVista:
227            case Windows7:
228                if(profile.isRelative()) {
229                    return localAppDataPath
230                            .resolve("Thunderbird")
231                            .resolve(profile.getPathStr())
232                            .resolve("Mail")
233                            .resolve("Local Folders");
234                }
235                else {
236                    return Paths.get(profile.getPathStr(), "Mail", "Local Folders");
237                }
238
239            case MacOSX:
240                if(profile.isRelative()) {
241                    path = homePath
242                            .resolve("Library")
243                            .resolve("Application Support")
244                            .resolve("Thunderbird")
245                            .resolve(profile.getPathStr())
246                            .resolve("Mail")
247                            .resolve("Local Folders");
248                    if(Files.exists(path)) {
249                        return path;
250                    }
251                    return homePath
252                            .resolve("Library")
253                            .resolve("Thunderbird")
254                            .resolve(profile.getPathStr())
255                            .resolve("Mail")
256                            .resolve("Local Folders");
257                }
258                else {
259                    return Paths.get(profile.getPathStr(), "Mail", "Local Folders");
260                }
261
262            case Linux:
263                if(profile.isRelative()) {
264                    path = homePath
265                            .resolve(".thunderbird")
266                            .resolve(profile.getPathStr())
267                            .resolve("Mail")
268                            .resolve("Local Folders");
269                    if(Files.exists(path)) {
270                        return path;
271                    }
272                    return homePath
273                            .resolve(".mozilla-thunderbird")
274                            .resolve(profile.getPathStr())
275                            .resolve("Mail")
276                            .resolve("Local Folders");
277                }
278                else {
279                    return Paths.get(profile.getPathStr(), "Mail", "Local Folders");
280                }
281
282            default:
283                return null;
284        }
285    }
286
287
288    public MozillaProfile getDefaultFirefoxProfile() {
289        Path iniPath = getFirefoxProfilesIniPath();
290        return ((iniPath != null) && Files.isReadable(iniPath))
291                ? getDefaultMozillaProfile(iniPath)
292                : null;
293    }
294
295    public List<MozillaProfile> getFirefoxProfiles() {
296        Path iniPath = getFirefoxProfilesIniPath();
297        return (List<MozillaProfile>) (((iniPath != null) && Files.isReadable(iniPath))
298                ? getMozillaProfiles(iniPath)
299                : Collections.emptyList());
300    }
301
302    public MozillaProfile getDefaultThunderbirdProfile() {
303        Path iniPath = getThunderbirdProfilesIniPath();
304        return ((iniPath != null) && Files.isReadable(iniPath))
305                ? getDefaultMozillaProfile(iniPath)
306                : null;
307    }
308
309    public List<MozillaProfile> getThunderbirdProfiles() {
310        Path iniPath = getThunderbirdProfilesIniPath();
311        return (List<MozillaProfile>) (((iniPath != null) && Files.isReadable(iniPath))
312                ? getMozillaProfiles(iniPath)
313                : Collections.emptyList());
314    }
315
316    private MozillaProfile getDefaultMozillaProfile(Path iniPath) {
317
318        Ini ini = null;
319        try {
320            ini = new Ini(iniPath);
321        }
322        catch (IOException ex) {
323            log.error(ex);
324            return null;
325        }
326
327        Ini.Section section = null;
328        for(Map.Entry<String, Ini.Section> entry : ini.entrySet()) {
329            if( ! entry.getKey().startsWith("Profile")) {
330                continue;
331            }
332            section = entry.getValue();
333            String def = section.get("Default");
334            if((def != null) && def.equals("1")) {
335                break;
336            }
337        }
338
339        //se nenhuma seção marcada como default, usa a última,
340        //provavelmente a única
341        if(section != null) {
342            return new MozillaProfile(
343                section.get("Name"),
344                section.get("Path"),
345                section.get("IsRelative").equals("1"));
346        }
347
348        return null;
349    }
350
351    private List<MozillaProfile> getMozillaProfiles(Path iniPath) {
352
353        Ini ini = null;
354        try {
355            ini = new Ini(iniPath);
356        }
357        catch(IOException ex) {
358            log.error(ex);
359            return null;
360        }
361
362        List<MozillaProfile> profiles = new ArrayList<MozillaProfile>();
363
364        for(Map.Entry<String, Ini.Section> entry : ini.entrySet()) {
365            if( ! entry.getKey().startsWith("Profile")) {
366                continue;
367            }
368            Ini.Section section = entry.getValue();
369            profiles.add(new MozillaProfile(
370                section.get("Name"),
371                section.get("Path"),
372                section.get("IsRelative").equals("1")));
373        }
374
375        return profiles;
376    }
377
378    private class Ini extends org.ini4j.Ini {
379        public Ini(Path iniPath) throws IOException {
380            super(new FilterReader(new FileReader(iniPath.toFile())) {
381                @Override
382                public int read(char[] cbuf, int off, int len) throws IOException {
383                    // escape '\'
384                    // necessario quando o perfil usa um caminho absoluto no Windows
385                    // ex: Path=C:\perfis_firefox\perfil_X  ==>  Path=C:\\perfis_firefox\\perfil_X
386                    int c = in.read();
387                    if(c == -1) {
388                        return -1;
389                    }
390                    if(c == '\\') {
391                        cbuf[off] = '\\';
392                        cbuf[off + 1] = '\\';
393                        return 2;
394                    }
395                    else {
396                        cbuf[off] = (char) c;
397                        return 1;
398                    }
399                }
400            });
401        }
402    }
403
404    public Path getGearsPath(Browser browser) {
405
406        OperatingSystem os = env.getOperatingSystem();
407
408        MozillaProfile profile;
409
410        switch(os.id|browser.id) {
411
412            case 0x501: // Windows7 + InternetExplorer
413            case 0x401: // WindowsVista + InternetExplorer
414                return homePath
415                        .resolve("AppData")
416                        .resolve("LocalLow")
417                        .resolve("Google")
418                        .resolve("Google Gears for Internet Explorer");
419
420            case 0x301: // WindowsXP + InternetExplorer
421                return localAppDataPath
422                        .resolve("Google")
423                        .resolve("Google Gears for Internet Explorer");
424
425            case 0x502: // Windows7 + Firefox
426            case 0x402: // WindowsVista + Firefox
427            case 0x302: // WindowsXP + Firefox
428                profile = getDefaultFirefoxProfile();
429                if(profile == null) {
430                    return localAppDataPath
431                            .resolve("Mozilla")
432                            .resolve("Firefox")
433                            .resolve("null")
434                            .resolve("Google Gears for Firefox");
435                }
436                if(profile.isRelative()) {
437                    return localAppDataPath
438                            .resolve("Mozilla")
439                            .resolve("Firefox")
440                            .resolve(profile.getPathStr())
441                            .resolve("Google Gears for Firefox");
442                }
443                else {
444                    return Paths.get(profile.getPathStr(), "Google Gears for Firefox");
445                }
446
447            case 0x503: // Windows7 + Chrome
448            case 0x403: // WindowsVista + Chrome
449            case 0x303: // WindowsXP + Chrome
450                return localAppDataPath
451                        .resolve("Google")
452                        .resolve("Chrome")
453                        .resolve("User Data")
454                        .resolve("Default")
455                        .resolve("Plugin Data")
456                        .resolve("Google Gears");
457
458            case 0x202: // MacOSX + Firefox
459                profile = getDefaultFirefoxProfile();
460                if(profile == null) {
461                    return homePath
462                            .resolve("Library")
463                            .resolve("Caches")
464                            .resolve("Firefox")
465                            .resolve("null")
466                            .resolve("Google Gears for Firefox");
467                }
468                if(profile.isRelative()) {
469                    return homePath
470                            .resolve("Library")
471                            .resolve("Caches")
472                            .resolve("Firefox")
473                            .resolve(profile.getPathStr())
474                            .resolve("Google Gears for Firefox");
475                }
476                else {
477                    return Paths.get(profile.getPathStr(), "Google Gears for Firefox");
478                }
479
480            case 0x204: // MacOSX + Safari
481                return homePath
482                        .resolve("Library")
483                        .resolve("Application Support")
484                        .resolve("Google")
485                        .resolve("Google Gears for Safari");
486
487            case 0x102: // Linux + Firefox
488                profile = getDefaultFirefoxProfile();
489                if(profile == null) {
490                    return homePath
491                            .resolve(".mozilla")
492                            .resolve("firefox")
493                            .resolve("null")
494                            .resolve("Google Gears for Firefox");
495                }
496                if(profile.isRelative()) {
497                    return homePath
498                            .resolve(".mozilla")
499                            .resolve("firefox")
500                            .resolve(profile.getPathStr())
501                            .resolve("Google Gears for Firefox");
502                }
503                else {
504                    return Paths.get(profile.getPathStr(), "Google Gears for Firefox");
505                }
506
507            default:
508                return null;
509        }
510    }
511
512    public List<Path> getFirefoxGearsPaths() {
513
514        List<Path> paths = new ArrayList<Path>();
515
516        switch(env.getOperatingSystem()) {
517
518            case Windows7:
519            case WindowsVista:
520            case WindowsXP:
521
522                for(MozillaProfile profile : getFirefoxProfiles()) {
523                    if(profile.isRelative()) {
524                        paths.add(localAppDataPath
525                            .resolve("Mozilla")
526                            .resolve("Firefox")
527                            .resolve(profile.getPathStr())
528                            .resolve("Google Gears for Firefox"));
529                    }
530                    else {
531                        paths.add(Paths.get(profile.getPathStr(), "Google Gears for Firefox"));
532                    }
533                }
534                return paths;
535
536            case MacOSX:
537
538                for(MozillaProfile profile : getFirefoxProfiles()) {
539                    if(profile.isRelative()) {
540                        paths.add(homePath
541                                .resolve("Library")
542                                .resolve("Caches")
543                                .resolve("Firefox")
544                                .resolve(profile.getPathStr())
545                                .resolve("Google Gears for Firefox"));
546                    }
547                    else {
548                        paths.add(Paths.get(profile.getPathStr(), "Google Gears for Firefox"));
549                    }
550                }
551                return paths;
552
553            case Linux:
554
555                for(MozillaProfile profile : getFirefoxProfiles()) {
556                    if(profile.isRelative()) {
557                        paths.add(homePath
558                                .resolve(".mozilla")
559                                .resolve("firefox")
560                                .resolve(profile.getPathStr())
561                                .resolve("Google Gears for Firefox"));
562                    }
563                    else {
564                        paths.add(Paths.get(profile.getPathStr(), "Google Gears for Firefox"));
565                    }
566                }
567                return paths;
568
569            default:
570                return null;
571        }
572    }
573}
Note: See TracBrowser for help on using the repository browser.