-
Notifications
You must be signed in to change notification settings - Fork 9
/
pop_auth.c
93 lines (74 loc) · 2.12 KB
/
pop_auth.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* Author: Vivek Prakash
* Date: Aug 1, 2011
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <netdb.h>
int main(int argc, char **argv)
{
const int port = 110;
const char *auth_host = "192.168.121.26";
struct sockaddr_in server;
struct hostent *host;
int n, sfd;
bzero((char *)&server, sizeof(server));
if (inet_aton(auth_host, &server.sin_addr) == 0)
err(1, "Not a valid auth host: %s\n", auth_host);
/*
host = (struct hostent *) gethostbyaddr ((char *)&server.sin_addr.s_addr, sizeof (server.sin_addr.s_addr), AF_INET);
if(host == NULL)
err(1, "%s: No such host", auth_host);
*/
if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
err(1, "Cannot create socket");
server.sin_family = AF_INET;
// bcopy((char *)host->h_addr, (char *)&server.sin_addr.s_addr, host->h_length);
server.sin_port = htons(port);
if (connect(sfd, (struct sockaddr *)&server, sizeof(server)) < 0 )
err(1, "Cannot connect to the socket");
char buf[256];
bzero(buf, 256);
char input[256];
char *key;
/* Send User info */
printf("\nUSER : ");
key = "USER ";
fgets(input, 255, stdin);
strcat(buf, key);
strcat(buf, input);
printf("sending %s\n", buf);
if ((n = write(sfd, buf, strlen(buf))) < 0 )
err(1, "Cannot write to the socket");
/* Get first reply: connection is established */
bzero(buf, 256);
if ((n = read(sfd, buf, 255)) < 0 )
err(1, "Cannot read from the socket");
printf("Reply: %s\n", buf);
/* Get second reply: valid mailbox */
bzero(buf, 256);
if ((n = read(sfd, buf, 255)) < 0 )
err(1, "Cannot read from the socket");
printf("Reply: %s\n", buf);
/* Send Pass info */
printf("\nPASS: ");
key = "PASS ";
fgets(input, 255, stdin);
strcat(buf, key);
strcat(buf, input);
printf("sending %s\n", buf);
if ((n = write(sfd, buf, strlen(buf))) < 0 )
err(1, "Cannot write to the socket");
/* Get third reply: password ok */
bzero(buf, 256);
if ((n = read(sfd, buf, 255)) < 0 )
err(1, "Cannot read from the socket");
printf("Reply: %s\n", buf);
close(sfd);
return 0;
}