Browse Source

Merge pull request #2272 from Neilpang/dev

sync
neil 6 years ago
parent
commit
57fc5d28a9
5 changed files with 341 additions and 5 deletions
  1. 1 1
      dnsapi/dns_active24.sh
  2. 1 1
      dnsapi/dns_aws.sh
  3. 176 0
      dnsapi/dns_durabledns.sh
  4. 108 3
      notify/mail.sh
  5. 55 0
      notify/slack.sh

+ 1 - 1
dnsapi/dns_active24.sh

@@ -129,7 +129,7 @@ _active24_init() {
     return 1
     return 1
   fi
   fi
 
 
-  _saveaccountconf_mutable ACTIVE24_Token "ACTIVE24_Token"
+  _saveaccountconf_mutable ACTIVE24_Token "$ACTIVE24_Token"
 
 
   _debug "First detect the root zone"
   _debug "First detect the root zone"
   if ! _get_root "$fulldomain"; then
   if ! _get_root "$fulldomain"; then

+ 1 - 1
dnsapi/dns_aws.sh

@@ -49,7 +49,7 @@ dns_aws_add() {
   _debug _sub_domain "$_sub_domain"
   _debug _sub_domain "$_sub_domain"
   _debug _domain "$_domain"
   _debug _domain "$_domain"
 
 
-  _info "Geting existing records for $fulldomain"
+  _info "Getting existing records for $fulldomain"
   if ! aws_rest GET "2013-04-01$_domain_id/rrset" "name=$fulldomain&type=TXT"; then
   if ! aws_rest GET "2013-04-01$_domain_id/rrset" "name=$fulldomain&type=TXT"; then
     return 1
     return 1
   fi
   fi

+ 176 - 0
dnsapi/dns_durabledns.sh

@@ -0,0 +1,176 @@
+#!/usr/bin/env sh
+
+#DD_API_User="xxxxx"
+#DD_API_Key="xxxxxx"
+
+_DD_BASE="https://durabledns.com/services/dns"
+
+########  Public functions #####################
+
+#Usage: add  _acme-challenge.www.domain.com   "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
+dns_durabledns_add() {
+  fulldomain=$1
+  txtvalue=$2
+
+  DD_API_User="${DD_API_User:-$(_readaccountconf_mutable DD_API_User)}"
+  DD_API_Key="${DD_API_Key:-$(_readaccountconf_mutable DD_API_Key)}"
+  if [ -z "$DD_API_User" ] || [ -z "$DD_API_Key" ]; then
+    DD_API_User=""
+    DD_API_Key=""
+    _err "You didn't specify a durabledns api user or key yet."
+    _err "You can get yours from here https://durabledns.com/dashboard/index.php"
+    return 1
+  fi
+
+  #save the api key and email to the account conf file.
+  _saveaccountconf_mutable DD_API_User "$DD_API_User"
+  _saveaccountconf_mutable DD_API_Key "$DD_API_Key"
+
+  _debug "First detect the root zone"
+  if ! _get_root "$fulldomain"; then
+    _err "invalid domain"
+    return 1
+  fi
+
+  _debug _sub_domain "$_sub_domain"
+  _debug _domain "$_domain"
+
+  _dd_soap createRecord string zonename "$_domain." string name "$_sub_domain" string type "TXT" string data "$txtvalue" int aux 0 int ttl 10 string ddns_enabled N
+  _contains "$response" "createRecordResponse"
+}
+
+dns_durabledns_rm() {
+  fulldomain=$1
+  txtvalue=$2
+
+  DD_API_User="${DD_API_User:-$(_readaccountconf_mutable DD_API_User)}"
+  DD_API_Key="${DD_API_Key:-$(_readaccountconf_mutable DD_API_Key)}"
+  if [ -z "$DD_API_User" ] || [ -z "$DD_API_Key" ]; then
+    DD_API_User=""
+    DD_API_Key=""
+    _err "You didn't specify a durabledns api user or key yet."
+    _err "You can get yours from here https://durabledns.com/dashboard/index.php"
+    return 1
+  fi
+
+  _debug "First detect the root zone"
+  if ! _get_root "$fulldomain"; then
+    _err "invalid domain"
+    return 1
+  fi
+  _debug _sub_domain "$_sub_domain"
+  _debug _domain "$_domain"
+
+  _debug "Find record id"
+  if ! _dd_soap listRecords string zonename "$_domain."; then
+    _err "can not listRecords"
+    return 1
+  fi
+
+  subtxt="$(echo "$txtvalue" | cut -c 1-30)"
+  record="$(echo "$response" | sed 's/<item\>/#<item>/g' | tr '#' '\n' | grep ">$subtxt")"
+  _debug record "$record"
+  if [ -z "$record" ]; then
+    _err "can not find record for txtvalue" "$txtvalue"
+    _err "$response"
+    return 1
+  fi
+
+  recordid="$(echo "$record" | _egrep_o '<id xsi:type="xsd:int">[0-9]*</id>' | cut -d '>' -f 2 | cut -d '<' -f 1)"
+  _debug recordid "$recordid"
+  if [ -z "$recordid" ]; then
+    _err "can not find record id"
+    return 1
+  fi
+
+  if ! _dd_soap deleteRecord string zonename "$_domain." int id "$recordid"; then
+    _err "delete error"
+    return 1
+  fi
+
+  _contains "$response" "Success"
+}
+
+#_acme-challenge.www.domain.com
+#returns
+# _sub_domain=_acme-challenge.www
+# _domain=domain.com
+_get_root() {
+  domain=$1
+  if ! _dd_soap "listZones"; then
+    return 1
+  fi
+
+  i=1
+  p=1
+  while true; do
+    h=$(printf "%s" "$domain" | cut -d . -f $i-100)
+    _debug h "$h"
+    if [ -z "$h" ]; then
+      #not valid
+      return 1
+    fi
+
+    if _contains "$response" ">$h.</origin>"; then
+      _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
+      _domain=$h
+      return 0
+    fi
+    p=$i
+    i=$(_math "$i" + 1)
+  done
+  return 1
+
+}
+
+#method
+_dd_soap() {
+  _method="$1"
+  shift
+  _urn="${_method}wsdl"
+  # put the parameters to xml
+  body="<tns:$_method>
+      <apiuser xsi:type=\"xsd:string\">$DD_API_User</apiuser>
+      <apikey xsi:type=\"xsd:string\">$DD_API_Key</apikey>
+    "
+  while [ "$1" ]; do
+    _t="$1"
+    shift
+    _k="$1"
+    shift
+    _v="$1"
+    shift
+    body="$body<$_k xsi:type=\"xsd:$_t\">$_v</$_k>"
+  done
+  body="$body</tns:$_method>"
+  _debug2 "SOAP request ${body}"
+
+  # build SOAP XML
+  _xml='<?xml version="1.0" encoding="utf-8"?>
+<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
+xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
+xmlns:tns="urn:'$_urn'" 
+xmlns:types="urn:'$_urn'/encodedTypes" 
+xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
+xmlns:xsd="http://www.w3.org/2001/XMLSchema">
+  <soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'"$body"'</soap:Body>
+</soap:Envelope>'
+
+  _debug2 _xml "$_xml"
+  # set SOAP headers
+  _action="SOAPAction: \"urn:$_urn#$_method\""
+  _debug2 "_action" "$_action"
+  export _H1="$_action"
+  export _H2="Content-Type: text/xml; charset=utf-8"
+
+  _url="$_DD_BASE/$_method.php"
+  _debug "_url" "$_url"
+  if ! response="$(_post "${_xml}" "${_url}")"; then
+    _err "Error <$1>"
+    return 1
+  fi
+  _debug2 "response" "$response"
+  response="$(echo "$response" | tr -d "\r\n" | _egrep_o ":${_method}Response .*:${_method}Response><")"
+  _debug2 "response" "$response"
+  return 0
+}

+ 108 - 3
notify/mail.sh

@@ -1,6 +1,10 @@
 #!/usr/bin/env sh
 #!/usr/bin/env sh
 
 
-# support local mail app
+#Support local mail app
+
+#MAIL_BIN="sendmail"
+#MAIL_FROM="yyyy@gmail.com"
+#MAIL_TO="yyyy@gmail.com"
 
 
 mail_send() {
 mail_send() {
   _subject="$1"
   _subject="$1"
@@ -10,6 +14,107 @@ mail_send() {
   _debug "_content" "$_content"
   _debug "_content" "$_content"
   _debug "_statusCode" "$_statusCode"
   _debug "_statusCode" "$_statusCode"
 
 
-  _err "Not implemented yet."
-  return 1
+  MAIL_BIN="${MAIL_BIN:-$(_readaccountconf_mutable MAIL_BIN)}"
+  if [ -n "$MAIL_BIN" ] && ! _exists "$MAIL_BIN"; then
+    _err "It seems that the command $MAIL_BIN is not in path."
+    return 1
+  fi
+  _MAIL_CMD=$(_mail_cmnd)
+  if [ -n "$MAIL_BIN" ]; then
+    _saveaccountconf_mutable MAIL_BIN "$MAIL_BIN"
+  else
+    _clearaccountconf "MAIL_BIN"
+  fi
+  _MAIL_BODY=$(_mail_body)
+
+  MAIL_FROM="${MAIL_FROM:-$(_readaccountconf_mutable MAIL_FROM)}"
+  if [ -n "$MAIL_FROM" ]; then
+    if ! _contains "$MAIL_FROM" "@"; then
+      _err "It seems that the MAIL_FROM=$MAIL_FROM is not a valid email address."
+      return 1
+    fi
+
+    _saveaccountconf_mutable MAIL_FROM "$MAIL_FROM"
+  fi
+
+  MAIL_TO="${MAIL_TO:-$(_readaccountconf_mutable MAIL_TO)}"
+  if [ -n "$MAIL_TO" ]; then
+    if ! _contains "$MAIL_TO" "@"; then
+      _err "It seems that the MAIL_TO=$MAIL_TO is not a valid email address."
+      return 1
+    fi
+
+    _saveaccountconf_mutable MAIL_TO "$MAIL_TO"
+  else
+    MAIL_TO="$(_readaccountconf ACCOUNT_EMAIL)"
+    if [ -z "$MAIL_TO" ]; then
+      _err "It seems that account email is empty."
+      return 1
+    fi
+  fi
+
+  contenttype="text/plain; charset=utf-8"
+  subject="=?UTF-8?B?$(echo "$_subject" | _base64)?="
+  result=$({ echo "$_MAIL_BODY" | eval "$_MAIL_CMD"; } 2>&1)
+
+  if [ $? -ne 0 ]; then
+    _debug "mail send error."
+    _err "$result"
+    return 1
+  fi
+
+  _debug "mail send success."
+  return 0
+}
+
+_mail_cmnd() {
+  if [ -n "$MAIL_BIN" ]; then
+    _MAIL_BIN="$MAIL_BIN"
+  elif _exists "sendmail"; then
+    _MAIL_BIN="sendmail"
+  elif _exists "ssmtp"; then
+    _MAIL_BIN="ssmtp"
+  elif _exists "mutt"; then
+    _MAIL_BIN="mutt"
+  elif _exists "mail"; then
+    _MAIL_BIN="mail"
+  else
+    _err "Please install sendmail, ssmtp, mutt or mail first."
+    return 1
+  fi
+
+  case $(basename "$_MAIL_BIN") in
+    sendmail)
+      if [ -n "$MAIL_FROM" ]; then
+        echo "'$_MAIL_BIN' -f '$MAIL_FROM' '$MAIL_TO'"
+      else
+        echo "'$_MAIL_BIN' '$MAIL_TO'"
+      fi
+      ;;
+    ssmtp)
+      echo "'$_MAIL_BIN' '$MAIL_TO'"
+      ;;
+    mutt | mail)
+      echo "'$_MAIL_BIN' -s '$_subject' '$MAIL_TO'"
+      ;;
+    *)
+      _err "Command $MAIL_BIN is not supported, use sendmail, ssmtp, mutt or mail."
+      return 1
+      ;;
+  esac
+}
+
+_mail_body() {
+  if [ "$_MAIL_BIN" = "sendmail" ] || [ "$_MAIL_BIN" = "ssmtp" ]; then
+    if [ -n "$MAIL_FROM" ]; then
+      echo "From: $MAIL_FROM"
+    fi
+
+    echo "To: $MAIL_TO"
+    echo "Subject: $subject"
+    echo "Content-Type: $contenttype"
+    echo
+  fi
+
+  echo "$_content"
 }
 }

+ 55 - 0
notify/slack.sh

@@ -0,0 +1,55 @@
+#!/usr/bin/env sh
+
+#Support Slack webhooks
+
+#SLACK_WEBHOOK_URL=""
+#SLACK_CHANNEL=""
+#SLACK_USERNAME=""
+
+slack_send() {
+  _subject="$1"
+  _content="$2"
+  _statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped
+  _debug "_statusCode" "$_statusCode"
+
+  SLACK_WEBHOOK_URL="${SLACK_WEBHOOK_URL:-$(_readaccountconf_mutable SLACK_WEBHOOK_URL)}"
+  if [ -z "$SLACK_WEBHOOK_URL" ]; then
+    SLACK_WEBHOOK_URL=""
+    _err "You didn't specify a Slack webhook url SLACK_WEBHOOK_URL yet."
+    return 1
+  fi
+  _saveaccountconf_mutable SLACK_WEBHOOK_URL "$SLACK_WEBHOOK_URL"
+
+  SLACK_CHANNEL="${SLACK_CHANNEL:-$(_readaccountconf_mutable SLACK_CHANNEL)}"
+  if [ -n "$SLACK_CHANNEL" ]; then
+    _saveaccountconf_mutable SLACK_CHANNEL "$SLACK_CHANNEL"
+  fi
+
+  SLACK_USERNAME="${SLACK_USERNAME:-$(_readaccountconf_mutable SLACK_USERNAME)}"
+  if [ -n "$SLACK_USERNAME" ]; then
+    _saveaccountconf_mutable SLACK_USERNAME "$SLACK_USERNAME"
+  fi
+
+  export _H1="Content-Type: application/json"
+
+  _content="$(printf "*%s*\n%s" "$_subject" "$_content" | _json_encode)"
+  _data="{\"text\": \"$_content\", "
+  if [ -n "$SLACK_CHANNEL" ]; then
+    _data="$_data\"channel\": \"$SLACK_CHANNEL\", "
+  fi
+  if [ -n "$SLACK_USERNAME" ]; then
+    _data="$_data\"username\": \"$SLACK_USERNAME\", "
+  fi
+  _data="$_data\"mrkdwn\": \"true\"}"
+
+  if _post "$_data" "$SLACK_WEBHOOK_URL"; then
+    # shellcheck disable=SC2154
+    if [ "$response" = "ok" ]; then
+      _info "slack send success."
+      return 0
+    fi
+  fi
+  _err "slack send error."
+  _err "$response"
+  return 1
+}