public Void invoke(File f, VirtualChannel channel) throws IOException, InterruptedException { f.getParentFile().mkdirs(); OutputStream os = new FileOutputStream(f); try { props.store(os,"Credential store"); } finally { os.close(); } // try to protect this file from other users, if we can. PosixAPI.get().chmod(f.getAbsolutePath(),0600); return null; } });
FileStat st = api.stat("/etc/shadow"); if(st==null) return FormValidation.error(Messages.PAMSecurityRealm_ReadPermission()); Passwd pwd = api.getpwuid(api.geteuid()); String user; if(pwd!=null) user=Messages.PAMSecurityRealm_User(pwd.getLoginName()); Group g = api.getgrgid(st.gid()); if(g!=null) group=g.getName(); else group=String.valueOf(st.gid()); Passwd opwd = api.getpwuid(st.uid()); String owner; if(opwd!=null) owner=opwd.getLoginName();
public boolean createSymlink(String targetPath, File symlinkFile, boolean usePosix) throws NativeAccessException { if (usePosix) { return PosixAPI.get().symlink(symlinkFile.getAbsolutePath(), targetPath) == 0; } else { try { return LIBC.symlink(symlinkFile.getAbsolutePath(), targetPath) == 0; } catch (LinkageError exc) { logger.info("Could not create symlink with JNA. From - " + symlinkFile + " to " + targetPath + ". " + exc.getLocalizedMessage() + " Trying Posix API.."); // if JNA is unavailable, fall back. // we still prefer to try JNA first as PosixAPI supports even smaller platforms. try { return PosixAPI.get().symlink(symlinkFile.getAbsolutePath(), targetPath) == 0; } catch (Throwable ex) { throw new NativeAccessException("Failed to Create Symlink. " + ex.getLocalizedMessage()); } } } }
@Override public boolean makeFileWritable(File file) { try { POSIX posix = PosixAPI.get(); String path = file.getAbsolutePath(); FileStat stat = posix.stat(path); return posix.chmod(path, stat.mode() | 0200) == 0; // u+w } catch (Throwable ex) { throw new NativeAccessException("Failed to make file writable. " + ex.getLocalizedMessage()); } }
/** * Чтение информации о файле * <br>Генерирует StdErr в случаи ошибки * @param filename Файл * @return информация о файле, размер, тип, владелец .... * @see xyz.cofe.unix.libc.StdErr */ public FileStat lstat( String filename ){ if( filename==null )throw new IllegalArgumentException( "filename==null" ); org.jruby.ext.posix.FileStat fstat = null; synchronized( syncObj ){ fstat = posix.lstat(filename); if( fstat==null ){ int errno = posix.errno(); throw new StdErr(errno, LibC.strerror(errno)); } // if( fstat.mode()==0 ){ // int errno = posix.errno(); // throw new StdErr(errno, LibC.strerror(errno)); // } } return new LinuxFileStatImpl(fstat); } //</editor-fold>
@JRubyMethod(name = "switch", module = true, visibility = Visibility.PRIVATE) public static IRubyObject switch_rb(ThreadContext context, IRubyObject self, Block block) { Ruby runtime = self.getRuntime(); int uid = runtime.getPosix().getuid(); int euid = runtime.getPosix().geteuid(); if (block.isGiven()) { try { runtime.getPosix().seteuid(uid); runtime.getPosix().setuid(euid); return block.yield(context, runtime.getNil()); } finally { runtime.getPosix().seteuid(euid); runtime.getPosix().setuid(uid); } } else { runtime.getPosix().seteuid(uid); runtime.getPosix().setuid(euid); return RubyFixnum.zero(runtime); } } }
public Integer invoke(File f, VirtualChannel channel) throws IOException { return PosixAPI.get().stat(f.getPath()).mode(); } });
private void setup(String filename, boolean lstat) { if (Platform.IS_WINDOWS && filename.length() == 2 && filename.charAt(1) == ':' && Character.isLetter(filename.charAt(0))) { filename += "/"; } file = JRubyFile.create(getRuntime().getCurrentDirectory(), filename); if (lstat) { stat = getRuntime().getPosix().lstat(file.getAbsolutePath()); } else { stat = getRuntime().getPosix().stat(file.getAbsolutePath()); } }
return PosixAPI.get().readlink(filename);
@JRubyMethod(name = "getgrgid", optional=1, module = true) public static IRubyObject getgrgid(IRubyObject recv, IRubyObject[] args) { Ruby runtime = recv.getRuntime(); POSIX posix = runtime.getPosix(); int gid = args.length == 0 ? posix.getgid() : RubyNumeric.fix2int(args[0]); Group gr = posix.getgrgid(gid); if(gr == null) { if (Platform.IS_WINDOWS) { // MRI behavior return runtime.getNil(); } throw runtime.newArgumentError("can't find group for " + gid); } return setupGroup(runtime, gr); }
@JRubyMethod(name = "getpwuid", optional=1, module = true) public static IRubyObject getpwuid(IRubyObject recv, IRubyObject[] args) { Ruby runtime = recv.getRuntime(); POSIX posix = runtime.getPosix(); int uid = args.length == 0 ? posix.getuid() : RubyNumeric.fix2int(args[0]); Passwd pwd = posix.getpwuid(uid); if(pwd == null) { if (Platform.IS_WINDOWS) { // MRI behavior return recv.getRuntime().getNil(); } throw runtime.newArgumentError("can't find user for " + uid); } return setupPasswd(runtime, pwd); }
@JRubyMethod(name = "euid", module = true, visibility = Visibility.PRIVATE) public static IRubyObject euid(IRubyObject recv) { return recv.getRuntime().newFixnum(recv.getRuntime().getPosix().geteuid()); }
@JRubyMethod(name = "symlink?", required = 1, module = true) public static RubyBoolean symlink_p(IRubyObject recv, IRubyObject filename) { Ruby runtime = recv.getRuntime(); JRubyFile file = file(filename); try { // Note: We can't use file.exists() to check whether the symlink // exists or not, because that method returns false for existing // but broken symlink. So, we try without the existence check, // but in the try-catch block. // MRI behavior: symlink? on broken symlink should return true. return runtime.newBoolean(runtime.getPosix().lstat(file.getAbsolutePath()).isSymlink()); } catch (RaiseException re) { return runtime.getFalse(); } }
public Integer invoke(File f, VirtualChannel channel) throws IOException { return PosixAPI.get().stat(f.getPath()).mode(); } });
/** * Makes the given file writable by any means possible. */ @IgnoreJRERequirement private static void makeWritable(File f) { // try chmod. this becomes no-op if this is not Unix. try { Chmod chmod = new Chmod(); chmod.setProject(new Project()); chmod.setFile(f); chmod.setPerm("u+w"); chmod.execute(); } catch (BuildException e) { LOGGER.log(Level.INFO,"Failed to chmod "+f,e); } // also try JDK6-way of doing it. try { f.setWritable(true); } catch (NoSuchMethodError e) { // not JDK6 } try {// try libc chmod POSIX posix = PosixAPI.get(); String path = f.getAbsolutePath(); FileStat stat = posix.stat(path); posix.chmod(path, stat.mode()|0200); // u+w } catch (Throwable t) { LOGGER.log(Level.FINE,"Failed to chmod(2) "+f,t); } }
return PosixAPI.get().readlink(filename);
FileStat st = api.stat("/etc/shadow"); if(st==null) return FormValidation.error(Messages.PAMSecurityRealm_ReadPermission()); Passwd pwd = api.getpwuid(api.geteuid()); String user; if(pwd!=null) user=Messages.PAMSecurityRealm_User(pwd.getLoginName()); Group g = api.getgrgid(st.gid()); if(g!=null) group=g.getName(); else group=String.valueOf(st.gid()); Passwd opwd = api.getpwuid(st.uid()); String owner; if(opwd!=null) owner=opwd.getLoginName();
public Void invoke(File f, VirtualChannel channel) throws IOException, InterruptedException { f.getParentFile().mkdirs(); OutputStream os = new FileOutputStream(f); try { props.store(os,"Credential store"); } finally { os.close(); } // try to protect this file from other users, if we can. PosixAPI.get().chmod(f.getAbsolutePath(),0600); return null; } });
@Override public int mode(File file) { try { return PosixAPI.get().stat(file.getPath()).mode(); } catch (Throwable ex) { throw new NativeAccessException("Failed to get File mode. " + ex.getLocalizedMessage()); } }
/** * Makes the given file writable by any means possible. */ @IgnoreJRERequirement private static void makeWritable(File f) { // try chmod. this becomes no-op if this is not Unix. try { Chmod chmod = new Chmod(); chmod.setProject(new Project()); chmod.setFile(f); chmod.setPerm("u+w"); chmod.execute(); } catch (BuildException e) { LOGGER.log(Level.INFO,"Failed to chmod "+f,e); } // also try JDK6-way of doing it. try { f.setWritable(true); } catch (NoSuchMethodError e) { // not JDK6 } try {// try libc chmod POSIX posix = PosixAPI.get(); String path = f.getAbsolutePath(); FileStat stat = posix.stat(path); posix.chmod(path, stat.mode()|0200); // u+w } catch (Throwable t) { LOGGER.log(Level.FINE,"Failed to chmod(2) "+f,t); } }