Add a text based callback function to the client.

This commit is contained in:
Andreas Schneider 2008-05-13 13:40:06 +02:00
parent 4e6a65ab9d
commit b13fba29da
4 changed files with 186 additions and 0 deletions

View file

@ -23,6 +23,7 @@ set(CLIENT_LINK_LIBRARIES
set(client_SRCS set(client_SRCS
csync_client.c csync_client.c
csync_auth.c
) )
include_directories( include_directories(

145
client/csync_auth.c Normal file
View file

@ -0,0 +1,145 @@
/*
* libcsync -- a library to sync a directory with another
*
* Copyright (c) 2008 by Andreas Schneider <mail@cynapses.org>
*
* 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 2
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* vim: ts=2 sw=2 et cindent
*/
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include "csync_auth.h"
/** 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,"\n");
fprintf(stdout, "%s", prompt);
fflush(stdout);
while (! fgets(buf, len, stdin));
if ((ptr = strchr(buf, '\n'))) {
*ptr = '\0';
}
ok = 1;
fprintf(stdout,"\n");
}
/* 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) {
struct termios attr;
struct termios old_attr;
int ok = 0;
int fd = -1;
char *ptr = NULL;
ZERO_STRUCT(attr);
ZERO_STRUCT(old_attr);
/* get local terminal attributes */
if (tcgetattr(STDIN_FILENO, &attr) < 0) {
perror("tcgetattr");
return -1;
}
/* save terminal attributes */
memcpy(&old_attr, &attr, sizeof(attr));
if((fd = fcntl(0, F_GETFL, 0)) < 0) {
perror("fcntl");
return -1;
}
/* disable echo */
attr.c_lflag &= ~(ECHO);
/* write attributes to terminal */
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &attr) < 0) {
perror("tcsetattr");
return -1;
}
/* disable nonblocking I/O */
if (fd & O_NDELAY) {
fcntl(0, F_SETFL, fd & ~O_NDELAY);
}
/* read the password */
while (!ok) {
fprintf(stdout,"\n");
fprintf(stdout, "%s", prompt);
fflush(stdout);
while (! fgets(buf, len, stdin));
if ((ptr = strchr(buf, '\n'))) {
*ptr = '\0';
}
if (verify) {
char key_string[len];
fprintf(stdout, "\nVerifying, please re-enter. %s", prompt);
fflush(stdout);
if (! fgets(key_string, sizeof(key_string), stdin)) {
clearerr(stdin);
continue;
}
if ((ptr = strchr(key_string, '\n'))) {
*ptr = '\0';
}
if (strcmp(buf ,key_string)) {
printf("\n\07\07Mismatch - try again\n");
fflush(stdout);
continue;
}
}
ok = 1;
fprintf(stdout,"\n");
}
/* reset terminal */
if (tcsetattr(STDIN_FILENO, TCSANOW, &old_attr)) {
ok = 0;
}
/* close fd */
if (fd & O_NDELAY) {
fcntl(0, F_SETFL, fd);
}
/* force termination */
buf[len - 1] = 0;
/* return nonzero if not okay */
return !ok;
}

30
client/csync_auth.h Normal file
View file

@ -0,0 +1,30 @@
/*
* libcsync -- a library to sync a directory with another
*
* Copyright (c) 2008 by Andreas Schneider <mail@cynapses.org>
*
* 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 2
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* vim: ft=c.doxygen ts=2 sw=2 et cindent
*/
#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);
#endif /* _CSYNC_CLIENT_AUTH_H */

View file

@ -27,6 +27,8 @@
#include <csync.h> #include <csync.h>
#include "csync_auth.h"
const char *argp_program_version = "csync commandline client 0.42"; const char *argp_program_version = "csync commandline client 0.42";
const char *argp_program_bug_address = "<csync-devel@csync.org>"; const char *argp_program_bug_address = "<csync-devel@csync.org>";
@ -120,6 +122,13 @@ static error_t parse_opt (int key, char *arg, struct argp_state *state) {
return 0; return 0;
} }
static void csync_auth_fn(char *usr, size_t usrlen, char *pwd, size_t pwdlen) {
/* get username */
csync_text_prompt("Username: ", usr, usrlen);
/* get password */
csync_password_prompt("Password: ", pwd, pwdlen, 0);
}
/* Our argp parser. */ /* Our argp parser. */
static struct argp argp = {options, parse_opt, args_doc, doc, NULL, NULL, NULL}; static struct argp argp = {options, parse_opt, args_doc, doc, NULL, NULL, NULL};
@ -146,6 +155,7 @@ int main(int argc, char **argv) {
} }
csync_init(csync); csync_init(csync);
csync_set_module_auth_callback(csync, csync_auth_fn);
printf("Version: %s\n", csync_version()); printf("Version: %s\n", csync_version());
if (arguments.update) { if (arguments.update) {