Skip to content
This repository has been archived by the owner on Aug 21, 2020. It is now read-only.

Commit

Permalink
Merge 20140814-security branch
Browse files Browse the repository at this point in the history
  • Loading branch information
simoncadman committed Dec 13, 2014
2 parents 69c6142 + 876381e commit 12be712
Show file tree
Hide file tree
Showing 44 changed files with 908 additions and 834 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,4 +1,5 @@
*.pyc
*.pyo
cloudprint.ppd
test-capabilities.serial
.coverage
Expand All @@ -8,3 +9,4 @@ config.log
config.status
.DS_Store
.*swp
autom4te.cache
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -6,7 +6,7 @@ python:
before_install:
- sudo apt-get update -qq
- sudo apt-get install -qq debhelper autotools-dev cups-ppdc libcups2-dev cups imagemagick ghostscript
- pip install httplib2 pycups pytest-cov python-coveralls pyOpenSSL --use-mirrors
- pip install httplib2 pycups==1.9.68 pytest-cov python-coveralls pyOpenSSL six
install:
- ./configure
- sudo gpasswd -a `whoami` lpadmin
Expand All @@ -25,7 +25,7 @@ script:
- cd $TRAVIS_BUILD_DIR
- ./upgrade.py
- sudo python -m compileall .
- py.test -rxs --cov . -v --cov-report term-missing
- py.test -rxs --cov . --doctest-modules -v --cov-report term-missing --ignore=oauth2client
- ls -al /tmp/
- sudo cat /var/log/cups/cloudprint_log /var/log/cups/error_log
- sudo unlink /usr/share/cups/model/testppd.ppd
Expand Down
28 changes: 23 additions & 5 deletions auth.py
Expand Up @@ -17,6 +17,17 @@
import json
import os
import sys
# workaround for ubuntu 12.04 / older python-six version
try:
from six.moves import urllib
except ImportError:
import six
import urllib
import urlparse
six.moves.urllib = urllib
six.moves.urllib.parse = urlparse
six.moves.urllib.parse.urlencode = urllib.urlencode

from oauth2client import client
from oauth2client import multistore_file
from cloudprintrequestor import CloudPrintRequestor
Expand Down Expand Up @@ -91,6 +102,13 @@ def AddAccount(storage, userid=None, permissions=None):
userid = raw_input(
"Name for this user account ( eg something@gmail.com )? ")

# setup storage again if just got userid now
storage = multistore_file.get_credential_storage(
Auth.config,
Auth.clientid,
userid,
permissions)

while True:
flow, auth_uri = Auth.AddAccountStep1(userid, permissions)
message = "Open this URL, grant access to CUPS Cloud Print, "
Expand Down Expand Up @@ -123,11 +141,11 @@ def AddAccountStep1(userid, permissions=None):
if permissions is None:
permissions = Auth.normal_permissions
flow = client.OAuth2WebServerFlow(
Auth.clientid,
Auth.clientsecret,
permissions,
'urn:ietf:wg:oauth:2.0:oob',
userid)
client_id=Auth.clientid,
client_secret=Auth.clientsecret,
scope=permissions,
user_agent=userid,
redirect_uri='urn:ietf:wg:oauth:2.0:oob')
auth_uri = flow.step1_get_authorize_url()
return flow, auth_uri

Expand Down
79 changes: 37 additions & 42 deletions backend.py
Expand Up @@ -42,7 +42,7 @@
Utils.SetupLogging()

# line below is replaced on commit
CCPVersion = "20140713 215426"
CCPVersion = "20140814.2 000000"
Utils.ShowVersion(CCPVersion)

if len(sys.argv) != 1 and len(sys.argv) < 6 or len(sys.argv) > 7:
Expand All @@ -51,15 +51,19 @@
sys.exit(0)

if len(sys.argv) >= 4 and sys.argv[3] == "Set Default Options":
print "ERROR: Unimplemented command: " + sys.argv[3]
logging.error("Unimplemented command: %s", sys.argv[3])
sys.stderr.write("ERROR: Unimplemented command: " + sys.argv[3] + "\n")
logging.error("Unimplemented command: " + sys.argv[3])
sys.exit(0)

printFile = None

if len(sys.argv) == 7:
prog, jobID, userName, jobTitle, copies, printOptions, printFile = sys.argv[0:7]
sys.stderr.write("ERROR: Sorry, CUPS Cloud Print no longer supports printing\
files directly for security reasons\n")
sys.exit(1)
if len(sys.argv) == 6:
prog, jobID, userName, jobTitle, copies, printOptions = sys.argv[0:6]
printFile = None
prog, jobID, userName, jobTitle, copies, printOptions = sys.argv
printFile = jobTitle

requestors, storage = Auth.SetupAuth(False)
if not requestors:
Expand All @@ -82,14 +86,13 @@
sys.exit(1)
sys.exit(0)

# if no printfile, put stdin to a temp file
if printFile is None:
logging.info("Reading file from stdin")
printFile = Utils.StdInToTempFile(jobID, userName)
filedata = ""
for line in sys.stdin:
filedata += line

# Backends should only produce multiple copies if a file name is
# supplied (see CUPS Software Programmers Manual)
copies = 1
# Backends should only produce multiple copies if a file name is
# supplied (see CUPS Software Programmers Manual)
copies = 1

uri = os.getenv('DEVICE_URI')
cupsprintername = os.getenv('PRINTER')
Expand All @@ -98,59 +101,51 @@
sys.stdout.write(message)
sys.exit(255)

logging.info("Printing file %s", printFile)
logging.info("Printing file %s" , str(printFile))
optionsstring = ' '.join(["'%s'" % option for option in sys.argv])
logging.info("Device is %s , printername is %s, params are: %s",
uri, cupsprintername, optionsstring)

pdfFile = printFile + ".pdf"
# setup
convertToPDFParams = ["ps2pdf", "-dPDFSETTINGS=/printer",
"-dUseCIEColor", "-", "-"]
if Utils.which("ps2pdf") is None:
convertToPDFParams = ["pstopdf", printFile, pdfFile]
else:
convertToPDFParams = ["ps2pdf", "-dPDFSETTINGS=/printer",
"-dUseCIEColor", printFile, pdfFile]
convertToPDFParams = ["pstopdf", "-", "-"]

logging.debug('is this a pdf? ' + str(printFile))
result = 0

logging.debug('is this a pdf? %s', printFile)
if not os.path.exists(printFile):
sys.stderr.write('ERROR: file "%s" not found\n', printFile)
result = 1
elif not Utils.fileIsPDF(printFile):
if not Utils.fileIsPDF(filedata):
# read file as pdf
sys.stderr.write("INFO: Converting print job to PDF\n")
if subprocess.call(convertToPDFParams) != 0:
p = subprocess.Popen(convertToPDFParams, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
filedata = p.communicate(filedata)[0]
if p.returncode != 0:
sys.stderr.write("ERROR: Failed to convert file to pdf\n")
result = 1
else:
logging.info("Converted to PDF as %s", pdfFile)
logging.info("Converted to PDF - %s bytes" % str(len(filedata)))
else:
pdfFile = printFile + '.pdf'
os.rename(printFile, pdfFile)
logging.info("Using %s as is already PDF", pdfFile)
# read file normally
logging.info("Using %s as is already PDF - %s bytes" % (printFile, len(filedata)))

# send pdf data to GCP
if result == 0:
sys.stderr.write("INFO: Sending document to Cloud Print\n")
logging.info("Sending %s to cloud", pdfFile)
logging.info("Sending %s to cloud" % printFile)

printer = printer_manager.getPrinterByURI(uri)
if printer is None:
print "ERROR: PrinterManager '%s' not found" % uri
sys.stderr.write("ERROR: PrinterManager '%s' not found\n" % uri)
result = 1
elif printer.submitJob('pdf', pdfFile, jobTitle, cupsprintername, printOptions):
print "INFO: Successfully printed"
elif printer.submitJob('pdf', printFile, filedata, jobTitle, cupsprintername, printOptions):
sys.stderr.write("INFO: Successfully printed\n")
result = 0
else:
print "ERROR: Failed to submit job to cloud print"
sys.stderr.write("ERROR: Failed to submit job to cloud print\n")
result = 1
logging.info(str(printFile) + " sent to cloud print")

logging.info("%s sent to cloud print, deleting", pdfFile)
if os.path.exists(printFile):
os.unlink(printFile)
sys.stderr.write("INFO: Cleaning up temporary files\n")
logging.info("Deleted %s", printFile)
if os.path.exists(pdfFile):
os.unlink(pdfFile)
logging.info("Deleted %s", pdfFile)
if result != 0:
sys.stderr.write("INFO: Printing Failed\n")
logging.info("Failed printing")
Expand Down
37 changes: 16 additions & 21 deletions ccputils.py
Expand Up @@ -19,7 +19,6 @@
import logging
import sys
import grp
import mimetypes
import base64
import fcntl
import termios
Expand All @@ -36,6 +35,10 @@ class Utils(object):
PROTOCOL = PROTOCOL_NAME + '://'
OLD_PROTOCOL_NAME = 'cloudprint'
OLD_PROTOCOL = OLD_PROTOCOL_NAME + '://'
_MIMETYPES_JOBTYPES = {'pdf': 'application/pdf',
'other': 'application/octet-stream',
'jpg': 'image/jpeg',
'png': 'image/png'}

@staticmethod
def FixFilePermissions(filename):
Expand Down Expand Up @@ -91,16 +94,16 @@ def SetupLogging(logpath=None):
return returnValue

@staticmethod
def fileIsPDF(filename):
def fileIsPDF(filedata):
"""Check if a file is or isnt a PDF
Args:
filename: string, name of the file to check
Returns:
boolean: True = is a PDF, False = not a PDF.
"""
p = subprocess.Popen(["file", filename.lstrip('-')], stdout=subprocess.PIPE)
output = p.communicate()[0]
p = subprocess.Popen(["file", '-'], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
output = p.communicate(filedata)[0]
logging.debug("File output was: " + output)
return "PDF document" in output

Expand Down Expand Up @@ -207,31 +210,23 @@ def WriteFile(file_name, data):
return status

@staticmethod
def Base64Encode(pathname):
def Base64Encode(data, jobtype):
"""Convert a file to a base64 encoded file.
Args:
pathname: path name of file to base64 encode..
pathname: data to base64 encode
jobtype: job type being encoded - pdf, jpg etc
Returns:
string, name of base64 encoded file.
string, base64 encoded string.
For more info on data urls, see:
http://en.wikipedia.org/wiki/Data_URI_scheme
"""
b64_pathname = pathname + '.b64'
file_type = mimetypes.guess_type(
pathname)[0] or 'application/octet-stream'
data = Utils.ReadFile(pathname)
if data is None:
return None

# Convert binary data to base64 encoded data.
header = 'data:%s;base64,' % file_type
b64data = header + base64.b64encode(data)

if Utils.WriteFile(b64_pathname, b64data):
return b64_pathname
else:
return None
mimetype = Utils._MIMETYPES_JOBTYPES['other']
if jobtype in Utils._MIMETYPES_JOBTYPES:
mimetype = Utils._MIMETYPES_JOBTYPES[jobtype]
header = 'data:%s;base64,' % mimetype
return header + base64.b64encode(data)

@staticmethod
def GetLanguage(locale):
Expand Down
2 changes: 1 addition & 1 deletion deleteaccount.py
Expand Up @@ -32,7 +32,7 @@
Utils.SetupLogging()

# line below is replaced on commit
CCPVersion = "20140501 203545"
CCPVersion = "20140814.2 000000"
Utils.ShowVersion(CCPVersion)

while True:
Expand Down
2 changes: 1 addition & 1 deletion dynamicppd.py
Expand Up @@ -87,7 +87,7 @@ def showUsage():
Utils.SetupLogging()

# line below is replaced on commit
CCPVersion = "20140607 101641"
CCPVersion = "20140814.2 000000"
Utils.ShowVersion(CCPVersion)

requestors, storage = Auth.SetupAuth(False)
Expand Down
2 changes: 1 addition & 1 deletion listcloudprinters.py
Expand Up @@ -33,7 +33,7 @@
Utils.SetupLogging()

# line below is replaced on commit
CCPVersion = "20140501 203545"
CCPVersion = "20140814.2 000000"
Utils.ShowVersion(CCPVersion)

requestors, storage = Auth.SetupAuth(True)
Expand Down
5 changes: 4 additions & 1 deletion oauth2client/__init__.py
@@ -1,5 +1,8 @@
__version__ = "1.2"
"""Client library for using OAuth2, especially with Google APIs."""

__version__ = '1.4.2'

GOOGLE_AUTH_URI = 'https://accounts.google.com/o/oauth2/auth'
GOOGLE_DEVICE_URI = 'https://accounts.google.com/o/oauth2/device/code'
GOOGLE_REVOKE_URI = 'https://accounts.google.com/o/oauth2/revoke'
GOOGLE_TOKEN_URI = 'https://accounts.google.com/o/oauth2/token'
32 changes: 0 additions & 32 deletions oauth2client/anyjson.py

This file was deleted.

0 comments on commit 12be712

Please sign in to comment.