Sunday, November 14, 2010

Downsampling MP3 file

#!/usr/bin/tclsh

set ffmpeg [exec which ffmpeg]
puts "ffmpeg  = $ffmpeg"
if { [llength $argv] < 2 } {
    puts "\n$argv0  n bps\n"
    exit -1
}
set fi [lindex $argv 0]
set br [lindex $argv 1]
puts "$fi"
puts "target $br bps"

if { [regexp -all {(.*)\.([mM][pP]3)} $fi a b c] } {
    set fo "${b}_${br}bps.$c"
    puts "$fi ==> $fo, ext=$c, new bitrate=$br"
    set id3 [exec id3v2 -l $fi]
    set id3par ""
    if { [regexp -line {TIT2.*: (.*)\n} $id3 dummy title c] } { puts Title=$title }
    if { [regexp -line {TPE1.*: (.*)\n} $id3 dummy singer c] } { puts singer=$singer }
    if { [regexp -line {TALB.*: (.*)\n} $id3 dummy album c] } { puts album=$album }
    if { [regexp -line {TYER.*: (.*)\n} $id3 dummy year c] } { puts year=$year }
    if { [regexp -line {TCON.*\(([0-9]+)\)\n} $id3 dummy genre c] } { puts genre=$genre }
    set cmd "$ffmpeg -threads 16 -y -ab $br -i $fi $fo"
    if {![file exists $fo]} {
        if { [catch { set res [eval exec $cmd] fid }] } {
            #puts stderr "Could not execute $cmd"
            if {[info exists fid] } { puts stderr $fid" }
            #exit 1
        }
    }
    if {[info exists title]} { append idpar " --TIT2 \"$title\"" }
    if {[info exists singer]} { append idpar " --TPE1 \"$singer\"" }
    if {[info exists album]} { append idpar " --TALB \"$album\"" }
    if {[info exists year]} { append idpar " --TYER \"$year\"" }
    if {[info exists genre]} { append idpar " --TCON \"$genre\"" }
    set cmd "id3v2 $idpar $fo"
    puts $cmd
    if { [file exists $fo]} {
        eval exec $cmd
    }
} else {
    puts "Not an MP3 file"
}


PAM Security explained

PAM Security: http://articles.techrepublic.com.com/5100-10878_11-1055269.html

Login restriction/Limitation based on time schedue

This is how to limit a user (in this case, his name is "Joko") to access computer during weekdays and weekends at certain time duration.

  1. Edit file
    /etc/pam.d/common-account
    and add a line "account required pam_time.so"
  2. Edit file
    /etc/security/time.conf
    and add the following lines to the end of file.

#services  ttys users   times
#
# allow Joko to use computer during weekdays 3 pm-8:30pm OR weekends 9:00am - 9:00pm
* ; * ; joko; Wk1500-2030 | Wk0900-2100

This will not allow joko to login during weekdays before 3 pm or after 8:30 pm or weekends outside 9 am - 9 pm. To disallow certain service, replace '*' in the first entry with a PAM service name (e.g, login, etc.). Files in /etc/pam.d reflect the service names.

On my computer, the following files are the service names can be used:

-rw-r--r-- 1 root root 217 2010-07-05 04:57 atd
-rw-r--r-- 1 root root 167 2010-07-05 06:44 chage
-rw-r--r-- 1 root root 218 2010-07-05 06:44 chfn
-rw-r--r-- 1 root root 218 2010-07-05 06:44 chsh
lrwxrwxrwx 1 root root  17 2010-09-13 10:39 common-account -> common-account-pc
-rw-r--r-- 1 root root 378 2010-07-05 05:15 common-account.pam-config-backup
-rw-r--r-- 1 root root 446 2010-11-14 09:01 common-account-pc
lrwxrwxrwx 1 root root  14 2010-09-13 10:39 common-auth -> common-auth-pc
-rw-r--r-- 1 root root 448 2010-07-05 05:15 common-auth.pam-config-backup
-rw-r--r-- 1 root root 557 2010-11-14 09:01 common-auth-pc
lrwxrwxrwx 1 root root  18 2010-09-13 10:39 common-password -> common-password-pc
-rw-r--r-- 1 root root 855 2010-07-05 05:15 common-password.pam-config-backup
-rw-r--r-- 1 root root 506 2010-11-14 09:01 common-password-pc
lrwxrwxrwx 1 root root  17 2010-09-13 10:39 common-session -> common-session-pc
-rw-r--r-- 1 root root 435 2010-07-05 05:15 common-session.pam-config-backup
-rw-r--r-- 1 root root 573 2010-11-14 09:01 common-session-pc
-rw-r--r-- 1 root root 287 2010-07-05 04:56 crond
-rw-r--r-- 1 root root  56 2010-09-15 13:49 cups
-rw-r--r-- 1 root root 204 2010-07-05 16:24 gdm
-rw-r--r-- 1 root root 206 2010-07-05 16:24 gdm-autologin
-rw-r--r-- 1 root root 239 2010-07-05 08:06 gnomesu-pam
-rw-r--r-- 1 root root 216 2010-07-28 09:45 init
-rw-r--r-- 1 root root 419 2010-07-05 22:36 login
-rw-r--r-- 1 root root 251 2010-07-05 05:15 other
-rw-r--r-- 1 root root 133 2010-07-05 06:44 passwd
-rw-r--r-- 1 root root 165 2010-07-05 06:18 polkit
-rw-r--r-- 1 root root 165 2010-07-05 07:10 polkit-1
-rw-r--r-- 1 root root 173 2010-07-05 14:34 ppp
-rw-r--r-- 1 root root 481 2010-07-05 05:32 remote
-rw-r--r-- 1 root root 165 2010-09-15 14:14 samba
-rw-r--r-- 1 root root 209 2010-07-05 06:44 shadow
-rw-r--r-- 1 root root 165 2010-09-14 07:07 smtp
-rw-r--r-- 1 root root 268 2010-07-05 14:41 sshd
-rw-r--r-- 1 root root 239 2010-07-05 05:00 su
-rw-r--r-- 1 root root 203 2010-09-07 06:01 sudo
-rw-r--r-- 1 root root 239 2010-07-05 05:00 su-l
-rw-r--r-- 1 root root 172 2010-07-05 06:44 useradd
-rw-r--r-- 1 root root 569 2010-07-05 05:58 vsftpd
-rw-r--r-- 1 root root 241 2010-07-29 04:36 wxconsole
-rw-r--r-- 1 root root 204 2008-09-03 08:45 xdm
-rw-r--r-- 1 root root 166 2008-09-03 08:45 xdm-np
-rw-r--r-- 1 root root 265 2010-08-23 16:44 xen-api

A good solution to limit use of computer from kids during school days!

Monday, November 8, 2010

My First Objective-C

List.h:

#import <objc/Object.h>

@interface List : Object                          // List is a subclass of the superclass Object
{
    int list[100];                                // These are instance variables.
    int size;
}


/* Public methods */
- free;
- (int) addEntry: (int) num;
- print;

/* Private methods */
/* Other programs should not use these methods. */

- resetSize;

@end


List.m:

#import "List.h"

@implementation List

+ new                                           // factory method
{
    self = [super new];
    [self resetSize];
    return self;
}


- free
{
    return [super free];
}


- (int) addEntry: (int) num
{
    list[size++] = num;
    return size;
}


- print
{
    int i;

    printf("\n");
    for (i = 0; i < size; ++i)
        printf ("%i ", list[i]);
        puts("");
    return self;                                // Always return self
                                                                                                // if n
othing else makes sense.
}


- resetSize
{
    size = 0;
    return self;
}
Stack.h:
#import <objc/Object.h>

typedef struct StackLink {
        struct StackLink        *next;
        //void                  *data;
        int                     data;
} StackLink;


@interface Stack : Object
{
        StackLink *top;
    unsigned int size;
}

- free;
- push: (int) anInt;
- addEntry: (int) anInt;
- (int) pop;
- (unsigned int) size;

@end
Stack.m:
#import "Stack.h"
#import <stdlib.h>

@implementation Stack

#define NULL_LINK (StackLink *) 0

+ new
{
    self = [super new];
    top = NULL_LINK; //(StackLink *) 0;
    return self;
}


- free
{
    StackLink *next;

    while (top != NULL_LINK)
    {
        next = top->next;
        free ((char *) top);
        top = next;
    }
    return [super free];
}


- push: (int) value
{
    StackLink *newLink;

    newLink = (StackLink *) malloc (sizeof (StackLink));
    if (newLink == 0)
    {
        fprintf(stderr, "Out of memory\n");
        return nil;
    }
    newLink->data = value;
    newLink->next = top;
    top = newLink;
    size++;

    return self;
}


- addEntry: (int) value
{
        return [self push: value];
}

- (int) pop
{
    int value;
    StackLink *topLink;

    if (0 != size)
    {
        topLink = top;
        top = top->next;
        value = topLink->data;
        free (topLink);
        size--;
    }
    else
    {
        value = 0;
    }
    return value;
}


- (unsigned int) size
{
    return size;
}

- print
{
        StackLink *startLink;
        int i = size;

        startLink = top;

        while (startLink) {
                printf("Stack[%d] = %d\n", i, startLink->data);
                startLink = startLink->next;
                i--;
        }
        return self;
}

@end
main.m:
#import <objc/Object.h>
#import "List.h"
#import "Stack.h"

//int main(int argc, char *argv[])

void perform (id obj)
{
        [obj addEntry: 5];
        [obj print];
        [obj addEntry: 6];
        [obj addEntry: 3];
        [obj print];
}

main()
{
        id list;

        list = [List new];
        perform(list);
        [list free];

        id stack;
        stack = [Stack new];
        perform(stack);
        [stack free];
        printf("\n");
        return 0;
}

Makefile:
.SUFFIXES: .o .m

.m.o:
        $(CC) -c $(CFLAGS) $< -o $@

# Macros

CC=gcc

#CFLAGS=-Wall -Wno-import -mtune=core2 -mfpmath=sse -ffast-math -msse3 -fobjc-exceptions
CFLAGS=-Wno-import -mtune=core2 -g

LDFLAGS=-lobjc
SRCS=main.m List.m Stack.m
OBJS=$(SRCS:.m=.o)
EXECUTABLE=main

.PHONY: all

all: $(EXECUTABLE)

$(EXECUTABLE): $(OBJS)
        $(CC) $(LDFLAGS) -o $@ $(OBJS)

#$(OBJS): $(SRCS)
#       echo "$(CC) -c $(CFLAGS) $< -o $@"

clean:
        rm -f *.o $(EXECUTABLE)

TAGS: $(SRCS)
        ctags -R $(SRCS)

Tuesday, November 2, 2010

MiniDuckSimulator.java


import java.io.Console;

/**
 * @author mlutfi
 *
 */
public class MiniDuckSimulator {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Duck mallard = new MallardDuck();
  mallard.display();
  mallard.performQuack();
  mallard.performFly();
  
  Duck model = new ModelDuck();
  model.display();
  model.performFly();
  model.setFlyBehavior(new FlyRocketPowered());
  model.performFly();
 }
}

Sunday, October 31, 2010

How to block certain inbound IP addresses

Create a file /etc/iptables/blockip.sh:

#!/bin/bash
# Simple iptables IP/subnet block script
# -------------------------------------------------------------------------
IPT=`which iptables`
SPAMLIST="spamlist"
SPAMDROPMSG="SPAM LIST DROP"
BADIPS=$(egrep -v -e "^#|^$" /etc/iptables/blocked.ips)

# create a new iptables list
$IPT -N $SPAMLIST

for ipblock in $BADIPS
do
   $IPT -A $SPAMLIST -s $ipblock -j LOG --log-prefix "$SPAMDROPMSG"
   $IPT -A $SPAMLIST -s $ipblock -j DROP
done

$IPT -I INPUT -j $SPAMLIST
$IPT -I OUTPUT -j $SPAMLIST
$IPT -I FORWARD -j $SPAMLIST

Create a file /etc/iptables/blocked.ips:

put IP addresses here! (one per line)


edit file /etc/boot.local and add this line:

/etc/iptables/blockip.sh