static int pop_retr (popserver server, int msgno, FILE *arg) { char *line; int ret; if (pop_retrieve_first (server, msgno, &line)) { snprintf (Errmsg, sizeof Errmsg, "Error from POP server: %s", pop_error); return (NOTOK); } while ((ret = pop_retrieve_next (server, &line)) >= 0) { if (! line) break; if (mbx_write (line, ret, arg) != OK) { strcpy (Errmsg, strerror (errno)); pop_close (server); return (NOTOK); } } if (ret) { snprintf (Errmsg, sizeof Errmsg, "Error from POP server: %s", pop_error); return (NOTOK); } return (OK); }
static int pop_retr (popserver server, int msgno, FILE *arg) { char *line; int ret; if (pop_retrieve_first (server, msgno, &line)) { char *msg = concat ("Error from POP server: ", pop_error, ""); strncpy (Errmsg, msg, sizeof (Errmsg)); Errmsg[sizeof (Errmsg)-1] = '\0'; free (msg); return (NOTOK); } while ((ret = pop_retrieve_next (server, &line)) >= 0) { if (! line) break; if (mbx_write (line, ret, arg) != OK) { strcpy (Errmsg, strerror (errno)); pop_close (server); return (NOTOK); } } if (ret) { char *msg = concat ("Error from POP server: ", pop_error, ""); strncpy (Errmsg, msg, sizeof (Errmsg)); Errmsg[sizeof (Errmsg)-1] = '\0'; free (msg); return (NOTOK); } return (OK); }
static int popmail (char *mailbox, char *outfile, int preserve, char *password, int reverse_order) { int nmsgs, nbytes; register int i; int mbfi; FILE *mbf; char *getenv (const char *); popserver server; int start, end, increment; char *user, *hostname; user = mailbox; if ((hostname = strchr (mailbox, ':'))) *hostname++ = '\0'; server = pop_open (hostname, user, password, POP_NO_GETPASS); if (! server) { error ("Error connecting to POP server: %s", pop_error, 0); return EXIT_FAILURE; } if (pop_stat (server, &nmsgs, &nbytes)) { error ("Error getting message count from POP server: %s", pop_error, 0); return EXIT_FAILURE; } if (!nmsgs) { pop_close (server); return EXIT_SUCCESS; } mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666); if (mbfi < 0) { pop_close (server); error ("Error in open: %s, %s", strerror (errno), outfile); return EXIT_FAILURE; } if (fchown (mbfi, getuid (), -1) != 0) { int fchown_errno = errno; struct stat st; if (fstat (mbfi, &st) != 0 || st.st_uid != getuid ()) { pop_close (server); error ("Error in fchown: %s, %s", strerror (fchown_errno), outfile); return EXIT_FAILURE; } } if ((mbf = fdopen (mbfi, "wb")) == NULL) { pop_close (server); error ("Error in fdopen: %s", strerror (errno), 0); close (mbfi); unlink (outfile); return EXIT_FAILURE; } if (reverse_order) { start = nmsgs; end = 1; increment = -1; } else { start = 1; end = nmsgs; increment = 1; } for (i = start; i * increment <= end * increment; i += increment) { mbx_delimit_begin (mbf); if (pop_retr (server, i, mbf) != OK) { error ("%s", Errmsg, 0); close (mbfi); return EXIT_FAILURE; } mbx_delimit_end (mbf); fflush (mbf); if (ferror (mbf)) { error ("Error in fflush: %s", strerror (errno), 0); pop_close (server); close (mbfi); return EXIT_FAILURE; } } /* On AFS, a call to write only modifies the file in the local * workstation's AFS cache. The changes are not written to the server * until a call to fsync or close is made. Users with AFS home * directories have lost mail when over quota because these checks were * not made in previous versions of movemail. */ #ifdef BSD_SYSTEM if (fsync (mbfi) < 0) { error ("Error in fsync: %s", strerror (errno), 0); return EXIT_FAILURE; } #endif if (close (mbfi) == -1) { error ("Error in close: %s", strerror (errno), 0); return EXIT_FAILURE; } if (! preserve) for (i = 1; i <= nmsgs; i++) { if (pop_delete (server, i)) { error ("Error from POP server: %s", pop_error, 0); pop_close (server); return EXIT_FAILURE; } } if (pop_quit (server)) { error ("Error from POP server: %s", pop_error, 0); return EXIT_FAILURE; } return EXIT_SUCCESS; }
/* * Function: pop_open (char *host, char *username, char *password, * int flags) * * Purpose: Establishes a connection with a post-office server, and * completes the authorization portion of the session. * * Arguments: * host The server host with which the connection should be * established. Optional. If omitted, internal * heuristics will be used to determine the server host, * if possible. * username * The username of the mail-drop to access. Optional. * If omitted, internal heuristics will be used to * determine the username, if possible. * password * The password to use for authorization. If omitted, * internal heuristics will be used to determine the * password, if possible. * flags A bit mask containing flags controlling certain * functions of the routine. Valid flags are defined in * the file pop.h * * Return value: Upon successful establishment of a connection, a * non-null popserver will be returned. Otherwise, null will be * returned, and the string variable pop_error will contain an * explanation of the error. */ popserver pop_open (char *host, char *username, char *password, int flags) { int sock; popserver server; /* Determine the user name */ if (! username) { username = getenv ("USER"); if (! (username && *username)) { username = getlogin (); if (! (username && *username)) { struct passwd *passwd; passwd = getpwuid (getuid ()); if (passwd && passwd->pw_name && *passwd->pw_name) { username = passwd->pw_name; } else { strcpy (pop_error, "Could not determine username"); return (0); } } } } /* * Determine the mail host. */ if (! host) { host = getenv ("MAILHOST"); } #ifdef HESIOD if ((! host) && (! (flags & POP_NO_HESIOD))) { struct hes_postoffice *office; office = hes_getmailhost (username); if (office && office->po_type && (! strcmp (office->po_type, "POP")) && office->po_name && *office->po_name && office->po_host && *office->po_host) { host = office->po_host; username = office->po_name; } } #endif #ifdef MAILHOST if (! host) { host = MAILHOST; } #endif if (! host) { strcpy (pop_error, "Could not determine POP server"); return (0); } /* Determine the password */ #ifdef KERBEROS #define DONT_NEED_PASSWORD (! (flags & POP_NO_KERBEROS)) #else #define DONT_NEED_PASSWORD 0 #endif if ((! password) && (! DONT_NEED_PASSWORD)) { if (! (flags & POP_NO_GETPASS)) { password = getpass ("Enter POP password:"******"Could not determine POP password"); return (0); } } if (password) /* always true, detected 20060515 */ flags |= POP_NO_KERBEROS; else password = username; /* dead code, detected 20060515 */ /** "kpop" service is never used: look for 20060515 to see why **/ sock = socket_connection (host, flags); if (sock == -1) return (0); server = (popserver) malloc (sizeof (struct _popserver)); if (! server) { strcpy (pop_error, "Out of memory in pop_open"); return (0); } server->buffer = (char *) malloc (GETLINE_MIN); if (! server->buffer) { strcpy (pop_error, "Out of memory in pop_open"); free ((char *) server); return (0); } server->file = sock; server->data = 0; server->buffer_index = 0; server->buffer_size = GETLINE_MIN; server->in_multi = false; server->trash_started = false; if (getok (server)) return (0); /* * I really shouldn't use the pop_error variable like this, but.... */ if (strlen (username) > ERROR_MAX - 6) { pop_close (server); strcpy (pop_error, "Username too long; recompile pop.c with larger ERROR_MAX"); return (0); } sprintf (pop_error, "USER %s", username); if (sendline (server, pop_error) || getok (server)) { return (0); } if (strlen (password) > ERROR_MAX - 6) { pop_close (server); strcpy (pop_error, "Password too long; recompile pop.c with larger ERROR_MAX"); return (0); } sprintf (pop_error, "PASS %s", password); if (sendline (server, pop_error) || getok (server)) { return (0); } return (server); }
static int popmail (char *mailbox, char *outfile, int preserve, char *password, int reverse_order) { int nmsgs, nbytes; register int i; int mbfi; FILE *mbf; popserver server; int start, end, increment; char *user, *hostname; user = mailbox; if ((hostname = strchr (mailbox, ':'))) *hostname++ = '\0'; server = pop_open (hostname, user, password, POP_NO_GETPASS); if (! server) { error ("Error connecting to POP server: %s", pop_error, 0); return EXIT_FAILURE; } if (pop_stat (server, &nmsgs, &nbytes)) { error ("Error getting message count from POP server: %s", pop_error, 0); return EXIT_FAILURE; } if (!nmsgs) { pop_close (server); return EXIT_SUCCESS; } mbfi = open (outfile, O_WRONLY | O_BINARY | O_CREAT | O_EXCL, 0666); if (mbfi < 0) { pop_close (server); error ("Error in open: %s, %s", strerror (errno), outfile); return EXIT_FAILURE; } if (fchown (mbfi, getuid (), -1) != 0) { int fchown_errno = errno; struct stat st; if (fstat (mbfi, &st) != 0 || st.st_uid != getuid ()) { pop_close (server); error ("Error in fchown: %s, %s", strerror (fchown_errno), outfile); return EXIT_FAILURE; } } if ((mbf = fdopen (mbfi, "wb")) == NULL) { pop_close (server); error ("Error in fdopen: %s", strerror (errno), 0); close (mbfi); unlink (outfile); return EXIT_FAILURE; } if (reverse_order) { start = nmsgs; end = 1; increment = -1; } else { start = 1; end = nmsgs; increment = 1; } for (i = start; i * increment <= end * increment; i += increment) { mbx_delimit_begin (mbf); if (pop_retr (server, i, mbf) != OK) { error ("%s", Errmsg, 0); close (mbfi); return EXIT_FAILURE; } mbx_delimit_end (mbf); fflush (mbf); if (ferror (mbf)) { error ("Error in fflush: %s", strerror (errno), 0); pop_close (server); close (mbfi); return EXIT_FAILURE; } } if (fsync (mbfi) != 0 && errno != EINVAL) { error ("Error in fsync: %s", strerror (errno), 0); close (mbfi); return EXIT_FAILURE; } if (close (mbfi) != 0) { error ("Error in close: %s", strerror (errno), 0); return EXIT_FAILURE; } if (! preserve) for (i = 1; i <= nmsgs; i++) { if (pop_delete (server, i)) { error ("Error from POP server: %s", pop_error, 0); pop_close (server); return EXIT_FAILURE; } } if (pop_quit (server)) { error ("Error from POP server: %s", pop_error, 0); return EXIT_FAILURE; } return EXIT_SUCCESS; }