Thursday, November 27, 2008

Linux - Proxy Cache SQUID (ACLs)

If you need restrict the access to determined web sites for a subset of computers inside a LAN, then it could work for you . It work for me ;)
Edit the configuration of your SQUID (squid.conf)

# add these ACLs
acl netSubsetLAN src "/etc/squid/subsetLAN"
acl govar_all dstdomain -i .gov.ar
acl webDomain dstdomain "/etc/squid/webDomain"
acl webIPs dst "/etc/squid/webIPs"

# add these lines before the line http_access deny all
http_access allow netSubsetLAN
http_access allow netSubsetLAN webIPs
http_access allow netSubsetLAN webDomain
http_access allow netSubsetLAN govar_all



A very brief description:

The file "/etc/squid/subsetLAN" define an IP for each line. The IPs here are for the subset of IPs inside the LAN. For example,
192.168.1.20
192.168.1.21
192.168.1.22

The ACL govar_all defines a subset of destination domains where all domains with *.gov.ar will belong to this subset.

The file "/etc/squid/webDomain" define a domain for each line. For example,
www.google.com
maps.google.com


The file "/etc/squid/webIPs" define an IP for each line. The IPs here are for the destination IPs. You could use the command dig in a terminal. For example, open a terminal and type: dig www.google.com to find out the IPs of it domain. Then add them to this file.


http_access allow netSubsetLAN webDomain, it define an AND operation between netSubsetLAN and webDomain. It means, is allowed the http access for the IPs defined in netSubsetLAN only for those domains defined in webDomain.

References:
     Concepto de ACL en SQUID
     Squid Cache Wiki
     email list - ACL squid?

Friday, November 21, 2008

Linux - connected by 3G modem (Ubuntu Hardy)

To connect to Internet by mean a usb modem with 3G technology, you could use GNOME PPP or Vodafone Mobile Connect Card driver for Linux (Vodafone 3G devices Internet connection assistant).

sudo apt-get install gnome-ppp
or
sudo apt-get install vodafone-mobile-connect-card-driver-for-linux

I used GNOME PPP for the first time connection, to find out DNS's and auto-detect the modem device. This information is available in the log file.

Wednesday, November 19, 2008

Linux - Install man pages (Ubuntu Hardy)

In order to be able to read C/C++ man pages in a Terminal,
type these commnads:

$ sudo apt-get install manpages-posix-dev - for c headers
$ sudo apt-get install manpages-dev - for c functions

First, you could look the output of
$ sudo apt-cache search manpages

Reference:
http://ubuntuforums.org/showthread.php?t=10420

Linux - Install Mplayer and Multimedia Codecs

Install libdvdcss2 and w32 video codecs in Ubuntu 8.04 (Hardy Heron)

Support for WMV, RealMedia and other formats has been bundled into the w32codecs package. This package is not available from the Ubuntu repositories due to licensing and legal restrictions.

For Ubuntu 8.04 (Hardy Heron) Users run the following command

sudo wget http://www.medibuntu.org/sources.list.d/hardy.list -O /etc/apt/sources.list.d/medibuntu.list

Then, add the GPG Key using the following commands

sudo apt-get update

sudo apt-get install medibuntu-keyring

sudo apt-get update

For i386 Users install Codecs using the following command

sudo apt-get install w32codecs libdvdcss2

For amd64 Users install Codecs using the following command

sudo apt-get install w64codecs libdvdcss2

Using above download locations you can install most of the mutimedia codecs for ubuntu.

Mplayer Plugin for Firefox

If you want to install Mplayer with plug-in for Mozilla Firefox run the following command

sudo apt-get install mozilla-mplayer

Note: When I finished the installation above described, I must restart my computer in order to take the new packages. Then I could play videos with audio.


Install a different media player (optional)

The default media player (totem) is good, but I like vlc media player better, especially for watching a dvd.

In a terminal

sudo apt-get install vlc


References:

http://blog.csdn.net/emlinux/archive/2008/05/03/2370050.aspx

http://linuxowns.wordpress.com/2008/06/23/install-audio-and-video-codecs-in-ubuntu/

Monday, November 3, 2008

Python - keyGenerator class

Here is a very simple code to generate randomly a password with a predetermined size.
It would be better written with a switch case sentence instead of nested if-then-else sentences.

========================================================
import crypt
import string
from random import choice

class keyGenerator:
def __init__(self, password_size):
self.size = password_size
self.flag_letters = True
self.flag_digits = True
self.letters_lower = True
self.password_user = ''


def generatePassword(self):
# password with letters and digits
if ( (self.flag_letters == True) and (self.flag_digits == True) ):
self.password_user = ''.join ( [ choice(string.letters +
string.digits ) for i in range ( self.size ) ] )
else:
# password with digits, but without letters
if ( (self.flag_letter == False) and (self.flag_digits == True)):
self.password_user = ''.join ( [ choice( string.digits )
for i in range ( self.size ) ] )
else:
# password with letters, but without digits
if ( (self.flag_letter == True) and
(self.flag_digits == False)):
self.password_user = ''.join ( [ choice(string.letters )
for i in range ( self.size ) ] )
else:
print "Error: Letters and digits cannot be false
together! \n"

# password with letters lower or upper
if (self.letters_lower == True):
self.password_user = string.lower( self.password_user )
else:
self.password_user = string.upper( self.password_user )


def get_passwordUser(self):
return self.password_user

References:
Programming in Python - Generating a random string

Python - LDAP user management




This post will be ready in a few days.... be patient:
in advance, here is available the program code

========================================================

Installing Python-LDAP requires only one commnad:

$ sudo apt-get install python-ldap



#!/usr/bin/python

import ldap
import ldif
import MySQLdb
import sys
from myldapClass import *
from keyGenerator import *

class myLDIF:
def __init__(self, database, user_db, passw_db, db):
self.database = database
self.user_db = user_db
self.passw_db = passw_db
self.db = db

def connect(self):
try:
print 'connecting to database...'
self.conn = MySQLdb.connect( host=self.database, user=self.user_db,passwd=self.passw_db, db=self.db )
self.conn.set_character_set('utf8')
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)
print 'succesfully connected...'

def get_row(self, ldap_conn, base_dn_toAdd):
try:
cursor = self.conn.cursor()
cursor.execute ("SELECT * FROM princi ORDER BY legajo")

result_set = cursor.fetchall()
for row in result_set:
#print "%s, %s, %s" % (row[0], row[1], row[2])
if ( row[1].find(',') != -1):
#split by the first comma character
apellido, nombre = row[1].split(',',1)
else:
#split by the first whitespace
apellido1, nombre1 = row[1].split(' ',1)


dni = row[5].__str__()

dn = 'uid=' + dni + ',' + base_dn_toAdd
search_filter = '(objectclass=*)'
attrs = ['cn','sn', 'employeeNumber']

ldap_conn.search(dn, ldap.SCOPE_SUBTREE, search_filter, attrs)
if (ldap_conn.result_count > 0):
print 'This user already exist in the Ldap Server'
else:
# add this user to Ldap Server
kg = keyGenerator(9)
kg.generatePassword()

password_user = kg.password_user
legajo = row[0].__str__()

entry={'objectClass':['top','person','organizationalPerson','inetOrgPerson'],'cn':[nombre[1:]+' '+ apellido],'sn':[apellido], 'employeeNumber':[legajo],'mail':['cambiar@noexiste.com'],'uid':[dni],'userPassword':[password_user]}
ldif_writer=ldif.LDIFWriter(sys.stdout)
ldif_writer.unparse(dn,entry)

ldap_conn.addEntry(dn, entry)


cursor.close()

except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)

def close(self):
self.conn.close()

======================

change this references!

References:
http://www.grotan.com/ldap/python-ldap-samples.html
http://python-ldap.sourceforge.net/doc/html/index.html
http://www.linuxjournal.com/article/6988
http://python-ldap.sourceforge.net/apps.shtml
http://www.iaeste.or.at/doc/python-ldap-doc/html/node3.html
http://www.packtpub.com/article/installing-and-configuring-the-python-ldap-library-and-binding-to-an-ldap-directory