1 Index: libtransmission/native-auth.c
2 ===================================================================
3 --- libtransmission/native-auth.c (revision 0)
4 +++ libtransmission/native-auth.c (revision 0)
6 +/******************************************************************************
9 + * Copyright (c) Transmission authors and contributors
11 + * Permission is hereby granted, free of charge, to any person obtaining a
12 + * copy of this software and associated documentation files (the "Software"),
13 + * to deal in the Software without restriction, including without limitation
14 + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 + * and/or sell copies of the Software, and to permit persons to whom the
16 + * Software is furnished to do so, subject to the following conditions:
18 + * The above copyright notice and this permission notice shall be included in
19 + * all copies or substantial portions of the Software.
21 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 + * DEALINGS IN THE SOFTWARE.
28 + *****************************************************************************/
30 +#include "native-auth.h"
33 +#if defined(HAVE_NATIVE_AUTH_ENABLED) && defined(linux)
34 + #define NATIVE_AUTH_ENABLED 1
36 + #include "transmission.h"
42 + #include <security/pam_appl.h>
45 +#if defined(NATIVE_AUTH_ENABLED)
46 +static struct pam_response *
47 +makePAMResponse( const char * data )
49 + struct pam_response * response = ( struct pam_response * )tr_malloc( sizeof( struct pam_response ) );
52 + tr_err( _("Cannot allocate memory for PAM response") );
56 + response->resp_retcode = PAM_SUCCESS;
57 + response->resp = strdup( data );
63 +pamAuthConversation( int numMsg,
64 + const struct pam_message ** messages,
65 + struct pam_response ** responses,
69 + bool errorOccurred = false;
73 + *responses = (struct pam_response *)tr_malloc0( numMsg*sizeof( struct pam_response ) );
76 + return PAM_CONV_ERR;
79 + for( msgIndex = 0; msgIndex < numMsg && !errorOccurred; ++msgIndex )
81 + const struct pam_message * msg = *messages++;
82 + switch( msg->msg_style )
84 + case PAM_PROMPT_ECHO_OFF:
85 + case PAM_PROMPT_ECHO_ON:
87 + const char * password = (char *)data;
88 + struct pam_response * response = makePAMResponse( password );
91 + errorOccurred = true;
93 + responses[msgIndex] = response;
102 + errorOccurred = true;
103 + tr_err( _("Error while PAM conversation occurred") );
107 + if( errorOccurred )
109 + tr_free( *responses );
110 + return PAM_CONV_ERR;
113 + return PAM_SUCCESS;
117 +performPAMAuthentication( const char * user, const char * password, bool * isSuccess )
119 + const char * PAM_AUTH_MODULE = "auth";
120 + struct pam_conv pamConversation = { pamAuthConversation, (void *) password };
122 + pam_handle_t * pamHandle = NULL;
124 + *isSuccess = false;
126 + pamError = pam_start( PAM_AUTH_MODULE, user, &pamConversation, &pamHandle );
127 + if ( PAM_SUCCESS != pamError )
129 + tr_err( _( "Failed to start PAM session: %s" ), pam_strerror( pamHandle, pamError ) );
133 + pamError = pam_authenticate( pamHandle, PAM_SILENT );
134 + if ( PAM_SUCCESS == pamError )
137 + pamError = pam_end( pamHandle, /* last status */ 0 );
139 + if ( PAM_SUCCESS != pamError )
141 + tr_err( _( "Failed to close PAM session: %s" ), pam_strerror( pamHandle, pamError ) );
147 +tr_performNativeAuthentication( const char * user, const char * password, tr_rpcNativeAuthenticationResult * result )
149 + bool isSuccessAuth = false;
151 +#ifdef HAVE_NATIVE_AUTH_ENABLED
154 + performPAMAuthentication( user, password, &isSuccessAuth );
156 + #warning Native user authentication for this platform is not supported yet.
159 +#endif /* HAVE_NATIVE_AUTH_ENABLED */
161 + *result = ( isSuccessAuth ) ? NATIVE_AUTHENTICATON_SUCCESS : NATIVE_AUTHENTICATON_FAIL;