2014-06-05 11:48:45 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-06-09 09:51:02 +00:00
|
|
|
#include <errno.h>
|
2014-06-05 11:48:45 +00:00
|
|
|
#include <pwd.h>
|
2014-06-09 09:51:02 +00:00
|
|
|
#include <shadow.h>
|
2014-06-05 11:48:45 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-06-09 09:51:02 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include "../passwd.h"
|
2014-06-05 11:48:45 +00:00
|
|
|
#include "../text.h"
|
|
|
|
#include "../util.h"
|
|
|
|
|
2014-06-09 11:58:40 +00:00
|
|
|
/* Returns -1 on error, 0 for incorrect password
|
|
|
|
* and 1 if all went OK */
|
2014-06-09 09:51:02 +00:00
|
|
|
int
|
|
|
|
pw_check(struct passwd *pw, const char *pass)
|
|
|
|
{
|
|
|
|
char *cryptpass, *p;
|
|
|
|
struct spwd *spw;
|
|
|
|
|
|
|
|
p = pw->pw_passwd;
|
2014-06-09 11:58:40 +00:00
|
|
|
if (p[0] == '!' || p[0] == '*') {
|
|
|
|
weprintf("denied\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-09 09:51:02 +00:00
|
|
|
|
2014-06-09 12:03:19 +00:00
|
|
|
if (pw->pw_passwd[0] == '\0') {
|
|
|
|
if (pass[0] == '\0')
|
|
|
|
return 1;
|
|
|
|
weprintf("incorrect password\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-09 09:51:02 +00:00
|
|
|
|
|
|
|
if (pw->pw_passwd[0] == 'x' && pw->pw_passwd[1] == '\0') {
|
|
|
|
errno = 0;
|
|
|
|
spw = getspnam(pw->pw_name);
|
2014-06-09 11:58:40 +00:00
|
|
|
if (errno) {
|
|
|
|
weprintf("getspnam: %s:", pw->pw_name);
|
|
|
|
return -1;
|
|
|
|
} else if (!spw) {
|
|
|
|
weprintf("who are you?\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-09 09:51:02 +00:00
|
|
|
p = spw->sp_pwdp;
|
2014-06-09 11:58:40 +00:00
|
|
|
if (p[0] == '!' || p[0] == '*') {
|
|
|
|
weprintf("denied\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-09 09:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cryptpass = crypt(pass, p);
|
2014-06-09 11:58:40 +00:00
|
|
|
if (!cryptpass) {
|
|
|
|
weprintf("crypt:");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (strcmp(cryptpass, p) != 0) {
|
|
|
|
weprintf("incorrect password\n");
|
2014-06-09 09:51:02 +00:00
|
|
|
return 0;
|
2014-06-09 11:58:40 +00:00
|
|
|
}
|
2014-06-09 09:51:02 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pw_copy(int ffd, int tfd, const struct passwd *newpw)
|
|
|
|
{
|
|
|
|
struct passwd pw;
|
|
|
|
char *buf = NULL, *p;
|
|
|
|
size_t size = 0;
|
|
|
|
FILE *from, *to;
|
|
|
|
|
|
|
|
from = fdopen(ffd, "r");
|
|
|
|
if (!from) {
|
|
|
|
weprintf("fdopen:");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
to = fdopen(tfd, "w");
|
|
|
|
if (!to) {
|
|
|
|
weprintf("fdopen:");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
while (agetline(&buf, &size, from) != -1) {
|
|
|
|
p = strdup(buf);
|
|
|
|
if (!p) {
|
|
|
|
weprintf("strdup:");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (newpw) {
|
|
|
|
if (pw_scan(p, &pw) < 0)
|
|
|
|
return -1;
|
|
|
|
if (strcmp(pw.pw_name, newpw->pw_name) == 0) {
|
|
|
|
fprintf(to, "%s:%s:%u:%u:%s:%s:%s\n",
|
|
|
|
newpw->pw_name,
|
|
|
|
newpw->pw_passwd,
|
|
|
|
newpw->pw_uid,
|
|
|
|
newpw->pw_gid,
|
|
|
|
newpw->pw_gecos,
|
|
|
|
newpw->pw_dir,
|
|
|
|
newpw->pw_shell);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(to, "%s", buf);
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
fflush(to);
|
|
|
|
free(buf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-05 11:48:45 +00:00
|
|
|
int
|
|
|
|
pw_scan(char *bp, struct passwd *pw)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
memset(pw, 0, sizeof(*pw));
|
|
|
|
|
|
|
|
/* login name */
|
|
|
|
p = strsep(&bp, ":");
|
|
|
|
if (!p || *p == '\0')
|
|
|
|
goto corrupt;
|
|
|
|
pw->pw_name = p;
|
|
|
|
|
|
|
|
/* passwd */
|
|
|
|
p = strsep(&bp, ":");
|
|
|
|
if (!p)
|
|
|
|
goto corrupt;
|
|
|
|
pw->pw_passwd = p;
|
|
|
|
|
|
|
|
/* uid */
|
|
|
|
p = strsep(&bp, ":");
|
|
|
|
if (!p)
|
|
|
|
goto corrupt;
|
|
|
|
pw->pw_uid = estrtol(p, 10);
|
|
|
|
|
|
|
|
/* gid */
|
|
|
|
p = strsep(&bp, ":");
|
|
|
|
if (!p)
|
|
|
|
goto corrupt;
|
|
|
|
pw->pw_gid = estrtol(p, 10);
|
|
|
|
|
|
|
|
/* user name or comment */
|
|
|
|
p = strsep(&bp, ":");
|
|
|
|
if (!p)
|
|
|
|
goto corrupt;
|
|
|
|
pw->pw_gecos = p;
|
|
|
|
|
|
|
|
/* home directory */
|
|
|
|
p = strsep(&bp, ":");
|
|
|
|
if (!p)
|
|
|
|
goto corrupt;
|
|
|
|
pw->pw_dir = p;
|
|
|
|
|
|
|
|
/* optional shell */
|
|
|
|
p = strsep(&bp, ":");
|
|
|
|
if (!p)
|
|
|
|
goto corrupt;
|
|
|
|
pw->pw_shell = p;
|
|
|
|
|
|
|
|
/* look for redundant entries */
|
|
|
|
p = strsep(&bp, ":");
|
|
|
|
if (p)
|
|
|
|
goto corrupt;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
corrupt:
|
|
|
|
weprintf("corrupted passwd entry\n");
|
|
|
|
return -1;
|
|
|
|
}
|