aboutsummaryrefslogtreecommitdiffstats
path: root/camel/providers/nntp/camel-nntp-auth.c
blob: 6eba6cc066ab8b208c78b9ec5180f1eba9d81a5e (plain) (blame)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* camel-nntp-auth.c : authentication for nntp */

/* 
 *
 * Copyright (C) 2000 Ximian, Inc. <toshok@ximian.com>
 *
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <camel-nntp-auth.h>
#include <camel-nntp-store.h>
#include <camel-nntp-resp-codes.h>
#include <camel-exception.h>
#include <camel-session.h>

int
camel_nntp_auth_authenticate (CamelNNTPStore *store, CamelException *ex)
{
    CamelService *service = CAMEL_SERVICE (store);
    CamelSession *session = camel_service_get_session (service);
    int resp;

    if (!service->url->authmech && !service->url->passwd) {
        gchar *prompt;
            
        prompt = g_strdup_printf (_("Please enter the NNTP password for %s@%s"),
                      service->url->user, service->url->host);
        service->url->passwd =
            camel_session_get_password (session, prompt,
                            TRUE, service, "password", ex);
        g_free (prompt);
            
        if (!service->url->passwd) {
            camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL, 
                         "You didn\'t enter a password.");
            resp = 666;
            goto done;
        }
    }

    /* first send username */
    resp = camel_nntp_command (store, ex, NULL, "AUTHINFO USER %s", service->url->user);

    if (resp == NNTP_AUTH_REJECTED) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
                      _("Server rejected username"));
        goto done;

    }
    else if (resp != NNTP_AUTH_CONTINUE) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
                      _("Failed to send username to server"));
        goto done;
    }

    /* then send the username if the server asks for it */
    resp = camel_nntp_command (store, ex, NULL, "AUTHINFO PASS %s", service->url->passwd);

    if (resp == NNTP_AUTH_REJECTED) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
                      _("Server rejected username/password"));
        goto done;
    }

 done:

    if (service->url->passwd) {
        /* let's be paranoid */
        memset (service->url->passwd, 0, strlen (service->url->passwd));
        g_free (service->url->passwd);
        service->url->passwd = NULL;
    }
    return resp;
}