Changeset 827 in openpam


Ignore:
Timestamp:
Oct 22, 2014, 10:03:14 AM (7 years ago)
Author:
Dag-Erling Smørgrav
Message:

Add a calc command that prints the current code.

Location:
trunk/bin/oathkey
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/bin/oathkey/oathkey.1

    r818 r827  
    2929.\" $Id$
    3030.\"
    31 .Dd October 8, 2014
     31.Dd October 22, 2014
    3232.Dt OATHKEY 1
    3333.Os
     
    6868The commands are:
    6969.Bl -tag -width 6n
     70.It Cm calc
     71Compute and display the current code for the given key.
     72If writeback mode is enabled, the user's keyfile is updated to prevent
     73reuse.
    7074.It Cm genkey
    7175Generate a new key.
     
    7882.It Cm setkey Ar uri
    7983Set the user's key to the given otpauth URI.
     84.It Cm uri
     85Deprecated synonym for
     86.Cm geturi .
    8087.It Cm verify Ar code
    8188Verify that the given code is the correct current response for the
     
    8390If writeback mode is enabled and the response matched, the user's
    8491keyfile is updated to prevent reuse.
    85 .It Cm uri
    86 Deprecated synonym for
    87 .Cm geturi .
    8892.El
    8993.Sh SEE ALSO
  • trunk/bin/oathkey/oathkey.c

    r818 r827  
    9393
    9494/*
     95 * Load key from file
     96 */
     97static int
     98oathkey_load(struct oath_key **key)
     99{
     100
     101        if (verbose)
     102                warnx("loading key from %s", keyfile);
     103        if ((*key = oath_key_from_file(keyfile)) == NULL) {
     104                warn("%s", keyfile);
     105                if (errno == EACCES || errno == EPERM)
     106                        return (RET_UNAUTH);
     107                return (RET_ERROR);
     108        }
     109        return (RET_SUCCESS);
     110}
     111
     112/*
    95113 * Save key to file
    96114 * XXX liboath should take care of this for us
     
    184202        if (!isroot && !issameuser)
    185203                return (RET_UNAUTH);
    186         if (verbose)
    187                 warnx("loading key from %s", keyfile);
    188         if ((key = oath_key_from_file(keyfile)) == NULL)
    189                 return (RET_ERROR);
     204        if ((ret = oathkey_load(&key)) != RET_SUCCESS)
     205                return (ret);
    190206        ret = oathkey_print_hex(key);
    191207        oath_key_free(key);
     
    207223        if (!isroot && !issameuser)
    208224                return (RET_UNAUTH);
    209         if (verbose)
    210                 warnx("loading key from %s", keyfile);
    211         if ((key = oath_key_from_file(keyfile)) == NULL)
    212                 return (RET_ERROR);
     225        if ((ret = oathkey_load(&key)) != RET_SUCCESS)
     226                return (ret);
    213227        ret = oathkey_print_uri(key);
    214228        oath_key_free(key);
     
    229243        if (argc < 1)
    230244                return (RET_USAGE);
    231         if (verbose)
    232                 warnx("loading key from %s", keyfile);
    233         if ((key = oath_key_from_file(keyfile)) == NULL)
    234                 return (RET_ERROR);
     245        if ((ret = oathkey_load(&key)) != RET_SUCCESS)
     246                return (ret);
    235247        response = strtoul(*argv, &end, 10);
    236248        if (end == *argv || *end != '\0')
     
    258270
    259271/*
     272 * Compute the current code
     273 */
     274static int
     275oathkey_calc(int argc, char *argv[])
     276{
     277        struct oath_key *key;
     278        unsigned int current;
     279        int ret;
     280
     281        if (argc != 0)
     282                return (RET_USAGE);
     283        (void)argv;
     284        if ((ret = oathkey_load(&key)) != RET_SUCCESS)
     285                return (ret);
     286        if (key->mode == om_totp)
     287                current = oath_totp_current(key);
     288        else if (key->mode == om_hotp)
     289                current = oath_hotp_current(key);
     290        else
     291                current = -1;
     292        if (current == (unsigned int)-1) {
     293                warnx("OATH error");
     294                ret = RET_ERROR;
     295        } else {
     296                printf("%.*d\n", (int)key->digits, current);
     297                ret = RET_SUCCESS;
     298                if (writeback)
     299                        ret = oathkey_save(key);
     300        }
     301        oath_key_free(key);
     302        return (ret);
     303}
     304
     305/*
    260306 * Print usage string and exit.
    261307 */
     
    267313            "\n"
    268314            "Commands:\n"
     315            "    calc        Print the current code\n"
    269316            "    genkey      Generate a new key\n"
    270317            "    getkey      Print the key in hexadecimal form\n"
     
    355402        if (strcmp(cmd, "help") == 0)
    356403                ret = RET_USAGE;
     404        else if (strcmp(cmd, "calc") == 0)
     405                ret = oathkey_calc(argc, argv);
    357406        else if (strcmp(cmd, "genkey") == 0)
    358407                ret = oathkey_genkey(argc, argv);
    359408        else if (strcmp(cmd, "getkey") == 0)
    360                 ret = oathkey_getkey(argc, argv);       
     409                ret = oathkey_getkey(argc, argv);
    361410        else if (strcmp(cmd, "geturi") == 0 || strcmp(cmd, "uri") == 0)
    362411                ret = oathkey_geturi(argc, argv);
Note: See TracChangeset for help on using the changeset viewer.