#include "packer.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
static char *buf;
static uint_t *offsets;
static uint_t off_idx = 0;
static size_t off_size = 0;
#define FNAME_TEMPLATE "/var/tmp/authtok_check.XXXXXX"
#define MAXTMP 64
static FILE *tmpfp[MAXTMP];
static int tmpfp_idx = 0;
#define MODNAME "pam_authtok_check::packer"
int
writeout(void)
{
int i = 0;
char tmpname[sizeof (FNAME_TEMPLATE)];
int fd;
if (tmpfp_idx == MAXTMP) {
syslog(LOG_ERR, MODNAME ": too many temporary "
"files (maximum %d exceeded)", MAXTMP);
return (-1);
}
(void) strcpy(tmpname, FNAME_TEMPLATE);
if ((fd = mkstemp(tmpname)) == -1) {
syslog(LOG_ERR, MODNAME ": mkstemp() failed: %s\n",
strerror(errno));
return (-1);
}
(void) unlink(tmpname);
if ((tmpfp[tmpfp_idx] = fdopen(fd, "w+F")) == NULL) {
syslog(LOG_ERR, MODNAME ": fdopen failed: %s",
strerror(errno));
(void) close(fd);
return (-1);
}
while (i < off_idx) {
if (fprintf(tmpfp[tmpfp_idx], "%s\n", &buf[offsets[i++]]) < 0) {
syslog(LOG_ERR, MODNAME ": write to file failed: %s",
strerror(errno));
(void) close(fd);
return (-1);
}
}
tmpfp_idx++;
return (0);
}
int
insert_word(int off)
{
#define CHUNK 10000
if (off_idx == off_size) {
uint_t *tmp;
off_size += CHUNK;
tmp = realloc(offsets, sizeof (uint_t) * off_size);
if (tmp == NULL) {
syslog(LOG_ERR, MODNAME ": out of memory");
free(offsets);
off_idx = off_size = 0;
offsets = NULL;
return (-1);
}
offsets = tmp;
}
offsets[off_idx++] = off;
return (0);
}
int
translate(char *buf, size_t size)
{
char *p, *q, *e;
char c;
int wordstart;
e = &buf[size];
wordstart = 0;
for (p = buf, q = buf; q < e; q++) {
c = *q;
if (c >= 'A' && c <= 'Z') {
*(p++) = tolower(c);
} else if (c == '\n') {
*(p++) = '\0';
if (p-&buf[wordstart] > MAXWORDLEN)
buf[wordstart+MAXWORDLEN-1] = '\0';
if (insert_word(wordstart) != 0)
return (-1);
wordstart = p-buf;
} else if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
*(p++) = c;
}
}
return (0);
}
int
compare(const void *a, const void *b)
{
int idx_a = *(uint_t *)a, idx_b = *(uint_t *)b;
return (strcmp(&buf[idx_a], &buf[idx_b]));
}
int
sort_file(char *fname)
{
int fd;
struct stat statbuf;
ssize_t n;
int ret = -1;
if ((fd = open(fname, O_RDONLY)) == -1) {
syslog(LOG_ERR, MODNAME ": failed to open %s: %s",
fname, strerror(errno));
return (-1);
}
if (fstat(fd, &statbuf) == -1) {
syslog(LOG_ERR, MODNAME ": fstat() failed (%s)",
strerror(errno));
(void) close(fd);
return (-1);
}
if ((buf = malloc(statbuf.st_size + 1)) == NULL) {
syslog(LOG_ERR, MODNAME ": out of memory");
goto error;
}
n = read(fd, buf, statbuf.st_size);
if (n == -1) {
if (errno == EINVAL)
syslog(LOG_ERR, MODNAME ": %s is too big. "
"Split the file into smaller files.", fname);
else
syslog(LOG_ERR, MODNAME ": read failed: %s",
strerror(errno));
goto error;
}
if (translate(buf, n) == 0) {
qsort((void *)offsets, off_idx, sizeof (int), compare);
if (writeout() == 0)
ret = 0;
}
error:
(void) close(fd);
if (buf != NULL)
free(buf);
if (offsets != NULL)
free(offsets);
offsets = NULL;
off_size = 0;
off_idx = 0;
return (ret);
}
int
merge_files(PWDICT *pwp)
{
int ti;
char *words[MAXTMP];
char lastword[MAXWORDLEN];
int choice;
lastword[0] = '\0';
for (ti = 0; ti < tmpfp_idx; ti++)
if ((words[ti] = malloc(MAXWORDLEN)) == NULL) {
while (--ti >= 0)
free(words[ti]);
return (-1);
}
for (ti = 0; ti < tmpfp_idx; ti++) {
(void) fseek(tmpfp[ti], 0, SEEK_SET);
(void) fgets(words[ti], MAXWORDLEN, tmpfp[ti]);
words[ti][MAXWORDLEN-1] = '\0';
}
while (tmpfp_idx != 0) {
choice = 0;
for (ti = 1; ti < tmpfp_idx; ti++)
if (strcmp(words[choice], words[ti]) > 0)
choice = ti;
(void) Chomp(words[choice]);
if (words[choice][0] != '\0' &&
strcmp(lastword, words[choice]) != 0) {
(void) PutPW(pwp, words[choice]);
(void) strncpy(lastword, words[choice], MAXWORDLEN);
}
if (fgets(words[choice], MAXWORDLEN, tmpfp[choice]) == NULL) {
(void) fclose(tmpfp[choice]);
tmpfp[choice] = tmpfp[tmpfp_idx - 1];
tmpfp_idx--;
} else
words[choice][MAXWORDLEN-1] = '\0';
}
return (0);
}
int
packer(char *list, char *path)
{
PWDICT *pwp;
char *listcopy, *fname;
int ret = 0;
if ((listcopy = strdup(list)) == NULL) {
syslog(LOG_ERR, MODNAME ": out of memory");
return (-1);
}
if (!(pwp = PWOpen(path, "wF")))
return (-1);
fname = strtok(listcopy, " \t,");
while (ret == 0 && fname != NULL) {
if ((ret = sort_file(fname)) == 0)
fname = strtok(NULL, " \t,");
}
free(listcopy);
if (ret == 0)
ret = merge_files(pwp);
(void) PWClose(pwp);
return (ret);
}