#include #include #include #include #include #include #include #include #include "dns.h" #include "qrschedule.h" #include "mmap.h" #include "log.h" #include "queuefn.h" /** * loadrecips - load all remaining recipients from disk * @recips: pointer to list structure where recipients will be stored * @returns: number of items loaded, negative on error * * step 1: create the list of recipients in memory */ int loadrecips(struct recip **recips, const unsigned int inr) { unsigned int rcpts = 0; unsigned int idx = 0; int fd; char *fbuf; q_off_t flen, pos; char qfile[35]; fd = queuefn(qfile, sizeof(qfile), "queue/remote", inr); if (fd == 0) return 0; printf("%s\n", qfile); fbuf = mmap_name(qfile, &flen, &fd); if (fbuf == NULL) return 0; *recips = NULL; pos = 0; while (pos < flen - 1) { if (fbuf[pos] == 'D') { void *endp = memchr(fbuf + pos + 1, '\0', flen - pos - 1); if (endp == NULL) { const char *errmsg[] = { "corrupt file \"", qfile, "\": missing \\0", NULL }; freerecips(*recips, rcpts); log_writen(LOG_ERR, errmsg); errno = EINVAL; return 0; } pos += (((unsigned long)endp) - ((unsigned long)fbuf)); } else if (fbuf[pos] == 'T') { void *endp = memchr(fbuf + pos + 1, '\0', flen - pos - 1); struct recip *tmp; size_t addrlen; if (endp == NULL) { const char *errmsg[] = { "corrupt file \"", qfile, "\": missing \\0", NULL }; freerecips(*recips, rcpts); log_writen(LOG_ERR, errmsg); errno = EINVAL; return 0; } tmp = realloc(*recips, sizeof(*tmp) * (rcpts + 1)); if (tmp == NULL) { freerecips(*recips, rcpts); errno = ENOMEM; return 0; } addrlen = strlen(fbuf + pos + 1); tmp[rcpts].address = malloc(addrlen + 1); if (tmp->address == NULL) { freerecips(*recips, rcpts); errno = ENOMEM; return 0; } memcpy(tmp[rcpts].address, fbuf + pos + 1, addrlen + 1); tmp[rcpts].next = NULL; tmp[rcpts].index = idx; tmp[rcpts].pos = pos; tmp[rcpts].msg = NULL; tmp[rcpts].mx = NULL; tmp[rcpts].domain = NULL; tmp[rcpts].next = NULL; *recips = tmp; rcpts++; pos += addrlen + 2; } else { char ch[2]; const char *errmsg[] = { "corrupt file \"", qfile, "\": unexpected intro '", ch, "'", NULL }; ch[0] = fbuf[pos]; ch[1] = '\0'; freerecips(*recips, rcpts); log_writen(LOG_ERR, errmsg); errno = EINVAL; return 0; } idx++; } return rcpts; }