Friday, November 23, 2018

Firewalling IPs on macOS

I needed to selectively block some IPs from macOS and this is how I did it. First create a new anchor for the rules to go in. The file to create is:/etc/pf.anchors/org.user.block.out and it should contain:

table <blocked-hosts> persist
block in quick from <blocked-hosts>

Then edit: /etc/pf.conf and append the lines:

anchor "org.user.block.out"
load anchor "org.user.block.out" from "/etc/pf.anchors/org.user.block.out"

Then to reload the firewalling rules run:

$ sudo pfctl -f /etc/pf.conf

and if you haven't got pf enabled you also need to enable it with:

$ sudo pfctl -e

Then you can manage the blocked IPs with these commands:

# Block some IPs
$ sudo pfctl -a org.user.block.out -t blocked-hosts -T add 1.2.3.4 5.6.7.8
# Remove all the blocked IPs
$ sudo pfctl -a org.user.block.out -t blocked-hosts -T flush
# Remove a single IP
$ sudo pfctl -a org.user.block.out -t blocked-hosts -T delete 1.2.3.4






Friday, September 25, 2015

Bulk upload with cURL to a WebDAV server

To quickly upload a load of files to a WebDAV server using cURL:

ls *.txt | xargs -t -n 1 -P10  -J %  curl  -u username:password  -T %  https://mywebdav.server/

This does the upload with 10 parallel cURL processes which can make uploading a large set of files much faster.

Tuesday, September 10, 2013

Texet Bluetooth Mouse (BTM-01) Pairing

I recently got a very cheap Bluetooth mouse that is branded as a Texet Bluetooth Mouse (Model No: BTM-01) and pairing it is non-obvious. With the mouse on hold down:
  • left click
  • middle click (scroll wheel)
  • right click
When you release them the mouse should be discoverable. One thing to note is that the mouse is very cheap and it looks like they haven't got any bluetooth addresses as my mouse has a bluetooth address of 00:00:00:00:00:1D, but it does at least work.

Tuesday, April 16, 2013

Generate some files in folders for testing using bash.

I wanted a small test data set consisting of some files in folders, heres a quick shell snippet to generate some:

for dir in {1..100}; do
for dir2 in {a..f}; do
folder=dir-${dir}/dir-${dir2}; mkdir -p ${folder}
echo "A file with some text" > $folder/`pwgen -A10`.txt
done
done

Every time you run it you get more files, but the folders remain the same. It needs bash 3 or greater to run and creates folder like:

./dir-1/dir-a/euciphel.txt
./dir-1/dir-b/aineijof.txt
./dir-1/dir-c/naungeix.txt
./dir-1/dir-d/oopoolah.txt
./dir-1/dir-e/epahgome.txt
./dir-1/dir-f/ahshicia.txt
./dir-2/dir-a/busaenga.txt
./dir-2/dir-b/waepheep.txt
./dir-2/dir-c/jaeyahbi.txt
./dir-2/dir-d/biejoong.txt

Tuesday, April 02, 2013

Kerberos Tickets and VNC utilities on a Mac

A Mac ships with two useful utilities which are hidden away by default. The first it Ticket View which allows you to see the kerberos tickets you currently have and the second is Screen Sharing, which is a VNC client. I find it useful to create an alias to these two programs in the Applications/Utilities folder so that spotlight will index them and make them easy to launch. By default the applications are installed in:

/System/Library/CoreServices/

In finder you can go here in Finder by pressing ⇧⌘G and pasting in the path. The drag the programs to:

/Applications/

Not you can easily find these programs and open them from spotlight.


Thursday, February 14, 2013

Testing with foreign character sets on a Mac

To help with testing foriegn character sets I have created two additional character sets on my Mac that remap the standard British QUERTY keyboard to create readable but non-standard characters. This allows me to easily enter characters outside the normal ASCII range that are still readable. I created these character maps with Ukelele that allows me to load the British character map and then remap the keys. Once I've remapped the keys I export the character maps as a bundle and put them in /Library/Keyboard Layouts and then use System Preferences to add them to the list of available keyboard maps.
So the two character maps are:

  • British - Foreign : This uses accented characters that are below 0xFFFF in Unicode and so in plane 0. Ḥėṙė ïṡ ȧṅ ėẋȧṁṗĺė ȯḟ ṫẏṗïṅġ ẇïṫḥ ṫḥïṡ ḳėẏḃȯȧṙḋ ṁȧṗ.
    Ukelele File
  • British - Mathematical : This uses mathematical versions of the standard lating characters and are above 0xFFFF, in this case in plane 1 in Unicode. 𝖧𝖾𝗋𝖾 𝗂𝗌 𝖺𝗇 𝖾𝗑𝖺𝗆𝗉𝗅𝖾 𝗈𝖿 𝗍𝗒𝗉𝗂𝗇𝗀 𝗐𝗂𝗍𝗁 𝗍𝗁𝗂𝗌 𝗄𝖾𝗒𝖻𝗈𝖺𝗋𝖽 𝗆𝖺𝗉.
    Ukelele File

Character encoding on requests and Sakai

So an issue has come to light in our local Sakai deployment with character encodings. Some request were being incorrectly interpreted to be encoded using ISO-8859-1 instead of UTF-8. Before I explain what was going on here is some background.

HTML specification

The HTML specification has stuff about encodings and the short of it is that if you are making a request to a web server using a GET then you shouldn't have any foreign characters in the URL, you should just be using ASCII. In practice you can use foreign characters if you UTF-8 encode them as that's commonly assumed to be the used encoding by browsers, but it's not part of the standard. For example here is Google Chrome displaying a URL with UTF-8 encoded characters at the end.
The URL in the referencing page ends 096/%E1%B8%9F%C8%AF%E1%B9%99%C3%AF%C4%97%C4%A1%E1%B9%85.%E1%B9%AB%E1%BA%8B%E1%B9%AB which is the URL encoded version of the UTF-8 characters "096/ḟȯṙïėġṅ.ṫẋṫ".
If you have a browser making POSTs to the server, then you have a choice of two ways of submitting the data, application/x-www-form-urlencoded (which is the default on a
tag) ormultipart/form-data. If you are using characters in your form outside ASCII then you should use multipart/form-data as browsers don't typically say what encoding they are using when performing application/x-www-form-urlencoded, although lots of people assume it to be UTF-8.

Servlet request decoding.

When a request comes in to Tomcat a HttpServletRequest object is built and this includes the raw request path as well as a decoded one. Commonly containers such as Tomcat will use ISO-8859-1 to decode the path although this can be overridden in configuration. If the request is a POST and the content type is application/x-www-form-urlencoded then the container must also make the form data available as parameters and it will decode any characters using the character set supplied by the browser, however most browsers don't appear to sent a character set when submitting urlencoded POSTs and so it falls back to the containers default which in the case of Tomcat is ISO-8859-1, this can be overridden by calling ServletRequest.setCharacterEncoding(String).

If a request is a POST and the content type is multipart/form-data then the container doesn't do any decoding and it's up to the application to decode the body of the request and extract any parameters from it. This is one reason why people adopt application/x-www-form-urlencoded forms as it means they don't have to deal with parsing the requests, although there are lots of frameworks that help with this.

Part of the reason for only having the container decode application/x-www-form-urlencoded requests is that multipart/form-data are often used when file uploads are performed and may be large in which case you have to be careful about when you consume the upload and where you put the data.

Sakai and character sets

So Sakai supports unicode and uses UTF-8 as it's default encoding. But it does this through configuring all requests to use UTF-8. So in the Tomcat connector configuration the URI encoding is specified to be UTF-8 instead of ISO-8859-1 and the Sakai request filter which preprocesses all requests to Sakai (RequestFilter) sets the request encoding (if not already set) to be UTF-8 for any URL encoded form submissions.

This means that you can create a form in Sakai and leave the encoding as application/x-www-form-urlencoded and because the RequestFilter sets the encoding to UTF-8 everything works, really this is a bug and the form should be changed to use the correct encoding but as it generally works nobody notices. The more technically correct solution would be to have the original form submission made using multipart/form-data as this way you normally get the character encoding used by the browser in submitting the request.

Back to the problem...

We had a filter that was doing some authentication (OAuth) before the standard Sakai request filter. It was all working correctly but we started seeing bugs when people submitted some requests with foreign characters in them. After some investigation it turned out that the cause was the OAuth filter and through it's inspection of the request parameters.

The OAuth filter needs to look at the request parameters to extract any authentication information but in doing so it causes the servlet container to decode all the request paramaters. By default (and following the spec) a servlet container will decode URL encoded parameters according to the ISO-8859-1 character set. Once decoded the parameters remain decoded with that initial character set.

References

Monday, October 29, 2012

Allow Cyberduck to login to Amazon S3

If you have created a user in the Amazon IAM Console and wish to allow them to use Cyberduck to connect to Amazon S3 you need to grant them the ability to list all the buckets. This is done by applying a policy on the user of:


{
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "s3:ListAllMyBuckets"
      ],
      "Resource": "arn:aws:s3:::*"
    }
  ]
}


This means they can see all the buckets that exist on your account but can't actually see then contents of them without getting some policies applied on specific buckets.

This all comes about from the way Cyberduck does it's initial login to Amazon S3.

Update: If you just connect directly to the bucket using it's bucket name as part of the hostname (eg www.example.com.s3.amazonaws.com), you don't need to grant this permission but you do get an SSL issue.

Thursday, July 05, 2012

virsh vol-upload error

I was attempting to upload a file into a volume using virsh with libvirt to a qemu/kvm hypervisor storage pool and I was getting:

vol-upload error: unknown procedure: 208


Turns out this is because the remote libvirt version was 0.7.5 and upload support wasn't added until 0.9.0. Hope this helps someone else.

Tuesday, June 05, 2012

tftpd on Mac OS X

Mac OS X comes with a TFTP daemon which is all setup to use launchctl, the only issue is it's disabled by default. launchctl allows you to override the disabled entry when loading the file so to use the TFTP daemon:

sudo launchctl load -w /System/Library/LaunchDaemons/tftp.plist

Then it will serve files up from /private/tftpboot/ just make sure they are readable by all. To shutdown the daemon just run:

sudo launchctl remove com.apple.tftpd

Monday, May 28, 2012

svn diff of just modified files

I was working against a project in which I had a few conflicts, but wanted to generate a patch containing some of the changes to that project, in this case I wanted files I had modified but not the ones that had conflicts. In short svn doesn't provide a way todo this but using a few additional tools it works fine:


svn st | awk '$1 == "M"{print $2;}'| xargs svn diff


Monday, March 19, 2012

Random Skype Disconnections

Recently I'd started having problems with Skype disconnecting several minutes into a call and losing internet access in other applications as well. This initially was only happening with Skype on the iPad so I put it down to bugginess on the iOS version of Skype crashing the router or something on the iPad, however I just had the same disconnection symptoms after about 5 minutes of a video call on my Mac. This time I went to the router's administration page and found that the router (Speedtouch ST780) hadn't recently rebooted but in the event log it said:

Mar 19 22:04:10IDS dos parser : udp flood (1 of 1) : 192.168.1.69 80.229.225.26 1018 UDP 65177->45237

Now this tied in exactly with the time of the call dropping so it looks like Skype is triggering the udp flood detection on the router. There isn't a simple way to disable this through the web interface. So you have to telnet to the router and issue the commands:


ids config state=disabled
saveall


which disables the intrusion detection system. I'm not too worried about this as the router doesn't have any real services listening on it's WAN IP and I trust all the internal clients. Anyway, hopefully problem solved for now.

Saturday, December 31, 2011

General Logging in MySQL



I was looking to investigate some connection problems we were having to a MySQL database and wanted to be sure that the client was connecting to correct machine. MySQL 5.1 doesn't provide this information on it's own, but it is contained in the general log. However the log contains all statements executed against MySQL so will have a performance impact on a production machine. The general log can be enabled/disabled while MySQL is still running which means you can enable the logging, capture the events your interested in and then disable it again. Todo this connect to MySQL as a user who can set GLOBALS (typically root) and run:

mysql> SET GLOBAL general_log_file='/var/log/mysql.log';
Query OK, 0 rows affected (0.00 sec)

mysql> SET GLOBAL general_log = 1;
Query OK, 0 rows affected (0.00 sec)

Then the file /var/log/mysql.log should start filling up with statements. Once your done disable the general log:

mysql> SET GLOBAL general_log = 0;
Query OK, 0 rows affected (0.01 sec)

In my case I was looking for connection messages so a simple grep pulled out the lines I was interested in:

grep Connect /var/log/mysql.log | less

Tuesday, June 01, 2010

Using a maven SNAPSHOT plugin from repository.apache.org

I was working on a project and hitting a bug in the maven eclipse plugin which was causing the JRE to be placed incorrectly in the Eclipse .classpath. If it was just a single project I'd have gone and edited the resulting file manually but it affected a few, so I thought I'd try and grab a newer copy of the maven eclipse plugin. I did this by first adding a profile to my maven setttings.xml file ($HOME/.m2/settings.xml on UNIX).

<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

   <profiles>

     <!-- This is the additional bit you need -->
     <profile>
       <id>apache-plugin-snapshots</id>
       <pluginRepositories>
         <pluginRepository>
           <id>apache-snapshots</id>
           <name>Apache Snapshot Repository</name>
           <url>https://repository.apache.org/content/groups/snapshots-group/</url>
           <releases>
             <enabled>false</enabled>
           </releases>
           <snapshots>
             <enabled>true</enabled>
           </snapshots>
         </pluginRepository>
       </pluginRepositories>
     </profile>

   </profiles>

</settings>

and then run the mvn eclipse command:

mvn -Papache-plugin-snapshots org.apache.maven.plugins:maven-eclipse-plugin:2.9-SNAPSHOT:eclipse

which downloaded the newer plugin and then ran it against the project I had checked out.

Monday, March 23, 2009

Java host caching (sun.net.inetaddr.ttl/networkaddress.cache.ttl) on OS X

On my Mac OS X box I was trying to get some round robin DNS working from Java (1.5) but I was the host was always resolving to the same IP. I'd set sun.net.inetaddr.ttl=0, but that didn't seem to help. It seems out of the box a Mac will cache lookups, but you can clean out the cache with the command dscacheutil -flushcache

Monday, February 02, 2009

gitweb - 500 - HEAD ref not found for project

We have some git repositories here at Oxford and a gitweb interface to view them. For some of our projects I was seeing:

500 - HEAD ref not found for project

It turns out this was because the HEAD file ( {git-directoy}/HEAD ) was pointing to ref: refs/heads/master and these projects didn't have a master branch. Changing the HEAD file to point to the correct branch fixed gitweb.

Thursday, January 22, 2009

Trimpath error "context._MODIFIERS is undefined"

I was doing a little JavaScript templating with Trimpath today and at one point I was getting a nice error in my console of:

context._MODIFIERS is undefined

Looking through the trimpath code it turned out this was because I was passing in a string rather than an object as my context for the generation of the template. So I was effectively doing:

TrimPath.processDOMTemplate("someNodeId", "some string");

rather than:

TrimPath.processDOMTemplate("someNodeId", {key: "value", otherKey: "value"});

In my case I was getting the error because I forgot to eval some JSON before passing it to trimpath.

Wednesday, December 24, 2008

Pooling in Sakai LDAP

Here at Oxford we connect to a LDAP server to get details about users for our Sakai service. Previously we had reports of some pages taking a long time to load when lots of users details where requested from the LDAP. One obvious change we could make was to switch to use pooling on our connections to the LDAP server. To check that this was improving the performance I wrote a little test case which attempts to get details about 100 users individually from the LDAP.
  • Without pooling - 100 users in 10 sec = 10 users/sec.
  • With pooling - 100 users in 5 secs = 20 users/sec.
This is a nice doubling of performance, although I was still expecting to see better performance generally and while the test is running the local load on the machine isn't very high so I suspect any further performance improvements will have to come on the server.

Tuesday, December 23, 2008

Tomcat UNIX aliases

To make managing development instances of tomcat a little easier I have a few bash aliases (you can add these lines to your ~/.bash_profile):
alias tchome='export CATALINA_HOME=`pwd`; echo CATALINA_HOME=$CATALINA_HOME'
alias tctail='tail -f ${CATALINA_HOME}/logs/catalina.out'
alias tcless='less ${CATALINA_HOME}/logs/catalina.out'
alias tcshow='echo ${CATALINA_HOME}'
alias tc='${CATALINA_HOME}/bin/catalina.sh'
so my typical workflow is cd {tomcat-directory}, tchome and then I move back around the filesystem and use tc start and tctail.

Monday, September 29, 2008

Keep on rolling...

Well after a quite a few weeks of waiting my new set of wheels turned up. A Brompton S6L in apple green. The gears needs a little adjusting but otherwise it's a great ride.