File: wvpam.cc

package info (click to toggle)
wvstreams 4.0.2-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,420 kB
  • ctags: 6,518
  • sloc: cpp: 52,544; sh: 5,770; ansic: 810; makefile: 461; tcl: 114; perl: 18
file content (274 lines) | stat: -rw-r--r-- 6,643 bytes parent folder | download
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
/*
 * Worldvisions Weaver Software:
 *   Copyright (C) 1997-2003 Net Integration Technologies, Inc.
 *
 * A WvStream that authenticates with PAM before allowing any reading or
 * writing.  See wvpam.h.
 */
#include "wvlog.h"
#include "wvpam.h"
#include "wvautoconf.h"

// If PAM not installed at compile time, stub this out
#ifndef HAVE_SECURITY_PAM_APPL_H

WvPam::WvPam(WvStringParm _appname)
    : log("PAM Auth", WvLog::Info), appname(_appname)
{
    err.seterr("Compiled without PAM Support!\n");
}
         
          
WvPam::WvPam(WvStringParm _appname, WvStringParm rhost,
	     WvStringParm user, WvStringParm password) 
    : log("PAM Auth", WvLog::Info), appname(_appname)  
{
    err.seterr("Compiled without PAM Support!\n");
}


WvPam::~WvPam()
{
}

bool WvPam::authenticate(WvStringParm rhost, WvStringParm user, WvStringParm password)
{
    return false;
}

WvString WvPam::getuser() const
{
    return WvString::null;
}
 
 
void WvPam::getgroups(WvStringList &l) const
{
}

#else   // HAVE_SECURITY_PAM_APPL_H

#include <security/pam_appl.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>

#include "wvaddr.h"


class WvPamData
{
public:
    pam_handle_t *pamh;
    int status;
    WvString failmsg, user;
    WvStringList groups;

    WvPamData()
	: pamh(NULL), status(PAM_SUCCESS), user("")
	{ }
    
    WvPamData(WvStringParm _failmsg)
	: pamh(NULL), status(PAM_SUCCESS), failmsg(_failmsg)
	{ }
};


/** noconv: null PAM conversation function */
#if HAVE_BROKEN_PAM
int noconv(int num_msg, struct pam_message **msgm,
        struct pam_response **response, void *userdata)
#else
int noconv(int num_msg, const struct pam_message **msgm,
        struct pam_response **response, void *userdata)
#endif
{
    // if you need to ask things, it won't work
    return PAM_CONV_ERR;
}


// The password gets passed in from userdata, and we simply echo it back
// out in the response... *sigh* This is because pam expects this function
// to actually interact with the user, and get their password.
#if HAVE_BROKEN_PAM
static int passconv(int num_msg, struct pam_message **msgm,
        struct pam_response **response, void *userdata)
#else
static int passconv(int num_msg, const struct pam_message **msgm,
        struct pam_response **response, void *userdata)
#endif
{
    struct pam_response *password_echo;
    
    password_echo = (struct pam_response *)calloc(num_msg,
                                                  sizeof(struct pam_response));
    password_echo->resp = (char *)userdata;
    password_echo->resp_retcode = 0;
    
    *response = password_echo;
    
    return PAM_SUCCESS;
}

WvPam::WvPam(WvStringParm _appname)
    : log("PAM Auth", WvLog::Info), appname(_appname)
{
    init();
}
 

WvPam::WvPam(WvStringParm _appname, WvStringParm rhost,
             WvStringParm user, WvStringParm password)
    : log("PAM Auth", WvLog::Info), appname(_appname)
{
    if (init())
        authenticate(rhost, user, password);
}

WvPam::~WvPam()
{
    log(WvLog::Debug2, "Shutting down PAM Session for: %s\n", appname);
    if (d->status == PAM_SUCCESS)
        pam_close_session(d->pamh, 0);
    pam_end(d->pamh, d->status);
    d->groups.zap();
    delete d;       
}
 
 
bool WvPam::init()
{
    d = new WvPamData();
    log(WvLog::Debug2, "Starting up PAM Session for: %s\n", appname);
    err.seterr("Not yet authenticated...");
    
    struct pam_conv c;
    c.conv = noconv;  
    c.appdata_ptr = NULL;

    d->pamh = NULL;
    d->status = pam_start(appname, d->user, &c, &d->pamh);
    if (check_pam_status("pam_start")) return true;
    return false;
}

bool WvPam::authenticate(WvStringParm rhost, WvStringParm user, WvStringParm password)
{
    if (!!rhost)
    {
        d->status = pam_set_item(d->pamh, PAM_RHOST, rhost);
        if (!check_pam_status("rhost setup")) return false; 
    }

    if (!!user)
    {
        d->user = user;
        d->status = pam_set_item(d->pamh, PAM_USER, user);
        if (!check_pam_status("user setup")) return false;
    }

    if (!!password)
    {
        struct pam_conv c;
        c.conv = passconv;
        c.appdata_ptr = strdup(password);
        d->status = pam_set_item(d->pamh, PAM_CONV, &c);
        if (!check_pam_status("conversation setup")) return false;
        
        d->status = pam_set_item(d->pamh, PAM_AUTHTOK, password);
        if (!check_pam_status("password setup")) return false;   
    }
     
#if HAVE_BROKEN_PAM
    void *x = NULL;
#else
    const void *x = NULL;
#endif
    d->status = pam_get_item(d->pamh, PAM_USER, &x);
    if (!check_pam_status("get username")) return false;
    d->user = (const char *)x;

    log("Starting Authentication for %s\n", d->user);
    
    d->status = pam_authenticate(d->pamh, PAM_DISALLOW_NULL_AUTHTOK | PAM_SILENT);
    if (!check_pam_status("authentication")) return false;

    d->status = pam_acct_mgmt(d->pamh, PAM_DISALLOW_NULL_AUTHTOK | PAM_SILENT);
    if (!check_pam_status("account management")) return false;
    
    d->status = pam_setcred(d->pamh, PAM_ESTABLISH_CRED);
    if (!check_pam_status("credentials")) return false;  

    d->status = pam_open_session(d->pamh, 0);
    if (!check_pam_status("session open")) return false;
    
    log("Session open as user '%s'\n", d->user);
    
    // If we made it here, we're clear of everything, and we can go
    // to a no error status.
    err.noerr();
    
    return true;
}


bool WvPam::check_pam_status(WvStringParm s)
{
    if (d->status == PAM_SUCCESS)
    {
        log(WvLog::Debug2, "PAM %s succeeded.\n", s);
        return true;
    }
    else
    {   
        WvString msg("PAM %s failed: %s\n", s, pam_strerror(d->pamh, d->status));
        log(WvLog::Info, msg);
        err.seterr(msg);
        d->user = WvString::null;
        d->groups.zap();
        return false;   
    }
}
 
 
WvString WvPam::getuser() const
{
    return d->user;
}
 
 
void WvPam::getgroups(WvStringList &l) const
{
    assert(l.isempty());

    // Cache after the first time...
    if (d->groups.isempty())
    {
        setgrent();
        struct group *gr;
        while ((gr = getgrent()))
        {
            for (char **i = gr->gr_mem; *i != NULL; i++)
            {
                if (strcmp(*i, d->user))
                {                       
                    d->groups.append(new WvString(gr->gr_name), true);
                    break;                                            
                }
            }
        }    
        endgrent();
    }
     
    WvStringList::Iter i(d->groups);
    for (i.rewind(); i.next(); )
        l.append(new WvString(*i), true);
}






#endif // HAVE_SECURITY_PAM_APPL_H