2 This document was created by Steven Baltakatei Sandoval on
 
   3 ~2020-06-29T12:14Z~ under a [[https://creativecommons.org/licenses/by-sa/4.0/][Creative Commons BY-SA 4.0 license]]. It
 
   4 was updated by Steven Baltakatei Sandoval on ~2020-07-12T21:04Z~
 
   5 #+TITLE: Ninfacyzga-01 Manual
 
   6 #+AUTHOR: Steven Baltakatei Sandoval
 
   7 #+EMAIL: baltakatei@gmail.com
 
   9 Ninfacyzga-01 records (logs) its position in time and space using a
 
  10 [[https://en.wikipedia.org/wiki/Satellite_navigation_device][GPS receiver]]. The NMEA location data produced by the receiver is
 
  11 converted into the more commonly used GPS data storage formats of GPX
 
  12 and KML. All three types of data are then compressed and encrypted
 
  13 against a set of public keys. The encrypted data is then written to
 
  14 disk. Data produced by the receiver is segmented into 60-second chunks
 
  15 before being processed and written to disk.
 
  18 **** Raspberry Pi Zero W
 
  19 See the [[https://www.raspberrypi.org/pi-zero-w/][OEM]] webpage for this product.
 
  21 See the [[https://alchemy-power.com/piz-uptime-2-0/][OEM]] webpage for this product.
 
  23 ~bklog~ : A bash script that saves its stdin stream to a tar file. The
 
  24 file may be compressed by ~gzip~ and encrypted by ~age~. It is an
 
  25 executable file contained within this repository at ~exec/bklog~. It
 
  26 should be copied to ~$HOME/.local/bin~.
 
  28 ~bkgpslog~ : A legacy bash script similar to ~bklog~ but narrower in
 
  29 scope in that it only records output from ~gpspipe~.
 
  31 ~gpsd~ : A background daemon app capable of interfacing with the
 
  32 Ozzmaker BerryGPS-IMU's GPS submodule. Installed and initialized by
 
  35 ~gpspipe~ : A command line app that polls ~gpsd~ and produces a stream
 
  36 stdout consisting of GPS data lines in NMEA format. Installed via
 
  39 ~gpsbabel~ : A command line app that converts GPS data from one format
 
  40 into another. ~bklog~ may be used to convert NMEA data into GPX and
 
  41 KML. Installed via ~apt~.
 
  43 ~gzip~ : A simple command line app that compresses stdin into a
 
  44 smaller stdout stream.
 
  46 ~age~ : A simple command line app that encrypts stdin against public
 
  47 keys specified in its options. Produces encrypted stdout. Is an
 
  48 executable file contained within this repository at ~exec/age~. It
 
  49 should be copied to ~$HOME/.local/bin~.
 
  52 ~bklog~ may be used to log location data by receiving stdout produced
 
  53 by ~gpspipe~. ~bklog~ contains options that allow file writes to be
 
  54 performed at adjustable time intervals (default: 300 seconds),
 
  55 compressed (with ~gzip~), and encrypted (with ~age~). Files are
 
  56 written in the form of appendages to a ~tar~ archive saved to a
 
  57 specified location. The NMEA data produced by ~gpspipe~ may be
 
  58 processed via a ~gpsbabel~ command string specified as an option to
 
  59 ~bklog~, assuming ~gpsbabel~ is installed.
 
  63 Several output file formats have been tested with ~bklog~.
 
  67 See the [[https://en.wikipedia.org/wiki/NMEA_0183][Wikipedia page]] for this. This file format is the default
 
  68 output of the ~gpspipe -r~ command.
 
  70 See the [[https://en.wikipedia.org/wiki/GPS_Exchange_Format][Wikipedia page]] for this. [[http://wiki.gis.com/wiki/index.php/WGS84][WGS84]] is the datum used. An NMEA file
 
  71 may be converted to this format using ~gpsbabel~.
 
  73 See the [[https://en.wikipedia.org/wiki/Keyhole_Markup_Language][Wikipedia page]] for this. [[http://wiki.gis.com/wiki/index.php/WGS84][WGS84]] is the datum used. An NMEA file
 
  74 may be converted to this format using ~gpsbabel~.
 
  76 **** Encryption Method
 
  77 Files produced by the bkgpslog script are encrypted against a set of
 
  78 public keys using [[https://github.com/FiloSottile/age][~age~]], a simple command line encryption tool
 
  79 selected over ~gpg~ because of ~age~'s deliberate lack of
 
  82 The public keys are bech32 strings supplied as options to bkgpslog
 
  83 when called. The secret key should *NOT* be stored in Ninfacyzga-01.
 
  85 If a key pair was generated using ~age-keygen~, then it is an [[https://en.wikipedia.org/wiki/Curve25519][~X25519~]]
 
  86 key pair. See the [[https://age-encryption.org/v1][~age~ Version 1 specification]].
 
  88 An ~ssh-rsa~ or ~ssh-ed25519~ SSH public key string may be used instead of
 
  89 the bech32 public key string produced by ~age-keygen~ for convenience.
 
  91 Help information for ~age~ is available by running ~$ age --help~.
 
  92 ***** Encryption Commands
 
  93 ****** Encryption through ~age~
 
  94 In order to illustrate how ~bklog~ encrypts files, below is an example
 
  95 command illustrating how ~age~ may be used to encrypt a file.
 
  98 $ echo "asdf" | age -r \
 
  99 age1kza7pfshy7xwygf9349zgmk7x53mquvedgw9r98qwyyqhssh830qqjzlsw \
 
 100 > "$HOME/secret_file"
 
 103 The resulting ~secret-file~ is a binary blob with a plaintext header
 
 104 indicating how the blob was encrypted (which version of age was used,
 
 105 which public key was used).
 
 107 ****** Encryption through ~bklog~
 
 108 ~bklog~ may instructed to encrypt files via the ~-e~ and ~-r [pubkey
 
 109 string]~ options. An example is shown below:
 
 112 $ gpspipe -r | bklog -e \
 
 113 -r age1kza7pfshy7xwygf9349zgmk7x53mquvedgw9r98qwyyqhssh830qqjzlsw \
 
 114 -r age1ce3pvzrqfcn2pc6zqzglc8ac8yjk3fzukpy08cesqjjwns53xywqmaq7xw \
 
 115 -r age1pu5usxm743sx7rf22985xv2f4s0luzv6r6yx4fa7p8c2zyvp9fvqus2xr5 \
 
 119 ~bklog~ may be instructed via the ~-e~ and ~-R~ options to watch a
 
 120 directory in order to locate public key strings in its files. ~bklog~
 
 121 reads the first line of each file and interprets it as a public key
 
 124 In this example, the strings beginning with ~age1...~ are
 
 125 bech32-formatted public key strings. Please see the [[*Key Generation][Key Generation]]
 
 126 section for an explanation.
 
 128 Since ~age~ also accepts ~ssh~ public key strings, these may also be
 
 129 used if they are of the following form (no comment).
 
 131 : ssh-rsa AAAAB3NzaC1yc2EAAAADAQABA…AACAQDLnJbPs7CjwPT+OxXd
 
 133 ***** Decryption Commands
 
 134 Files may be decrypted using a command similar to:
 
 137 cat location.gpx.age | age -d -i key.txt > location.gpx
 
 140 The version of ~age~ used to perform the encryption 
 
 142 ** Operating Procedures
 
 144 See OEM (Ozzmaker) [[https://ozzmaker.com/berrygps-berrygps-imu-quick-start-guide/][quickstart guide for the BerryGPS-IMU]].
 
 148 BerryGPS-IMU must be electrically connected to the correct pins on the
 
 149 GPIO header of a Raspberry Pi Zero W.
 
 151 *Optional*: stack together with PiZ Uptime 2.0 module. No GPIO pins
 
 152 conflict so a simple stacking and soldering with long header pins is
 
 156 ***** Install Executables
 
 158 Install Raspbian 10 Buster onto an SD card image. See the Raspberry Pi
 
 159 Foundation [[https://www.raspberrypi.org/documentation/installation/installing-images/README.md][installation instructions]]. Configure WiFi to permit log
 
 160 file transfer. Configure SSH to permit remote administration via the
 
 161 command line interface.
 
 163 Make sure to install the ~unattended-upgrades~ package to make sure
 
 164 the latest security patches for packages are installed. See [[https://linux-audit.com/using-unattended-upgrades-on-debian-and-ubuntu/][this page]]
 
 165 for a description of how ~unattended-upgrades~ works.
 
 167 Install ~gpsd~, ~gpspipe~, ~git~, and this repository for location
 
 170 Install ~syncthing~ for log file transfer capability.
 
 172 Place ~age~ binary (the one compiled for ARM CPU architecture for
 
 173 Linux) in ~$HOME/.local/bin~.
 
 175 ***** Disable Swap File
 
 176 Since standard Raspbian 10 (Buster) install involves copying
 
 177 unencrypted file system image to SD card which is mounted by the
 
 178 Raspberry Pi, system memory may be written to disk in the form of a
 
 179 Swap file as described [[https://ideaheap.com/2013/07/stopping-sd-card-corruption-on-a-raspberry-pi/][here]]. In order to reduce the chance that
 
 180 location log data is ever written to disk, swap file functionality
 
 181 must be disabled[fn:ideaheap_20130731_disableswap].
 
 183 Raspbian 10 uses dphys-swapfile to manage a swap file. It may be
 
 184 disabled persistently[fn:rpf_20190702_disableswappersist] by running
 
 185 the following command:
 
 187 : sudo systemctl disable dphys-swapfile.service
 
 189 To view the status of the swap file in Raspbian 10, run ~free -m~:
 
 192 pi@ninfacyzga-01:~$ free -m
 
 193           total    used    free  shared  buff/cache   available
 
 194 Mem:        432      86      36      21         309         268
 
 198 After disabling the swap file and rebooting:
 
 201 pi@ninfacyzga-01:~$ free -m
 
 202           total    used    free  shared  buff/cache   available
 
 203 Mem:        432      89     214       3         128         289
 
 207 [fn:ideaheap_20130731_disableswap] Explanation:
 
 208 https://ideaheap.com/2013/07/stopping-sd-card-corruption-on-a-raspberry-pi/
 
 210 [fn:rpf_20190702_disableswappersist] Persistant disabling of swap in
 
 212 https://www.raspberrypi.org/forums/viewtopic.php?p=1490692&sid=5c596a124b7805d6b10dab8d3d7caf16#p1490692
 
 214 ***** Automatic Start Configuration
 
 216 It is recommended to create a daily ~cron~ job that executes a bash
 
 217 script that starts ~bklog~ jobs to record location along with other
 
 218 types of data recording. An example of such a script is below:
 
 224 gpspipe -r | /bin/bash "$HOME/.local/bin/bklog" -c -e -z "UTC" -t "/dev/shm" \
 
 225           -r age1kza7pfshy7xwygf9349zgmk7x53mquvedgw9r98qwyyqhssh830qqjzlsw \
 
 226           -r age1ce3pvzrqfcn2pc6zqzglc8ac8yjk3fzukpy08cesqjjwns53xywqmaq7xw \
 
 227           -R "$HOME/.config/bklog/recipients" -w ".nmea" -b "300" -B "day" \
 
 228           -o "$HOME/Sync/Evanescent_Location" -l "location" \
 
 229           -p "gpsbabel -i nmea -f - -o gpx -F - " ".gpx" \
 
 230           -p "gpsbabel -i nmea -f - -o kml -F - " ".kml" &
 
 232 python ~/.local/bin/bmp388.py | /bin/bash "$HOME/git-OC/ninfacyzga-01/exec/bklog" \
 
 233           -c -e -z "UTC" -t "/dev/shm" \
 
 234           -r age1kza7pfshy7xwygf9349zgmk7x53mquvedgw9r98qwyyqhssh830qqjzlsw \
 
 235           -r age1ce3pvzrqfcn2pc6zqzglc8ac8yjk3fzukpy08cesqjjwns53xywqmaq7xw \
 
 236           -R "$HOME/.config/bklog/recipients" -w ".txt" -b "300" -B "day" \
 
 237           -o "$HOME/Sync/Evanescent_Location" -l "pressure" &
 
 240 This script, if it were saved at
 
 241 ~"$HOME/.local/bin/cron/dailylog.sh"~ would then be added as a
 
 242 line in the ~crontab~ file as shown below:
 
 246 0 0 * * * /bin/bash ~/.local/bin/cron/dailylog.sh
 
 247 @reboot /bin/bash ~/.local/bin/cron/dailylog.sh
 
 250 In the example script, the options are:
 
 252 : -c : tells bklog to compress output
 
 253 : -e : tells bklog log to encrypt output
 
 254 : -r : tells bklog to interpret the next argument as a pubkey string
 
 255 : -R : tells bklog to interpret the next argument as a directory
 
 256 :        where public keys may be found (first line of each file is
 
 258 : -o : tells bklog to write output files to the directory represented
 
 259 : -t : tells bklog to interpret the next argument as a directory
 
 260 :        for storing temporary files
 
 261 :        by the next argument
 
 262 : -p : tells bklog a command string through which output is piped
 
 263 :        before being compressed and encrypted. Also expected is a
 
 264 :        file extension to be appended before the compression and
 
 265 :        encryption file extensions.
 
 266 : -w : tells bklog to save the unprocessed stdin with a specified
 
 267 :        file extension (instead of the default '.stdin').
 
 268 : -b : tells bklog how long each buffer round (time between file
 
 269 :        writes) lasts in seconds.
 
 270 : -B : specifies the time-to-live for the bklog script. A valid value may
 
 271 :        one of the time elements such as "day" or "hour".
 
 272 : -l : specfies a custom string to be used in output file names to
 
 273 :        help differentiate tar files produced via bklog from different
 
 275 : -z : specifies a time zone to be used to determine the script time-to-live.
 
 276 :        By default, bklog uses whatever time is specified by the TZ
 
 277 :        environment variable.
 
 279 ***** Log Transfer Configuration
 
 280 Log files may be shared to other machines via ~syncthing~. See [[https://docs.syncthing.net/][this]]
 
 281 manual for how to set up a shared folder and add Ninfacyzga-01 as a
 
 282 device. Syncthing's directory synchronization capability allows a
 
 283 remote machine to delete files from Ninfacyzga-01 by deleting from the
 
 284 shared folder that they both share.
 
 286 When log files are removed from Ninfacyzga-01 is not within the scope
 
 290 An ~age~ encryption key may be generated like so:
 
 292 $ umask          # Gets current umask
 
 293 0022             # Note: This is the default umask for Raspbian 10
 
 294 $ umask 066      # So key.txt will have no perms except for owner (you)
 
 295 $ umask          # Confirm umask set to 066
 
 297 $ age-keygen > key.txt
 
 298 Public key: age1pu5usxm743sx7rf22985xv2f4s0luzv6r6yx4fa7p8c2zyvp9fvqus2xr5
 
 300 -rw------- 1 baltakatei baltakatei 184 Jun 29 18:28 key.txt
 
 301 $ umask 0022     # Return umask to default value
 
 306 The resulting public/private keypair data looks like:
 
 309 # created: 2020-06-29T18:01:56Z
 
 310 # public key: age1pu5usxm743sx7rf22985xv2f4s0luzv6r6yx4fa7p8c2zyvp9fvqus2xr5
 
 311 AGE-SECRET-KEY-1NEUU5U2XGZGL9UYWNPU5DL99TGJJHFSN4F2E2WCCSDJJ6L5ZMLESNTVTU0
 
 314 The file ~key.txt~ is not password-protected by default and should be
 
 315 secured like an SSH public key should. The ~$ umask 066~ command run
 
 316 before the ~$ age-keygen > key.txt~ command ensures ~key.txt~ will not
 
 317 be readable, writeable, or executable to anyone except the owner
 
 321 Turn on Ninfacyzga-01 by supplying 5VDC power to the Raspberry Pi. No
 
 322 further interaction should be required.
 
 324 No interaction beyond continually supplying approximately 100mA of
 
 325 5VDC power and occasionally removing log files to conserve disk space
 
 328 Log files may be transferred by use of ~syncthing~ shared folders.
 
 329 **** Automatic Updates
 
 330 The ~automatic-upgrades~ package, if installed, should automatically
 
 331 install security patches to packages installed via ~apt~.
 
 333 The system may be shutdown via SSH by running:
 
 335 : $ sudo shutdown -r 0
 
 337 *** Unscheduled Shutdown
 
 338 Ninfacyzga-01 as described and setup should tolerate unscheduled power
 
 339 loss. Log files being written every 60 seconds means, at most, 60
 
 340 seconds worth of location data may be lost.
 
 341 *** End of Life Disposal
 
 342 LiPo batteries used by the PiZ Uptime 2.0 module should be disposed of
 
 343 properly with their potential ignitability in mind, especially if they
 
 344 are not fully discharged.
 
 346 Consult your local municipality for its "E-Waste Disposal" (or
 
 347 equivalent) policy. Metals used in the Raspberry Pi and related
 
 348 components may be recycled.
 
 350 Take extra precuation if lead solder was used in assembling the
 
 351 electronics. Consumer electronics in early 21st century should use