Added password change with -p
This commit is contained in:
176
lib/dsmsession.c
176
lib/dsmsession.c
@@ -6,6 +6,9 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include <termio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "dsmrc.h"
|
||||
#include "dsmapitd.h"
|
||||
@@ -72,7 +75,7 @@ int tsm_checkapi(void) {
|
||||
}
|
||||
|
||||
/* Initialise a session to the TSM server */
|
||||
dsUint32_t tsm_initsess(char *options) {
|
||||
dsUint32_t tsm_initsess(char *options, char *password) {
|
||||
dsInt16_t rc=0;
|
||||
dsUint32_t dsmHandle=0;
|
||||
dsmApiVersionEx applApi;
|
||||
@@ -97,7 +100,7 @@ dsUint32_t tsm_initsess(char *options) {
|
||||
initIn.apiVersionExP = &applApi;
|
||||
initIn.clientNodeNameP = NULL;
|
||||
initIn.clientOwnerNameP = NULL;
|
||||
initIn.clientPasswordP = NULL;
|
||||
initIn.clientPasswordP = password;
|
||||
initIn.applicationTypeP = NULL;
|
||||
initIn.configfile = NULL;
|
||||
initIn.options = options;
|
||||
@@ -119,6 +122,7 @@ dsUint32_t tsm_initsess(char *options) {
|
||||
|
||||
if (rc != DSM_RC_OK) {
|
||||
printf("%s: dsmChangePW() failed %s\n",__func__,tsm_printerr(dsmHandle,rc));
|
||||
dsmTerminate(dsmHandle);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -397,3 +401,171 @@ dsInt16_t tsm_queryfile(dsUint32_t dsmHandle, dsmQueryType qType, tsm_query_call
|
||||
|
||||
return DSM_RC_OK;
|
||||
}
|
||||
|
||||
/* Signal handler */
|
||||
int intrupt = 0;
|
||||
void Catch(int signo) {
|
||||
if(signo) {}; /* so compiler won't complain about unused arguments */
|
||||
|
||||
intrupt = 1;
|
||||
}
|
||||
|
||||
/* Uses sigaction to establish signal handler */
|
||||
int install_sig_handler(int signum, void (*sig_handler)(int)) {
|
||||
struct sigaction action;
|
||||
int rc;
|
||||
|
||||
action.sa_handler = sig_handler; /* signal handler function */
|
||||
sigemptyset( &action.sa_mask ); /* mask of signals to block */
|
||||
|
||||
action.sa_flags = SA_NOCLDSTOP;
|
||||
|
||||
rc = sigaction(
|
||||
signum, /* I */ /* signal identifier */
|
||||
&action, /* *I */ /* new action for signal */
|
||||
NULL ); /* *O */ /* previous action - not needed */
|
||||
|
||||
return (rc);
|
||||
}
|
||||
|
||||
int ReadPass(char *text, char *buffer, int length) {
|
||||
struct termio ttyState, ttyStateSave;
|
||||
register char *p;
|
||||
register int c;
|
||||
int rc;
|
||||
FILE *ttyFH;
|
||||
|
||||
struct sigaction action;
|
||||
|
||||
/* Let's flush any prompt to the terminal that may be pending. */
|
||||
fflush(stdout);
|
||||
|
||||
/* Open the console input device */
|
||||
if((ttyFH = fopen("/dev/tty", "r")) == NULL)
|
||||
return(-1);
|
||||
else
|
||||
setbuf(ttyFH, (char *)NULL);
|
||||
|
||||
/* Reset the interrupt flag */
|
||||
intrupt = 0;
|
||||
|
||||
/* Trap the "BREAK" interrupt */
|
||||
(void) sigaction( SIGINT, NULL, &action ); /* save current hdlr */
|
||||
(void) install_sig_handler( SIGINT, Catch ); /* install new hdlr */
|
||||
|
||||
/* Get current state */
|
||||
rc = ioctl(fileno(ttyFH), TCGETA, &ttyStateSave);
|
||||
if (rc == -1)
|
||||
return(-1);
|
||||
|
||||
/* Copy the saved tty state into a work field */
|
||||
ttyState = ttyStateSave;
|
||||
|
||||
/* Turn off ECHO */
|
||||
ttyState.c_lflag &= ~ECHO;
|
||||
rc = ioctl(fileno(ttyFH), TCSETA, &ttyState);
|
||||
if (rc == -1)
|
||||
return(-1);
|
||||
|
||||
printf(text);
|
||||
|
||||
/* Read the password (quietly) */
|
||||
for(p=buffer; !intrupt && (c = getc(ttyFH)) != '\n' && c != EOF; ) {
|
||||
if(p < buffer+length)
|
||||
*p++ = c;
|
||||
}
|
||||
*p = '\0';
|
||||
|
||||
printf("\n");
|
||||
|
||||
/* Restore the tty state settings */
|
||||
rc = ioctl(fileno(ttyFH), TCSETA, &ttyStateSave);
|
||||
if (rc == -1)
|
||||
return(-1);
|
||||
|
||||
/* Reset the interrupt handler */
|
||||
(void) sigaction( SIGINT, &action, NULL );
|
||||
|
||||
if(ttyFH != stdin)
|
||||
(void) fclose(ttyFH);
|
||||
if(intrupt)
|
||||
(void) kill(getpid(), SIGINT);
|
||||
|
||||
return(strlen(buffer));
|
||||
}
|
||||
|
||||
/*
|
||||
* Set the TSM Node password
|
||||
*
|
||||
* We use a callback to process the list, the callback should return:
|
||||
*/
|
||||
int tsm_setpassword(char *options) {
|
||||
uint32 dsmHandle;
|
||||
bool_t done = bFalse;
|
||||
uint16 pw_trys = 0;
|
||||
uint16 len = 0;
|
||||
char input[INPUTLEN];
|
||||
char pw_cur[DSM_MAX_VERIFIER_LENGTH + 1];
|
||||
char pw_new [DSM_MAX_VERIFIER_LENGTH + 1];
|
||||
|
||||
int rc;
|
||||
|
||||
while (!done) {
|
||||
// Current Password
|
||||
rc = ReadPass("Enter your current password: ",input,INPUTLEN);
|
||||
len = strlen(input);
|
||||
if ((len > DSM_MAX_VERIFIER_LENGTH) || !len || (rc < 0)) {
|
||||
printf("Current password is invalid. Please try again.\n");
|
||||
continue;
|
||||
} else {
|
||||
strcpy(pw_cur,input);
|
||||
}
|
||||
|
||||
// New password
|
||||
rc = ReadPass("Enter your new password: ",input,INPUTLEN);
|
||||
len = strlen(input);
|
||||
if ((len > DSM_MAX_VERIFIER_LENGTH) || !len || (rc < 0)) {
|
||||
printf("New password is invalid. Please try again.\n");
|
||||
continue;
|
||||
} else {
|
||||
strcpy(pw_new,input);
|
||||
}
|
||||
|
||||
// Verify new password
|
||||
rc = ReadPass("Enter your new password again: ",input,INPUTLEN);
|
||||
len = strlen(input);
|
||||
if ((len > DSM_MAX_VERIFIER_LENGTH) || !len || (rc < 0)) {
|
||||
printf("New password is invalid. Please try again.\n");
|
||||
continue;
|
||||
|
||||
} else {
|
||||
// Compare new pw copies to make sure no typos from user.
|
||||
if ((strcmp(pw_new,input))) {
|
||||
pw_trys++;
|
||||
|
||||
if (pw_trys > 3)
|
||||
debugLog(0,__func__,"ERROR: Passwords dont match, tried too many times.",3);
|
||||
else
|
||||
debugLog(0,__func__,"WARN: Your new passwords do not match, please try again...",0);
|
||||
|
||||
} else {
|
||||
done = bTrue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dsmHandle = tsm_initsess(options,pw_cur);
|
||||
if (! dsmHandle) {
|
||||
debugLog(0,__func__,"ERROR: Unable to create TSM session with your password.",2);
|
||||
}
|
||||
|
||||
rc = dsmChangePW(dsmHandle,pw_cur,pw_new);
|
||||
if (rc)
|
||||
debugLog(0,__func__,"ERROR: Password change failed.",2);
|
||||
else
|
||||
printf("\nYour new password has been accepted and updated.\n");
|
||||
|
||||
dsmTerminate(dsmHandle);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
Reference in New Issue
Block a user