001: /**
002: * $Id: NfsFile.java,v 1.39 2005/11/30 11:26:35 ss150821 Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.netfile.servlet.java1;
014:
015: import com.sun.xfile.*;
016: import com.sun.portal.log.common.PortalLogger;
017: import java.io.*;
018: import java.util.Date;
019: import java.util.GregorianCalendar;
020: import java.util.TimeZone;
021: import java.util.logging.*;
022: import java.text.SimpleDateFormat;
023:
024: class NfsFile {
025: private static Logger logger = PortalLogger
026: .getLogger(NfsFile.class);
027: public static boolean SHOULD_HAVE_READ_PERMISSION = true;
028: public static boolean SHOULD_NOT_HAVE_READ_PERMISSION = false;
029: public static final String encoding = "UTF8";
030: private static final int[] ia_unable_to_login = new int[] { -1, -1 };
031: private String s_machine_encoding;
032:
033: private com.sun.xfile.XFileInputStream instream = null;
034: private com.sun.xfile.XFileOutputStream outstream = null;
035: protected int i_number_of_directories_traversed; //Keep track of number of directories scanned during single instance of a search
036:
037: private native boolean doNativeAuth(String username, String password)
038: throws Exception;
039:
040: public native int[] getUserInfo(String user_name) throws Exception;
041:
042: static {
043: if (System.getProperty("os.name").indexOf("indows") == -1) {
044: System.loadLibrary("getpwnam");
045: }
046: }
047: private NetFileLogManager logMgr;
048:
049: NfsFile(NetFileLogManager log_Mgr, String s_machine_encoding) {
050: this .logMgr = log_Mgr;
051: this .s_machine_encoding = s_machine_encoding;
052: i_number_of_directories_traversed = 0;
053: }
054:
055: NfsFile(NetFileLogManager log_Mgr, String s_machine_encoding,
056: int authPort) {
057: this (log_Mgr, s_machine_encoding);
058: }
059:
060: int[] getUserIDs(String user_name, String password)
061: throws NetFileException {
062: int[] i_ids = ia_unable_to_login;
063: try {
064: i_ids = getUserInfo(new String(user_name.getBytes(), "UTF8"));
065: } catch (java.io.UnsupportedEncodingException e) {
066: throw new NetFileException(
067: NetFileException.KEY_IDENTIFIER_PREFIX
068: + "unsupported_character_encoding");
069: } catch (Exception e) {
070: throw new NetFileException(
071: NetFileException.KEY_IDENTIFIER_PREFIX
072: + "unable_to_do_unix_login");
073: }
074: if (i_ids[0] == -1) {
075: throw new NetFileException(
076: NetFileException.KEY_IDENTIFIER_PREFIX + "error44");
077: }
078: return i_ids;
079: }
080:
081: String mkdir(String machine, String share, String user_name,
082: String password, String target_dir,
083: String directory_to_create,
084: NetFileResource nfr_user_locale_i18n_bucket)
085: throws NetFileException {
086: if (!doAuthenticate(user_name, password))
087: throw new NetFileException(
088: NetFileException.KEY_IDENTIFIER_PREFIX + "nf.2");
089:
090: try {
091: int[] i_ids = getUserIDs(user_name, password);
092: com.sun.xfile.XFile xf_parent_dir = getXFile(share,
093: machine, target_dir, true,
094: SHOULD_HAVE_READ_PERMISSION, i_ids);
095: if (!(xf_parent_dir.canWrite())) {
096: throw new NetFileException(new String[] {
097: NetFileException.KEY_IDENTIFIER_PREFIX
098: + "no_write_permission_to_parent",
099: NetFileException.KEY_IDENTIFIER_PREFIX
100: + "textseperator",
101: xf_parent_dir.getPath() });
102: }
103:
104: com.sun.xfile.XFile xfile = getXFile(share, machine,
105: target_dir + "/" + directory_to_create, false,
106: SHOULD_NOT_HAVE_READ_PERMISSION, i_ids);
107: if (!xfile.mkdir()) {
108: throw new NetFileException(
109: NetFileException.KEY_IDENTIFIER_PREFIX
110: + "could_not_create_directory");
111: }
112: } catch (Exception e) {
113: throw new NetFileException(
114: NetFileException.KEY_IDENTIFIER_PREFIX
115: + "could_not_create_directory");
116: }
117: return nfr_user_locale_i18n_bucket.getString("warning52");
118: }
119:
120: String[] getNFSDir(String username, String password, String VMSnam,
121: String machname, String dir_nam, String tmpdir,
122: NetFileResource nfr_user_locale_i18n_bucket)
123: throws NetFileException {
124: if (!doAuthenticate(username, password))
125: throw new NetFileException(
126: NetFileException.KEY_IDENTIFIER_PREFIX + "nf.2");
127:
128: int[] i_ids = getUserIDs(username, password);
129: com.sun.xfile.XFile xfile = getXFile(VMSnam, machname, dir_nam,
130: true, SHOULD_HAVE_READ_PERMISSION, i_ids);
131: String[] sa_files_list = xfile.list();
132: String[] sa_listing = null;
133:
134: if (sa_files_list == null) {
135: throw new NetFileException(
136: NetFileException.KEY_IDENTIFIER_PREFIX
137: + "unable_to_get_listing_error");
138: } else {
139: sa_listing = new String[sa_files_list.length * 4];
140: for (int i = 0, j = 0; i < sa_files_list.length; ++i) {
141: com.sun.xfile.XFile xf_curr_file = new com.sun.xfile.XFile(
142: xfile, sa_files_list[i]);
143: if (xf_curr_file.isFile()) {
144: sa_listing[j] = "-";
145: } else {
146: sa_listing[j] = "d";
147: }
148: ++j;
149: sa_listing[j] = xf_curr_file.getName();
150: ++j;
151: sa_listing[j] = (new Long(xf_curr_file.length()))
152: .toString();
153: ++j;
154: Date d = new java.util.Date(xf_curr_file.lastModified());
155: SimpleDateFormat sdf = new SimpleDateFormat(
156: "MMM dd yy, hh:mm a");
157: sdf.setCalendar(new GregorianCalendar(TimeZone
158: .getDefault()));
159: sa_listing[j] = sdf.format(d);
160: ++j;
161: }
162: }
163: if (sa_listing == null) {
164: throw new NetFileException(
165: NetFileException.KEY_IDENTIFIER_PREFIX
166: + "unable_to_get_listing_error");
167: }
168: return sa_listing;
169: }
170:
171: String getNFSFile(String username, String password, String VMSname,
172: String machname, String mainfilename, String dir_nam,
173: String tmpdir, NetFileResource nfr_user_locale_i18n_bucket)
174: throws NetFileException {
175: File temp_file = null;
176: byte[] buffer = new byte[8 * 1024];
177:
178: if (!doAuthenticate(username, password))
179: throw new NetFileException(
180: NetFileException.KEY_IDENTIFIER_PREFIX + "nf.2");
181:
182: try {
183: int[] i_ids = getUserIDs(username, password);
184: com.sun.xfile.XFile xfile = getXFile(VMSname, machname,
185: dir_nam + "/" + mainfilename, true,
186: SHOULD_HAVE_READ_PERMISSION, i_ids);
187: com.sun.xfile.XFileInputStream xfis_file = new com.sun.xfile.XFileInputStream(
188: xfile);
189: Long time = new Long(System.currentTimeMillis());
190: String s_temp_file = tmpdir + "/" + time.toString()
191: + username.toUpperCase() + mainfilename;
192: temp_file = new File(s_temp_file);
193: temp_file.createNewFile();
194: FileOutputStream fops_tmp = new FileOutputStream(temp_file);
195: int c = 0;
196: while ((c = xfis_file.read(buffer)) > -1) {
197: fops_tmp.write(buffer, 0, c);
198: }
199: fops_tmp.close();
200: xfis_file.close();
201: fops_tmp = null;
202: xfis_file = null;
203: xfile = null;
204: time = null;
205: temp_file = null;
206: return s_temp_file;
207: } catch (Exception e) {
208: // logger.log(Level.SEVERE, "Error in putting NFS File",e);
209: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1082");
210: return "ERROR:" + e.getMessage();
211: } finally {
212: if (temp_file != null) {
213: try {
214: temp_file.delete();
215: } catch (Exception e) {
216: }
217: temp_file = null;
218: }
219: }
220: }
221:
222: String[] search(String usernam, String passwrd, String machnam,
223: String VMSnam, String pattern, String dir_nam,
224: String domainname, int maxsrchdir,
225: NetFileResource nfr_user_locale_i18n_bucket)
226: throws NetFileException {
227: return search(usernam, passwrd, machnam, VMSnam, pattern,
228: dir_nam, maxsrchdir, nfr_user_locale_i18n_bucket);
229: }
230:
231: String[] search(String usernam, String passwrd, String machnam,
232: String VMSnam, String pattern, String dir_nam,
233: int maxsrchdir, NetFileResource nfRes)
234: throws NetFileException {
235:
236: if (!doAuthenticate(usernam, passwrd))
237: throw new NetFileException(
238: NetFileException.KEY_IDENTIFIER_PREFIX + "nf.2");
239:
240: int[] i_ids = getUserIDs(usernam, passwrd);
241: com.sun.xfile.XFile xfile = getXFile(VMSnam, machnam, dir_nam,
242: true, false, i_ids);
243: String s_prefix = "nfs://" + machnam;
244: int i_prefix_length = s_prefix.length();
245: if (xfile.isFile()) {
246: throw new NetFileException(
247: NetFileException.KEY_IDENTIFIER_PREFIX
248: + "cannot_search_a_file");
249: }
250: java.util.ArrayList v_search_results = new java.util.ArrayList();
251: search(xfile, maxsrchdir, i_prefix_length, v_search_results,
252: pattern, i_ids);
253: if (i_number_of_directories_traversed > maxsrchdir) {
254: String[] searchResults = new String[v_search_results.size() + 1];
255: searchResults[0] = "EXCEED:" + nfRes.getString("maxSearch");
256: for (int j = 1; j < searchResults.length; j++) {
257: searchResults[j] = (String) v_search_results.get(j - 1);
258: }
259: return searchResults;
260: }
261:
262: String[] sa_resulsts = new String[v_search_results.size()];
263: for (int i = 0; i < v_search_results.size(); ++i) {
264: sa_resulsts[i] = (String) (v_search_results.get(i));
265: }
266: return sa_resulsts;
267: }
268:
269: void search(com.sun.xfile.XFile xfile, int maxsrchdir,
270: int i_prefix_length, java.util.ArrayList v_search_results,
271: String pattern, int[] i_ids) throws NetFileException {
272: ++i_number_of_directories_traversed;
273: // logger.info(this+":Directory number being searched="+i_number_of_directories_traversed);
274: Object[] params1 = { ":Directory number being searched=",
275: new Integer(i_number_of_directories_traversed) };
276: logger.log(Level.INFO, "PSSRNF_CSPNSJ1083", params1);
277: if (i_number_of_directories_traversed > maxsrchdir) {
278: return;
279: }
280: if (xfile.isFile()) {
281: throw new NetFileException(
282: NetFileException.KEY_IDENTIFIER_PREFIX
283: + "cannot_search_a_file");
284: }
285: String[] sa_contents = xfile.list();
286: String s_path = xfile.toString();
287: if (sa_contents == null) {
288: v_search_results
289: .add("Permission denied :"
290: + s_path.substring(i_prefix_length, s_path
291: .length()));
292: } else {
293: for (int i = 0; ((i < sa_contents.length) && (!(i_number_of_directories_traversed > maxsrchdir))); ++i) {
294: String s_current_path = s_path + "/" + sa_contents[i];
295: com.sun.xfile.XFile x_file = null;
296: try {
297: x_file = getXFile(s_current_path, true, false,
298: i_ids);
299: } catch (Exception e) {
300: }
301: if (sa_contents[i].indexOf(pattern) > -1) {
302: String path = x_file.toString()
303: .substring(i_prefix_length,
304: x_file.toString().length());
305: if (x_file.isDirectory()) {
306: path += "/";
307: }
308: v_search_results.add(path);
309: }
310: if (x_file.isDirectory()) {
311: if (x_file.canRead()) {
312: search(x_file, maxsrchdir, i_prefix_length,
313: v_search_results, pattern, i_ids);
314: } else {
315: v_search_results.add("Permission denied :"
316: + x_file.toString().substring(
317: i_prefix_length,
318: x_file.toString().length()));
319: }
320: }
321: }
322: }
323: }
324:
325: String delNFSFile(String username, String password, String VMSname,
326: String machname, String mainfilename, String dir_nam,
327: String tmpdir, NetFileResource nfr_user_locale_i18n_bucket)
328: throws NetFileException {
329: if (!doAuthenticate(username, password))
330: throw new NetFileException(
331: NetFileException.KEY_IDENTIFIER_PREFIX + "nf.2");
332:
333: try {
334: int[] i_ids = getUserIDs(username, password);
335: com.sun.xfile.XFile xf_parent_directory = getXFile(VMSname,
336: machname, dir_nam, true,
337: SHOULD_HAVE_READ_PERMISSION, i_ids);
338: if (!xf_parent_directory.canWrite()) {
339: throw new NetFileException(
340: new String[] { NetFileException.KEY_IDENTIFIER_PREFIX
341: + "no_write_permission_to_parent" });
342: }
343: com.sun.xfile.XFile xf_file = getXFile(VMSname, machname,
344: dir_nam + "/" + mainfilename, true,
345: SHOULD_HAVE_READ_PERMISSION, i_ids);
346: if (!xf_file.canWrite()) {
347: throw new NetFileException(
348: new String[] { NetFileException.KEY_IDENTIFIER_PREFIX
349: + "no_write_permission" });
350: }
351: xf_file.delete();
352: } catch (Exception e) {
353: // logger.log(Level.SEVERE, "Error in putting NFS File",e);
354: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1084");
355: return "ERROR:" + e.getMessage();
356: }
357: return nfr_user_locale_i18n_bucket.getString("info5");
358: }
359:
360: private String getFileURL(String VMSnam, String machname,
361: String dir_nam) {
362: StringBuffer sb_file_pointer = new StringBuffer("nfs://");
363: sb_file_pointer.append(machname);
364: sb_file_pointer.append(VMSnam);
365: sb_file_pointer.append(dir_nam);
366: return sb_file_pointer.toString();
367: }
368:
369: public com.sun.xfile.XFileOutputStream getNFSOutputStream(
370: String username, String password, String machname,
371: String VMSname, String remotefil_arg, String dir_nam)
372: throws NetFileException {
373:
374: return this .getNFSOutputStream(username, password, machname,
375: VMSname, remotefil_arg, dir_nam, false);
376: }
377:
378: public com.sun.xfile.XFileOutputStream getNFSOutputStream(
379: String username, String password, String machname,
380: String VMSname, String remotefil_arg, String dir_nam,
381: boolean append) throws NetFileException {
382:
383: if (!doAuthenticate(username, password))
384: throw new NetFileException(
385: NetFileException.KEY_IDENTIFIER_PREFIX + "nf.2");
386:
387: try {
388: int[] i_ids = getUserIDs(username, password);
389: com.sun.xfile.XFile xf_parent_dir = getXFile(VMSname,
390: machname, dir_nam, true,
391: SHOULD_HAVE_READ_PERMISSION, i_ids);
392:
393: if (!(xf_parent_dir.canWrite())) {
394: throw new NetFileException(new String[] {
395: NetFileException.KEY_IDENTIFIER_PREFIX
396: + "no_write_permission_to_parent",
397: NetFileException.KEY_IDENTIFIER_PREFIX + " : "
398: + xf_parent_dir.getPath() });
399: }
400:
401: com.sun.xfile.XFile xfile = getXFile(VMSname, machname,
402: dir_nam + "/" + remotefil_arg, false,
403: SHOULD_NOT_HAVE_READ_PERMISSION, i_ids);
404:
405: if (append)
406: outstream = new XFileOutputStream(xfile, append);
407: else
408: outstream = new XFileOutputStream(xfile);
409:
410: return outstream;
411: } catch (Exception e) {
412: // logger.log(Level.SEVERE, "Error in getting output stream for NFS File",e);
413: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1085");
414:
415: if (e instanceof NetFileException)
416: throw (NetFileException) e;
417: throw new NetFileException(
418: NetFileException.KEY_IDENTIFIER_PREFIX
419: + e.getMessage());
420: }
421: }
422:
423: public com.sun.xfile.XFileInputStream getNFSInputStream(
424: String username, String password, String VMSname,
425: String machname, String mainfilename, String dir_nam)
426: throws Exception, NetFileException {
427: if (!doAuthenticate(username, password))
428: throw new NetFileException(
429: NetFileException.KEY_IDENTIFIER_PREFIX + "nf.2");
430:
431: int[] i_ids = getUserIDs(username, password);
432: com.sun.xfile.XFile xfile = getXFile(VMSname, machname, dir_nam
433: + "/" + mainfilename, true,
434: SHOULD_HAVE_READ_PERMISSION, i_ids);
435:
436: instream = new com.sun.xfile.XFileInputStream(xfile);
437: return instream;
438: }
439:
440: public void closeNfsFile() throws IOException {
441: if (instream != null)
442: instream.close();
443: if (outstream != null)
444: outstream.close();
445:
446: instream = null;
447: outstream = null;
448: }
449:
450: private com.sun.xfile.XFile getXFile(String s_nfs_url,
451: boolean b_file_should_exist, boolean b_is_read_allowed,
452: int[] i_ids) throws NetFileException {
453: com.sun.xfile.XFile xfile = new com.sun.xfile.XFile(s_nfs_url);
454: com.sun.nfs.XFileExtensionAccessor nfsx = new com.sun.nfs.XFileExtensionAccessor(
455: xfile);
456: int i_uid = i_ids[0];
457: int i_gid = i_ids[1];
458: int[] i_gids = new int[] { i_gid };
459: nfsx.loginUGID(i_uid, i_gid, i_gids);
460: if (b_file_should_exist) {
461: if (!(xfile.exists())) {
462: throw new NetFileException(new String[] {
463: xfile.getPath(),
464: " ",
465: NetFileException.KEY_IDENTIFIER_PREFIX
466: + "not_found" });
467: } else {
468: }
469: }
470:
471: if (b_is_read_allowed) {
472: if (!xfile.canRead()) {
473: throw new NetFileException(new String[] {
474: xfile.getPath(),
475: " ",
476: NetFileException.KEY_IDENTIFIER_PREFIX
477: + "no_read_permission" });
478: }
479: }
480: return xfile;
481: }
482:
483: private com.sun.xfile.XFile getXFile(String VMSName,
484: String machname, String file, boolean b_file_should_exist,
485: boolean b_is_read_allowed, int[] i_ids)
486: throws NetFileException {
487: if (!VMSName.startsWith("/")) {
488: VMSName = ("/" + VMSName);
489: }
490: if (!file.startsWith("/")) {
491: file = ("/" + file);
492: }
493: String s_nfs_url = getFileURL(VMSName, machname, file);
494: return getXFile(s_nfs_url, b_file_should_exist,
495: b_is_read_allowed, i_ids);
496: }
497:
498: void rename(String username, String password, String machine,
499: String share, String directory, String old_file_name,
500: String new_file_name) throws NetFileException {
501: if (!doAuthenticate(username, password))
502: throw new NetFileException(
503: NetFileException.KEY_IDENTIFIER_PREFIX + "nf.2");
504:
505: try {
506: int[] i_ids = getUserIDs(username, password);
507: com.sun.xfile.XFile xf_parent_directory = getXFile(share,
508: machine, directory, true,
509: SHOULD_HAVE_READ_PERMISSION, i_ids);
510: if (!xf_parent_directory.canWrite()) {
511: throw new NetFileException(new String[] {
512: NetFileException.KEY_IDENTIFIER_PREFIX
513: + "no_write_permission_to_parent",
514: NetFileException.KEY_IDENTIFIER_PREFIX
515: + "textseperator", " ",
516: xf_parent_directory.getPath() });
517: }
518: com.sun.xfile.XFile xf_file = getXFile(share, machine,
519: directory + "/" + old_file_name, true,
520: SHOULD_HAVE_READ_PERMISSION, i_ids);
521: if (!xf_file.canWrite()) {
522: throw new NetFileException(new String[] {
523: NetFileException.KEY_IDENTIFIER_PREFIX
524: + "no_write_permission",
525: NetFileException.KEY_IDENTIFIER_PREFIX
526: + "textseperator", " ",
527: xf_file.getPath() });
528: }
529: XFile xf_renamed_file = new XFile(xf_parent_directory,
530: new_file_name);
531: if (xf_renamed_file.exists()) {
532: if (!xf_renamed_file.canWrite()) {
533: throw new NetFileException(new String[] {
534: NetFileException.KEY_IDENTIFIER_PREFIX
535: + "no_write_permission",
536: NetFileException.KEY_IDENTIFIER_PREFIX
537: + "textseperator", " ",
538: xf_renamed_file.getPath() });
539: }
540: }
541: xf_file.renameTo(xf_renamed_file);
542: } catch (Exception e) {
543: // logger.log(Level.SEVERE, "Error in putting NFS File",e);
544: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1086");
545: throw new NetFileException(
546: NetFileException.KEY_IDENTIFIER_PREFIX
547: + "problem_rename_file");
548: }
549: }
550:
551: boolean doAuthenticate(String username, String passwd)
552: throws NetFileException {
553: try {
554: String osname = System.getProperty("os.name");
555: // logger.info("OS detected is:" + osname);
556: Object[] params5 = { osname };
557: logger.log(Level.INFO, "PSSRNF_CSPNSJ1087", params5);
558: if (osname.indexOf("Linux") != -1
559: || osname.indexOf("SunOS") != -1) {
560: // logger.info("Trying to do Native UNIX Auth");
561: logger.info("PSSRNF_CSPNSJ1088");
562: boolean ret = false;
563: ret = doNativeAuth(username, passwd);
564:
565: // logger.info("Native Auth code returned: " + ret);
566: Object[] params7 = { new Boolean(ret) };
567: logger.log(Level.INFO, "PSSRNF_CSPNSJ1089", params7);
568: return ret;
569: } else {
570: //TODO: use pcnfs for authentication
571: // logger.info("Not UNIX OS, so not authenticating");
572: logger.info("PSSRNF_CSPNSJ1090");
573: return true;
574: }
575: } catch (Exception e) {
576: // logger.log(Level.SEVERE, "NfsFile, Exception while authenticating, ",e);
577: logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1091");
578: return false;
579: }
580: }
581: }
|