-
Notifications
You must be signed in to change notification settings - Fork 304
Authy push authentication feature #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
krzole
wants to merge
17
commits into
google:master
Choose a base branch
from
krzole:authy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
355f40b
add authy authentication
krzole eef6d6f
authy: move authy calls to pam_authy
krzole 2b733f3
add README.authy.md
krzole f7459d2
authy: make authy feature optional (disabled by default)
krzole c27a6a5
authy: update README.authy.md
krzole 90562d3
authy: remove newlines from log_message() calls in pam_authy.c
krzole a7c61a1
authy: remove asprintf() calls
krzole f39faed
authy: dont check for NULL pointers before freeing them
krzole 09989ab
authy: check for pam_get_user() failure
krzole e17765d
authy: fix typo in the function name
krzole c054610
authy: reduce variable scope
krzole 013618e
authy: use clock_gettime() to check the timeout
krzole 82941d0
authy: fix too small string sizes
krzole 89ddccb
authy: auto-enable authy support if dependencies are met
krzole 60ffea7
authy: add client-side timeouts with CURLOPT_TIMEOUT option
krzole ebfb63c
authy: add a dummy pam_no_authy.c file
krzole 7adaa1a
authy: add licence header
krzole File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # Authy push authentication for Google Authenticator PAM module | ||
| This extension uses Authy (https://authy.com/) to authenticate | ||
| using push notifications. | ||
|
|
||
| ## Build, install, setup | ||
| See https://github.com/google/google-authenticator-libpam | ||
|
|
||
| Authy extension requires the following extra dependencies: | ||
| - libjansson | ||
| - libcurl | ||
|
|
||
| If running Debian-based distro, do: | ||
| ``` | ||
| sudo apt install libjansson-dev libcurl4-gnutls-dev | ||
| ``` | ||
|
|
||
| ## Extra PAM module options | ||
| ### enable_authy | ||
| If set, Authy extension is enabled. PAM module sends push notification | ||
| to the Authy authenticator on user login. If authentication passes, | ||
| login is granted. On failure, the classic Google OTP is used. | ||
|
|
||
| ## Extra .google_authenticator fields | ||
| To setup Authy authentication, generate .google_authenticator file as described | ||
| in https://github.com/google/google-authenticator-libpam and add the following | ||
| extra fields. | ||
|
|
||
| ### AUTHY_ID | ||
| See https://support.authy.com/hc/en-us/articles/360016449054-Find-your-Authy-ID | ||
| how to find out your Authy ID. | ||
|
|
||
| Example: | ||
| ``` | ||
| " AUTHY_ID 123456789" | ||
| ``` | ||
| ### AUTHY_API_KEY | ||
| See: https://www.twilio.com/docs/authy/twilioauth-sdk/quickstart/obtain-authy-api-key | ||
|
|
||
| Obtaining an Authy API Key: | ||
| 1. Create a Twilio account: https://www.twilio.com/try-twilio | ||
| 2. Create an Authy application in the Twilio Console. | ||
| 3. Once you've created a new Authy application, copy the API Key for | ||
| Production available in the Settings page of your Authy application. | ||
|
|
||
| Example: | ||
| ``` | ||
| " AUTHY_API_KEY aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP | ||
| ``` | ||
|
|
||
| ### .google_authenticator example | ||
| ``` | ||
| 00000000000000000000000000 | ||
| " RESETTING_TIME_SKEW 52968321+21 52968328+21 52968508+20 | ||
| " RATE_LIMIT 3 30 1589055237 | ||
| " WINDOW_SIZE 17 | ||
| " TOTP_AUTH | ||
| " AUTHY_ID 000000000 | ||
| " AUTHY_API_KEY xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | ||
| 11111111 | ||
| 22222222 | ||
| 33333333 | ||
| " | ||
| ``` | ||
|
|
||
| ### TODO: add Authy fields generation in google-authenticator tool |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,266 @@ | ||
| /* | ||
| * Google authenticator extension for Authy push notifications | ||
| * | ||
| * Copyright 2020 Krzysztof Olejarczyk | ||
| * | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #define _GNU_SOURCE | ||
|
|
||
| #include "config.h" | ||
| #include <stdio.h> | ||
| #include <unistd.h> | ||
| #include <unistd.h> | ||
| #include <string.h> | ||
| #include <errno.h> | ||
| #include <syslog.h> | ||
| #include <jansson.h> | ||
| #include <curl/curl.h> | ||
|
|
||
| #include <security/pam_appl.h> | ||
| #include <security/pam_modules.h> | ||
|
|
||
| #include "pam_util.h" | ||
| #include "pam_authy.h" | ||
|
|
||
| typedef struct { | ||
| char *memory; | ||
| size_t size; | ||
| } mblock_t; | ||
|
|
||
| static size_t ctrl_curl_receive(void *content, size_t size, size_t nmemb, | ||
| void *user_mem) | ||
| { | ||
| size_t realsize = size * nmemb; | ||
| mblock_t *mem = (mblock_t *)user_mem; | ||
|
|
||
| mem->memory = realloc(mem->memory, mem->size + realsize + 1); | ||
| if (mem->memory == NULL) { | ||
| return -ENOMEM; | ||
| } | ||
|
|
||
| memcpy(&(mem->memory[mem->size]), content, realsize); | ||
| mem->size += realsize; | ||
| mem->memory[mem->size] = 0; | ||
|
|
||
| return realsize; | ||
| } | ||
|
|
||
| static authy_rc_t authy_check_approval(pam_handle_t *pamh, char *api_key, char *uuid) | ||
| { | ||
| json_t *payload = NULL, *jt = NULL; | ||
| mblock_t buffer = {0}; | ||
| authy_rc_t rc; | ||
|
|
||
| CURL *curl = curl_easy_init(); | ||
| if (!curl) { | ||
| log_message(LOG_ERR, pamh, "authy_err: curl init failed"); | ||
| rc = AUTHY_LIB_ERROR; | ||
| goto exit_err; | ||
| } | ||
|
|
||
| char url[120]; | ||
| snprintf(url, sizeof(url), | ||
| "https://api.authy.com/onetouch/json/approval_requests/%s", | ||
| uuid); | ||
|
|
||
| char xheader[64]; | ||
| snprintf(xheader, sizeof(xheader), "X-Authy-API-Key: %s", api_key); | ||
|
|
||
| struct curl_slist *headers = NULL; | ||
| headers = curl_slist_append(headers, xheader); | ||
| curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); | ||
| curl_easy_setopt(curl, CURLOPT_URL, url); | ||
| curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ctrl_curl_receive); | ||
| curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&buffer); | ||
| curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L); | ||
|
|
||
| CURLcode res; | ||
| res = curl_easy_perform(curl); | ||
| if (res != CURLE_OK) { | ||
| log_message(LOG_ERR, pamh, "authy_err: curl call failed: %d (%s)", | ||
| res, curl_easy_strerror(res)); | ||
| rc = AUTHY_CONN_ERROR; | ||
| goto exit_err; | ||
| } | ||
| rc = AUTHY_OK; | ||
|
|
||
| payload = json_loads(buffer.memory, JSON_DECODE_ANY, NULL); | ||
| jt = json_object_get(payload, "approval_request"); | ||
| if (!jt) { | ||
| log_message(LOG_ERR, pamh, "authy_err: 'approval_request' field missing"); | ||
| rc = AUTHY_CONN_ERROR; | ||
| goto exit_err; | ||
| } | ||
|
|
||
| char *str = (char *)json_string_value(json_object_get(jt, "status")); | ||
| if (!str) { | ||
| rc = AUTHY_CONN_ERROR; | ||
| goto exit_err; | ||
| } | ||
|
|
||
| if (!strcmp(str, "pending")) { | ||
| rc = AUTHY_PENDING; | ||
| } else if (!strcmp(str, "expired")) { | ||
| rc = AUTHY_EXPIRED; | ||
| } else if (!strcmp(str, "denied")) { | ||
| rc = AUTHY_DENIED; | ||
| } else if (!strcmp(str, "approved")) { | ||
| rc = AUTHY_APPROVED; | ||
| } | ||
|
|
||
| exit_err: | ||
| free(buffer.memory); | ||
| free(jt); | ||
| free(payload); | ||
|
|
||
| if (curl) | ||
| curl_easy_cleanup(curl); | ||
|
|
||
| return rc; | ||
| } | ||
|
|
||
| static authy_rc_t authy_post_approval(pam_handle_t *pamh, long authy_id, char *api_key, int timeout, char **uuid) | ||
| { | ||
| json_t *payload = NULL, *jt = NULL; | ||
| mblock_t buffer = {0}; | ||
| authy_rc_t rc; | ||
|
|
||
| CURL *curl = curl_easy_init(); | ||
| if (!curl) { | ||
| log_message(LOG_ERR, pamh, "authy_err: curl init failed"); | ||
| rc = AUTHY_LIB_ERROR; | ||
| goto exit_err; | ||
| } | ||
|
|
||
| const char *username; | ||
| if (pam_get_user(pamh, &username, NULL) != PAM_SUCCESS || | ||
| !username || | ||
| !*username) { | ||
| log_message(LOG_ERR, pamh, "pam_get_user() failed to get a user name"); | ||
| rc = AUTHY_LIB_ERROR; | ||
| goto exit_err; | ||
| } | ||
|
|
||
| char hostname[128] = {0}; | ||
| if (gethostname(hostname, sizeof(hostname)-1)) { | ||
| strcpy(hostname, "unix"); | ||
| } | ||
|
|
||
| char url[80]; | ||
| snprintf(url, sizeof(url), | ||
| "https://api.authy.com/onetouch/json/users/%ld/approval_requests", | ||
| authy_id); | ||
|
|
||
| char xheader[64]; | ||
| snprintf(xheader, sizeof(xheader), "X-Authy-API-Key: %s", api_key); | ||
|
|
||
| char data[200]; | ||
| snprintf(data, sizeof(data), "message=Login authentication&" \ | ||
| "details=%s at %s&seconds_to_expire=%d", | ||
| username, hostname, timeout); | ||
|
|
||
| struct curl_slist *headers = NULL; | ||
| headers = curl_slist_append(headers, xheader); | ||
| curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(data)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a client-side timeout in case the server hangs?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree, CURLOPT_TIMEOUT will be added to the curl call |
||
| curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); | ||
| curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); | ||
| curl_easy_setopt(curl, CURLOPT_URL, url); | ||
| curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ctrl_curl_receive); | ||
| curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&buffer); | ||
| curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L); | ||
|
|
||
| CURLcode res; | ||
| res = curl_easy_perform(curl); | ||
| if (res != CURLE_OK) { | ||
| log_message(LOG_ERR, pamh, "authy_err: curl call failed: %d (%s)", | ||
| res, curl_easy_strerror(res)); | ||
| rc = AUTHY_CONN_ERROR; | ||
| goto exit_err; | ||
| } | ||
| rc = AUTHY_OK; | ||
|
|
||
| payload = json_loads(buffer.memory, JSON_DECODE_ANY, NULL); | ||
| jt = json_object_get(payload, "approval_request"); | ||
| if (!jt) { | ||
| log_message(LOG_ERR, pamh, "authy_err: 'approval_request' field missing"); | ||
| rc = AUTHY_CONN_ERROR; | ||
| goto exit_err; | ||
| } | ||
| *uuid = (char *)json_string_value(json_object_get(jt, "uuid")); | ||
| if (!*uuid) { | ||
| log_message(LOG_ERR, pamh, "authy_err: 'uuid' field missing"); | ||
| rc = AUTHY_CONN_ERROR; | ||
| goto exit_err; | ||
| } | ||
|
|
||
| exit_err: | ||
| free(buffer.memory); | ||
| free(jt); | ||
| free(payload); | ||
|
|
||
| if (curl) | ||
| curl_easy_cleanup(curl); | ||
|
|
||
| return rc; | ||
| } | ||
|
|
||
| authy_rc_t authy_login(pam_handle_t *pamh, long authy_id, char *api_key, int timeout) | ||
| { | ||
| authy_rc_t rc; | ||
| char *uuid = NULL; | ||
| char *err_str = NULL; | ||
|
|
||
| log_message(LOG_INFO, pamh, "authy_dbg: Sending Authy authentication push request"); | ||
| rc = authy_post_approval(pamh, authy_id, api_key, 30, &uuid); | ||
| if (rc != AUTHY_OK) { | ||
| log_message(LOG_ERR, pamh, "authy_err: Push Authentication request failed"); | ||
| goto exit_err; | ||
| } | ||
|
|
||
| log_message(LOG_INFO, pamh, "authy_dbg: Waiting for Authy authentication approval"); | ||
| struct timespec start_time, now; | ||
| clock_gettime(CLOCK_MONOTONIC, &start_time); | ||
| do { | ||
| rc = authy_check_approval(pamh, api_key, uuid); | ||
| switch (rc) { | ||
| case AUTHY_DENIED: | ||
| err_str = "denied"; | ||
| goto exit_err; | ||
| case AUTHY_EXPIRED: | ||
| err_str = "expired"; | ||
| goto exit_err; | ||
| case AUTHY_APPROVED: | ||
| log_message(LOG_INFO, pamh, "authy_dbg: Authentication approved"); | ||
| goto exit_err; | ||
| default: | ||
| break; | ||
| } | ||
| sleep(1); | ||
| clock_gettime(CLOCK_MONOTONIC, &now); | ||
| } while ((start_time.tv_sec + timeout + 5) > now.tv_sec); | ||
| rc = AUTHY_EXPIRED; | ||
| err_str = "expired (pam timeout)"; | ||
|
|
||
| exit_err: | ||
| if (err_str) | ||
| log_message(LOG_ERR, pamh, "authy_err: Authentication %s", err_str); | ||
|
|
||
| free(uuid); | ||
|
|
||
| return rc; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't look integer overflow safe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the reference example from libcurl documentation: (https://curl.haxx.se/libcurl/c/CURLOPT_WRITEFUNCTION.htm)
I have to take a closer look how to make it more safe.