Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / providers / nntp / camel-nntp-auth.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-nntp-auth.c : authentication for nntp */
3
4 /* 
5  *
6  * Copyright (C) 2000 Ximian, Inc. <toshok@ximian.com>
7  *
8  * This program is free software; you can redistribute it and/or 
9  * modify it under the terms of version 2 of the GNU Lesser General Public 
10  * License as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20  * USA
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28
29 #include "camel-exception.h"
30 #include "camel-session.h"
31
32 #include "camel-nntp-auth.h"
33 #include "camel-nntp-store.h"
34 #include "camel-nntp-resp-codes.h"
35
36 int
37 camel_nntp_auth_authenticate (CamelNNTPStore *store, CamelException *ex)
38 {
39         CamelService *service = CAMEL_SERVICE (store);
40         CamelSession *session = camel_service_get_session (service);
41         int resp;
42
43         if (!service->url->authmech && !service->url->passwd) {
44                 gchar *prompt;
45                         
46                 prompt = g_strdup_printf (_("Please enter the NNTP password for %s@%s"),
47                                           service->url->user, service->url->host);
48                 service->url->passwd =
49                         camel_session_get_password (session, prompt,
50                                                     TRUE, service, "password", ex);
51                 g_free (prompt);
52                         
53                 if (!service->url->passwd) {
54                         camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL, 
55                                              "You didn\'t enter a password.");
56                         resp = 666;
57                         goto done;
58                 }
59         }
60
61         /* first send username */
62         resp = camel_nntp_command (store, ex, NULL, "AUTHINFO USER %s", service->url->user);
63
64         if (resp == NNTP_AUTH_REJECTED) {
65                 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
66                                       _("Server rejected username"));
67                 goto done;
68
69         }
70         else if (resp != NNTP_AUTH_CONTINUE) {
71                 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
72                                       _("Failed to send username to server"));
73                 goto done;
74         }
75
76         /* then send the username if the server asks for it */
77         resp = camel_nntp_command (store, ex, NULL, "AUTHINFO PASS %s", service->url->passwd);
78
79         if (resp == NNTP_AUTH_REJECTED) {
80                 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
81                                       _("Server rejected username/password"));
82                 goto done;
83         }
84
85  done:
86
87         if (service->url->passwd) {
88                 /* let's be paranoid */
89                 memset (service->url->passwd, 0, strlen (service->url->passwd));
90                 g_free (service->url->passwd);
91                 service->url->passwd = NULL;
92         }
93         return resp;
94 }