|
| 1 | +import base64 |
| 2 | +import os.path |
| 3 | +import subprocess |
| 4 | +import uuid |
| 5 | +from email.message import EmailMessage |
| 6 | +from email.utils import make_msgid |
| 7 | +from io import BytesIO |
| 8 | + |
| 9 | +import qrcode |
| 10 | +from django import forms |
| 11 | +from django.forms import widgets |
| 12 | +from django.http import HttpRequest |
| 13 | +from django.http import HttpResponse |
| 14 | +from django.shortcuts import render |
| 15 | +from ocflib.misc.mail import MAIL_FROM |
| 16 | +from ocflib.misc.mail import SENDMAIL_PATH |
| 17 | +from paramiko import AuthenticationException |
| 18 | +from paramiko import SSHClient |
| 19 | +from paramiko.hostkeys import HostKeyEntry |
| 20 | + |
| 21 | +from ocfweb.component.forms import Form |
| 22 | + |
| 23 | + |
| 24 | +PRINT_FOLDER = '.user_print' |
| 25 | + |
| 26 | +EMAIL_BODY = ''' |
| 27 | +<html> |
| 28 | + <body> |
| 29 | + <p> |
| 30 | + Hi {username}, |
| 31 | + </p> |
| 32 | +
|
| 33 | + <p> |
| 34 | + Below you can find the QR code to start your print in the OCF computer lab. |
| 35 | + Show the QR code in front of the camera at the remote printing computer, |
| 36 | + and it will start printing your document. |
| 37 | + </p> |
| 38 | + <img src="cid:{image_cid}"> |
| 39 | + <p> |
| 40 | + Thanks for flying OCF! <br> |
| 41 | + ~ OCF volunteer staff |
| 42 | + </p> |
| 43 | + </body> |
| 44 | +</html> |
| 45 | +''' |
| 46 | + |
| 47 | + |
| 48 | +def send_qr_mail(username: str, qr_code: bytes) -> None: |
| 49 | + """Send the QR code needed to start the print in the lab to the user. |
| 50 | +
|
| 51 | + Based on https://stackoverflow.com/a/49098251/9688107 |
| 52 | + """ |
| 53 | + |
| 54 | + msg = EmailMessage() |
| 55 | + msg['Subject'] = 'OCF Remote Printing QR Code' |
| 56 | + msg['From'] = MAIL_FROM |
| 57 | + msg['To'] = f'{username}@ocf.berkeley.edu' |
| 58 | + |
| 59 | + msg.set_content( |
| 60 | + 'Sorry, we were unable to display your QR, please use the QR code on the website!.', |
| 61 | + ) |
| 62 | + |
| 63 | + image_cid = make_msgid(domain='ocf.berkeley.edu') |
| 64 | + msg.add_alternative(EMAIL_BODY.format(username=username, image_cid=image_cid), subtype='html') |
| 65 | + msg.get_payload()[1].add_related( |
| 66 | + qr_code, |
| 67 | + maintype='image', |
| 68 | + subtype='png', |
| 69 | + cid=image_cid, |
| 70 | + ) |
| 71 | + # we send the message via sendmail because direct traffic to port 25 |
| 72 | + # is firewalled off |
| 73 | + p = subprocess.Popen( |
| 74 | + (SENDMAIL_PATH, '-t', '-oi'), |
| 75 | + stdin=subprocess.PIPE, |
| 76 | + ) |
| 77 | + p.communicate(msg.as_string().encode('utf8')) |
| 78 | + |
| 79 | + |
| 80 | +def print(request: HttpRequest) -> HttpResponse: |
| 81 | + if request.method == 'POST': |
| 82 | + form = PrintForm( |
| 83 | + request.POST, |
| 84 | + request.FILES, |
| 85 | + initial={ |
| 86 | + 'double_or_single': 'single', |
| 87 | + }, |
| 88 | + ) |
| 89 | + qr_b64 = b'' |
| 90 | + double_or_single = 'single' |
| 91 | + error = '' |
| 92 | + if form.is_valid(): |
| 93 | + username = form.cleaned_data['username'] |
| 94 | + password = form.cleaned_data['password'] |
| 95 | + |
| 96 | + file = request.FILES['file'] |
| 97 | + |
| 98 | + double_or_single = form.cleaned_data['double_or_single'] |
| 99 | + |
| 100 | + ssh = SSHClient() |
| 101 | + |
| 102 | + host_keys = ssh.get_host_keys() |
| 103 | + entry = HostKeyEntry.from_line( |
| 104 | + 'ssh.ocf.berkeley.edu ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAqMkHVVoMl8md25iky7e2Xe3ARaC4H1PbIpv5Y+xT4KOT17gGvFSmfjGyW9P8ZTyqxq560iWdyELIn7efaGPbkUo9retcnT6WLmuh9nRIYwb6w7BGEEvlblBmH27Fkgt7JQ6+1sr5teuABfIMg22WTQAeDQe1jg0XsPu36OjbC7HjA3BXsiNBpxKDolYIXWzOD+r9FxZLP0lawh8dl//O5FW4ha1IbHklq2i9Mgl79wAH3jxf66kQJTvLmalKnQ0Dbp2+vYGGhIjVFXlGSzKsHAVhuVD6TBXZbxWOYoXanS7CC43MrEtBYYnc6zMn/k/rH0V+WeRhuzTnr/OZGJbBBw==', # noqa |
| 105 | + ) |
| 106 | + assert entry is not None # should never be none as we are passing a static string above |
| 107 | + host_keys.add( |
| 108 | + 'ssh.ocf.berkeley.edu', |
| 109 | + 'ssh-rsa', |
| 110 | + entry.key, |
| 111 | + ) |
| 112 | + |
| 113 | + try: |
| 114 | + ssh.connect( |
| 115 | + 'ssh.ocf.berkeley.edu', |
| 116 | + username=username, |
| 117 | + password=password, |
| 118 | + ) |
| 119 | + except AuthenticationException: |
| 120 | + error = 'Authentication failed. Did you type the wrong username or password?' |
| 121 | + |
| 122 | + if not error: |
| 123 | + sftp = ssh.open_sftp() |
| 124 | + try: |
| 125 | + folder = sftp.stat(PRINT_FOLDER) |
| 126 | + if folder.FLAG_PERMISSIONS != 0o700: |
| 127 | + sftp.chmod(PRINT_FOLDER, 0o700) |
| 128 | + except FileNotFoundError: |
| 129 | + sftp.mkdir(PRINT_FOLDER, 0o700) |
| 130 | + try: |
| 131 | + rid = uuid.uuid4().hex |
| 132 | + filename = f'{rid}-{file.name}' |
| 133 | + with sftp.open(os.path.join(PRINT_FOLDER, filename), 'wb+') as dest: |
| 134 | + for chunk in file.chunks(): |
| 135 | + dest.write(chunk) |
| 136 | + except OSError as e: |
| 137 | + error = 'Failed to open file in user home directory, ' + \ |
| 138 | + f'please report this to [email protected] with the traceback\n{e}' |
| 139 | + else: |
| 140 | + qr = qrcode.QRCode( |
| 141 | + version=1, |
| 142 | + box_size=10, |
| 143 | + border=5, |
| 144 | + ) |
| 145 | + qr.add_data(f'{username}:{filename}:{double_or_single}') |
| 146 | + qr.make(fit=True) |
| 147 | + img = qr.make_image(fill='black', back_color='white') |
| 148 | + buff = BytesIO() |
| 149 | + img.save(buff, format='PNG') |
| 150 | + qr_data = buff.getvalue() |
| 151 | + qr_b64 = b'data:image/png;base64,%b' % base64.b64encode(qr_data) |
| 152 | + send_qr_mail( |
| 153 | + username=username, |
| 154 | + qr_code=qr_data, |
| 155 | + ) |
| 156 | + return render( |
| 157 | + request, |
| 158 | + 'account/print/qr.html', |
| 159 | + { |
| 160 | + 'title': 'QR Code', |
| 161 | + 'qr_b64': qr_b64.decode('utf-8'), |
| 162 | + 'error': error, |
| 163 | + }, |
| 164 | + ) |
| 165 | + else: |
| 166 | + form = PrintForm( |
| 167 | + initial={ |
| 168 | + 'double_or_single': 'single', |
| 169 | + }, |
| 170 | + ) |
| 171 | + |
| 172 | + return render( |
| 173 | + request, |
| 174 | + 'account/print/index.html', { |
| 175 | + 'title': 'Print remotely', |
| 176 | + 'form': form, |
| 177 | + }, |
| 178 | + ) |
| 179 | + |
| 180 | + |
| 181 | +class PrintForm(Form): |
| 182 | + username = forms.CharField( |
| 183 | + label='OCF username', |
| 184 | + min_length=3, |
| 185 | + max_length=16, |
| 186 | + ) |
| 187 | + password = forms.CharField( |
| 188 | + widget=forms.PasswordInput, |
| 189 | + label='Password', |
| 190 | + min_length=8, |
| 191 | + max_length=256, |
| 192 | + ) |
| 193 | + |
| 194 | + file = forms.FileField() |
| 195 | + |
| 196 | + PRINT_CHOICES = ( |
| 197 | + ( |
| 198 | + 'single', |
| 199 | + 'single sided -- one page per piece of paper', |
| 200 | + ), |
| 201 | + ( |
| 202 | + 'double', |
| 203 | + 'double sided -- two pages per piece of paper', |
| 204 | + ), |
| 205 | + ) |
| 206 | + |
| 207 | + double_or_single = forms.ChoiceField( |
| 208 | + choices=PRINT_CHOICES, |
| 209 | + label='Print double or single sided', |
| 210 | + widget=widgets.RadioSelect, |
| 211 | + ) |
0 commit comments