Creating a toggle for netatalk on the iPhone
Netatalk on the iPhone is great, but what if you want to turn it off. Using Bossprefs it is easy to create a toggle which allows you to control the state of your netatalk daemon.
For this to work you need a jailbroken iphone with netatalk, openssh, and Bossprefs installed, all avaialable through Cydia. I would also suggest grabbing insomnia to stop your iphone going to sleep (or change the settings in system preferences).
With you iphone and computer on the same Wifi network ssh to you iphone as root, or as mobile and su to root. Go to the Bossprefs directory scripts:
# cd /var/stash/Applications/Bossprefs/scripts
On the 2.1 firmware there may be a bunch of random characters in the name of the Applications folder, eg. ‘Applications.IYKeBo’.
We need to create 2 scripts to start and stop the netatalk server in this directory.
afp_start.sh:
#!/bin/sh launchctl load -w /System/Library/LaunchDaemons/net.sourceforge.netatalk.afpd.plist launchctl load -w /System/Library/LaunchDaemons/com.saurik.afpd.dns-sd.plist
afp_stop.sh:
#!/bin/sh launchctl unload -w /System/Library/LaunchDaemons/net.sourceforge.netatalk.afpd.plist launchctl unload -w /System/Library/LaunchDaemons/com.saurik.afpd.dns-sd.plist
Now make them executable:
# chmod +x afp_start.sh afp_stop.sh
These scripts will start and stop the daemons for netatalk which normally happen at startup and shutdown.
The ‘-w’ flag in load removes the ‘Disabled’ tag if there, in unload it adds it if not there. What this effectively does is keeps the state of afp the same after a restart. If you have unloaded the daemon it will not start in restart, if it was running it will start on restart.
Now we need to create a service for Bossprefs. Move to the ‘service’ directory.
# cd ../services
Create a file called ‘AFP’:
/var/stash/Applications/BossPrefs.app/scripts/afp_start.sh /var/stash/Applications/BossPrefs.app/scripts/afp_stop.sh afpd /var/stash/Applications/BossPrefs.app/afp.png
For a bit of explanation.
- The first line in the line used to turn on the service.
- The second line turns off the service.
- The third line is the daemon which Bossprefs will look for to determine whether the service is running.
- The fourth line is of course the icon.
Fire up Bossprefs and now you can control your netatalk daemon.
Posted in Uncategorized
September 20th, 2008 at 11:12 am
Thanks for the description. A few notes… you might add, that for editing and creating files on the iPhone, one could use nano. Furthermore it didn’t work for me the first time – the problem was, that copying and pasting with firefox resulted in some additional line breaks in the scripts. After fixing them, it worked like a charm…
September 20th, 2008 at 11:18 am
Ah and on my 2.1 iPhone I did have a simple /var/stash/Application path without additional letters…
September 23rd, 2008 at 9:10 pm
When trying the afp_stop command, I am getting the following errors in syslog:
Sep 23 12:58:44 Mark-s-iPhone UIKitApplication:com.bossprefs.bigboss[0xf2f1][747]: launchctl: CFURLWriteDataAndPropertiesToResource(/System/Library/LaunchDaemons/net.sourceforge.netatalk.afpd.plist) failed: -10
Sep 23 12:58:44 Mark-s-iPhone UIKitApplication:com.bossprefs.bigboss[0xf2f1][747]: launchctl: Error unloading: net.sourceforge.netatalk.afpd
Sep 23 12:58:44 Mark-s-iPhone UIKitApplication:com.bossprefs.bigboss[0xf2f1][747]: launchctl: CFURLWriteDataAndPropertiesToResource(/System/Library/LaunchDaemons/com.saurik.afpd.dns-sd.plist) failed: -10
Sep 23 12:58:44 Mark-s-iPhone UIKitApplication:com.bossprefs.bigboss[0xf2f1][747]: launchctl: Error unloading: com.saurik.afpd.dns-sd
I believe this is because launchctl is not being run as root. Any ideas why the script works for you and not for me?
September 24th, 2008 at 10:39 am
If start is working, I can only assume that it is an error in the afp_stop.sh script. If it was an error in both scripts I would look for permissions (maybe).
Have a look for rogue spaces in in the launchctl lines, especially in the /System/Library …. paths.
Hopefully you can nail it. Can you post the contents of the file to a comment, or email me the script – robg (at) sumostyle (dot) net.
September 25th, 2008 at 10:54 am
Have you tried to run this as root from an ssh session to see it if works?
September 26th, 2008 at 4:20 am
[...] mobile AppleVolumes All going well when you restart netatalk (I recommend installing a toggle switch for it in BossPrefs) and connect to it using ‘u:mobile p:alpine’ you should see 3 volumes [...]
September 26th, 2008 at 6:35 pm
Wow, it works great. Thank you so much for such a detailed write-up.
October 1st, 2008 at 11:32 pm
Rob,
Sorry for the late response. The scripts worked fine when run as su in a terminal session on the phone. So I tried setting SUID as root for both scripts using:
chown root afp_start.sh
chwon root aftp_stop.sh
chmod 4755 afp_start.sh
chmod 4755 afp_stop.sh
Now all is well! The switch works perfectly!
Thanks
October 2nd, 2008 at 12:02 am
Spoke too soon. Both scripts work fine when run as root (using su) in the terminal on the iphone. Tried setting SUID root as already described. If I then run them from Terminal on the iphone logged in as mobile (i.e. not root), I get the same errors I got before. So launchctl is still not being run as root even though the script is SUID root. I don’t get it… I’m not a unix / linux boffin but I don’t understand why I am getting this error when no one else seems to be. Very annoying… any ideas?
October 2nd, 2008 at 11:53 pm
[...] but also a security measure. You’ll need Boss Prefs and write a plugin as detailed in this post. « Remember iPod touch Apps’ Location in [...]
October 3rd, 2008 at 9:28 pm
Have narrowed this down a bit. The -w flag requires launchctl to add or remove the Disabled flag from the plist file. If launchctl is not run as root, it is unable to write to the plist file and so the script fails (at least it does on my iphone). If I run the above commands as user mobile without the -w flag, they work with no errors. The only time I can get the above scripts (including the -w flag) to work with no errors is if I change to the root user using su and then run them. No amount of changing of SUID or chmod or whatever seems to have any impact. I’m at a loss here and would appreciate any and all help to get this working.
October 3rd, 2008 at 10:46 pm
Final post on this… I have found a solution. Launchctl had to be run as root. This could be done in one of three different ways:
1) SETUID root on /bin/launchctl (not so good from a security perspective I think)
2) Use the copy of launchctl which is in the BossPrefs.app directory which is already SETUID root; or
3) Make the script run as SETUID root – this was where I was going wrong. The script has to include absolute paths to each command and has to have permissions so that the owner (root) has Read/Write/Execute and all others i.e. Group and Others have only Execute. When I did this the script worked fine when run from BossPrefs. Oddly enough, when I ran it from Terminal with user mobile I got “Permission Denied” but I don’t care … my switch works now. Still not totally sure I know what is going on… but there you go… maybe this will help someone else.
October 7th, 2008 at 8:05 pm
[...] but also as a safe guard to unauthorised access. For that, we will install a plugin written by Rob Garth for use in [...]
October 10th, 2008 at 7:31 am
I have just purchased an iPhone3G and ran through the whole procedure of jailbreaking and installing Netatalk via Cynthia. I then installed Boss Prefs and your files. But nothing seemed to work, even though I got the switch in the Boss Prefs GUI. But I had a problem: Every time I switched AFP off and quit Boss Prefs, saw that my iPhone was still showing up in the Finder, and opened Boss Prefs again, the AFP switch was on again. I then made a test via SSH: manually checked that I could stop and start Netatalk via the start & stop scripts. This worked fine! I then concluded that there was something screwed with the service file “AFP\ (Netatalk)” and indeed: the paths were all incorrect. For example:
/var/stash/Applications/BossPrefs.app/scripts/afp_start.sh
At least on my iPhone the correct path is /Applications/BossPrefs.app/scripts/afp_start.sh
I changed the path and I can toggle!
Works!
Regards,
Davidw
October 17th, 2008 at 6:51 am
Hi, just one thing about the execute permissions: THEY ARE NOT REQUIRED!
To avoid all the mess with the permissions, all that you have to do is to put at “sh ” before the scripts at the services on BossPrefs.app directory.
Basicaly, change this:
/var/stash/Applications/BossPrefs.app/scripts/afp_start.sh
/var/stash/Applications/BossPrefs.app/scripts/afp_stop.sh
afpd
/var/stash/Applications/BossPrefs.app/afp.png
For this:
sh /var/stash/Applications/BossPrefs.app/scripts/afp_start.sh
sh /var/stash/Applications/BossPrefs.app/scripts/afp_stop.sh
afpd
/var/stash/Applications/BossPrefs.app/afp.png
I hope it helps. I particularly don’t like to mess with permissions, so, that is the solution I came up with.
Morph21
October 24th, 2008 at 2:27 am
I’ve implemented all of the suggestions above, but it’s still not working for me. I can run the scripts as mobile, and they turn the afp service on and off as they should. And the entry in the Services directory is perfect. But when I try to run the toggle in BossPrefs, nothing happens.
October 24th, 2008 at 2:30 am
Wait a minute, scratch that last comment. After I rebooted the iTouch a couple of times, the toggle is now working correctly. I have no idea why, but as long as it’s working, I’m happy.
October 24th, 2008 at 11:28 pm
Install worked fine, toggle works great thankyou!
But I’m left with another service called “.AppleDouble” in BossPrefs… I cant find it anywhere by trawling through the directories on my iPhone… its not in the services dir in BossPrefs.app/servies, so I have no idea where its coming from. Any help?
October 26th, 2008 at 9:21 pm
You’ll probably find that it is a hidden file in the BossPrefs services directory. The apple finder has put it there. Do an “ls -a” and you should see it in a terminal. Just remove the file with a “rm .AppleDouble”
October 29th, 2008 at 9:08 pm
thankyou! i didnt know of the -a argument to see the extra-hidden files
for anyone else getting this problem and is a terminal-challenged, you’ll need to “rm -r” to recursively remove the directory .AppleDouble
November 30th, 2008 at 11:27 am
Rob, just a quick thanks! Wow, that works great. I, like a few others had some issues, but made sure the scripts were correct and then read through the comments again.
Sure enough, Morph21′s suggestion to slightly modify two lines in the services file did the trick. This seemed much better than trying to modify permissions or open potential security holes.
BTW, this is on an unlocked and jailbroken 3G (from HK), recently upgraded to 2.2.
Thanks!
December 1st, 2008 at 9:02 am
Sorry, but I need to comment. Changing the permissions of a script you write to executable and read-only is not a security risk. Well no more than throwing a sh in front of it. Changing the permission of a system file might be silly. But you created these files.
Permissions are not scary, the a feature of a unix based OS to be used.
December 14th, 2008 at 2:24 pm
i cant change to that directory because i see var@ instead of var/
what does that mean?
December 14th, 2008 at 2:55 pm
NEVERmind! i got it….. now i am having toruble following the creation of files….
December 14th, 2008 at 4:01 pm
great, somehow
i effed up netatalk and my iphone doesnt show up in my finder anymore…. im sad…
December 15th, 2008 at 9:36 pm
A reinstall of netatalk from cydia should clobber any stuff ups. I use vi to edit/create files, but that is what I am use to. Try nano, it is available in cydia and is a pretty simple terminal based text editor.
May 13th, 2009 at 9:32 pm
Thanks, works perfectly. I use SBSettings instead, so I didn’t know how to make a little widget, but I just run it in terminal. I’ve don’t use netatalk much anyway, but it is a good thing to have, just incase i need it, but 90% of the time, it will be turned off.
July 4th, 2009 at 9:36 am
Hola!
I tried installing netatalk using Cydia… but i get an error after cydia finishes the installation:
launchctl: Couldn’t
stat(“/System/Library/LaunchDaemons/net.sourceforge.netatalk.afpd.plist”):No such file or directory
nothing found to unload
launchctl: Couldn’t
stat(“/System/Library/LaunchDaemons/net.sourceforge.netatalk.afpd.plist”):No such file or directory
nothing found to load
I have SSHed my iphone and i could not find such file there,
any idead how do i get a copy of it? or maybe some of you could post the contents of the file so i can create it on my iphone.
I have looked on the web but i could not find anything related to this problem.
Thanks so much!
July 6th, 2009 at 10:36 am
Have you tried mroe than once using Cydia? I have installs fail with a flaky connection before.
July 7th, 2009 at 11:55 am
Yes i have tried that but netatalk fails to install the missing plist file. But finally i decided to just restore the phone and start from scratch… but thank you so much for the input
Now i will try and create the toggle for netatalk… i hope i dont mess up
cya!