|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2009 - 2018 Deutsches Elektronen-Synchroton, |
| 2 | + * Copyright (c) 2009 - 2019 Deutsches Elektronen-Synchroton, |
3 | 3 | * Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY
|
4 | 4 | *
|
5 | 5 | * This library is free software; you can redistribute it and/or modify
|
|
21 | 21 |
|
22 | 22 | import com.sun.security.auth.UnixNumericGroupPrincipal;
|
23 | 23 | import com.sun.security.auth.UnixNumericUserPrincipal;
|
| 24 | +import com.sun.security.auth.module.UnixSystem; |
24 | 25 | import org.dcache.oncrpc4j.xdr.XdrAble;
|
25 | 26 | import org.dcache.oncrpc4j.xdr.XdrDecodingStream;
|
26 | 27 | import org.dcache.oncrpc4j.xdr.XdrEncodingStream;
|
|
29 | 30 |
|
30 | 31 | import javax.security.auth.Subject;
|
31 | 32 | import java.io.IOException;
|
| 33 | +import java.net.InetAddress; |
32 | 34 | import java.security.Principal;
|
| 35 | +import java.time.Instant; |
33 | 36 | import java.util.Arrays;
|
34 | 37 | import java.util.Set;
|
35 | 38 |
|
| 39 | +import static com.google.common.base.Preconditions.checkState; |
| 40 | + |
36 | 41 | public class RpcAuthTypeUnix implements RpcAuth, XdrAble {
|
37 | 42 |
|
38 | 43 | private final int _type = RpcAuthType.UNIX;
|
@@ -137,4 +142,32 @@ public int gid() {
|
137 | 142 | public int[] gids() {
|
138 | 143 | return _gids;
|
139 | 144 | }
|
| 145 | + |
| 146 | + /** |
| 147 | + * Get {@link RpcAuthTypeUnix} corresponding to the UNIX user running |
| 148 | + * this application. |
| 149 | + * @return RPC auth corresponding to UNIX credentials. |
| 150 | + * @throws IllegalStateException |
| 151 | + * @throws IOException if failed to get information to build UNIX credentials. |
| 152 | + */ |
| 153 | + public static RpcAuthTypeUnix ofCurrentUnixUser() throws |
| 154 | + IllegalStateException, IOException { |
| 155 | + |
| 156 | + String os = System.getProperty("os.name"); |
| 157 | + checkState(!os.toLowerCase().startsWith("windows"), "%s platform not supported", os); |
| 158 | + |
| 159 | + UnixSystem user = new UnixSystem(); |
| 160 | + |
| 161 | + long uid = user.getUid(); |
| 162 | + long gid = user.getGid(); |
| 163 | + long[] gids = user.getGroups(); |
| 164 | + |
| 165 | + return new RpcAuthTypeUnix( |
| 166 | + (int)uid, |
| 167 | + (int)gid, |
| 168 | + Arrays.stream(gids).mapToInt(l -> (int)l).toArray(), |
| 169 | + (int)Instant.now().getEpochSecond(), |
| 170 | + InetAddress.getLocalHost().getHostName() |
| 171 | + ); |
| 172 | + } |
140 | 173 | }
|
0 commit comments