Advertisement
Guest User

Untitled

a guest
Nov 29th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import os
  4. import mimetypes
  5. import getpass
  6. import smtplib
  7. import subprocess
  8. import argparse
  9. from smtplib import SMTPAuthenticationError
  10. from email.mime.multipart import MIMEMultipart
  11. from email import encoders
  12. from email.message import Message
  13. from email.mime.audio import MIMEAudio
  14. from email.mime.base import MIMEBase
  15. from email.mime.image import MIMEImage
  16. from email.mime.text import MIMEText
  17.  
  18. # twp-client.py
  19. #
  20. # author: Kendrick Cline
  21. # kdecline@gmail.com
  22. # 01/26/2016
  23. #
  24. # This script serves to be a hands free client
  25. # program which creates a default RSA Key for
  26. # students if they don't exist, then e-mails their
  27. # public key to the appropriate server for registration
  28.  
  29. #####
  30. # Argument Parser
  31. #####
  32. parser = argparse.ArgumentParser(description = 'Client program for the Trenary Workflow Project to be used by students to initialize their \
  33. machines')
  34. parser.add_argument('-v', '--verbose', action='store_true', help='enable more logging')
  35. parser.add_argument('--dest-email', action='store', help='override default destination email')
  36. args = vars(parser.parse_args())
  37.  
  38. #####
  39. # Variables
  40. #####
  41. MAX_PASSWORD_ATTEMPTS = 3
  42. class_emails = {
  43. 'a' : 'trenarycs223@gmail.com',
  44. 'b' : 'trenarycs224@gmail.com',
  45. 'c' : 'carrcs595@gmail.com'
  46. }
  47. rsa_key_path = os.path.expanduser('~/.ssh/id_rsa')
  48. rsa_pub_key_path = os.path.expanduser('~/.ssh/id_rsa.pub')
  49. ssh_keygen_cmd = "ssh-keygen -b 2048 -t rsa -f {0} -q -N \"\"".format(rsa_key_path)
  50.  
  51. #####
  52. # Functions
  53. #####
  54. def debug(msg):
  55. if args['verbose']:
  56. print '[DEBUG]: {0}'.format(msg)
  57.  
  58. #####
  59. # Begin Script
  60. #####
  61. emailto = args['dest_email']
  62. if emailto is None:
  63. class_arg = raw_input('2230(a), 2240(b) or 5950(c)?: ')
  64. emailto = class_emails.get(class_arg, None)
  65. if emailto is None:
  66. print 'Invalid option, please enter a, b or c'
  67. exit(1)
  68. debug('set target email to {0}'.format(emailto))
  69.  
  70. emailfrom = raw_input('Enter your wmich email: ')
  71. username = raw_input('Enter your Bronco NET ID: ')
  72.  
  73. #####
  74. # Validate username and password
  75. #
  76. # Invalidate the smtp session after we validate the user session
  77. # leaving a connection open during work would be a bad idea
  78. #####
  79. ct = 0
  80. while True:
  81. if ct == MAX_PASSWORD_ATTEMPTS:
  82. print 'Authentication failed'
  83. server.quit()
  84. exit(1)
  85.  
  86. password = getpass.getpass('Enter your wmich password: ')
  87. server = smtplib.SMTP("smtp.wmich.edu:587")
  88. server.starttls()
  89.  
  90. try:
  91. server.login(username,password)
  92. except SMTPAuthenticationError:
  93. print 'Incorrect username/password combination, please try again.\
  94. Attempt {0} of {1}.'.format(ct+1, MAX_PASSWORD_ATTEMPTS)
  95. ct = ct + 1
  96. server.quit()
  97. continue
  98. server.quit()
  99. break
  100.  
  101. debug('successfully validated credentials for user {0}'.format(username))
  102. print 'Working...'
  103.  
  104. #####
  105. # If there is no default RSA Key, generate one for them
  106. #####
  107. if not os.path.isfile(rsa_key_path) and \
  108. not os.path.isfile(rsa_pub_key_path):
  109. debug('default RSA Key does not exist, creating one at {0}'.format(rsa_key_path))
  110. debug('running command {0}'.format(ssh_keygen_cmd))
  111. p = subprocess.Popen(ssh_keygen_cmd, shell=True)
  112. p.wait()
  113.  
  114. #####
  115. # Create the message mime-type and attach the RSA Key to it
  116. #####
  117. msg = MIMEMultipart()
  118. msg['From'] = emailfrom
  119. msg['To'] = emailto
  120. msg['Subject'] = '[KEYS]'
  121.  
  122. debug('opening and checking file type of {0}'.format(rsa_pub_key_path))
  123. ctype, encoding = mimetypes.guess_type(rsa_pub_key_path)
  124. if ctype is None or encoding is not None:
  125. ctype = "application/octet-stream"
  126. maintype, subtype = ctype.split("/", 1)
  127.  
  128. # check for pub file type
  129. fp = open(rsa_pub_key_path)
  130. # Note: we should handle calculating the charset
  131. attachment = MIMEText(fp.read(), _subtype=subtype)
  132. fp.close()
  133.  
  134. attachment.add_header('Content-Disposition', 'attachment', filename='id_rsa.pub')
  135. msg.attach(attachment)
  136.  
  137. #####
  138. # Create smtp session and send the email
  139. #####
  140. debug('creating smtp TLS connection to smtp.wmich.edu')
  141. server = smtplib.SMTP("smtp.wmich.edu:587")
  142. server.starttls()
  143. debug('logging in with student credentials')
  144. server.login(username,password)
  145. debug('sending mail')
  146. server.sendmail(emailfrom, emailto, msg.as_string())
  147. server.quit()
  148. debug('successfully sent mail')
  149.  
  150. print 'Success!'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement