what's the purpose of dup2 in this code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#define MAXLINE 256
#define MAX_PASSWORD 8

#define SUCCESS "Password verified\n"
#define INVALID "Invalid password\n"
#define NO_USER "No such user\n"

int main(void) {
    char user_id[MAXLINE];
    char password[MAXLINE];

    if (fgets(user_id, MAXLINE, stdin) == NULL) {
        perror("fgets");
        exit(1);
    }
    if (fgets(password, MAXLINE, stdin) == NULL) {
        perror("fgets");
        exit(1);
    }

    int fd[2];
    if (pipe(fd) == -1) {
        perror("pipe");
        exit(1);
    }

    pid_t pid = fork();
    if (pid == -1) {
        perror("fork");
        exit(1);
    }

    if (pid == 0) {
        close(fd[0]);
        dup2(fd[1], STDOUT_FILENO);
        close(fd[1]);
        execlp("./validate", "./validate", NULL);
        perror("execlp");
        exit(1);
    } else {
        close(fd[1]);
        dup2(fd[0], STDIN_FILENO);
        close(fd[0]);
        wait(NULL);
        char buf[MAXLINE];
        if (fgets(buf, MAXLINE, stdin) == NULL) {
            perror("fgets");
            exit(1);
        }
        if (strcmp(buf, SUCCESS) == 0) {
            printf("Password verified");
        } else if (strcmp(buf, INVALID) == 0) {
            printf("Invalid password");
        } else if (strcmp(buf, NO_USER) == 0) {
            printf("No such user");
        } else {
            printf("Unknown error");
        }
    }

    return 0;
}
