aboutsummaryrefslogtreecommitdiff
path: root/src/appl/popper/pop_init.c
blob: b5f05c6900c1073c2ab749658a20ba16ea77d189 (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 * Copyright (c) 1989 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "popper.h"

#ifdef KERBEROS
#ifdef KRB4
#ifdef KRB5
 #error you can only use one of KRB4, KRB5
#endif
#include <krb.h>
AUTH_DAT kdata;
#endif /* KRB4 */
#ifdef KRB5    
#include "krb5.h"
#include "com_err.h"
#include <ctype.h>
krb5_principal ext_client;
krb5_context pop_context;
char *client_name;
#endif /* KRB5 */
#endif /* KERBEROS */

/* 
 *  init:   Start a Post Office Protocol session
 */

pop_init(p,argcount,argmessage)
POP     *       p;
int             argcount;
char    **      argmessage;
{

    struct sockaddr_in      cs;                 /*  Communication parameters */
    struct hostent      *   ch;                 /*  Client host information */
    int                     errflag = 0;
    int                     c;
    int                     len;
    extern char         *   optarg;
    int                     options = 0;
    int                     sp = 0;             /*  Socket pointer */
    char                *   trace_file_name;
    int                     standalone = 0;

    /*  Initialize the POP parameter block */
    memset ((char *)p, 0, (int)sizeof(POP));

    /*  Save my name in a global variable */
    p->myname = argmessage[0];

    /*  Get the name of our host */
    (void)gethostname(p->myhost,MAXHOSTNAMELEN);

    /*  Open the log file */
#ifdef SYSLOG42
    (void)openlog(p->myname,0);
#else
    (void)openlog(p->myname,POP_LOGOPTS,POP_FACILITY);
#endif

    /*  Process command line arguments */
    while ((c = getopt(argcount,argmessage,"dt:s")) != EOF)
        switch (c) {

            /*  Debugging requested */
            case 'd':
                p->debug++;
                options |= SO_DEBUG;
                break;

            /*  Debugging trace file specified */
            case 't':
                p->debug++;
                if ((p->trace = fopen(optarg,"a+")) == NULL) {
                    pop_log(p,POP_PRIORITY,
                        "Unable to open trace file \"%s\", err = %d",
                            optarg,errno);
                    exit(-1);
                }
                trace_file_name = optarg;
                break;

	    /* Standalone operation */
	    case 's':
		standalone++;
		break;

            /*  Unknown option received */
            default:
                errflag++;
        }

    /*  Exit if bad options specified */
    if (errflag) {
        (void)fprintf(stderr,"Usage: %s [-d]\n",argmessage[0]);
        exit(-1);
    }

    if (standalone) {
	int acc, sock;
	struct sockaddr_in sin;
	struct servent *spr;

	if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
	    syslog(LOG_ERR, "socket: %m");
	    exit(3);
	}

	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = 0;

#ifdef KERBEROS
#ifndef KPOP_SERVICE
#define KPOP_SERVICE "kpop"
#endif
	if (!(spr = getservbyname(KPOP_SERVICE, "tcp"))) {
	    syslog(LOG_ERR, "%s/tcp: unknown service", KPOP_SERVICE);
	    exit(3);
	}
#else
	if (!(spr = getservbyname("pop", "tcp"))) {
	    syslog(LOG_ERR, "pop/tcp: unknown service");
	    exit(3);
	}
#endif
	sin.sin_port = spr->s_port;
	if (bind(sock, (struct sockaddr *)&sin, sizeof(sin))) {
	    syslog(LOG_ERR, "bind: %m");
	    exit(3);
	}
	if (listen(sock, 1) == -1) {
	    syslog(LOG_ERR, "listen: %m");
	    exit(3);
	}
	len = sizeof(cs);
	if ((acc = accept(sock, (struct sockaddr *)&cs, &len)) == -1) {
	    syslog(LOG_ERR, "accept: %m");
	    exit(3);
	}
	dup2(acc, sp);
	close(sock);
	close(acc);
    }

    /*  Get the address and socket of the client to whom I am speaking */
    len = sizeof(cs);
    if (getpeername(sp,(struct sockaddr *)&cs,&len) < 0){
        pop_log(p,POP_PRIORITY,
            "Unable to obtain socket and address of client, err = %d",errno);
        exit(-1);
    }

    /*  Save the dotted decimal form of the client's IP address 
        in the POP parameter block */
    p->ipaddr = inet_ntoa(cs.sin_addr);

    /*  Save the client's port */
    p->ipport = ntohs(cs.sin_port);

    /*  Get the canonical name of the host to whom I am speaking */
    ch = gethostbyaddr((char *) &cs.sin_addr, sizeof(cs.sin_addr), AF_INET);
    if (ch == NULL){
        pop_log(p,POP_PRIORITY,
            "Unable to get canonical name of client, err = %d",errno);
        p->client = p->ipaddr;
    }
    /*  Save the cannonical name of the client host in 
        the POP parameter block */
    else {

#ifndef BIND43
        p->client = (char *) ch->h_name;
#else
#       include <arpa/nameser.h>
#       include <resolv.h>

        /*  Distrust distant nameservers */
#if (__RES < 19931104)
        extern struct state     _res;
#endif
        struct hostent      *   ch_again;
        char            *   *   addrp;

        /*  We already have a fully-qualified name */
        _res.options &= ~RES_DEFNAMES;

        /*  See if the name obtained for the client's IP 
            address returns an address */
        if ((ch_again = gethostbyname(ch->h_name)) == NULL) {
            pop_log(p,POP_PRIORITY,
                "Client at \"%s\" resolves to an unknown host name \"%s\"",
                    p->ipaddr,ch->h_name);
            p->client = p->ipaddr;
        }
        else {
            /*  Save the host name (the previous value was 
                destroyed by gethostbyname) */
            p->client = ch_again->h_name;

            /*  Look for the client's IP address in the list returned 
                for its name */
            for (addrp=ch_again->h_addr_list; *addrp; ++addrp)
                if (memcmp(*addrp,&(cs.sin_addr),sizeof(cs.sin_addr)) == 0) break;

            if (!*addrp) {
                pop_log (p,POP_PRIORITY,
                    "Client address \"%s\" not listed for its host name \"%s\"",
                        p->ipaddr,ch->h_name);
                p->client = p->ipaddr;
            }
        }
#endif
    }

    /*  Create input file stream for TCP/IP communication */
    if ((p->input = fdopen(sp,"r")) == NULL){
        pop_log(p,POP_PRIORITY,
            "Unable to open communication stream for input, err = %d",errno);
        exit (-1);
    }

    /*  Create output file stream for TCP/IP communication */
    if ((p->output = fdopen(sp,"w")) == NULL){
        pop_log(p,POP_PRIORITY,
            "Unable to open communication stream for output, err = %d",errno);
        exit (-1);
    }

    pop_log(p,POP_PRIORITY,
        "(v%s) Servicing request from \"%s\" at %s\n",
            VERSION,p->client,p->ipaddr);

#ifdef DEBUG
    if (p->trace)
        pop_log(p,POP_PRIORITY,
            "Tracing session and debugging information in file \"%s\"",
                trace_file_name);
    else if (p->debug)
        pop_log(p,POP_PRIORITY,"Debugging turned on");
#endif

    return(authenticate(p, &cs));
}

authenticate(p, addr)
     POP     *p;
     struct sockaddr_in *addr;
{

#ifdef KERBEROS
#ifdef KRB4
    Key_schedule schedule;
    KTEXT_ST ticket;
    char instance[INST_SZ];  
    char version[9];
    int auth;
  
    strcpy(instance, "*");
    auth = krb_recvauth(0L, 0, &ticket, "pop", instance,
	  	        addr, (struct sockaddr_in *) NULL,
			&kdata, "", schedule, version);
    
    if (auth != KSUCCESS) {
        pop_msg(p, POP_FAILURE, "Kerberos authentication failure: %s", 
		krb_err_txt[auth]);
	pop_log(p, POP_WARNING, "%s: (%s.%s@%s) %s", p->client, 
		kdata.pname, kdata.pinst, kdata.prealm, krb_err_txt[auth]);
        exit(-1);
    }

#ifdef DEBUG
    pop_log(p, POP_DEBUG, "%s.%s@%s (%s): ok", kdata.pname, 
	    kdata.pinst, kdata.prealm, inet_ntoa(addr->sin_addr));
#endif /* DEBUG */

    strcpy(p->user, kdata.pname);
#endif

#ifdef KRB5
    krb5_auth_context auth_context = NULL;
    krb5_error_code retval;
    krb5_principal server;
    krb5_ticket *ticket;
    int sock = 0;

    retval = krb5_init_context(&pop_context);
    if (retval) {
	    com_err("pop_authenticate", retval, "while initializing krb5");
	    exit(1);
    }

    if (retval = krb5_sname_to_principal(pop_context, p->myhost, "pop", 
					 KRB5_NT_SRV_HST, &server)) {
	pop_msg(p, POP_FAILURE,
		"server '%s' mis-configured, can't get principal--%s",
		p->myhost, error_message(retval));
	pop_log(p, POP_WARNING,  "%s: mis-configured, can't get principal--%s",
		p->client, error_message(retval));
	exit(-1);
    }

    if (retval = krb5_recvauth(pop_context, &auth_context, (krb5_pointer)&sock,
			       "KPOPV1.0", server,
			       0, 	/* no flags */
			       NULL,	/* default keytab */
			       &ticket	/* need ticket for client name */
			       )) {
	pop_msg(p, POP_FAILURE, "recvauth failed--%s", error_message(retval));
	pop_log(p, POP_WARNING, "%s: recvauth failed--%s",
		p->client, error_message(retval));
	exit(-1);
    }
    krb5_free_principal(pop_context, server);
    krb5_auth_con_free(pop_context, auth_context);
    if (retval = krb5_copy_principal(pop_context, ticket->enc_part2->client,
				     &ext_client)) {
	pop_msg(p, POP_FAILURE, "unable to copy principal--%s",
		error_message(retval));
	pop_msg(p, POP_FAILURE, "unable to copy principal (%s)",
		inet_ntoa(addr->sin_addr));
	exit(-1);
    }
    krb5_free_ticket(pop_context, ticket);
    if (retval = krb5_unparse_name(pop_context, ext_client, &client_name)) {
	pop_msg(p, POP_FAILURE, "name not parsable--%s",
		error_message(retval));
	pop_log(p, POP_DEBUG, "name not parsable (%s)",
		inet_ntoa(addr->sin_addr));
	exit(-1);
    }
#ifdef DEBUG
    pop_log(p, POP_DEBUG, "%s (%s): ok", client_name, inet_ntoa(addr->sin_addr));
#endif /* DEBUG */

    if (retval= krb5_aname_to_localname(pop_context, ext_client, 
					sizeof(p->user), p->user)) {
	pop_msg(p, POP_FAILURE, "unable to convert aname(%s) to localname --%s",
		client_name,
		error_message(retval));
	pop_log(p, POP_DEBUG, "unable to convert aname to localname (%s)",
		client_name);
	exit(-1);
    }
#endif /* KRB5 */
#endif /* KERBEROS */

    return(POP_SUCCESS);
}