def add_group(group='', guid='1000', root='/mnt/'): '''add_group(group='', guid=1000', root='/mnt/') Añade un grupo ''' from utils import debug if group == '': debug.echo_debug('No se han pasado los parámetros correctos', 'ERROR') return False elif group_exist(group): return True from utils import shell file = 'etc/group' pass_list = [] if is_shadow(root): file = 'shadow' else: file = 'passwd' filename = shell.joinpath(root, file) groupfile = open(filename, 'a') groupfile.write('%s:x:%s:' % (group, guid)) if is_shadow('gshadow'): filename = shell.joinpath(root, 'etc/gshadow') groupfile = open(filename, 'a') groupfile.write('%s:!::' % group) return True
def is_shadow(root='/mnt/', file='shadow'): from os.path import isfile from utils import debug, shell filename = shell.joinpath(root, 'etc/' + file) if file in ['shadow', 'gshadow']: return isfile(filename) else: debug.echo_debug( 'is_shadow: No ha introducido un nombre de archivo válido', 'ERROR') return False
def add_user(user='', passwd='', root='/mnt/'): '''add_user(user='', passwd='', root='/mnt/') Añade un usuario ''' from utils import debug if user == '' or passwd == '': debug.echo_debug('No se han pasado los parámetros correctos', 'ERROR') return False if user_exist(user): change_pass(user, passwd, root) else: #FIXME: Acabar funcion pass
def user_exist(user='', root='/mnt/'): '''user_exist(user='', root='/mnt/') Devuelve True si esiste el usuario y False si no existe ''' import re from utils import debug, shell if user == '': debug.echo_debug('No se ha pasado un nombre de usuario', 'ERROR') return False is_user = re.compile('^(%s):' % user) passwd = shell.joinpath(root, 'etc/passwd') lines = open(passwd).readlines() for line in lines: found = is_user.match(line) if found: return True return False
def change_pass(user='', passwd='', root='/mnt/'): '''change_pass(user='', passwd='', root='/mnt/') Cambia la clave a un usuario ''' from utils import debug if user == '' or passwd == '': debug.echo_debug('No se han pasado los parámetros correctos', 'ERROR') return False elif not user_exist(user): debug.echo_debug( 'El usuario %s, no existe, no se ha podido cambiar la clave' % user, 'ERROR') return False import crypt from utils import shell pass_list = [] if is_shadow(root): file = 'shadow' else: file = 'passwd' passfile = shell.joinpath(root, 'etc/' + file) lines = open(passfile).readlines() for line in lines: fields = line.split(':') if fields[0] == user: fields[1] = crypt.crypt(passwd, 'o3l*2_X<.3q¡^') line = ':'.join(fields) pass_list.append(line) filename = open(passfile, 'w') for line in pass_list: filename.write(line) filename.close() return True
def make_interfaces(configs = None, root='/mnt'): '''make_interfaces(configs = None, root='/mnt') -> bool Crea el archivo /etc/network/interfaces a partir de los datos de un diccionario pasado por parametro. ''' from os.path import isfile from utils.debug import echo_debug, read_file from utils.shell import joinpath filename = joinpath(root, 'etc/network/interfaces') if not isfile(filename): echo_debug('make_interfaces: No exite el archivo %s' % filename,'ERROR') return False file = open(filename, 'w') file.write(''' # /etc/network/interfaces -- configuration file for ifup(8), ifdown(8) # The loopback interface auto lo iface lo inet loopback # The network cards ''') interfaces = get_interfaces() wireless = get_wireless() #FIXME: Terminar for for iter in interfaces: if iter in wireless: print 'configuraciones wireless' else: print 'configuraciones ethernet' file.close() return True
#!/usr/bin/python '''Script de pruebas Este Script sirve para comprobar que las librarias funcionan bien. ''' import os.path, sys PATH = os.path.realpath('metaconf') if PATH not in sys.path: sys.path.append(PATH) from utils import debug, shell debug.echo_debug('Empieza el script') shell.mkdir('/tmp/etc') shell.mkdir('/tmp/etc/X11') from hardware import kmodules, disks, xconf for i in kmodules.get_pci_modules(): print i, print '\n' X = xconf.Xconf() print 'X Driver: %s' % X.get_card() kmodules.make_modules('/tmp') disks.make_fstab('/tmp') for i in disks.get_cdroms():
def make_xf86config(root = '/'): '''make_xf86config(root = '/') Crea el archivo /etc/X11/XF86Config-4 a partir de la clase Xconf. Admite el parámetro "root" que indica el directorio padre del sistema donde se quieren configurar las X. ''' template = ''' Section "Files" FontPath "unix/:7100" # local font server # if the local font server has problems, we can fall back on these FontPath "/usr/lib/X11/fonts/Type1" FontPath "/usr/lib/X11/fonts/CID" FontPath "/usr/lib/X11/fonts/Speedo" FontPath "/usr/lib/X11/fonts/misc" FontPath "/usr/lib/X11/fonts/cyrillic" FontPath "/usr/lib/X11/fonts/100dpi" FontPath "/usr/lib/X11/fonts/75dpi" EndSection Section "Module" Load "GLcore" Load "bitmap" Load "dbe" Load "ddc" Load "dri" Load "extmod" Load "glx" Load "int10" Load "record" Load "speedo" Load "type1" Load "vbe" Load "xtt" EndSection Section "InputDevice" Identifier "Generic Keyboard" Driver "keyboard" Option "CoreKeyboard" Option "XkbRules" "xfree86" Option "XkbModel" "pc105" Option "XkbLayout" "es" EndSection Section "ServerFlags" Option "AllowMouseOpenFail" "true" EndSection Section "InputDevice" Identifier "Generic Mouse Serial" Driver "mouse" Option "CorePointer" Option "Device" "/dev/ttyS0" Option "Protocol" "Microsoft" Option "Emulate3Buttons" "true" Option "ZAxisMapping" "4 5" EndSection Section "InputDevice" Identifier "Generic Mouse PS/2" Driver "mouse" Option "Protocol" "ImPS/2" Option "Device" "/dev/psaux" Option "Emulate3Buttons" "true" Option "Emulate3Timeout" "70" Option "ZAxisMapping" "4 5" Option "SendCoreEvents" "true" EndSection Section "InputDevice" Identifier "Generic Mouse USB" Driver "mouse" Option "SendCoreEvents" "true" Option "Device" "/dev/input/mice" Option "Protocol" "ImPS/2" Option "Emulate3Buttons" "true" Option "ZAxisMapping" "4 5" EndSection Section "Device" Identifier "Generic Video Card" Driver "@@DRIVER@@" BusID "@@BUSID@@" EndSection Section "Monitor" Identifier "Generic Monitor" HorizSync @@HSYNC@@ VertRefresh @@VSYNC@@ EndSection Section "Screen" Identifier "Default Screen" Device "Generic Video Card" Monitor "Generic Monitor" DefaultDepth 16 SubSection "Display" Depth 1 Modes "1024x768" "800x600" "640x480" EndSubSection SubSection "Display" Depth 4 Modes "1024x768" "800x600" "640x480" EndSubSection SubSection "Display" Depth 8 Modes "1024x768" "800x600" "640x480" EndSubSection SubSection "Display" Depth 15 Modes "1024x768" "800x600" "640x480" EndSubSection SubSection "Display" Depth 16 Modes "1024x768" "800x600" "640x480" EndSubSection SubSection "Display" Depth 24 Modes "1024x768" "800x600" "640x480" EndSubSection EndSection Section "ServerLayout" Identifier "Default Layout" Screen "Default Screen" InputDevice "Generic Keyboard" InputDevice "Generic Mouse Serial" InputDevice "Generic Mouse PS/2" InputDevice "Generic Mouse USB" EndSection Section "DRI" Mode 0666 EndSection ''' from utils.debug import echo_debug from utils.shell import joinpath, mkdir # Se crea un objeto Xconf X = Xconf() # Se sustituyen los valores newx = template.replace('@@DRIVER@@',X.get_card()) newx = newx.replace('@@BUSID@@',X.get_busid()) newx = newx.replace('@@HSYNC@@',X.get_hsync()) newx = newx.replace('@@VSYNC@@',X.get_vsync()) # Se crea el archivo con los nuevos valores try: dirname = joinpath(root, 'etc/X11/') mkdir(dirname) filename = dirname + 'XF86Config-4' xf86config = open(filename, 'w') except IOError: echo_debug('''No se ha podido crear el archivo XF86Config-4 en %s/etc/X11/, compruebe que existen los directorios.''' % root \ , 'ERROR') import sys sys.exit(1) xf86config.write(newx) xf86config.close()