aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src
diff options
context:
space:
mode:
authorDavid M. Syzdek <david@syzdek.net>2022-12-31 23:40:40 -0900
committerDavid M. Syzdek <david@syzdek.net>2022-12-31 23:40:40 -0900
commit58db357d64ad596461acc8a9402d2d3d4e4eb7bc (patch)
tree314c771819041404af0c916ddaa4fd74b92cda18 /src
parentec51bfeea078d7e5688e19357e6def5a7d096bfa (diff)
downloadotputils-58db357d64ad596461acc8a9402d2d3d4e4eb7bc.tar.gz
otputils-58db357d64ad596461acc8a9402d2d3d4e4eb7bc.tar.bz2
otputils-58db357d64ad596461acc8a9402d2d3d4e4eb7bc.tar.xz
otputils-58db357d64ad596461acc8a9402d2d3d4e4eb7bc.zip
otputil: updating TOTP output
Diffstat (limited to 'src')
-rw-r--r--src/widget-totp.c77
1 files changed, 54 insertions, 23 deletions
diff --git a/src/widget-totp.c b/src/widget-totp.c
index dc7c996..f481e9e 100644
--- a/src/widget-totp.c
+++ b/src/widget-totp.c
@@ -69,6 +69,13 @@ otputil_widget_totp_code(
69 69
70 70
71static int 71static int
72otputil_widget_totp_verbose(
73 otputil_config_t * cnf,
74 const char * code,
75 const char * status );
76
77
78static int
72otputil_widget_totp_verify( 79otputil_widget_totp_verify(
73 otputil_config_t * cnf ); 80 otputil_config_t * cnf );
74 81
@@ -129,10 +136,6 @@ otputil_widget_totp_code(
129 char totp_tx_str[64]; 136 char totp_tx_str[64];
130 const char * code; 137 const char * code;
131 static char buff[OTPUTIL_MAX_CODE_SIZE]; 138 static char buff[OTPUTIL_MAX_CODE_SIZE];
132 uint64_t totp_t0;
133 uint64_t totp_time;
134 char * otp_kstr;
135 char * otp_desc;
136 139
137 assert(cnf != NULL); 140 assert(cnf != NULL);
138 141
@@ -142,26 +145,48 @@ otputil_widget_totp_code(
142 return(1); 145 return(1);
143 }; 146 };
144 147
145 otputil_get_param(NULL, OTPUTIL_OPT_TOTP_X, &totp_tx);
146 otputil_get_param(NULL, OTPUTIL_OPT_TOTP_T0, &totp_t0);
147 otputil_get_param(NULL, OTPUTIL_OPT_TOTP_TIME, &totp_time);
148 otputil_get_param(NULL, OTPUTIL_OPT_HOTP_KSTR, &otp_kstr);
149 otputil_get_param(NULL, OTPUTIL_OPT_DESC, &otp_desc);
150
151 totp_time = ((totp_time)) ? totp_time : (uint64_t)time(NULL);
152
153 if ((cnf->quiet)) 148 if ((cnf->quiet))
154 { 149 {
155 printf("%s\n", code); 150 printf("%s\n", code);
156 return(0); 151 }
157 }; 152 else if ((cnf->verbose))
158 if (!(cnf->verbose))
159 { 153 {
154 otputil_widget_totp_verbose(cnf, code, NULL);
155 }
156 else
157 {
158 otputil_get_param(NULL, OTPUTIL_OPT_TOTP_X, &totp_tx);
160 snprintf(totp_tx_str, sizeof(totp_tx_str), "%" PRId64, totp_tx); 159 snprintf(totp_tx_str, sizeof(totp_tx_str), "%" PRId64, totp_tx);
161 printf("%s (%*" PRId64 "s/%" PRId64 "s)\n", code, (int)strlen(totp_tx_str), otputil_timer(NULL), totp_tx); 160 printf("%s (%*" PRId64 "s/%" PRId64 "s)\n", code, (int)strlen(totp_tx_str), otputil_timer(NULL), totp_tx);
162 return(0);
163 }; 161 };
164 162
163 return(0);
164}
165
166
167int
168otputil_widget_totp_verbose(
169 otputil_config_t * cnf,
170 const char * code,
171 const char * status )
172{
173 uint64_t totp_tx;
174 uint64_t totp_t0;
175 uint64_t totp_time;
176 char * otp_kstr;
177 char * otp_desc;
178
179 assert(cnf != NULL);
180 assert(code != NULL);
181
182 otputil_get_param(NULL, OTPUTIL_OPT_TOTP_X, &totp_tx);
183 otputil_get_param(NULL, OTPUTIL_OPT_TOTP_T0, &totp_t0);
184 otputil_get_param(NULL, OTPUTIL_OPT_TOTP_TIME, &totp_time);
185 otputil_get_param(NULL, OTPUTIL_OPT_HOTP_KSTR, &otp_kstr);
186 otputil_get_param(NULL, OTPUTIL_OPT_DESC, &otp_desc);
187
188 totp_time = ((totp_time)) ? totp_time : (uint64_t)time(NULL);
189
165 // print secret information 190 // print secret information
166 printf("OTP Secret:\n"); 191 printf("OTP Secret:\n");
167 printf(" Description: %s\n", (((otp_desc)) ? otp_desc : "n/a") ); 192 printf(" Description: %s\n", (((otp_desc)) ? otp_desc : "n/a") );
@@ -171,7 +196,11 @@ otputil_widget_totp_code(
171 printf(" UNIX Start Time: %" PRIu64 "\n", totp_t0 ); 196 printf(" UNIX Start Time: %" PRIu64 "\n", totp_t0 );
172 printf(" UNIX Current Time: %" PRIu64 "\n", totp_time ); 197 printf(" UNIX Current Time: %" PRIu64 "\n", totp_time );
173 printf(" Valid for: %" PRIu64 "s\n", otputil_timer(NULL)); 198 printf(" Valid for: %" PRIu64 "s\n", otputil_timer(NULL));
199 if ((cnf->pass))
200 printf(" Expected Code: %s\n", cnf->pass );
174 printf(" Code: %s\n", code ); 201 printf(" Code: %s\n", code );
202 if ((status))
203 printf(" Status: %s\n", status );
175 printf("\n"); 204 printf("\n");
176 205
177 return(0); 206 return(0);
@@ -183,6 +212,8 @@ otputil_widget_totp_verify(
183 otputil_config_t * cnf ) 212 otputil_config_t * cnf )
184{ 213{
185 const char * code; 214 const char * code;
215 const char * status;
216 int rc;
186 217
187 assert(cnf != NULL); 218 assert(cnf != NULL);
188 219
@@ -192,15 +223,15 @@ otputil_widget_totp_verify(
192 return(1); 223 return(1);
193 }; 224 };
194 225
195 if ((strcasecmp(cnf->pass, code))) 226 rc = ((strcasecmp(cnf->pass, code))) ? 2 : 0;
196 { 227 status = ((rc)) ? "invalid code" : "valid code";
197 printf("%s\n", "invalid code");
198 return(2);
199 };
200 228
201 printf("%s\n", "valid code"); 229 if ((cnf->verbose))
230 otputil_widget_totp_verbose(cnf, code, status);
231 else
232 printf("%s\n", status);
202 233
203 return(0); 234 return(rc);
204} 235}
205 236
206/* end of source file */ 237/* end of source file */