| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Script to log into the login.tikona.in page |
| 4 |
# |
| 5 |
# Needs a config file with username and password in |
| 6 |
# /etc/network-auto-logins or in a user's home dir: |
| 7 |
# $HOME/etc/network-auto-logins |
| 8 |
# |
| 9 |
# The config file format is: |
| 10 |
# [Tikona] |
| 11 |
# username: xyz |
| 12 |
# password: 123 |
| 13 |
# |
| 14 |
# Ensure to adjust the permissions of the config file so that only |
| 15 |
# root or the user has read/write access. |
| 16 |
# |
| 17 |
# Author: Amit Shah <amitshah@gmx.net> |
| 18 |
# Copyright (C) 2010, Amit Shah <amitshah@gmx.net> |
| 19 |
# |
| 20 |
# Distributed under the GPL v2, see the file COPYING for details. |
| 21 |
|
| 22 |
import urllib, urllib2, cookielib, ConfigParser, os |
| 23 |
from re import search |
| 24 |
|
| 25 |
debug = 0 |
| 26 |
|
| 27 |
config = ConfigParser.RawConfigParser() |
| 28 |
cfg_file = config.read(['/etc/network-auto-logins', |
| 29 |
os.path.expanduser('~/etc/network-auto-logins')]) |
| 30 |
if not cfg_file: |
| 31 |
print "error: no accessible config file found" |
| 32 |
exit(1) |
| 33 |
|
| 34 |
try: |
| 35 |
username = config.get('Tikona', 'username') |
| 36 |
password = config.get('Tikona', 'password') |
| 37 |
except: |
| 38 |
print "error: no username or password specified in the 'Tikona' section" |
| 39 |
print "error: using config file(s) ", |
| 40 |
print cfg_file |
| 41 |
exit(2) |
| 42 |
|
| 43 |
cj = cookielib.CookieJar() |
| 44 |
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) |
| 45 |
|
| 46 |
try: |
| 47 |
f = opener.open("http://google.com") |
| 48 |
s = f.read() |
| 49 |
except: |
| 50 |
# network is up, but can't resolve hostnames? |
| 51 |
print "error: cannot reach google.com" |
| 52 |
exit(3) |
| 53 |
|
| 54 |
refresh_url_pattern = "HTTP-EQUIV=\"Refresh\" CONTENT=\"2;URL=(.*)\">" |
| 55 |
refresh_url = search(refresh_url_pattern, s) |
| 56 |
|
| 57 |
if not refresh_url: |
| 58 |
print "error: no refresh url obtained" |
| 59 |
if debug: |
| 60 |
print s |
| 61 |
exit(0) |
| 62 |
|
| 63 |
if debug: |
| 64 |
print refresh_url.group(1) |
| 65 |
|
| 66 |
if not search(".*tikona.*", refresh_url.group(0)): |
| 67 |
print "not on tikona network" |
| 68 |
exit(0) |
| 69 |
|
| 70 |
base_url_pattern = "(http.*/)(\?.*)$" |
| 71 |
base_url = search(base_url_pattern, refresh_url.group(0)) |
| 72 |
|
| 73 |
if not base_url: |
| 74 |
print "error: couldn't get baseurl" |
| 75 |
if debug: |
| 76 |
print refresh_url.group(1) |
| 77 |
exit(4) |
| 78 |
|
| 79 |
if debug: |
| 80 |
print "baseurl: " + base_url.group(1) |
| 81 |
|
| 82 |
f = opener.open(refresh_url.group(1)) |
| 83 |
s = f.read() |
| 84 |
|
| 85 |
load_form_pattern = ".*action=\"(.*)\";" |
| 86 |
load_form_id = search(load_form_pattern, s) |
| 87 |
load_form_url = base_url.group(1) + load_form_id.group(1) |
| 88 |
|
| 89 |
if debug: |
| 90 |
print s |
| 91 |
print "load form id: " + load_form_id.group(1) |
| 92 |
print "load_form_url: " + load_form_url |
| 93 |
|
| 94 |
f = opener.open(load_form_url) |
| 95 |
if debug: |
| 96 |
s = f.read() |
| 97 |
print s |
| 98 |
|
| 99 |
# This comes from cookie.js, hardcoding here |
| 100 |
# |
| 101 |
# TODO: Read all the js files and find out what the action is for |
| 102 |
# the savesetting() function. |
| 103 |
# |
| 104 |
# TODO: Also find out which function is invoked on submit: |
| 105 |
# currently it's the savesetting() function. |
| 106 |
# |
| 107 |
login_form_id = "newlogin.do?phone=0" |
| 108 |
type = "2" |
| 109 |
|
| 110 |
login_form_url = base_url.group(1) + login_form_id |
| 111 |
if debug: |
| 112 |
print "Using login form url: " + login_form_url |
| 113 |
|
| 114 |
login_data = urllib.urlencode({'username': username, 'password': password, |
| 115 |
'type': type}) |
| 116 |
|
| 117 |
f = opener.open(login_form_url, login_data) |
| 118 |
if debug: |
| 119 |
s = f.read() |
| 120 |
print s |
| 121 |
|
| 122 |
exit(0) |