10f66f451Sopenharmony_ci/* mkpasswd.c - encrypt the given passwd using salt
20f66f451Sopenharmony_ci *
30f66f451Sopenharmony_ci * Copyright 2013 Ashwini Kumar <ak.ashwini@gmail.com>
40f66f451Sopenharmony_ci * Copyright 2013 Kyungwan Han <asura321@gmail.com>
50f66f451Sopenharmony_ci *
60f66f451Sopenharmony_ci * No Standard
70f66f451Sopenharmony_ci
80f66f451Sopenharmony_ciUSE_MKPASSWD(NEWTOY(mkpasswd, ">2S:m:P#=0<0", TOYFLAG_USR|TOYFLAG_BIN))
90f66f451Sopenharmony_ci
100f66f451Sopenharmony_ciconfig MKPASSWD
110f66f451Sopenharmony_ci  bool "mkpasswd"
120f66f451Sopenharmony_ci  default y
130f66f451Sopenharmony_ci  depends on !TOYBOX_ON_ANDROID
140f66f451Sopenharmony_ci  help
150f66f451Sopenharmony_ci    usage: mkpasswd [-P FD] [-m TYPE] [-S SALT] [PASSWORD] [SALT]
160f66f451Sopenharmony_ci
170f66f451Sopenharmony_ci    Crypt PASSWORD using crypt(3)
180f66f451Sopenharmony_ci
190f66f451Sopenharmony_ci    -P FD	Read password from file descriptor FD
200f66f451Sopenharmony_ci    -m TYPE	Encryption method (des, md5, sha256, or sha512; default is des)
210f66f451Sopenharmony_ci    -S SALT
220f66f451Sopenharmony_ci*/
230f66f451Sopenharmony_ci
240f66f451Sopenharmony_ci#define FOR_mkpasswd
250f66f451Sopenharmony_ci#include "toys.h"
260f66f451Sopenharmony_ci
270f66f451Sopenharmony_ciGLOBALS(
280f66f451Sopenharmony_ci  long P;
290f66f451Sopenharmony_ci  char *m, *S;
300f66f451Sopenharmony_ci)
310f66f451Sopenharmony_ci
320f66f451Sopenharmony_civoid mkpasswd_main(void)
330f66f451Sopenharmony_ci{
340f66f451Sopenharmony_ci  char salt[MAX_SALT_LEN] = {0,};
350f66f451Sopenharmony_ci  int i;
360f66f451Sopenharmony_ci
370f66f451Sopenharmony_ci  if (!TT.m) TT.m = "des";
380f66f451Sopenharmony_ci  if (toys.optc == 2) {
390f66f451Sopenharmony_ci    if (TT.S) error_exit("duplicate salt");
400f66f451Sopenharmony_ci    TT.S = toys.optargs[1];
410f66f451Sopenharmony_ci  }
420f66f451Sopenharmony_ci
430f66f451Sopenharmony_ci  if (-1 == (i = get_salt(salt, TT.m))) error_exit("bad -m");
440f66f451Sopenharmony_ci  if (TT.S) {
450f66f451Sopenharmony_ci    char *s = TT.S;
460f66f451Sopenharmony_ci
470f66f451Sopenharmony_ci    // In C locale, isalnum() means [A-Za-Z0-0]
480f66f451Sopenharmony_ci    while (isalnum(*s) || *s == '.' || *s == '/') s++;
490f66f451Sopenharmony_ci    if (*s) error_exit("salt not in [./A-Za-z0-9]");
500f66f451Sopenharmony_ci
510f66f451Sopenharmony_ci    snprintf(salt+i, sizeof(salt)-i, "%s", TT.S);
520f66f451Sopenharmony_ci  }
530f66f451Sopenharmony_ci
540f66f451Sopenharmony_ci  // Because read_password() doesn't have an fd argument
550f66f451Sopenharmony_ci  if (TT.P) {
560f66f451Sopenharmony_ci    if (dup2(TT.P, 0) == -1) perror_exit("fd");
570f66f451Sopenharmony_ci    close(TT.P);
580f66f451Sopenharmony_ci  }
590f66f451Sopenharmony_ci
600f66f451Sopenharmony_ci  // If we haven't got a password on the command line, read it from tty or FD
610f66f451Sopenharmony_ci  if (!*toys.optargs) {
620f66f451Sopenharmony_ci    // Prompt and read interactively?
630f66f451Sopenharmony_ci    if (isatty(0)) {
640f66f451Sopenharmony_ci      if (read_password(toybuf, sizeof(toybuf), "Password: "))
650f66f451Sopenharmony_ci        perror_exit("password read failed");
660f66f451Sopenharmony_ci    } else {
670f66f451Sopenharmony_ci      for (i = 0; i<sizeof(toybuf)-1; i++) {
680f66f451Sopenharmony_ci        if (!xread(0, toybuf+i, 1)) break;
690f66f451Sopenharmony_ci        if (toybuf[i] == '\n' || toybuf[i] == '\r') break;
700f66f451Sopenharmony_ci      }
710f66f451Sopenharmony_ci      toybuf[i] = 0;
720f66f451Sopenharmony_ci    }
730f66f451Sopenharmony_ci  }
740f66f451Sopenharmony_ci
750f66f451Sopenharmony_ci  // encrypt & print the password
760f66f451Sopenharmony_ci  xprintf("%s\n",crypt(*toys.optargs ? *toys.optargs : toybuf, salt));
770f66f451Sopenharmony_ci}
78