зеркало из https://github.com/nextcloud/desktop.git
Create a more generic authentication callback.
This commit is contained in:
Родитель
ae304565af
Коммит
95f4db7c4f
|
@ -31,38 +31,17 @@
|
|||
/** Zero a structure */
|
||||
#define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
|
||||
|
||||
int csync_text_prompt(const char *prompt, char *buf, size_t len) {
|
||||
char *ptr = NULL;
|
||||
int ok = 0;
|
||||
|
||||
/* read the password */
|
||||
while (!ok) {
|
||||
fprintf(stdout, "%s", prompt);
|
||||
fflush(stdout);
|
||||
while (! fgets(buf, len, stdin));
|
||||
|
||||
if ((ptr = strchr(buf, '\n'))) {
|
||||
*ptr = '\0';
|
||||
}
|
||||
ok = 1;
|
||||
}
|
||||
|
||||
/* force termination */
|
||||
buf[len - 1] = 0;
|
||||
|
||||
/* return nonzero if not okay */
|
||||
return !ok;
|
||||
}
|
||||
|
||||
int csync_password_prompt(const char *prompt, char *buf, size_t len, int verify) {
|
||||
int csync_auth(const char *prompt, char *buf, size_t len, int echo, int verify) {
|
||||
struct termios attr;
|
||||
struct termios old_attr;
|
||||
int ok = 0;
|
||||
int fd = -1;
|
||||
char *ptr = NULL;
|
||||
char tmp[len];
|
||||
|
||||
ZERO_STRUCT(attr);
|
||||
ZERO_STRUCT(old_attr);
|
||||
ZERO_STRUCT(tmp);
|
||||
|
||||
/* get local terminal attributes */
|
||||
if (tcgetattr(STDIN_FILENO, &attr) < 0) {
|
||||
|
@ -78,7 +57,9 @@ int csync_password_prompt(const char *prompt, char *buf, size_t len, int verify)
|
|||
}
|
||||
|
||||
/* disable echo */
|
||||
attr.c_lflag &= ~(ECHO);
|
||||
if (!echo) {
|
||||
attr.c_lflag &= ~(ECHO);
|
||||
}
|
||||
|
||||
/* write attributes to terminal */
|
||||
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &attr) < 0) {
|
||||
|
@ -93,14 +74,23 @@ int csync_password_prompt(const char *prompt, char *buf, size_t len, int verify)
|
|||
|
||||
/* read the password */
|
||||
while (!ok) {
|
||||
fprintf(stdout, "%s", prompt);
|
||||
if (*buf) {
|
||||
fprintf(stdout, "%s: [%s] ", prompt, buf);
|
||||
} else {
|
||||
fprintf(stdout, "%s: ", prompt);
|
||||
}
|
||||
fflush(stdout);
|
||||
while (! fgets(buf, len, stdin));
|
||||
while (! fgets(tmp, len, stdin));
|
||||
|
||||
if ((ptr = strchr(buf, '\n'))) {
|
||||
if ((ptr = strchr(tmp, '\n'))) {
|
||||
*ptr = '\0';
|
||||
}
|
||||
|
||||
if (*tmp) {
|
||||
strncpy(buf, tmp, len);
|
||||
}
|
||||
|
||||
|
||||
if (verify) {
|
||||
char key_string[len];
|
||||
|
||||
|
@ -133,7 +123,8 @@ int csync_password_prompt(const char *prompt, char *buf, size_t len, int verify)
|
|||
}
|
||||
|
||||
/* force termination */
|
||||
buf[len - 1] = 0;
|
||||
buf[len - 1] = '\0';
|
||||
ZERO_STRUCT(tmp);
|
||||
|
||||
/* return nonzero if not okay */
|
||||
return !ok;
|
||||
|
|
|
@ -23,8 +23,6 @@
|
|||
#ifndef _CSYNC_CLIENT_AUTH_H
|
||||
#define _CSYNC_CLIENT_AUTH_H
|
||||
|
||||
int csync_text_prompt(const char *prompt, char *buf, size_t len);
|
||||
|
||||
int csync_password_prompt(const char *prompt, char *buf, size_t len, int verify);
|
||||
int csync_auth(const char *prompt, char *buf, size_t len, int echo, int verify);
|
||||
|
||||
#endif /* _CSYNC_CLIENT_AUTH_H */
|
||||
|
|
|
@ -157,27 +157,6 @@ static error_t parse_opt (int key, char *arg, struct argp_state *state) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
static void csync_auth_fn(char *usr, size_t usrlen, char *pwd, size_t pwdlen, int pwonly) {
|
||||
char tmp[256] = {0};
|
||||
|
||||
if (!pwonly) {
|
||||
/* get username */
|
||||
snprintf(tmp, 255, "Username: [%s] ", usr);
|
||||
csync_text_prompt(tmp, tmp, 255);
|
||||
|
||||
if (tmp[strlen(tmp) - 1] == '\n') {
|
||||
tmp[strlen(tmp) - 1] = '\0';
|
||||
}
|
||||
|
||||
if (tmp[0] != '\0') {
|
||||
strncpy(usr, tmp, usrlen - 1);
|
||||
}
|
||||
}
|
||||
|
||||
/* get password */
|
||||
csync_password_prompt("Password: ", pwd, pwdlen, 0);
|
||||
}
|
||||
|
||||
/* Our argp parser. */
|
||||
static struct argp argp = {options, parse_opt, args_doc, doc, NULL, NULL, NULL};
|
||||
|
||||
|
@ -207,7 +186,7 @@ int main(int argc, char **argv) {
|
|||
exit(1);
|
||||
}
|
||||
|
||||
csync_set_auth_callback(csync, csync_auth_fn);
|
||||
csync_set_auth_callback(csync, csync_auth);
|
||||
if (arguments.disable_statedb) {
|
||||
csync_disable_statedb(csync);
|
||||
}
|
||||
|
|
|
@ -76,7 +76,8 @@ static void get_auth_data_with_context_fn(SMBCCTX *c,
|
|||
/* Call the passwort prompt */
|
||||
if (auth_cb != NULL) {
|
||||
DEBUG_SMB(("csync_smb - execute authentication callback\n"));
|
||||
(*auth_cb) (un, unlen, pw, pwlen, 0);
|
||||
(*auth_cb) ("Username", un, unlen, 1, 0);
|
||||
(*auth_cb) ("Password", pw, pwlen, 0, 0);
|
||||
}
|
||||
|
||||
DEBUG_SMB(("csync_smb - user=%s, workgroup=%s, server=%s, share=%s\n",
|
||||
|
|
|
@ -54,7 +54,7 @@ extern "C" {
|
|||
#define CSYNC_EXCLUDE_FILE "csync_exclude.conf"
|
||||
#define CSYNC_LOCK_FILE "lock"
|
||||
|
||||
typedef void (*csync_auth_callback) (char *usr, size_t usrlen, char *pwd, size_t pwlen, int pwonly);
|
||||
typedef int (*csync_auth_callback) (const char *prompt, char *buf, size_t len, int echo, int verify);
|
||||
|
||||
/**
|
||||
* csync handle
|
||||
|
|
Загрузка…
Ссылка в новой задаче