[BACK]Return to encrypt.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / encrypt

Annotation of src/usr.bin/encrypt/encrypt.c, Revision 1.1

1.1     ! downsj      1: /*     $OpenBSD$       */
        !             2:
        !             3: /*
        !             4:  * Copyright (c) 1996, Jason Downs.  All rights reserved.
        !             5:  *
        !             6:  * Redistribution and use in source and binary forms, with or without
        !             7:  * modification, are permitted provided that the following conditions
        !             8:  * are met:
        !             9:  * 1. Redistributions of source code must retain the above copyright
        !            10:  *    notice, this list of conditions and the following disclaimer.
        !            11:  * 2. Redistributions in binary form must reproduce the above copyright
        !            12:  *    notice, this list of conditions and the following disclaimer in the
        !            13:  *    documentation and/or other materials provided with the distribution.
        !            14:  *
        !            15:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
        !            16:  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
        !            17:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
        !            18:  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
        !            19:  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
        !            20:  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
        !            21:  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
        !            22:  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            23:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            24:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            25:  * SUCH DAMAGE.
        !            26:  */
        !            27:
        !            28: #include <stdio.h>
        !            29: #include <sys/types.h>
        !            30: #include <err.h>
        !            31: #include <string.h>
        !            32: #include <unistd.h>
        !            33:
        !            34: /*
        !            35:  * Very simple little program, for encrypting passwords from the command
        !            36:  * line.  Useful for scripts and such.
        !            37:  */
        !            38:
        !            39: extern char *__progname;
        !            40: extern char *optarg;
        !            41: extern int optind;
        !            42:
        !            43: void usage()
        !            44: {
        !            45:     errx(1, "usage: %s [-m] [-s salt] [string]", __progname);
        !            46: }
        !            47:
        !            48: int main(argc, argv)
        !            49:     int argc;
        !            50:     char *argv[];
        !            51: {
        !            52:     int opt;
        !            53:     int do_md5 = 0;
        !            54:     char *salt = (char *)NULL;
        !            55:
        !            56:     while ((opt = getopt(argc, argv, "ms:")) != -1) {
        !            57:        switch (opt) {
        !            58:        case 'm':
        !            59:            do_md5 = 1;
        !            60:            break;
        !            61:        case 's':
        !            62:            salt = optarg;
        !            63:            break;
        !            64:        default:
        !            65:            usage();
        !            66:        }
        !            67:     }
        !            68:
        !            69:     if (do_md5 && (salt != (char *)NULL))
        !            70:        usage();
        !            71:
        !            72:     if (!do_md5 && (salt == (char *)NULL))
        !            73:        usage();
        !            74:
        !            75:     if ((argc - optind) < 1) {
        !            76:        char line[BUFSIZ];
        !            77:
        !            78:        /* Encrypt stdin to stdout. */
        !            79:        while (!feof(stdin) && (fgets(line, sizeof(line), stdin) != NULL)) {
        !            80:            if ((line[0] == '\0') || (line[0] == '\n'))
        !            81:                continue;
        !            82:
        !            83:            /* Kill the newline. */
        !            84:            if (line[strlen(line)] == '\n')
        !            85:                line[strlen(line)] = '\0';
        !            86:
        !            87:            fputs(crypt(line, (do_md5 ? "$1$" : salt)), stdout);
        !            88:            fputc('\n', stdout);
        !            89:        }
        !            90:     } else {
        !            91:        char *string;
        !            92:
        !            93:        /* Perhaps it isn't worth worrying about, but... */
        !            94:        string = strdup(argv[optind]);
        !            95:        if (string == (char *)NULL)
        !            96:            err(1, NULL);
        !            97:        /* Wipe the argument. */
        !            98:        bzero(argv[optind], strlen(argv[optind]));
        !            99:
        !           100:        fputs(crypt(string, (do_md5 ? "$1$" : salt)), stdout);
        !           101:        fputc('\n', stdout);
        !           102:
        !           103:        /* Wipe our copy, before we free it. */
        !           104:        bzero(string, strlen(string));
        !           105:        free(string);
        !           106:     }
        !           107:     exit(0);
        !           108: }