#!/bin/dash
# Unlocks SIM K3765-Z broadband modem.
# Copyright (C) 2011 Gero Treuner
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# See
# Attention:
# - Check permissions of this script, so that your PIN is not revealed.
# - Replace the string DEVICE_SERIAL with the serial number of your
# device, which is checked before operation.
# - Replace the string DEVICE_PIN with your PIN, or you may risk to
# exceed the SIM retry counter and require to enter the PUK!
# Permanent lock prevention:
# The script stores a sane result in the file $VARRUNDIR/accepted .
# It continues operation until this file is removed. The reason is to
# prevent further trying, otherwise after 3 failed attempts the SIM
# could lock permanently and require a PUK. To initially start
# operation call this script with the "-force" option.
VARRUNDIR=/var/run/k3765-z_unlock
PATH=/usr/bin:/bin:/usr/sbin
TTY=/dev/ttyUSB1
DEVICE_SERIAL=
DEVICE_PIN=
DEBUG=false
if ! test -d $VARRUNDIR
then
echo "The required directory $VARRUNDIR does not exist. Exiting."
exit 1
fi
# Delete enabling file on start for permanent lock prevention
if test -f $VARRUNDIR/accepted
then
rm -f $VARRUNDIR/accepted
if test -f $VARRUNDIR/accepted
then
if test "$1" = "-force"
then
echo "Could not delete control file $VARRUNDIR/accepted. Ignore..."
else
echo "Could not delete control file $VARRUNDIR/accepted. Exiting."
exit 2
fi
fi
else
if ! test "$1" = "-force"
then
echo "Control file $VARRUNDIR/accepted does not exist.\nRisk of locking device permanently (until PUK is provided). Consider to use the -force option.\nExiting."
exit 3
fi
fi
# Check existence of device
if test -z "$DEVNAME"
then
DEVNAME=$TTY
fi
if ! test -c "$DEVNAME"
then
echo "File $DEVNAME does not exist or is not of type \"character special\". Exiting."
exit 4
fi
# Chat with broadband modem
if test "$DEBUG" = true -o "$1" = "-force"
then
CHATOPTIONS=-v
fi
chat $CHATOPTIONS -t5 <$DEVNAME >$DEVNAME \
ABORT ERROR \
ABORT '+CPIN: READY' \
ABORT '+CPIN: SIM PUK' \
OK-AT-OK-AT-OK-AT-OK-AT-OK-AT-OK AT+CGSN \
$DEVICE_SERIAL AT+CPIN? \
'+CPIN: SIM PIN' AT+CPIN=${DEVICE_PIN} \
OK
# Save result
RESULT=$?
if test "$RESULT" = 0
then
echo "PIN accepted." >$VARRUNDIR/accepted
else
if test "$RESULT" = "5"
then
echo "Already unlocked." >$VARRUNDIR/accepted
else
exit 5
fi
fi