This file is indexed.

/usr/share/hplip/base/password.py is in hplip-data 3.14.3-0ubuntu3.4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# -*- coding: utf-8 -*-
#
# (c) Copyright @ 2013 Hewlett-Packard Development Company, L.P.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
# Author: Amarnath Chitumalla
#
import os
import getpass
import cStringIO
import time
import string

from base import utils, tui, os_utils
from base.g import *
import pexpect

PASSWORD_RETRY_COUNT = 3

AUTH_TYPES ={'mepis':'su',
             'debian':'su',
             'suse':'su',
             'mandriva':'su',
             'fedora':'su',
             'redhat':'su',
             'rhel':'su',
             'slackware':'su',
             'gentoo':'su',
             'redflag':'su',
             'ubuntu':'sudo',
             'xandros':'su',
             'freebsd':'su',
             'linspire':'su',
             'ark':'su',
             'pclinuxos':'su',
             'centos':'su',
             'igos':'su',
             'linuxmint':'sudo',
             'linpus':'sudo',
             'gos':'sudo',
             'boss':'su',
             'lfs':'su',
             }


# This function promts for the username and password and returns (username,password)
def showPasswordPrompt(prompt):
    import getpass
    print ""
    print ""
    print log.bold(prompt)
    username = raw_input("Username: ")
    password = getpass.getpass("Password: ")

    return (username, password)



#TBD this function shoud be removed once distro class implemented
def get_distro_name():
    os_name = None;
    if utils.which('lsb_release'):
       name = os.popen('lsb_release -i | cut -f 2')
       os_name = name.read().strip()
       name.close()

    if not os_name:
       name = os.popen("cat /etc/issue | awk '{print $1}' | head -n 1")
       os_name = name.read().strip()
       name.close()

    if "redhatenterprise" in os_name.lower():
        os_name = 'rhel'
    elif "suse" in os_name.lower():
        os_name = 'suse'

    return os_name




class Password(object):
    def __init__(self, Mode = INTERACTIVE_MODE):
        self.__password =""
        self.__passwordValidated = False
        self.__mode = Mode
        self.__readAuthType()  #self.__authType   
        self.__expectList =[]

        if not utils.to_bool(sys_conf.get('configure','qt4', '0')) and utils.to_bool(sys_conf.get('configure','qt3', '0')):
            self.__ui_toolkit = 'qt3'
        else:
            self.__ui_toolkit = 'qt4'
            
            
        for s in utils.EXPECT_WORD_LIST:
            try:
                p = re.compile(s, re.I)
            except TypeError:
                self.__expectList.append(s)
            else:
                self.__expectList.append(p)

    ##################### Private functions ######################


    def __readAuthType(self):
        #TBD: Getting distro name should get distro class
        distro_name =  get_distro_name().lower()

        self.__authType = user_conf.get('authentication', 'su_sudo', '')
        if self.__authType != "su" and self.__authType != "sudo":
            try:
                self.__authType = AUTH_TYPES[distro_name]
            except KeyError:
                log.warn("%s distro is not found in AUTH_TYPES"%distro_name)
                self.__authType = 'su'

    def __getPasswordDisplayString(self):
        if self.__authType == "su":
            return "Please enter the root/superuser password: "
        else:
            return "Please enter the sudoer (%s)'s password: " % os.getenv('USER')


    def __changeAuthType(self):
        if self.__authType == "sudo":
            self.__authType = "su"
        else:
            self.__authType = "sudo"
        user_conf.set('authentication', 'su_sudo', self.__authType)


    def __get_password(self,pswd_msg=''):
        if pswd_msg == '':
            if self.__authType == "su":
                pswd_msg = "Please enter the root/superuser password: "
            else:
                pswd_msg = "Please enter the sudoer (%s)'s password: " % os.getenv('USER')

        return getpass.getpass(log.bold(pswd_msg))



    def __get_password_ui(self,pswd_msg='', user ="root"):
        if pswd_msg == '':
            pswd_msg = "Your HP Device requires to install HP proprietary plugin\nPlease enter root/superuser password to continue"

        if self.__ui_toolkit == "qt3":
            from ui.setupform import showPasswordUI
            username, password = showPasswordUI(pswd_msg, user, False)
        else:       #self.__ui_toolkit == "qt4" --> default qt4
            from ui4.setupdialog import showPasswordUI
            username, password = showPasswordUI(pswd_msg, user, False)

        if username == "" and password == "":
            raise Exception("User Cancel")
            
        return  password


    def __password_check(self, cmd, timeout=1):
        output = cStringIO.StringIO()
        ok, ret = False, ''

        try:
            child = pexpect.spawn(cmd, timeout=timeout)
        except pexpect.ExceptionPexpect:
            return 1, ''

        try:
            try:
                start = time.time()

                while True:
                    update_spinner()

                    i = child.expect_list(self.__expectList)
                    cb = child.before
                    if cb:
                        # output
                        start = time.time()
                        log.log_to_file(cb)
                        log.debug(cb)
                        output.write(cb)

                    if i == 0: # EOF
                        ok, ret = True, output.getvalue()
                        break

                    elif i == 1: # TIMEOUT
                        continue

                    else: # password
                        child.sendline(self.__password)

            except (Exception, pexpect.ExceptionPexpect):
                log.exception()

        finally:
            cleanup_spinner()

            try:
                child.close()
            except OSError:
                pass

        if ok:
            return child.exitstatus, ret
        else:
            return 1, ''


    def __validatePassword(self ,pswd_msg):
        x = 1
        while True:
            if self.__mode == INTERACTIVE_MODE:
                self.__password = self.__get_password(pswd_msg)
            else:
                if self.getAuthType() == 'su':
                    self.__password = self.__get_password_ui(pswd_msg, "root")
                else:
                    self.__password = self.__get_password_ui(pswd_msg, os.getenv("USER"))

            cmd = self.getAuthCmd() % "true"
            log.debug(cmd)

            status, output = self.__password_check(cmd)
            log.debug("status = %s  output=%s "%(status,output))

            if self.__mode == GUI_MODE:
                if self.__ui_toolkit == "qt4":
                    from ui4.setupdialog import FailureMessageUI
                if self.__ui_toolkit == "qt3":
                    from ui.setupform import FailureMessageUI


            if status == 0:
                self.__passwordValidated = True
                break
            elif "not in the sudoers file" in output:
                #TBD.. IF user doesn't have sudo permissions, needs to change to "su" type and query for password
                self.__changeAuthType()
                msg = "User doesn't have sudo permissions.\nChanging Authentication Type. Try again."
                if self.__mode == GUI_MODE:
                    FailureMessageUI(msg)
                else:
                    log.error(msg)
                raise Exception("User is not in the sudoers file.")

            else:
                self.__password = ""
                x += 1
                if self.__mode == GUI_MODE:
                    if x > PASSWORD_RETRY_COUNT:
                        FailureMessageUI("Password incorrect. ")
                        return
                    else:
                        FailureMessageUI("Password incorrect. %d attempt(s) left." % (PASSWORD_RETRY_COUNT +1 -x ))
                else:
                    if x > PASSWORD_RETRY_COUNT:
                        log.error("Password incorrect. ")
                        return
                    else:
                        log.error("Password incorrect. %d attempt(s) left." % (PASSWORD_RETRY_COUNT +1 -x ))


    def __get_password_utils(self):
        if self.__authType == "su":
            AuthType, AuthCmd = 'su', 'su -c "%s"'
        else:
            AuthType, AuthCmd = 'sudo', 'sudo %s'

        return AuthType, AuthCmd


    def __get_password_utils_ui(self):
        distro_name =  get_distro_name().lower()
        if self.__authType == "sudo":
            AuthType, AuthCmd = 'sudo', 'sudo %s'
        elif distro_name == 'rhel':
            AuthType, AuthCmd  = 'su', 'su -c "%s"'
        else:
            AuthType, AuthCmd  = 'su', 'su - -c "%s"'
        '''
        if utils.which('kdesu'):
            AuthType, AuthCmd = 'kdesu', 'kdesu -- %s'
        elif utils.which('kdesudo'):
            AuthType, AuthCmd = 'kdesudo', 'kdesudo -- %s'
        elif utils.which('gnomesu'):
            AuthType, AuthCmd = 'gnomesu', 'gnomesu -c "%s"'
        elif utils.which('gksu'):
            AuthType, AuthCmd = 'gksu' , 'gksu "%s"'
        '''

        return AuthType, AuthCmd


    ##################### Public functions ######################

    def clearPassword(self):
        log.debug("Clearing password...")
        self.__password =""
        self.__passwordValidated = False
        if self.__authType == 'sudo':
            os_utils.execute("sudo -K")


    def getAuthType(self):
        if self.__mode == INTERACTIVE_MODE:
            retValue = self.__authType
        else:
            retValue, AuthCmd = self.__get_password_utils_ui()

        return retValue


    def getAuthCmd(self):
        if self.__mode == INTERACTIVE_MODE:
            AuthType, AuthCmd = self.__get_password_utils()
        else:
            AuthType, AuthCmd = self.__get_password_utils_ui()

        return AuthCmd


    def getPassword(self, pswd_msg='', psswd_queried_cnt = 0):
        if self.__passwordValidated:
            return self.__password

        if psswd_queried_cnt:
            return self.__password

        self.__validatePassword( pswd_msg)
        return self.__password