View Javadoc
1   /*
2    * junixsocket
3    *
4    * Copyright 2009-2024 Christian Kohlschütter
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.newsclub.net.unix.demo.rmi.fd;
19  
20  import java.io.IOException;
21  import java.rmi.AlreadyBoundException;
22  
23  import org.newsclub.net.unix.rmi.AFUNIXNaming;
24  
25  /**
26   * Demonstrates how to read/write files via FileDescriptors that are exchanged via RMI.
27   *
28   * This allows reading/writing from and to files that are otherwise not even accessible by the user.
29   * For example, starting the {@link StreamServer} as root and the {@link StreamClient} as a
30   * non-privileged user will allow the non-privileged user to read files only accessible to root.
31   *
32   * NOTE: For obvious security reasons, running this server without modification is not advised for
33   * anything other than demo purposes.
34   *
35   * @author Christian Kohlschütter
36   * @see StreamClient
37   */
38  public final class StreamServer {
39    private StreamServer() {
40      throw new IllegalStateException("No instances");
41    }
42  
43    /**
44     * {@link StreamServer} command-line tool.
45     *
46     * @param args Command-line arguments.
47     * @throws IOException on error.
48     * @throws AlreadyBoundException if there was already a server running.
49     */
50    public static void main(String[] args) throws IOException, AlreadyBoundException {
51      AFUNIXNaming naming = AFUNIXNaming.getInstance();
52      System.out.println("Socket directory: " + naming.getSocketFactory().getSocketDir());
53  
54      try (StreamServiceImpl service = new StreamServiceImpl(naming.getSocketFactory())) {
55        naming.exportAndBind("streamService", service);
56  
57        System.out.println("StreamServer ready; user.name=" + System.getProperty("user.name"));
58      }
59    }
60  }