mercurial/ui.py
changeset 3551 3b07e223534b
parent 3489 c3345b0f2fcd
child 3552 9b52239dc740
equal deleted inserted replaced
3550:ef80b13df85a 3551:3b07e223534b
    37             self.quiet = quiet
    37             self.quiet = quiet
    38             self.verbose = verbose
    38             self.verbose = verbose
    39             self.debugflag = debug
    39             self.debugflag = debug
    40             self.interactive = interactive
    40             self.interactive = interactive
    41             self.traceback = traceback
    41             self.traceback = traceback
       
    42             self.trusted_users = {}
       
    43             self.trusted_groups = {}
    42             self.cdata = util.configparser()
    44             self.cdata = util.configparser()
    43             self.readconfig(util.rcpath())
    45             self.readconfig(util.rcpath())
    44             self.updateopts(verbose, debug, quiet, interactive)
    46             self.updateopts(verbose, debug, quiet, interactive)
    45         else:
    47         else:
    46             # parentui may point to an ui object which is already a child
    48             # parentui may point to an ui object which is already a child
    47             self.parentui = parentui.parentui or parentui
    49             self.parentui = parentui.parentui or parentui
    48             self.readhooks = self.parentui.readhooks[:]
    50             self.readhooks = self.parentui.readhooks[:]
       
    51             self.trusted_users = parentui.trusted_users.copy()
       
    52             self.trusted_groups = parentui.trusted_groups.copy()
    49             self.cdata = dupconfig(self.parentui.cdata)
    53             self.cdata = dupconfig(self.parentui.cdata)
    50             if self.parentui.overlay:
    54             if self.parentui.overlay:
    51                 self.overlay = dupconfig(self.parentui.overlay)
    55                 self.overlay = dupconfig(self.parentui.overlay)
    52 
    56 
    53     def __getattr__(self, key):
    57     def __getattr__(self, key):
    80             self.verbose = True
    84             self.verbose = True
    81             self.quiet = False
    85             self.quiet = False
    82         elif self.verbose and self.quiet:
    86         elif self.verbose and self.quiet:
    83             self.quiet = self.verbose = False
    87             self.quiet = self.verbose = False
    84 
    88 
       
    89     def _is_trusted(self, fp, f, warn=True):
       
    90         tusers = self.trusted_users
       
    91         tgroups = self.trusted_groups
       
    92         if (tusers or tgroups) and '*' not in tusers and '*' not in tgroups:
       
    93             st = util.fstat(fp)
       
    94             user = util.username(st.st_uid)
       
    95             group = util.groupname(st.st_gid)
       
    96             if user not in tusers and group not in tgroups:
       
    97                 if warn:
       
    98                     self.warn(_('Not reading file %s from untrusted '
       
    99                                 'user %s, group %s\n') % (f, user, group))
       
   100                 return False
       
   101         return True
       
   102 
    85     def readconfig(self, fn, root=None):
   103     def readconfig(self, fn, root=None):
    86         if isinstance(fn, basestring):
   104         if isinstance(fn, basestring):
    87             fn = [fn]
   105             fn = [fn]
    88         for f in fn:
   106         for f in fn:
    89             try:
   107             try:
    90                 self.cdata.read(f)
   108                 fp = open(f)
       
   109             except IOError:
       
   110                 continue
       
   111             if not self._is_trusted(fp, f):
       
   112                 continue
       
   113             try:
       
   114                 self.cdata.readfp(fp, f)
    91             except ConfigParser.ParsingError, inst:
   115             except ConfigParser.ParsingError, inst:
    92                 raise util.Abort(_("Failed to parse %s\n%s") % (f, inst))
   116                 raise util.Abort(_("Failed to parse %s\n%s") % (f, inst))
    93         # override data from config files with data set with ui.setconfig
   117         # override data from config files with data set with ui.setconfig
    94         if self.overlay:
   118         if self.overlay:
    95             updateconfig(self.overlay, self.cdata)
   119             updateconfig(self.overlay, self.cdata)
   141             if name is None or name in ('quiet', 'verbose', 'debug'):
   165             if name is None or name in ('quiet', 'verbose', 'debug'):
   142                 self.verbosity_constraints()
   166                 self.verbosity_constraints()
   143 
   167 
   144             if name is None or name == 'interactive':
   168             if name is None or name == 'interactive':
   145                 self.interactive = self.configbool("ui", "interactive", True)
   169                 self.interactive = self.configbool("ui", "interactive", True)
       
   170 
       
   171         # update trust information
       
   172         if section is None or section == 'trusted':
       
   173             user = util.username()
       
   174             if user is not None:
       
   175                 self.trusted_users[user] = 1
       
   176                 for user in self.configlist('trusted', 'users'):
       
   177                     self.trusted_users[user] = 1
       
   178                 for group in self.configlist('trusted', 'groups'):
       
   179                     self.trusted_groups[group] = 1
   146 
   180 
   147     def setconfig(self, section, name, value):
   181     def setconfig(self, section, name, value):
   148         if not self.overlay:
   182         if not self.overlay:
   149             self.overlay = util.configparser()
   183             self.overlay = util.configparser()
   150         for cdata in (self.overlay, self.cdata):
   184         for cdata in (self.overlay, self.cdata):