- undo broken workaround to svn diff -c4481
[opensuse:build-service.git] / dist / bugz-login
1 #!/usr/bin/python
2
3 # log into bugzilla and write bugz cookie file
4
5 import sys, os
6
7
8 def update_bugz_cookie(cookie):
9     """overwrites ~/.bugz_cookie file with bugzilla cookie in LWP format"""
10     jar = os.path.expanduser('~/.bugz_cookie')
11     jarfd = open(jar, 'w')
12     os.chmod(jar, 0600)
13     jarfd.write("""#LWP-Cookies-1.0
14
15 Set-Cookie3: IPCZQX018ef15359=%s; path="/"; domain=.novell.com; expires="2010-10-10 12:34:56"
16 """ % cookie.split()[2])
17     jarfd.close()
18
19
20 def get_cookie():
21     import httplib
22     import getpass
23     from urllib import urlencode
24     from urlparse import urlparse, urljoin
25     import os
26
27     usernamefile = os.path.expanduser('~/.bzuser')
28     if os.path.isfile(usernamefile):
29         username = open(usernamefile).readlines()[0]
30     else:
31         username = getpass.getuser()
32     print >>sys.stderr, 'Password: ',
33     password = getpass.getpass(prompt='')
34     url_base, url_rel = 'https://bugzilla.novell.com', '/ICSLogin/'
35     target_url = 'https://bugzilla.novell.com/ichainlogin.cgi?target=index.cgi'
36
37     params = {'url': target_url,
38           'context': 'default',
39           'message': 'Please log In',
40           'proxypath': 'reverse',
41           'username': username, 
42           'password': password,
43           }
44     data = urlencode(params)
45
46     host = urlparse(url_base)[1]
47     #host = 'aust.suse.de'
48
49     h = httplib.HTTPS(host)
50     #h.set_debuglevel(1)
51     h.putrequest('POST', url_rel)
52     h.putheader('User-agent', 'python-httplib 1.0')
53     h.putheader('Host', host)
54     h.putheader('Content-Length', str(len(data)))
55     h.putheader('Content-Type', 'application/x-www-form-urlencoded')
56     h.endheaders()
57
58     h.send(data)
59
60     errcode, errmsg, headers = h.getreply()
61     #print >>sys.stderr, 'errcode: ', errcode
62     #print >>sys.stderr, 'headers: ', headers
63     #h.close()
64
65     if errcode == 302:
66         if not headers.has_key('set-cookie'):
67             print >>sys.stderr, 'no cookie received...'
68             return None
69         else:
70             c = headers['set-cookie']
71             c = c.split('; ')
72
73             cookie = '\t'.join([urljoin(url_base, url_rel),
74                         c[0].split('=')[0],
75                         c[0].split('=')[1],
76                         '1877472000',
77                         c[2].split('=')[1],
78                         c[1].split('=')[1],
79                         '9',
80                         '0',
81                        ])
82
83             return cookie
84             
85
86     else:
87         print >>sys.stderr, 'could not log in'
88         print >>sys.stderr, errcode
89         print >>sys.stderr, errmsg
90         print >>sys.stderr, headers
91         if headers: print >>sys.stderr, headers.has_key('set-cookie')
92         return None
93
94
95 if __name__ == '__main__':
96     cookie = get_cookie()
97
98     if not cookie:
99         sys.exit(1)
100     else:
101         update_bugz_cookie(cookie)
102         sys.exit(0)
103