]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_ispconfig.sh
Merge pull request #1161 from hiskang/deploy/strongswan
[mirror_acme.sh.git] / dnsapi / dns_ispconfig.sh
1 #!/usr/bin/env sh
2
3 # ISPConfig 3.1 API
4 # User must provide login data and URL to the ISPConfig installation incl. port. The remote user in ISPConfig must have access to:
5 # - DNS txt Functions
6
7 # Report bugs to https://github.com/sjau/acme.sh
8
9 # Values to export:
10 # export ISPC_User="remoteUser"
11 # export ISPC_Password="remotePassword"
12 # export ISPC_Api="https://ispc.domain.tld:8080/remote/json.php"
13 # export ISPC_Api_Insecure=1 # Set 1 for insecure and 0 for secure -> difference is whether ssl cert is checked for validity (0) or whether it is just accepted (1)
14
15 ######## Public functions #####################
16
17 #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
18 dns_ispconfig_add() {
19 fulldomain="${1}"
20 txtvalue="${2}"
21 _debug "Calling: dns_ispconfig_add() '${fulldomain}' '${txtvalue}'"
22 _ISPC_credentials && _ISPC_login && _ISPC_getZoneInfo && _ISPC_addTxt
23 }
24
25 #Usage: dns_myapi_rm _acme-challenge.www.domain.com
26 dns_ispconfig_rm() {
27 fulldomain="${1}"
28 _debug "Calling: dns_ispconfig_rm() '${fulldomain}'"
29 _ISPC_credentials && _ISPC_login && _ISPC_rmTxt
30 }
31
32 #################### Private functions below ##################################
33
34 _ISPC_credentials() {
35 if [ -z "${ISPC_User}" ] || [ -z "$ISPC_Password" ] || [ -z "${ISPC_Api}" ] || [ -z "${ISPC_Api_Insecure}" ]; then
36 ISPC_User=""
37 ISPC_Password=""
38 ISPC_Api=""
39 ISPC_Api_Insecure=""
40 _err "You haven't specified the ISPConfig Login data, URL and whether you want check the ISPC SSL cert. Please try again."
41 return 1
42 else
43 _saveaccountconf ISPC_User "${ISPC_User}"
44 _saveaccountconf ISPC_Password "${ISPC_Password}"
45 _saveaccountconf ISPC_Api "${ISPC_Api}"
46 _saveaccountconf ISPC_Api_Insecure "${ISPC_Api_Insecure}"
47 # Set whether curl should use secure or insecure mode
48 export HTTPS_INSECURE="${ISPC_Api_Insecure}"
49 fi
50 }
51
52 _ISPC_login() {
53 _info "Getting Session ID"
54 curData="{\"username\":\"${ISPC_User}\",\"password\":\"${ISPC_Password}\",\"client_login\":false}"
55 curResult="$(_post "${curData}" "${ISPC_Api}?login")"
56 _debug "Calling _ISPC_login: '${curData}' '${ISPC_Api}?login'"
57 _debug "Result of _ISPC_login: '$curResult'"
58 if _contains "${curResult}" '"code":"ok"'; then
59 sessionID=$(echo "${curResult}" | _egrep_o "response.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
60 _info "Retrieved Session ID."
61 _debug "Session ID: '${sessionID}'"
62 else
63 _err "Couldn't retrieve the Session ID."
64 return 1
65 fi
66 }
67
68 _ISPC_getZoneInfo() {
69 _info "Getting Zoneinfo"
70 zoneEnd=false
71 curZone="${fulldomain}"
72 while [ "${zoneEnd}" = false ]; do
73 # we can strip the first part of the fulldomain, since it's just the _acme-challenge string
74 curZone="${curZone#*.}"
75 # suffix . needed for zone -> domain.tld.
76 curData="{\"session_id\":\"${sessionID}\",\"primary_id\":{\"origin\":\"${curZone}.\"}}"
77 curResult="$(_post "${curData}" "${ISPC_Api}?dns_zone_get")"
78 _debug "Calling _ISPC_getZoneInfo: '${curData}' '${ISPC_Api}?login'"
79 _debug "Result of _ISPC_getZoneInfo: '$curResult'"
80 if _contains "${curResult}" '"id":"'; then
81 zoneFound=true
82 zoneEnd=true
83 _info "Retrieved zone data."
84 _debug "Zone data: '${curResult}'"
85 fi
86 if [ "${curZone#*.}" != "$curZone" ]; then
87 _debug2 "$curZone still contains a '.' - so we can check next higher level"
88 else
89 zoneEnd=true
90 _err "Couldn't retrieve zone data."
91 return 1
92 fi
93 done
94 if [ "${zoneFound}" ]; then
95 server_id=$(echo "${curResult}" | _egrep_o "server_id.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
96 _debug "Server ID: '${server_id}'"
97 case "${server_id}" in
98 '' | *[!0-9]*)
99 _err "Server ID is not numeric."
100 return 1
101 ;;
102 *) _info "Retrieved Server ID" ;;
103 esac
104 zone=$(echo "${curResult}" | _egrep_o "\"id.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
105 _debug "Zone: '${zone}'"
106 case "${zone}" in
107 '' | *[!0-9]*)
108 _err "Zone ID is not numeric."
109 return 1
110 ;;
111 *) _info "Retrieved Zone ID" ;;
112 esac
113 client_id=$(echo "${curResult}" | _egrep_o "sys_userid.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
114 _debug "Client ID: '${client_id}'"
115 case "${client_id}" in
116 '' | *[!0-9]*)
117 _err "Client ID is not numeric."
118 return 1
119 ;;
120 *) _info "Retrieved Client ID." ;;
121 esac
122 zoneFound=""
123 zoneEnd=""
124 fi
125 }
126
127 _ISPC_addTxt() {
128 curSerial="$(date +%s)"
129 curStamp="$(date +'%F %T')"
130 params="\"server_id\":\"${server_id}\",\"zone\":\"${zone}\",\"name\":\"${fulldomain}.\",\"type\":\"txt\",\"data\":\"${txtvalue}\",\"aux\":\"0\",\"ttl\":\"3600\",\"active\":\"y\",\"stamp\":\"${curStamp}\",\"serial\":\"${curSerial}\""
131 curData="{\"session_id\":\"${sessionID}\",\"client_id\":\"${client_id}\",\"params\":{${params}}}"
132 curResult="$(_post "${curData}" "${ISPC_Api}?dns_txt_add")"
133 _debug "Calling _ISPC_addTxt: '${curData}' '${ISPC_Api}?dns_txt_add'"
134 _debug "Result of _ISPC_addTxt: '$curResult'"
135 record_id=$(echo "${curResult}" | _egrep_o "\"response.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
136 _debug "Record ID: '${record_id}'"
137 case "${record_id}" in
138 '' | *[!0-9]*)
139 _err "Couldn't add ACME Challenge TXT record to zone."
140 return 1
141 ;;
142 *) _info "Added ACME Challenge TXT record to zone." ;;
143 esac
144 }
145
146 _ISPC_rmTxt() {
147 # Need to get the record ID.
148 curData="{\"session_id\":\"${sessionID}\",\"primary_id\":{\"name\":\"${fulldomain}.\",\"type\":\"TXT\"}}"
149 curResult="$(_post "${curData}" "${ISPC_Api}?dns_txt_get")"
150 _debug "Calling _ISPC_rmTxt: '${curData}' '${ISPC_Api}?dns_txt_get'"
151 _debug "Result of _ISPC_rmTxt: '$curResult'"
152 if _contains "${curResult}" '"code":"ok"'; then
153 record_id=$(echo "${curResult}" | _egrep_o "\"id.*" | cut -d ':' -f 2 | cut -d '"' -f 2)
154 _debug "Record ID: '${record_id}'"
155 case "${record_id}" in
156 '' | *[!0-9]*)
157 _err "Record ID is not numeric."
158 return 1
159 ;;
160 *)
161 unset IFS
162 _info "Retrieved Record ID."
163 curData="{\"session_id\":\"${sessionID}\",\"primary_id\":\"${record_id}\"}"
164 curResult="$(_post "${curData}" "${ISPC_Api}?dns_txt_delete")"
165 _debug "Calling _ISPC_rmTxt: '${curData}' '${ISPC_Api}?dns_txt_delete'"
166 _debug "Result of _ISPC_rmTxt: '$curResult'"
167 if _contains "${curResult}" '"code":"ok"'; then
168 _info "Removed ACME Challenge TXT record from zone."
169 else
170 _err "Couldn't remove ACME Challenge TXT record from zone."
171 return 1
172 fi
173 ;;
174 esac
175 fi
176 }