Structured Data in C programming

I was learning about Structured Data and I have found some plenty of weird
code. Here is the simplest code I have found, so everyone can understand 
the concepts.
#include

struct person
{
  char *name;
  int age;
};

int main()
{
  struct person p;
  p.name = "John Smith";
  p.age = 25;
  printf("%s",p.name);
  printf("%d",p.age);
  return 0;
}

Additionally, I have wrote this more elaborated code
(based on the ebook Learning GNU C from Ciarán O'Riordan):

#include 

struct Person
{
  char *name;
  int age;
  int height_in_cm;
};

int
main()
{
  struct Person hero = {"Robin Hood",20, 191 };
  struct Person sidekick;

  sidekick.age = 31;
  sidekick.name = "John Little";
  sidekick.height_in_cm = 237;

  printf("%s is %d years old and stands %dcm tall in his socks\n",
         sidekick.name, sidekick.age, sidekick.height_in_cm);

  printf("He is often seen with %s. \n", hero.name);

  return 0;
}

If you are on Linux, just do it to compile:
#gcc -Wall -o code code.c

Editing date format with sed

So, a few days ago at work I had this problem, I had a file containing many lines of dates in the following format:

mm-dd-yyyy but I had to convert it to yyyy-mm-dd and then join the sed and it’s arguments in a shell script (but I will not cover this shell script today, it is out of focus here).

To solve my problem, I just did the following:

#sed -e “s_\(..\)\-\(..\)\-\(….\)_\3-\1-\2_” sample.txt

Once I did it, I had my date format converted to the new one and sent to the output. So if you want it to be inserted in a new file, just add “>” after the file name, just like this:

#sed -e “s_\(..\)\-\(..\)\-\(….\)_\3-\1-\2_” sample.txt > sample_new.txt

Now you’ve got a new file containing ALL the lines in the new format! 😀

References:

http://www.grymoire.com/Unix/Sed.html

Small shell script to check webserver availability

With the following shell script we will:

1. By every 5 minutes will be checked if the w3m is getting the “200 OK” output.

2. If yes, then the script stops.

3. If no, then the command mail sends an email message warning the administrator about this issue.

4. The “/root/mailmsg.txt” file is where your customized warning message will be. You can certainly place it anywhere else if you wish so.

So, where’s the beef? Here it is, add the following code to your crontab (or write a separate file and point it in your crontab):

 */5 * * * * w3m -dump_head my-hi.com | grep -q '200 OK' || echo 'Your website is down' | mail -s 'Your website is down' admin@domainxyz.com < /root/mailmsg.txt 

 

FreeBSD ports – make arguments

Here I list some arguments very useful for the FreeBSD ports.

  • config – Opens up the package’s configuration dialog to turn options on or off.
  • config-recursive – Same as “config,” but also shows dialogs for package’s dependencies.
  • rmconfig – Removes user-specified configuration settings.
  • rmconfig-recursive – Same as “rmconfig” but removes settings for dependencies as well.
  • clean – Removes compiled work files, but not the installed files or downloaded source code.
  • distclean – Removes downloaded source code and work files for the package and its dependencies.
  • install – Installs the package once compiled.
  • deinstall – Removes a package from the system, akin to “pkg_delete.”
  • deinstall-all – Same as “deinstall,” but removes the package’s dependencies too.
  • reinstall – Used to upgrade a Port that first had to be removed with “deinstall”.

Usage example:

# cd /usr/ports/net-p2p/transmission

# make config-recursive

# make install distclean

 

Source: http://blog.zolp.us/2010/09/its-free-free-free-freebsd-part-2.html

Dovecot migration error

We migrated IMAP servers at work and for some reason I was the only one that was affected by a small problem. I couldn’t get my email! The error that would show in the maillog was:

dovecot: IMAP(user): FETCH for mailbox INBOX UID 176705 failed to read message input: Is a directory
dovecot: IMAP(user): Disconnected: BUG: Unknown internal error bytes=473/4475

After looking at several things I eventually saw that my cur directory had some subdirecties named like emails. They were empty, so I went ahead and deleted it and that fixed it!

Just thought I’d let other people know since it’s one of those things that Googling didn’t really give any answers. (Imagine that!)

 

Source: http://notjustlinux.blogspot.com/2009/05/dovecot-migration-error.html

Easy way to convert flash videos to mp3

An easy way to extract and convert the flash video audios (like youtube) to mp3:

ffmpeg -i inpuvvideofile.flv outputaudiofile.mp3

A knockd configuration file

Knockd is a Port Knocking implementation and I really like this one for being quite simple and flexible to work with almost any POSIX Operating System.

There are many ways to implement it, but some can be quite confusing and sometimes even useless if you write it wrong.

I personally like the following kind of configuration for it’s simplicity. I’m going to present you the code and then, after these code lines, I’m going to comment it:

/* Start of the knockd.conf file */
[options]
logfile = /var/log/knockd.log

[opencloseSSH]
sequence = 7000,8000,9000
seq_timeout = 15
command = /sbin/iptables -I INPUT -s %IP% -p tcp –dport 22 -j ACCEPT
cmd_timeout = 10
tcpflags = syn
stop_command = /sbin/iptables -D INPUT -s %IP% -p tcp –dport 22 -j ACCEPT

/* End of the knockd.conf file */

I will not get into deeper details about EACH line, but I will explain you how it works in general for this example.

Basically, knockd will be waiting for the three knock attempts (7000, 8000, 9000). The user has 15 seconds between the first knock (7000) until the last knock (9000). Once the server got those 3 knocks in less than 15 seconds, the iptables will open the port 22. Then you have 10 seconds to establish the connection, after these 10 seconds, the “stop_command” will be launched, which in this case will delete the iptables rule above. Remember, the path “/sbin/iptables” may vary, depending on which Linux distro you are using.

In order to make use of the following configuration scheme, it’s important that you have ESTABLISHED,RELATED rules in your iptables firewalling settings. Like this:

iptables -A INPUT -m –state ESTABLISHED,RELATED -j ACCEPT

Otherwise, you may loose your SSH session after those 10 seconds (even if you connected already).

And, of course, the iptables DROP policy:

iptables -P INPUT DROP

If you are interested about different configuration methods, you should check for the official man page. In the shell: man knockd

I wrote this tutorial based on Gentoo Linux, however, knockd should works in different Linux distros and also with any other *NIX.

References:

http://www.zeroflux.org/projects/knock

http://www.portknocking.org/