pppoe.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright (C) 2005, TUBITAK/UEKAE
  5. #
  6. # This program is free software; you can redistribute it and/or modify it under
  7. # the terms of the GNU General Public License as published by the Free
  8. # Software Foundation; either version 2 of the License, or (at your option)
  9. # any later version.
  10. #
  11. # Please read the COPYING file.
  12. #
  13. import popen2
  14. import os
  15. class pppoe:
  16. """ Functions to configure and run PPP over Ethernet connections """
  17. tmpl_pppoe_conf = """
  18. #***********************************************************************
  19. #
  20. # /etc/ppp/pppoe.conf
  21. #
  22. # Configuration file for rp-pppoe.
  23. #
  24. # NOTE: This file is used by the adsl-start, adsl-stop, adsl-connect and
  25. # adsl-status shell scripts. It is *not* used in any way by the
  26. # "pppoe" executable.
  27. #
  28. #***********************************************************************
  29. # When you configure a variable, DO NOT leave spaces around the "=" sign.
  30. # Ethernet card connected to ADSL modem
  31. ETH=\"%s\"
  32. # ADSL user name. You may have to supply "@provider.com" Sympatico
  33. # users in Canada do need to include "@sympatico.ca"
  34. # Sympatico uses PAP authentication. Make sure /etc/ppp/pap-secrets
  35. # contains the right username/password combination.
  36. # For Magma, use xxyyzz@magma.ca
  37. USER=\"%s\"
  38. # Bring link up on demand? Default is to leave link up all the time.
  39. # If you want the link to come up on demand, set DEMAND to a number indicating
  40. # the idle time after which the link is brought down.
  41. DEMAND=no
  42. #DEMAND=300
  43. # DNS type: SERVER=obtain from server; SPECIFY=use DNS1 and DNS2;
  44. # NOCHANGE=do not adjust.
  45. DNSTYPE=COMAR
  46. # Obtain DNS server addresses from the peer (recent versions of pppd only)
  47. # In old config files, this used to be called USEPEERDNS. Changed to
  48. # PEERDNS for better Red Hat compatibility
  49. PEERDNS=yes
  50. DNS1=
  51. DNS2=
  52. # Make the PPPoE connection your default route. Set to
  53. # DEFAULTROUTE=no if you don't want this.
  54. DEFAULTROUTE=yes
  55. ### ONLY TOUCH THE FOLLOWING SETTINGS IF YOU'RE AN EXPERT
  56. # How long adsl-start waits for a new PPP interface to appear before
  57. # concluding something went wrong. If you use 0, then adsl-start
  58. # exits immediately with a successful status and does not wait for the
  59. # link to come up. Time is in seconds.
  60. #
  61. # WARNING WARNING WARNING:
  62. #
  63. # If you are using rp-pppoe on a physically-inaccessible host, set
  64. # CONNECT_TIMEOUT to 0. This makes SURE that the machine keeps trying
  65. # to connect forever after adsl-start is called. Otherwise, it will
  66. # give out after CONNECT_TIMEOUT seconds and will not attempt to
  67. # connect again, making it impossible to reach.
  68. CONNECT_TIMEOUT=30
  69. # How often in seconds adsl-start polls to check if link is up
  70. CONNECT_POLL=2
  71. # Specific desired AC Name
  72. ACNAME=
  73. # Specific desired service name
  74. SERVICENAME=
  75. # Character to echo at each poll. Use PING="" if you don't want
  76. # anything echoed
  77. PING="."
  78. # File where the adsl-connect script writes its process-ID.
  79. # Three files are actually used:
  80. # $PIDFILE contains PID of adsl-connect script
  81. # $PIDFILE.pppoe contains PID of pppoe process
  82. # $PIDFILE.pppd contains PID of pppd process
  83. #
  84. # PIDFILE="/var/run/$CF_BASE-adsl.pid"
  85. PIDFILE="/var/run/adsl.pid"
  86. # Do you want to use synchronous PPP? "yes" or "no". "yes" is much
  87. # easier on CPU usage, but may not work for you. It is safer to use
  88. # "no", but you may want to experiment with "yes". "yes" is generally
  89. # safe on Linux machines with the n_hdlc line discipline; unsafe on others.
  90. SYNCHRONOUS=no
  91. # Do you want to clamp the MSS? Here's how to decide:
  92. # - If you have only a SINGLE computer connected to the ADSL modem, choose
  93. # "no".
  94. # - If you have a computer acting as a gateway for a LAN, choose "1412".
  95. # The setting of 1412 is safe for either setup, but uses slightly more
  96. # CPU power.
  97. CLAMPMSS=1412
  98. #CLAMPMSS=no
  99. # LCP echo interval and failure count.
  100. LCP_INTERVAL=20
  101. LCP_FAILURE=3
  102. # PPPOE_TIMEOUT should be about 4*LCP_INTERVAL
  103. PPPOE_TIMEOUT=80
  104. # Firewalling: One of NONE, STANDALONE or MASQUERADE
  105. FIREWALL=NONE
  106. # Linux kernel-mode plugin for pppd. If you want to try the kernel-mode
  107. # plugin, use LINUX_PLUGIN=rp-pppoe.so
  108. LINUX_PLUGIN=
  109. # Any extra arguments to pass to pppoe. Normally, use a blank string
  110. # like this:
  111. PPPOE_EXTRA=""
  112. # Rumour has it that "Citizen's Communications" with a 3Com
  113. # HomeConnect ADSL Modem DualLink requires these extra options:
  114. # PPPOE_EXTRA="-f 3c12:3c13 -S ISP"
  115. # Any extra arguments to pass to pppd. Normally, use a blank string
  116. # like this:
  117. PPPD_EXTRA=""
  118. ########## DON'T CHANGE BELOW UNLESS YOU KNOW WHAT YOU ARE DOING
  119. # If you wish to COMPLETELY overrride the pppd invocation:
  120. # Example:
  121. # OVERRIDE_PPPD_COMMAND="pppd call dsl"
  122. # If you want adsl-connect to exit when connection drops:
  123. # RETRY_ON_FAILURE=no
  124. """
  125. tmpl_options = """
  126. noipdefault
  127. hide-password
  128. defaultroute
  129. persist
  130. lock
  131. """
  132. def silentUnlink(self, path):
  133. """ Try to unlink a file, if exists """
  134. try:
  135. os.unlink(path)
  136. except:
  137. pass
  138. def capture(self, cmd):
  139. """ Run a command and capture the output """
  140. out = []
  141. a = popen2.Popen4(cmd)
  142. while 1:
  143. b = a.fromchild.readline()
  144. if b == None or b == "":
  145. break
  146. out.append(b)
  147. return (a.wait(), out)
  148. def getDNS(self):
  149. """ Try to get DNS server adress provided by remote peer """
  150. list = []
  151. try:
  152. f = file("/etc/ppp/resolv.conf", "r")
  153. for line in f.readlines():
  154. if line.strip().startswith("nameserver"):
  155. list.append(line[line.find("nameserver") + 10:].rstrip('\n').strip())
  156. f.close()
  157. except IOError:
  158. return None
  159. return list
  160. def createConf(self, dev, user):
  161. """ Create configuration file for pppoe connections """
  162. self.silentUnlink("/etc/ppp/pppoe.conf")
  163. try:
  164. f = open("/etc/ppp/pppoe.conf", "w")
  165. f.write(self.tmpl_pppoe_conf % (dev, user))
  166. f.close()
  167. except:
  168. return True
  169. return None
  170. def createOptions(self):
  171. """ Create options file for pppoe connections """
  172. self.silentUnlink("/etc/ppp/options-pppoe")
  173. try:
  174. f = open("/etc/ppp/options-pppoe", "w")
  175. f.write(self.tmpl_options)
  176. f.close()
  177. except:
  178. return True
  179. return None
  180. def createSecrets(self, user, pwd):
  181. """ Create authentication files """
  182. try:
  183. # Ugly way to clean up secrets and recreate
  184. self.silentUnlink("/etc/ppp/pap-secrets")
  185. self.silentUnlink("/etc/ppp/chap-secrets")
  186. f = os.open("/etc/ppp/pap-secrets", os.O_CREAT, 0600)
  187. os.close(f)
  188. os.symlink("/etc/ppp/pap-secrets", "/etc/ppp/chap-secrets")
  189. except:
  190. return True
  191. f = open("/etc/ppp/pap-secrets", "w")
  192. data = "\"%s\" * \"%s\"\n" % (user, pwd)
  193. f.write(data)
  194. f.close()
  195. return None
  196. def getStatus(self):
  197. """ Stop the pppoe connection """
  198. cmd = "/usr/sbin/adsl-status"
  199. i, output = self.capture(cmd)
  200. return output
  201. def stopPPPD(self):
  202. """ Stop the pppoe connection """
  203. cmd = "/usr/sbin/adsl-stop"
  204. i, output = self.capture(cmd)
  205. return output
  206. def startPPPD(self):
  207. """ Start the PPP daemon """
  208. cmd = "/usr/sbin/adsl-start"
  209. i, output = self.capture(cmd)
  210. return output
  211. def connect(self, dev, user, pwd):
  212. """ Try to start a pppoe connection through dev and login """
  213. if self.createConf(dev, user) is True:
  214. return "Could not manage pppoe configuration"
  215. if self.createOptions() is True:
  216. return "Could not manage pppd parameters"
  217. if self.createSecrets(user, pwd) is True:
  218. return "Could not manage authentication files"
  219. output = self.startPPPD()
  220. return output
  221. if __name__ == "__main__":
  222. rp = pppoe()
  223. rp.connect("eth0", "parbusman@uludag", "pek gizli")