001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.security;
019
020import java.io.BufferedReader;
021import java.io.File;
022import java.io.FileInputStream;
023import java.io.IOException;
024import java.io.InputStreamReader;
025import java.util.HashMap;
026import java.util.Map;
027import java.util.regex.Matcher;
028import java.util.regex.Pattern;
029
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032import org.apache.hadoop.conf.Configuration;
033import org.apache.hadoop.util.Time;
034
035import com.google.common.annotations.VisibleForTesting;
036import com.google.common.collect.BiMap;
037import com.google.common.collect.HashBiMap;
038
039/**
040 * A simple shell-based implementation of {@link IdMappingServiceProvider} 
041 * Map id to user name or group name. It does update every 15 minutes. Only a
042 * single instance of this class is expected to be on the server.
043 * 
044 * The maps are incrementally updated as described below:
045 *   1. Initialize the maps as empty. 
046 *   2. Incrementally update the maps
047 *      - When ShellBasedIdMapping is requested for user or group name given 
048 *        an ID, or for ID given a user or group name, do look up in the map
049 *        first, if it doesn't exist, find the corresponding entry with shell
050 *        command, and insert the entry to the maps.
051 *      - When group ID is requested for a given group name, and if the
052 *        group name is numerical, the full group map is loaded. Because we
053 *        don't have a good way to find the entry for a numerical group name,
054 *        loading the full map helps to get in all entries.
055 *   3. Periodically refresh the maps for both user and group, e.g,
056 *      do step 1.
057 *   Note: for testing purpose, step 1 may initial the maps with full mapping
058 *   when using constructor
059 *   {@link ShellBasedIdMapping#ShellBasedIdMapping(Configuration, boolean)}.
060 */
061public class ShellBasedIdMapping implements IdMappingServiceProvider {
062
063  private static final Log LOG =
064      LogFactory.getLog(ShellBasedIdMapping.class);
065
066  private final static String OS = System.getProperty("os.name");
067
068  /** Shell commands to get users and groups */
069  static final String GET_ALL_USERS_CMD = "getent passwd | cut -d: -f1,3";
070  static final String GET_ALL_GROUPS_CMD = "getent group | cut -d: -f1,3";
071  static final String MAC_GET_ALL_USERS_CMD = "dscl . -list /Users UniqueID";
072  static final String MAC_GET_ALL_GROUPS_CMD = "dscl . -list /Groups PrimaryGroupID";
073
074  private final File staticMappingFile;
075  private StaticMapping staticMapping = null;
076  private boolean constructFullMapAtInit = false;
077
078  // Used for parsing the static mapping file.
079  private static final Pattern EMPTY_LINE = Pattern.compile("^\\s*$");
080  private static final Pattern COMMENT_LINE = Pattern.compile("^\\s*#.*$");
081  private static final Pattern MAPPING_LINE =
082      Pattern.compile("^(uid|gid)\\s+(\\d+)\\s+(\\d+)\\s*(#.*)?$");
083
084  final private long timeout;
085  
086  // Maps for id to name map. Guarded by this object monitor lock
087  private BiMap<Integer, String> uidNameMap = HashBiMap.create();
088  private BiMap<Integer, String> gidNameMap = HashBiMap.create();
089
090  private long lastUpdateTime = 0; // Last time maps were updated
091
092  /*
093   * Constructor
094   * @param conf the configuration
095   * @param constructFullMapAtInit initialize the maps with full mapping when
096   *        true, otherwise initialize the maps to empty. This parameter is
097   *        intended for testing only, its default is false.
098   */
099  @VisibleForTesting
100  public ShellBasedIdMapping(Configuration conf,
101      boolean constructFullMapAtInit) throws IOException {
102    this.constructFullMapAtInit = constructFullMapAtInit;
103    long updateTime = conf.getLong(
104        IdMappingConstant.USERGROUPID_UPDATE_MILLIS_KEY,
105        IdMappingConstant.USERGROUPID_UPDATE_MILLIS_DEFAULT);
106    // Minimal interval is 1 minute
107    if (updateTime < IdMappingConstant.USERGROUPID_UPDATE_MILLIS_MIN) {
108      LOG.info("User configured user account update time is less"
109          + " than 1 minute. Use 1 minute instead.");
110      timeout = IdMappingConstant.USERGROUPID_UPDATE_MILLIS_MIN;
111    } else {
112      timeout = updateTime;
113    }
114    
115    String staticFilePath = 
116        conf.get(IdMappingConstant.STATIC_ID_MAPPING_FILE_KEY,
117            IdMappingConstant.STATIC_ID_MAPPING_FILE_DEFAULT);
118    staticMappingFile = new File(staticFilePath);
119
120    updateMaps();
121  }
122
123  /*
124   * Constructor
125   * initialize user and group maps to empty
126   * @param conf the configuration
127   */
128  public ShellBasedIdMapping(Configuration conf) throws IOException {
129    this(conf, false);
130  }
131
132  @VisibleForTesting
133  public long getTimeout() {
134    return timeout;
135  }
136
137  @VisibleForTesting
138  public BiMap<Integer, String> getUidNameMap() {
139    return uidNameMap;
140  }
141
142  @VisibleForTesting
143  public BiMap<Integer, String> getGidNameMap() {
144    return gidNameMap;
145  }
146
147  @VisibleForTesting  
148  synchronized public void clearNameMaps() {
149    uidNameMap.clear();
150    gidNameMap.clear();
151    lastUpdateTime = Time.monotonicNow();
152  }  
153
154  synchronized private boolean isExpired() {
155    return Time.monotonicNow() - lastUpdateTime > timeout;
156  }
157
158  // If can't update the maps, will keep using the old ones
159  private void checkAndUpdateMaps() {
160    if (isExpired()) {
161      LOG.info("Update cache now");
162      try {
163        updateMaps();
164      } catch (IOException e) {
165        LOG.error("Can't update the maps. Will use the old ones,"
166            + " which can potentially cause problem.", e);
167      }
168    }
169  }
170
171  private static final String DUPLICATE_NAME_ID_DEBUG_INFO =
172      "NFS gateway could have problem starting with duplicate name or id on the host system.\n"
173      + "This is because HDFS (non-kerberos cluster) uses name as the only way to identify a user or group.\n"
174      + "The host system with duplicated user/group name or id might work fine most of the time by itself.\n"
175      + "However when NFS gateway talks to HDFS, HDFS accepts only user and group name.\n"
176      + "Therefore, same name means the same user or same group. To find the duplicated names/ids, one can do:\n"
177      + "<getent passwd | cut -d: -f1,3> and <getent group | cut -d: -f1,3> on Linux systems,\n"
178      + "<dscl . -list /Users UniqueID> and <dscl . -list /Groups PrimaryGroupID> on MacOS.";
179  
180  private static void reportDuplicateEntry(final String header,
181      final Integer key, final String value,
182      final Integer ekey, final String evalue) {    
183      LOG.warn("\n" + header + String.format(
184          "new entry (%d, %s), existing entry: (%d, %s).\n%s\n%s",
185          key, value, ekey, evalue,
186          "The new entry is to be ignored for the following reason.",
187          DUPLICATE_NAME_ID_DEBUG_INFO));
188  }
189
190  /**
191   * uid and gid are defined as uint32 in linux. Some systems create
192   * (intended or unintended) <nfsnobody, 4294967294> kind of <name,Id>
193   * mapping, where 4294967294 is 2**32-2 as unsigned int32. As an example,
194   *   https://bugzilla.redhat.com/show_bug.cgi?id=511876.
195   * Because user or group id are treated as Integer (signed integer or int32)
196   * here, the number 4294967294 is out of range. The solution is to convert
197   * uint32 to int32, so to map the out-of-range ID to the negative side of
198   * Integer, e.g. 4294967294 maps to -2 and 4294967295 maps to -1.
199   */
200  private static Integer parseId(final String idStr) {
201    Long longVal = Long.parseLong(idStr);
202    int intVal = longVal.intValue();
203    return Integer.valueOf(intVal);
204  }
205  
206  /**
207   * Get the list of users or groups returned by the specified command,
208   * and save them in the corresponding map.
209   * @throws IOException 
210   */
211  @VisibleForTesting
212  public static boolean updateMapInternal(BiMap<Integer, String> map,
213      String mapName, String command, String regex,
214      Map<Integer, Integer> staticMapping) throws IOException  {
215    boolean updated = false;
216    BufferedReader br = null;
217    try {
218      Process process = Runtime.getRuntime().exec(
219          new String[] { "bash", "-c", command });
220      br = new BufferedReader(new InputStreamReader(process.getInputStream()));
221      String line = null;
222      while ((line = br.readLine()) != null) {
223        String[] nameId = line.split(regex);
224        if ((nameId == null) || (nameId.length != 2)) {
225          throw new IOException("Can't parse " + mapName + " list entry:" + line);
226        }
227        LOG.debug("add to " + mapName + "map:" + nameId[0] + " id:" + nameId[1]);
228        // HDFS can't differentiate duplicate names with simple authentication
229        final Integer key = staticMapping.get(parseId(nameId[1]));
230        final String value = nameId[0];
231        if (map.containsKey(key)) {
232          final String prevValue = map.get(key);
233          if (value.equals(prevValue)) {
234            // silently ignore equivalent entries
235            continue;
236          }
237          reportDuplicateEntry(
238              "Got multiple names associated with the same id: ",
239              key, value, key, prevValue);           
240          continue;
241        }
242        if (map.containsValue(value)) {
243          final Integer prevKey = map.inverse().get(value);
244          reportDuplicateEntry(
245              "Got multiple ids associated with the same name: ",
246              key, value, prevKey, value);
247          continue;
248        }
249        map.put(key, value);
250        updated = true;
251      }
252      LOG.debug("Updated " + mapName + " map size: " + map.size());
253      
254    } catch (IOException e) {
255      LOG.error("Can't update " + mapName + " map");
256      throw e;
257    } finally {
258      if (br != null) {
259        try {
260          br.close();
261        } catch (IOException e1) {
262          LOG.error("Can't close BufferedReader of command result", e1);
263        }
264      }
265    }
266    return updated;
267  }
268
269  private boolean checkSupportedPlatform() {
270    if (!OS.startsWith("Linux") && !OS.startsWith("Mac")) {
271      LOG.error("Platform is not supported:" + OS
272          + ". Can't update user map and group map and"
273          + " 'nobody' will be used for any user and group.");
274      return false;
275    }
276    return true;
277  }
278
279  private static boolean isInteger(final String s) {
280    try { 
281      Integer.parseInt(s); 
282    } catch(NumberFormatException e) { 
283      return false; 
284    }
285    // only got here if we didn't return false
286    return true;
287  }
288
289  private void initStaticMapping() throws IOException {
290    staticMapping = new StaticMapping(
291        new HashMap<Integer, Integer>(), new HashMap<Integer, Integer>());
292    if (staticMappingFile.exists()) {
293      LOG.info("Using '" + staticMappingFile + "' for static UID/GID mapping...");
294      staticMapping = parseStaticMap(staticMappingFile);
295    } else {
296      LOG.info("Not doing static UID/GID mapping because '" + staticMappingFile
297          + "' does not exist.");
298    }
299  }  
300
301  /*
302   * Reset the maps to empty.
303   * For testing code, a full map may be re-constructed here when the object
304   * was created with constructFullMapAtInit being set to true.
305   */
306  synchronized public void updateMaps() throws IOException {
307    if (!checkSupportedPlatform()) {
308      return;
309    }
310
311    if (constructFullMapAtInit) {
312      loadFullMaps();
313    } else {
314      clearNameMaps();
315    }
316  }
317  
318  synchronized private void loadFullUserMap() throws IOException {
319    if (staticMapping == null) {
320      initStaticMapping();
321    }
322    BiMap<Integer, String> uMap = HashBiMap.create();
323    if (OS.startsWith("Mac")) {
324      updateMapInternal(uMap, "user", MAC_GET_ALL_USERS_CMD, "\\s+",
325          staticMapping.uidMapping);
326    } else {
327      updateMapInternal(uMap, "user", GET_ALL_USERS_CMD, ":",
328          staticMapping.uidMapping);
329    }
330    uidNameMap = uMap;
331    lastUpdateTime = Time.monotonicNow();
332  }
333
334  synchronized private void loadFullGroupMap() throws IOException {
335    if (staticMapping == null) {
336      initStaticMapping();
337    }
338    BiMap<Integer, String> gMap = HashBiMap.create();
339
340    if (OS.startsWith("Mac")) {
341      updateMapInternal(gMap, "group", MAC_GET_ALL_GROUPS_CMD, "\\s+",
342          staticMapping.gidMapping);
343    } else {
344      updateMapInternal(gMap, "group", GET_ALL_GROUPS_CMD, ":",
345          staticMapping.gidMapping);
346    }
347    gidNameMap = gMap;
348    lastUpdateTime = Time.monotonicNow();
349  }
350
351  synchronized private void loadFullMaps() throws IOException {
352    initStaticMapping();
353    loadFullUserMap();
354    loadFullGroupMap();
355  }
356
357  // search for id with given name, return "<name>:<id>"
358  // return
359  //     getent group <name> | cut -d: -f1,3
360  // OR
361  //     id -u <name> | awk '{print "<name>:"$1 }'
362  //
363  private String getName2IdCmdLinux(final String name, final boolean isGrp) {
364    String cmd;
365    if (isGrp) {
366      cmd = "getent group " + name + " | cut -d: -f1,3";   
367    } else {
368      cmd = "id -u " + name + " | awk '{print \"" + name + ":\"$1 }'";
369    }
370    return cmd;
371  }
372  
373  // search for name with given id, return "<name>:<id>"
374  private String getId2NameCmdLinux(final int id, final boolean isGrp) {
375    String cmd = "getent ";
376    cmd += isGrp? "group " : "passwd ";
377    cmd += String.valueOf(id) + " | cut -d: -f1,3";
378    return cmd;
379  }
380
381  // "dscl . -read /Users/<name> | grep UniqueID" returns "UniqueId: <id>",
382  // "dscl . -read /Groups/<name> | grep PrimaryGroupID" returns "PrimaryGoupID: <id>"
383  // The following method returns a command that uses awk to process the result,
384  // of these commands, and returns "<name> <id>", to simulate one entry returned by 
385  // MAC_GET_ALL_USERS_CMD or MAC_GET_ALL_GROUPS_CMD.
386  // Specificially, this method returns:
387  // id -u <name> | awk '{print "<name>:"$1 }'
388  // OR
389  // dscl . -read /Groups/<name> | grep PrimaryGroupID | awk '($1 == "PrimaryGroupID:") { print "<name> " $2 }'
390  //
391  private String getName2IdCmdMac(final String name, final boolean isGrp) {
392    String cmd;
393    if (isGrp) {
394      cmd = "dscl . -read /Groups/" + name;
395      cmd += " | grep PrimaryGroupID | awk '($1 == \"PrimaryGroupID:\") ";
396      cmd += "{ print \"" + name + "  \" $2 }'";
397    } else {
398      cmd = "id -u " + name + " | awk '{print \"" + name + "  \"$1 }'";
399    }
400    return cmd;
401  }
402
403  // "dscl . -search /Users UniqueID <id>" returns 
404  //    <name> UniqueID = (
405  //      <id>
406  //    )
407  // "dscl . -search /Groups PrimaryGroupID <id>" returns
408  //    <name> PrimaryGroupID = (
409  //      <id>
410  //    )
411  // The following method returns a command that uses sed to process the
412  // the result and returns "<name> <id>" to simulate one entry returned
413  // by MAC_GET_ALL_USERS_CMD or MAC_GET_ALL_GROUPS_CMD.
414  // For certain negative id case like nfsnobody, the <id> is quoted as
415  // "<id>", added one sed section to remove the quote.
416  // Specifically, the method returns:
417  // dscl . -search /Users UniqueID <id> | sed 'N;s/\\n//g;N;s/\\n//g' | sed 's/UniqueID =//g' | sed 's/)//g' | sed 's/\"//g'
418  // OR
419  // dscl . -search /Groups PrimaryGroupID <id> | sed 'N;s/\\n//g;N;s/\\n//g' | sed 's/PrimaryGroupID =//g' | sed 's/)//g' | sed 's/\"//g'
420  //
421  private String getId2NameCmdMac(final int id, final boolean isGrp) {
422    String cmd = "dscl . -search /";
423    cmd += isGrp? "Groups PrimaryGroupID " : "Users UniqueID ";
424    cmd += String.valueOf(id);
425    cmd += " | sed 'N;s/\\n//g;N;s/\\n//g' | sed 's/";
426    cmd += isGrp? "PrimaryGroupID" : "UniqueID";
427    cmd += " = (//g' | sed 's/)//g' | sed 's/\\\"//g'";
428    return cmd;
429  }
430
431  synchronized private void updateMapIncr(final String name,
432      final boolean isGrp) throws IOException {
433    if (!checkSupportedPlatform()) {
434      return;
435    }
436    if (isInteger(name) && isGrp) {
437      loadFullGroupMap();
438      return;
439    }
440
441    boolean updated = false;
442    if (staticMapping == null) {
443      initStaticMapping();
444    }
445
446    if (OS.startsWith("Linux")) {
447      if (isGrp) {
448        updated = updateMapInternal(gidNameMap, "group",
449            getName2IdCmdLinux(name, true), ":",
450            staticMapping.gidMapping);
451      } else {
452        updated = updateMapInternal(uidNameMap, "user",
453            getName2IdCmdLinux(name, false), ":",
454            staticMapping.uidMapping);
455      }
456    } else {
457      // Mac
458      if (isGrp) {        
459        updated = updateMapInternal(gidNameMap, "group",
460            getName2IdCmdMac(name, true), "\\s+",
461            staticMapping.gidMapping);
462      } else {
463        updated = updateMapInternal(uidNameMap, "user",
464            getName2IdCmdMac(name, false), "\\s+",
465            staticMapping.uidMapping);
466      }
467    }
468    if (updated) {
469      lastUpdateTime = Time.monotonicNow();
470    }
471  }
472
473  synchronized private void updateMapIncr(final int id,
474      final boolean isGrp) throws IOException {
475    if (!checkSupportedPlatform()) {
476      return;
477    }
478    
479    boolean updated = false;
480    if (staticMapping == null) {
481      initStaticMapping();
482    }
483
484    if (OS.startsWith("Linux")) {
485      if (isGrp) {
486        updated = updateMapInternal(gidNameMap, "group",
487            getId2NameCmdLinux(id, true), ":",
488            staticMapping.gidMapping);
489      } else {
490        updated = updateMapInternal(uidNameMap, "user",
491            getId2NameCmdLinux(id, false), ":",
492            staticMapping.uidMapping);
493      }
494    } else {
495      // Mac
496      if (isGrp) {
497        updated = updateMapInternal(gidNameMap, "group",
498            getId2NameCmdMac(id, true), "\\s+",
499            staticMapping.gidMapping);
500      } else {
501        updated = updateMapInternal(uidNameMap, "user",
502            getId2NameCmdMac(id, false), "\\s+",
503            staticMapping.uidMapping);
504      }
505    }
506    if (updated) {
507      lastUpdateTime = Time.monotonicNow();
508    }
509  }
510
511  @SuppressWarnings("serial")
512  static final class PassThroughMap<K> extends HashMap<K, K> {
513    
514    public PassThroughMap() {
515      this(new HashMap<K, K>());
516    }
517    
518    public PassThroughMap(Map<K, K> mapping) {
519      super();
520      for (Map.Entry<K, K> entry : mapping.entrySet()) {
521        super.put(entry.getKey(), entry.getValue());
522      }
523    }
524
525    @SuppressWarnings("unchecked")
526    @Override
527    public K get(Object key) {
528      if (super.containsKey(key)) {
529        return super.get(key);
530      } else {
531        return (K) key;
532      }
533    }
534  }
535  
536  @VisibleForTesting
537  static final class StaticMapping {
538    final Map<Integer, Integer> uidMapping;
539    final Map<Integer, Integer> gidMapping;
540    
541    public StaticMapping(Map<Integer, Integer> uidMapping,
542        Map<Integer, Integer> gidMapping) {
543      this.uidMapping = new PassThroughMap<Integer>(uidMapping);
544      this.gidMapping = new PassThroughMap<Integer>(gidMapping);
545    }
546  }
547  
548  static StaticMapping parseStaticMap(File staticMapFile)
549      throws IOException {
550    
551    Map<Integer, Integer> uidMapping = new HashMap<Integer, Integer>();
552    Map<Integer, Integer> gidMapping = new HashMap<Integer, Integer>();
553    
554    BufferedReader in = new BufferedReader(new InputStreamReader(
555        new FileInputStream(staticMapFile)));
556    
557    try {
558      String line = null;
559      while ((line = in.readLine()) != null) {
560        // Skip entirely empty and comment lines.
561        if (EMPTY_LINE.matcher(line).matches() ||
562            COMMENT_LINE.matcher(line).matches()) {
563          continue;
564        }
565        
566        Matcher lineMatcher = MAPPING_LINE.matcher(line);
567        if (!lineMatcher.matches()) {
568          LOG.warn("Could not parse line '" + line + "'. Lines should be of " +
569              "the form '[uid|gid] [remote id] [local id]'. Blank lines and " +
570              "everything following a '#' on a line will be ignored.");
571          continue;
572        }
573        
574        // We know the line is fine to parse without error checking like this
575        // since it matched the regex above.
576        String firstComponent = lineMatcher.group(1);
577        int remoteId = Integer.parseInt(lineMatcher.group(2));
578        int localId = Integer.parseInt(lineMatcher.group(3));
579        if (firstComponent.equals("uid")) {
580          uidMapping.put(localId, remoteId);
581        } else {
582          gidMapping.put(localId, remoteId);
583        }
584      }
585    } finally {
586      in.close();
587    }
588    
589    return new StaticMapping(uidMapping, gidMapping);
590  }
591
592  synchronized public int getUid(String user) throws IOException {
593    checkAndUpdateMaps();
594
595    Integer id = uidNameMap.inverse().get(user);
596    if (id == null) {
597      updateMapIncr(user, false);
598      id = uidNameMap.inverse().get(user);
599      if (id == null) {
600        throw new IOException("User just deleted?:" + user);
601      }
602    }
603    return id.intValue();
604  }
605
606  synchronized public int getGid(String group) throws IOException {
607    checkAndUpdateMaps();
608
609    Integer id = gidNameMap.inverse().get(group);
610    if (id == null) {
611      updateMapIncr(group, true);
612      id = gidNameMap.inverse().get(group);
613      if (id == null) {
614        throw new IOException("No such group:" + group);
615      }
616    }
617    return id.intValue();
618  }
619
620  synchronized public String getUserName(int uid, String unknown) {
621    checkAndUpdateMaps();
622    String uname = uidNameMap.get(uid);
623    if (uname == null) {
624      try {
625        updateMapIncr(uid, false);
626      } catch (Exception e) {        
627      }
628      uname = uidNameMap.get(uid);
629      if (uname == null) {     
630        LOG.warn("Can't find user name for uid " + uid
631            + ". Use default user name " + unknown);
632        uname = unknown;
633      }
634    }
635    return uname;
636  }
637
638  synchronized public String getGroupName(int gid, String unknown) {
639    checkAndUpdateMaps();
640    String gname = gidNameMap.get(gid);
641    if (gname == null) {
642      try {
643        updateMapIncr(gid, true);
644      } catch (Exception e) {        
645      }
646      gname = gidNameMap.get(gid);
647      if (gname == null) {
648        LOG.warn("Can't find group name for gid " + gid
649            + ". Use default group name " + unknown);
650        gname = unknown;
651      }
652    }
653    return gname;
654  }
655
656  // When can't map user, return user name's string hashcode
657  public int getUidAllowingUnknown(String user) {
658    checkAndUpdateMaps();
659    int uid;
660    try {
661      uid = getUid(user);
662    } catch (IOException e) {
663      uid = user.hashCode();
664      LOG.info("Can't map user " + user + ". Use its string hashcode:" + uid);
665    }
666    return uid;
667  }
668
669  // When can't map group, return group name's string hashcode
670  public int getGidAllowingUnknown(String group) {
671    checkAndUpdateMaps();
672    int gid;
673    try {
674      gid = getGid(group);
675    } catch (IOException e) {
676      gid = group.hashCode();
677      LOG.info("Can't map group " + group + ". Use its string hashcode:" + gid);
678    }
679    return gid;
680  }
681}