f37b2cedd3cdbf0f1c1e130ef9231d1beec58e4e
[bootmanager.git] / source / BootServerRequest.py
1 #!/usr/bin/python2
2
3 # Copyright (c) 2003 Intel Corporation
4 # All rights reserved.
5
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
8 # met:
9
10 #     * Redistributions of source code must retain the above copyright
11 #       notice, this list of conditions and the following disclaimer.
12
13 #     * Redistributions in binary form must reproduce the above
14 #       copyright notice, this list of conditions and the following
15 #       disclaimer in the documentation and/or other materials provided
16 #       with the distribution.
17
18 #     * Neither the name of the Intel Corporation nor the names of its
19 #       contributors may be used to endorse or promote products derived
20 #       from this software without specific prior written permission.
21
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
26 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34 # EXPORT LAWS: THIS LICENSE ADDS NO RESTRICTIONS TO THE EXPORT LAWS OF
35 # YOUR JURISDICTION. It is licensee's responsibility to comply with any
36 # export regulations applicable in licensee's jurisdiction. Under
37 # CURRENT (May 2000) U.S. export regulations this software is eligible
38 # for export from the U.S. and can be downloaded by or otherwise
39 # exported or reexported worldwide EXCEPT to U.S. embargoed destinations
40 # which include Cuba, Iraq, Libya, North Korea, Iran, Syria, Sudan,
41 # Afghanistan and any other country to which the U.S. has embargoed
42 # goods and services.
43
44
45 import os, sys
46 import re
47 import string
48 import urllib
49
50 # try to load pycurl
51 try:
52     import pycurl
53     PYCURL_LOADED= 1
54 except:
55     PYCURL_LOADED= 0
56
57
58 # if there is no cStringIO, fall back to the original
59 try:
60     from cStringIO import StringIO
61 except:
62     from StringIO import StringIO
63
64
65
66 class BootServerRequest:
67
68     VERBOSE = 0
69
70     # all possible places to check the cdrom mount point.
71     # /mnt/cdrom is typically after the machine has come up,
72     # and /usr is when the boot cd is running
73     CDROM_MOUNT_PATH = ("/mnt/cdrom/","/usr/")
74
75     # this is the server to contact if we don't have a bootcd
76     DEFAULT_BOOT_SERVER = "boot.planet-lab.org"
77
78     BOOTCD_VERSION_FILE = "bootme/ID"
79     BOOTCD_SERVER_FILE = "bootme/BOOTSERVER"
80     BOOTCD_SERVER_CERT_DIR = "bootme/cacert"
81     CACERT_NAME = "cacert.pem"
82     
83     # location of file containing http/https proxy info, if needed
84     PROXY_FILE = '/etc/planetlab/http_proxy'
85
86     # location of curl executable, if pycurl isn't available
87     # and the DownloadFile method is called (backup, only
88     # really need for the boot cd environment where pycurl
89     # doesn't exist
90     CURL_CMD = 'curl'
91
92     # in seconds, how maximum time allowed for connect
93     DEFAULT_CURL_CONNECT_TIMEOUT = 30
94
95     # in seconds, maximum time allowed for any transfer
96     DEFAULT_CURL_MAX_TRANSFER_TIME = 3600
97
98     CURL_SSL_VERSION = 3
99
100     HTTP_SUCCESS = 200
101
102     # proxy variables
103     USE_PROXY = 0
104     PROXY = 0
105
106     # bootcd variables
107     HAS_BOOTCD = 0
108     BOOTCD_VERSION = ""
109     BOOTSERVER_CERTS= {}
110
111     def __init__(self, verbose=0):
112
113         self.VERBOSE= verbose
114             
115         # see if we have a boot cd mounted by checking for the version file
116         # if HAS_BOOTCD == 0 then either the machine doesn't have
117         # a boot cd, or something else is mounted
118         self.HAS_BOOTCD = 0
119
120         for path in self.CDROM_MOUNT_PATH:
121             self.Message( "Checking existance of boot cd on %s" % path )
122
123             os.system("/bin/mount %s > /dev/null 2>&1" % path )
124                 
125             version_file= path + self.BOOTCD_VERSION_FILE
126             self.Message( "Looking for version file %s" % version_file )
127
128             if os.access(version_file, os.R_OK) == 0:
129                 self.Message( "No boot cd found." );
130             else:
131                 self.Message( "Found boot cd." )
132                 self.HAS_BOOTCD=1
133                 break
134
135
136         if self.HAS_BOOTCD:
137
138             # check the version of the boot cd, and locate the certs
139             self.Message( "Getting boot cd version." )
140         
141             versionRegExp= re.compile(r"PlanetLab BootCD v(\S+)")
142                 
143             bootcd_version_f= file(version_file,"r")
144             line= string.strip(bootcd_version_f.readline())
145             bootcd_version_f.close()
146             
147             match= versionRegExp.findall(line)
148             if match:
149                 (self.BOOTCD_VERSION)= match[0]
150             
151             # right now, all the versions of the bootcd are supported,
152             # so no need to check it
153             
154             # create a list of the servers we should
155             # attempt to contact, and the certs for each
156             server_list= path + self.BOOTCD_SERVER_FILE
157             self.Message( "Getting list of servers off of cd from %s." %
158                           server_list )
159             
160             bootservers_f= file(server_list,"r")
161             bootservers= bootservers_f.readlines()
162             bootservers_f.close()
163             
164             for bootserver in bootservers:
165                 bootserver = string.strip(bootserver)
166                 cacert_path= "%s/%s/%s/%s" % \
167                              (path,self.BOOTCD_SERVER_CERT_DIR,
168                               bootserver,self.CACERT_NAME)
169                 if os.access(cacert_path, os.R_OK):
170                     self.BOOTSERVER_CERTS[bootserver]= cacert_path
171
172             self.Message( "Set of servers to contact: %s" %
173                           str(self.BOOTSERVER_CERTS) )
174         else:
175             self.Message( "Using default boot server address." )
176             self.BOOTSERVER_CERTS[self.DEFAULT_BOOT_SERVER]= ""
177
178
179     def CheckProxy( self ):
180         # see if we have any proxy info from the machine
181         self.USE_PROXY= 0
182         self.Message( "Checking existance of proxy config file..." )
183         
184         if os.access(self.PROXY_FILE, os.R_OK) and \
185                os.path.isfile(self.PROXY_FILE):
186             self.PROXY= string.strip(file(self.PROXY_FILE,'r').readline())
187             self.USE_PROXY= 1
188             self.Message( "Using proxy %s." % self.PROXY )
189         else:
190             self.Message( "Not using any proxy." )
191
192
193
194     def Message( self, Msg ):
195         if( self.VERBOSE ):
196             print( Msg )
197
198
199
200     def Error( self, Msg ):
201         sys.stderr.write( Msg + "\n" )
202
203
204
205     def Warning( self, Msg ):
206         self.Error(Msg)
207
208
209
210     def MakeRequest( self, PartialPath, GetVars,
211                      PostVars, DoSSL, DoCertCheck,
212                      ConnectTimeout= DEFAULT_CURL_CONNECT_TIMEOUT,
213                      MaxTransferTime= DEFAULT_CURL_MAX_TRANSFER_TIME):
214
215         if PYCURL_LOADED == 0:
216             self.Error( "MakeRequest method requires pycurl." )
217             return None
218
219         self.CheckProxy()
220         
221         self.Message( "Attempting to retrieve %s" % PartialPath )
222         
223         # we can't do ssl and check the cert if we don't have a bootcd
224         if DoSSL and DoCertCheck and not self.HAS_BOOTCD:
225             self.Error( "No boot cd exists (needed to use -c and -s.\n" )
226             return None
227
228         # didn't pass a path in? just get the root doc
229         if PartialPath == "":
230             PartialPath= "/"
231
232         # ConnectTimeout has to be greater than 0
233         if ConnectTimeout <= 0:
234             self.Error( "Connect timeout must be greater than zero.\n" )
235             return None
236
237         # setup the post and get vars for the request
238         if PostVars:
239             dopostdata= 1
240             postdata = urllib.urlencode(PostVars)
241             self.Message( "Posting data:\n%s\n" % postdata )
242         else:
243             dopostdata= 0
244
245         getstr= ""
246         if GetVars:
247             getstr= "?" + urllib.urlencode(GetVars)
248             self.Message( "Get data:\n%s\n" % getstr )
249
250         # now, attempt to make the request, starting at the first
251         # server in the list
252         for server in self.BOOTSERVER_CERTS:
253             self.Message( "Contacting server %s." % server )
254             
255             certpath = self.BOOTSERVER_CERTS[server]
256             
257             curl= pycurl.Curl()
258
259             # don't want curl sending any signals
260             curl.setopt(pycurl.NOSIGNAL, 1)
261
262             curl.setopt(pycurl.CONNECTTIMEOUT, ConnectTimeout)
263             self.Message( "Connect timeout is %s seconds" % \
264                           ConnectTimeout )
265
266             curl.setopt(pycurl.TIMEOUT, MaxTransferTime)
267             self.Message( "Max transfer time is %s seconds" % \
268                           MaxTransferTime )
269             
270             curl.setopt(pycurl.FOLLOWLOCATION, 1)
271             curl.setopt(pycurl.MAXREDIRS, 2)
272
273             if self.USE_PROXY:
274                 curl.setopt(pycurl.PROXY, self.PROXY )
275             
276             if DoSSL:
277                 curl.setopt(pycurl.SSLVERSION, self.CURL_SSL_VERSION)
278
279                 url = "https://%s/%s%s" % (server,PartialPath,getstr)
280                 if DoCertCheck:
281                     curl.setopt(pycurl.CAINFO, certpath)
282                     curl.setopt(pycurl.SSL_VERIFYPEER, 2)    
283                     self.Message( "Using SSL version %d and verifying peer." % \
284                                   self.CURL_SSL_VERSION )
285                 else:
286                     curl.setopt(pycurl.SSL_VERIFYPEER, 0)
287                     self.Message( "Using SSL version %d" % \
288                                   self.CURL_SSL_VERSION )
289             else:
290                 url = "http://%s/%s%s" % (server,PartialPath,getstr)
291
292             if dopostdata:
293                 curl.setopt(pycurl.POSTFIELDS, postdata)
294                 
295             curl.setopt(pycurl.URL, url)
296             self.Message( "URL: %s" % url )
297             
298             # setup the output buffer
299             buffer = StringIO()
300             curl.setopt(pycurl.WRITEFUNCTION, buffer.write)
301             
302             try:
303                 self.Message( "Fetching..." )
304                 curl.perform()
305                 self.Message( "Done." )
306                 
307                 http_result= curl.getinfo(pycurl.HTTP_CODE)
308                 curl.close()
309
310                 # check the code, return the string only if it was successfull
311                 if http_result == self.HTTP_SUCCESS:
312                     self.Message( "Successfull!" )
313                     return buffer.getvalue()
314                 else:
315                     self.Message( "Failure, resultant http code: %d" % \
316                                   http_result )
317                     return None
318                 
319             except pycurl.error, err:
320                 errno, errstr= err
321                 self.Error( "connect to %s failed; curl error %d: '%s'\n" %
322                        (server,errno,errstr) ) 
323
324         self.Error( "Unable to successfully contact any boot servers.\n" )
325         return None
326    
327
328
329     def DownloadFile(self, PartialPath, GetVars, PostVars,
330                      DoSSL, DoCertCheck, DestFilePath,
331                      ConnectTimeout= DEFAULT_CURL_CONNECT_TIMEOUT,
332                      MaxTransferTime= DEFAULT_CURL_MAX_TRANSFER_TIME):
333
334         self.Message( "Attempting to retrieve %s" % PartialPath )
335
336         # we can't do ssl and check the cert if we don't have a bootcd
337         if DoSSL and DoCertCheck and not self.HAS_BOOTCD:
338             self.Error( "No boot cd exists (needed to use -c and -s.\n" )
339             return 0
340
341         if DoSSL and not PYCURL_LOADED:
342             self.Warning( "Using SSL without pycurl will by default " \
343                           "check at least standard certs." )
344
345         # ConnectTimeout has to be greater than 0
346         if ConnectTimeout <= 0:
347             self.Error( "Connect timeout must be greater than zero.\n" )
348             return 0
349
350
351         self.CheckProxy()
352
353         dopostdata= 0
354
355         # setup the post and get vars for the request
356         if PostVars:
357             dopostdata= 1
358             postdata = urllib.urlencode(PostVars)
359             self.Message( "Posting data:\n%s\n" % postdata )
360             
361         getstr= ""
362         if GetVars:
363             getstr= "?" + urllib.urlencode(GetVars)
364             self.Message( "Get data:\n%s\n" % getstr )
365
366         # now, attempt to make the request, starting at the first
367         # server in the list
368         
369         for server in self.BOOTSERVER_CERTS:
370             self.Message( "Contacting server %s." % server )
371                         
372             certpath = self.BOOTSERVER_CERTS[server]
373
374             
375             # output what we are going to be doing
376             self.Message( "Connect timeout is %s seconds" % \
377                           ConnectTimeout )
378
379             self.Message( "Max transfer time is %s seconds" % \
380                           MaxTransferTime )
381
382             if DoSSL:
383                 url = "https://%s/%s%s" % (server,PartialPath,getstr)
384                 
385                 if DoCertCheck and PYCURL_LOADED:
386                     self.Message( "Using SSL version %d and verifying peer." %
387                              self.CURL_SSL_VERSION )
388                 else:
389                     self.Message( "Using SSL version %d." %
390                              self.CURL_SSL_VERSION )
391             else:
392                 url = "http://%s/%s%s" % (server,PartialPath,getstr)
393                 
394             self.Message( "URL: %s" % url )
395             
396             # setup a new pycurl instance, or a curl command line string
397             # if we don't have pycurl
398             
399             if PYCURL_LOADED:
400                 curl= pycurl.Curl()
401
402                 # don't want curl sending any signals
403                 curl.setopt(pycurl.NOSIGNAL, 1)
404             
405                 curl.setopt(pycurl.CONNECTTIMEOUT, ConnectTimeout)
406                 curl.setopt(pycurl.TIMEOUT, MaxTransferTime)
407
408                 # do not follow location when attempting to download a file
409                 curl.setopt(pycurl.FOLLOWLOCATION, 0)
410
411                 if self.USE_PROXY:
412                     curl.setopt(pycurl.PROXY, self.PROXY )
413
414                 if DoSSL:
415                     curl.setopt(pycurl.SSLVERSION, self.CURL_SSL_VERSION)
416                 
417                     if DoCertCheck:
418                         curl.setopt(pycurl.CAINFO, certpath)
419                         curl.setopt(pycurl.SSL_VERIFYPEER, 2)
420                         
421                     else:
422                         curl.setopt(pycurl.SSL_VERIFYPEER, 0)
423                 
424                 if dopostdata:
425                     curl.setopt(pycurl.POSTFIELDS, postdata)
426
427                 curl.setopt(pycurl.URL, url)
428             else:
429
430                 cmdline = "%s " \
431                           "--connect-timeout %d " \
432                           "--max-time %d " \
433                           "--header Pragma: " \
434                           "--output %s " \
435                           "--fail " % \
436                           (self.CURL_CMD, ConnectTimeout,
437                            MaxTransferTime, DestFilePath)
438
439                 if dopostdata:
440                     cmdline = cmdline + "--data '" + postdata + "' "
441
442                 if not self.VERBOSE:
443                     cmdline = cmdline + "--silent "
444                     
445                 if self.USE_PROXY:
446                     cmdline = cmdline + "--proxy %s " % self.PROXY
447
448                 if DoSSL:
449                     cmdline = cmdline + "--sslv%d " % self.CURL_SSL_VERSION
450
451                     if DoCertCheck:
452                         cmdline = cmdline + "--cacert %s " % certpath
453                  
454                 cmdline = cmdline + url
455
456                 self.Message( "curl command: %s" % cmdline )
457                 
458                 
459             if PYCURL_LOADED:
460                 try:
461                     # setup the output file
462                     outfile = open(DestFilePath,"wb")
463                     
464                     self.Message( "Opened output file %s" % DestFilePath )
465                 
466                     curl.setopt(pycurl.WRITEDATA, outfile)
467                 
468                     self.Message( "Fetching..." )
469                     curl.perform()
470                     self.Message( "Done." )
471                 
472                     http_result= curl.getinfo(pycurl.HTTP_CODE)
473                     curl.close()
474                 
475                     outfile.close()
476                     self.Message( "Results saved in %s" % DestFilePath )
477
478                     # check the code, return 1 if successfull
479                     if http_result == self.HTTP_SUCCESS:
480                         self.Message( "Successfull!" )
481                         return 1
482                     else:
483                         self.Message( "Failure, resultant http code: %d" % \
484                                       http_result )
485
486                 except pycurl.error, err:
487                     errno, errstr= err
488                     self.Error( "connect to %s failed; curl error %d: '%s'\n" %
489                        (server,errno,errstr) )
490         
491                 if not outfile.closed:
492                     try:
493                         os.unlink(DestFilePath)
494                         outfile.close
495                     except OSError:
496                         pass
497
498             else:
499                 self.Message( "Fetching..." )
500                 rc = os.system(cmdline)
501                 self.Message( "Done." )
502                 
503                 if rc != 0:
504                     try:
505                         os.unlink( DestFilePath )
506                     except OSError:
507                         pass
508                     self.Message( "Failure, resultant curl code: %d" % rc )
509                     self.Message( "Removed %s" % DestFilePath )
510                 else:
511                     self.Message( "Successfull!" )
512                     return 1
513             
514         self.Error( "Unable to successfully contact any boot servers.\n" )
515         return 0
516
517
518
519
520 def usage():
521     print(
522     """
523 Usage: BootServerRequest.py [options] <partialpath>
524 Options:
525  -c/--checkcert        Check SSL certs. Ignored if -s/--ssl missing.
526  -h/--help             This help text
527  -o/--output <file>    Write result to file
528  -s/--ssl              Make the request over HTTPS
529  -v                    Makes the operation more talkative
530 """);  
531
532
533
534 if __name__ == "__main__":
535     import getopt
536     
537     # check out our command line options
538     try:
539         opt_list, arg_list = getopt.getopt(sys.argv[1:],
540                                            "o:vhsc",
541                                            [ "output=", "verbose", \
542                                              "help","ssl","checkcert"])
543
544         ssl= 0
545         checkcert= 0
546         output_file= None
547         verbose= 0
548         
549         for opt, arg in opt_list:
550             if opt in ("-h","--help"):
551                 usage(0)
552                 sys.exit()
553             
554             if opt in ("-c","--checkcert"):
555                 checkcert= 1
556             
557             if opt in ("-s","--ssl"):
558                 ssl= 1
559
560             if opt in ("-o","--output"):
561                 output_file= arg
562
563             if opt == "-v":
564                 verbose= 1
565     
566         if len(arg_list) != 1:
567             raise Exception
568
569         partialpath= arg_list[0]
570         if string.lower(partialpath[:4]) == "http":
571             raise Exception
572
573     except:
574         usage()
575         sys.exit(2)
576
577     # got the command line args straightened out
578     requestor= BootServerRequest(verbose)
579         
580     if output_file:
581         requestor.DownloadFile( partialpath, None, None, ssl,
582                                 checkcert, output_file)
583     else:
584         result= requestor.MakeRequest( partialpath, None, None, ssl, checkcert)
585         if result:
586             print result
587         else:
588             sys.exit(1)