Wednesday, April 18, 2012

/etc/sysconfig/sendmail

— SkyHi @ Wednesday, April 18, 2012
Next up is /etc/sysconfig/sendmail. This file is used to assign some simple variables for the sendmail initialization script. By default it contains the following information:
DAEMON=yes
    QUEUE=1h
    
Quite simply this information is passed to the sendmail initscripts telling sendmail to run as a daemon and that it should process its queue once an hour. Depending on your environment you may need to change these values. If you're running a 24x7 net host, you should know that the SMTP RFCs specify a minimum queue time of 5 minutes so a queue run frequency of one minute would violate the RFC specifications.

Change the Queue Runner Interval in Sendmail
The default in CentOS 5.x is 1 hour:
# pgrep -lf "Queue runner"
12443 sendmail: Queue runner@01:00:00 for /var/spool/clientmqueue
You can change it editing /etc/sysconfig/sendmail (ie. set 30 minutes):
DAEMON=yes
QUEUE=30m
It’s easy, isn’t it? Useful for mail backup MXs that may need shorter queue run times.


REFERENCES
http://www.redhat.com/support/resources/howto/RH-sendmail-HOWTO/x95.html
http://www.redhat.com/support/resources/faqs/RH-sendmail-FAQ/x190.html
http://www.cyberciti.biz/tips/force-sendmail-to-deliver-a-message-in-sendmails-mail-queue.html

Monday, April 16, 2012

How to Change Reply-To Email Address in Outlook 2010

— SkyHi @ Monday, April 16, 2012
Sometimes you might want send email from one account (i.e. your personal account) but want any replies from your recipients to go to another account (i.e. your business account), vice versa.
This is how you would do it on an instance by instance basis.
  1. Launch Outlook 2010.
  2. From the Home tab of the top ribbon, click the New E-mailbutton.
  3. In the New E-mail window, click the Options tab.
  4. On the Options  ribbon, click Direct Replies To to bring up the Properties dialog.
  5. Under Delivery options, make sure the Have replies sent to: checkbox is checked.Specify the email address where you want your replies to be sent to.  (You can also click theSelect Names… button to select from your contact list.)
You are done!  Now send yourself a test message.  When you receive the test message, hit reply to confirm it is showing the proper alternate Reply-To email address.
Now you know how to change the Reply-To Email Address on an instance by instance basis.


REFERENCES
http://blackbeltreview.wordpress.com/2011/03/01/how-to-change-reply-to-email-address-in-outlook-2010/

Tuesday, April 10, 2012

Having problems browsing SAMBA shares? ..README

— SkyHi @ Tuesday, April 10, 2012
I've seen "SAMBA isn't working, now I can't connect to shares on my HTPC" being posted in a few places so I thought I'd post a note to educate people on what's going on. The statement that SAMBA is broken is rarely true. It's usually running fine, and the cause of the problem is simply the ugly way that NetBios over TCP/IP name resolution works. 

The reason you cannot see an smb://share "that was working 5 mins ago" is down to browser election fights between the computers on the network. An election fight is a peer-to-peer negotiation over who gets elected to be in charge of the domain(s) and/or local master browser list; the list of computer names that will appear in the Windows Network Neighbourhood (or it's current equivalent). The machine that wins the election is determined through a combination of uptime, OS version and config settings - meaning the outcome can be unpredictable, and the election process can take time (minutes not milliseconds). Over time your HTPC (as longest uptime device) has probably been elected the master browser, so if you reboot your HTPC the browser list is gone and the remaining machines on the network will restart an election process to decide who owns/creates the replacement. Once the election is over it will take time (again, minutes not milliseconds) to distribute the list and ensure every client has the same version. If you reboot other computers they will join the network and announce their availability, so an election discussion takes place to see if they should become the master browser. It's easy to see how this process may go titsup.com from time to time. Sadly, Mac and Linux computers can suffer the same issues as their Windows counterparts as they (via their local SAMBA client) are ultimately participating in the same process.

In corporate networks this issue is mitigated by having a pre-determined machine configured to win all the elections and maintain the browser list(s) - usually a domain controller. Rigging the election guarantees the election result and ensures the election process is completed quicker. The good news is that it's simple to configure SAMBA on your OE based HTPC to do the same thing. The reason for picking the HTPC to be the master browser is that it's probably the going to be the computer that has the highest uptime in your home network.

First, ensure you have /storage/.config/samba.conf. If you don't there is a sample one in the directory that can be renamed. Then add the lines indicated below to your [global] section:

[global]
  server string = Media Centre(%i)
  workgroup = HOME
  domain master = yes  <-- add this line
  local master = yes  <-- add this line
  preferred master = yes  <-- add this line
  os level = 100  <-- add this line
  netbios name = AppleTV
  security = share
  guest account = root
  socket options = TCP_NODELAY IPTOS_LOWDELAY SO_RCVBUF=65536 SO_SNDBUF=65536
  wins support = yes
  syslog only = yes
  name resolve order = lmhosts wins bcast host
  printcap name = /dev/null
  load printers = no

Even if you do this the problem will never be completely eliminated, but Windows network browsing should in theory be more consistent (in theory!). If it does not clear the problem the solution is usually to "turn everything off and on again" so that the old (incorrect) browser list is lost and a new (correct) one is created, or access your HTPC directly via an IP address based URL, e.g. \\192.168.1.2\ 

NB: in pre-RC6 setups the last two lines shown above (printcap name and load printers) were incorrectly placed under the [printers] section of the sample configuration file. If you move them to [global] as above you'll see some SAMBA startup errors in /var/log/messages go away.

REFERENCES

Windows can’t see Samba Share

— SkyHi @ Tuesday, April 10, 2012
For those of you who are using Samba on Linux (including Ubuntu) systems and would like to be able to have Windows users see your server in their “Network” discovery browser, here’s what you need to do.
The following is required to get windows to see your samba share so you no longer need to type in the IP address of your server every time you want to access it on a new system.
With your favourite text editor, open smb.conf:
vim /etc/samba/smb.cnf
Add this to the top of the file:
wins support = yes
You may also want to check to ensure that wins support configuration doesn’t exist in any other part of the configuration file.
After this is done, restart samba and Windows should be able to see your share!
service samba restart



REFERENCES

SSH Keys Authentication With PuTTY

— SkyHi @ Tuesday, April 10, 2012

5 Generate A Private/Public Key Pair

We can use PuTTYgen to create a private/public key pair. Start it by double-clicking its executable file. Make sure you select SSH-2 RSA underType of key to generate and specify 1024 as the Number of bits in a generated key. Then click on Generate:
Please move the mouse pointer over the blank area during the key generation to generate some randomness:
Now a private/public key pair has been generated. Under Key comment, you can enter any comment; normally you use your email address here. Then specify a Key passphrase and repeat it under Confirm passphrase. You'll need that passphrase to log in to SSH with your new key. Then click on Save publick key and save it in some safe location on your computer. You are free to choose a filename and extension, but it should be one that lets you remember for which system it is.
Then click on Save private key. You can save it in the same location as the public key - it should be a location that only you can access and that you don't lose! (If you lose the keys and have disabled username/password logins, then you can't log in anymore!) Again, you're free to choose a filename, but this time the extension must be .ppk:
Then copy the public key from the PuTTYgen window:

6 Save The Public Key On The Server

Then log in to your SSH server (if you have closed the previous SSH session already), still with the username and password, and paste the public key into the file ~/.ssh/authorized_keys2 (in one line!) like this:
mkdir ~/.ssh
chmod 700 ~/.ssh
vi ~/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EA[...]Lg5whU0zMuYE5IZu8ZudnP6ds= myname@example.com
That file must be write/readable only by that user, so we run
chmod 600 ~/.ssh/authorized_keys


7 Attach The Private Key To The PuTTY Profile

Now launch PuTTY again and load the profile of your SSH server (192.168.0.100):
Then go to SSH -> Auth and click on Browse:
Browse your file system and select your previously created private key:
Then go to Session again and click on Save:
Now we have attached the private key to our 192.168.0.100 PuTTY profile.

8 Our First Key-Based Login

Now everything is ready for our first key-based login to our SSH server. Click on Open:
As you can see, the public key is now used for authentication, and you are asked for the passphrase (the one you specified in chapter 5):

REFERENCES

Thursday, March 22, 2012

Why are docx, xlsx, pptx downloading from webserver as zip files?

— SkyHi @ Thursday, March 22, 2012
The server doesn't have proper mime types set up for the new ms file types
add the following to the /etc/mime.types file and restart apache and the problem should be fixed.
# Added by myname 2009-06-03
application/vnd.ms-word.document.macroEnabled.12                        docm
application/vnd.openxmlformats-officedocument.wordprocessingml.document docx
application/vnd.openxmlformats-officedocument.wordprocessingml.template dotx
application/vnd.ms-powerpoint.template.macroEnabled.12                  potm
application/vnd.openxmlformats-officedocument.presentationml.template   potx
application/vnd.ms-powerpoint.addin.macroEnabled.12                     ppam
application/vnd.ms-powerpoint.slideshow.macroEnabled.12                 ppsm    
application/vnd.openxmlformats-officedocument.presentationml.slideshow  ppsx
application/vnd.ms-powerpoint.presentation.macroEnabled.12              pptm
application/vnd.openxmlformats-officedocument.presentationml.presentation       pptx
application/vnd.ms-excel.addin.macroEnabled.12                          xlam
application/vnd.ms-excel.sheet.binary.macroEnabled.12                   xlsb
application/vnd.ms-excel.sheet.macroEnabled.12                          xlsm
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet       xlsx    
application/vnd.ms-excel.template.macroEnabled.12                       xltm    
application/vnd.openxmlformats-officedocument.spreadsheetml.template    xltx











REFERENCES
http://serverfault.com/questions/19060/why-are-docx-xlsx-pptx-downloading-from-webserver-as-zip-files

Monday, March 19, 2012

5 simple ways to troubleshoot using Strace

— SkyHi @ Monday, March 19, 2012
I keep being surprised how few people are aware of all the things they can use strace for. It's always one of the first debug tools I pull out, because it's usually available on the Linux systems I run, and it can be used to troubleshoot such a wide variety of problems.

What is strace?

Strace is quite simply a tool that traces the execution of system calls. In its simplest form it can trace the execution of a binary from start to end, and output a line of text with the name of the system call, the arguments and the return value for every system call over the lifetime of the process.
But it can do a lot more:
  • It can filter based on the specific system call or groups of system calls
  • It can profile the use of system calls by tallying up the number of times a specific system call is used, and the time taken, and the number of successes and errors.
  • It traces signals sent to the process.
  • It can attach to any running process by pid.
If you've used other Unix systems, this is similar to "truss". Another (much more comprehensive) is Sun's Dtrace.

How to use it

This is just scratching the surface, and in no particular order of importance:

1) Find out which config files a program reads on startup

Ever tried figuring out why some program doesn't read the config file you thought it should? Had to wrestle with custom compiled or distro-specific binaries that read their config from what you consider the "wrong" location?
The naive approach:
$ strace php 2>&1 | grep php.ini
open("/usr/local/bin/php.ini", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/local/lib/php.ini", O_RDONLY) = 4
lstat64("/usr/local/lib/php.ini", {st_mode=S_IFLNK|0777, st_size=27, ...}) = 0
readlink("/usr/local/lib/php.ini", "/usr/local/Zend/etc/php.ini", 4096) = 27
lstat64("/usr/local/Zend/etc/php.ini", {st_mode=S_IFREG|0664, st_size=40971, ...}) = 0
So this version of PHP reads php.ini from /usr/local/lib/php.ini (but it tries /usr/local/bin first).
The more sophisticated approach if I only care about a specific syscall:
$ strace -e open php 2>&1 | grep php.ini
open("/usr/local/bin/php.ini", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/local/lib/php.ini", O_RDONLY) = 4
The same approach work for a lot of other things. Have multiple versions of a library installed at different paths and wonder exactly which actually gets loaded? etc.

2) Why does this program not open my file?

Ever run into a program that silently refuse to read a file it doesn't have read access to, but you only figured out after swearing for ages because you thought it didn't actually find the file? Well, you already know what to do:
$ strace -e open,access 2>&1 | grep your-filename
Look for an open() or access() syscall that fails

3) What is that process doing RIGHT NOW?

Ever had a process suddenly hog lots of CPU? Or had a process seem to be hanging?
Then you find the pid, and do this:
root@dev:~# strace -p 15427
Process 15427 attached - interrupt to quit
futex(0x402f4900, FUTEX_WAIT, 2, NULL 
Process 15427 detached
Ah. So in this case it's hanging in a call to futex(). Incidentally in this case it doesn't tell us all that much - hanging on a futex can be caused by a lot of things (a futex is a locking mechanism in the Linux kernel). The above is from a normally working but idle Apache child process that's just waiting to be handed a request.
But "strace -p" is highly useful because it removes a lot of guesswork, and often removes the need for restarting an app with more extensive logging (or even recompile it).

4) What is taking time?

You can always recompile an app with profiling turned on, and for accurate information, especially about what parts of your own code that is taking time that is what you should do. But often it is tremendously useful to be able to just quickly attach strace to a process to see what it's currently spending time on, especially to diagnose problems. Is that 90% CPU use because it's actually doing real work, or is something spinning out of control.
Here's what you do:
root@dev:~# strace -c -p 11084
Process 11084 attached - interrupt to quit
Process 11084 detached
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 94.59    0.001014          48        21           select
  2.89    0.000031           1        21           getppid
  2.52    0.000027           1        21           time
------ ----------- ----------- --------- --------- ----------------
100.00    0.001072                    63           total
root@dev:~# 
After you've started strace with -c -p you just wait for as long as you care to, and then exit with ctrl-c. Strace will spit out profiling data as above.
In this case, it's an idle Postgres "postmaster" process that's spending most of it's time quietly waiting in select(). In this case it's calling getppid() and time() in between each select() call, which is a fairly standard event loop.
You can also run this "start to finish", here with "ls":
root@dev:~# strace -c >/dev/null ls
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 23.62    0.000205         103         2           getdents64
 18.78    0.000163          15        11         1 open
 15.09    0.000131          19         7           read
 12.79    0.000111           7        16           old_mmap
  7.03    0.000061           6        11           close
  4.84    0.000042          11         4           munmap
  4.84    0.000042          11         4           mmap2
  4.03    0.000035           6         6         6 access
  3.80    0.000033           3        11           fstat64
  1.38    0.000012           3         4           brk
  0.92    0.000008           3         3         3 ioctl
  0.69    0.000006           6         1           uname
  0.58    0.000005           5         1           set_thread_area
  0.35    0.000003           3         1           write
  0.35    0.000003           3         1           rt_sigaction
  0.35    0.000003           3         1           fcntl64
  0.23    0.000002           2         1           getrlimit
  0.23    0.000002           2         1           set_tid_address
  0.12    0.000001           1         1           rt_sigprocmask
------ ----------- ----------- --------- --------- ----------------
100.00    0.000868                    87        10 total
Pretty much what you'd expect, it spents most of it's time in two calls to read the directory entries (only two since it was run on a small directory).

5) Why the **** can't I connect to that server?

Debugging why some process isn't connecting to a remote server can be exceedingly frustrating. DNS can fail, connect can hang, the server might send something unexpected back etc. You can use tcpdump to analyze a lot of that, and that too is a very nice tool, but a lot of the time strace will give you less chatter, simply because it will only ever return data related to the syscalls generated by "your" process. If you're trying to figure out what one of hundreds of running processes connecting to the same database server does for example (where picking out the right connection with tcpdump is a nightmare), strace makes life a lot easier.
This is an example of a trace of "nc" connecting to www.news.com on port 80 without any problems:
$ strace -e poll,select,connect,recvfrom,sendto nc www.news.com 80
sendto(3, "\24\0\0\0\26\0\1\3\255\373NH\0\0\0\0\0\0\0\0", 20, 0, {sa_family=AF_NETLINK, pid=0, groups=00000000}, 12) = 20
connect(3, {sa_family=AF_FILE, path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
connect(3, {sa_family=AF_FILE, path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
connect(3, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("62.30.112.39")}, 28) = 0
poll([{fd=3, events=POLLOUT, revents=POLLOUT}], 1, 0) = 1
sendto(3, "\213\321\1\0\0\1\0\0\0\0\0\0\3www\4news\3com\0\0\34\0\1", 30, MSG_NOSIGNAL, NULL, 0) = 30
poll([{fd=3, events=POLLIN, revents=POLLIN}], 1, 5000) = 1
recvfrom(3, "\213\321\201\200\0\1\0\1\0\1\0\0\3www\4news\3com\0\0\34\0\1\300\f"..., 1024, 0, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("62.30.112.39")}, [16]) = 153
connect(3, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("62.30.112.39")}, 28) = 0
poll([{fd=3, events=POLLOUT, revents=POLLOUT}], 1, 0) = 1
sendto(3, "k\374\1\0\0\1\0\0\0\0\0\0\3www\4news\3com\0\0\1\0\1", 30, MSG_NOSIGNAL, NULL, 0) = 30
poll([{fd=3, events=POLLIN, revents=POLLIN}], 1, 5000) = 1
recvfrom(3, "k\374\201\200\0\1\0\2\0\0\0\0\3www\4news\3com\0\0\1\0\1\300\f"..., 1024, 0, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("62.30.112.39")}, [16]) = 106
connect(3, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("62.30.112.39")}, 28) = 0
poll([{fd=3, events=POLLOUT, revents=POLLOUT}], 1, 0) = 1
sendto(3, "\\\2\1\0\0\1\0\0\0\0\0\0\3www\4news\3com\0\0\1\0\1", 30, MSG_NOSIGNAL, NULL, 0) = 30
poll([{fd=3, events=POLLIN, revents=POLLIN}], 1, 5000) = 1
recvfrom(3, "\\\2\201\200\0\1\0\2\0\0\0\0\3www\4news\3com\0\0\1\0\1\300\f"..., 1024, 0, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("62.30.112.39")}, [16]) = 106
connect(3, {sa_family=AF_INET, sin_port=htons(80), sin_addr=inet_addr("216.239.122.102")}, 16) = -1 EINPROGRESS (Operation now in progress)
select(4, NULL, [3], NULL, NULL)        = 1 (out [3])
So what happens here?
Notice the connection attempts to /var/run/nscd/socket? They mean nc first tries to connect to NSCD - the Name Service Cache Daemon - which is usually used in setups that rely on NIS, YP, LDAP or similar directory protocols for name lookups. In this case the connects fails.
It then moves on to DNS (DNS is port 53, hence the "sin_port=htons(53)" in the following connect. You can see it then does a "sendto()" call, sending a DNS packet that contains www.news.com. It then reads back a packet. For whatever reason it tries three times, the last with a slightly different request. My best guess why in this case is that www.news.com is a CNAME (an "alias"), and the multiple requests may just be an artifact of how nc deals with that.
Then in the end, it finally issues a connect() to the IP it found. Notice it returns EINPROGRESS. That means the connect was non-blocking - nc wants to go on processing. It then calls select(), which succeeds when the connection was successful.
Try adding "read" and "write" to the list of syscalls given to strace and enter a string when connected, and you'll get something like this:
read(0, "test\n", 1024)                 = 5
write(3, "test\n", 5)                   = 5
poll([{fd=3, events=POLLIN, revents=POLLIN}, {fd=0, events=POLLIN}], 2, -1) = 1
read(3, "
This shows it reading "test" + linefeed from standard in, and writing it back out to the network connection, then calling poll() to wait for a reply, reading the reply from the network connection and writing it to standard out. Everything seems to be working right.



REFERENCES
http://www.hokstad.com/5-simple-ways-to-troubleshoot-using-strace.html