0ae1b7ad7524acd887ecf12dd63d8691e4fc5cb2
[freeradius.git] / src / modules / rlm_unix / rlm_unix.c
1 /*
2  * rlm_unix.c   authentication: Unix user authentication
3  *              accounting:     Functions to write radwtmp file.
4  *              Also contains handler for "Group".
5  *
6  * Version:     $Id$
7  *
8  *   This program is free software; you can redistribute it and/or modify
9  *   it under the terms of the GNU General Public License as published by
10  *   the Free Software Foundation; either version 2 of the License, or
11  *   (at your option) any later version.
12  *
13  *   This program is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with this program; if not, write to the Free Software
20  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  *
22  * Copyright 2000,2006  The FreeRADIUS server project
23  * Copyright 2000  Jeff Carneal <jeff@apex.net>
24  * Copyright 2000  Alan Curry <pacman@world.std.com>
25  */
26
27 #include        <freeradius-devel/ident.h>
28 RCSID("$Id$")
29
30 #include        <freeradius-devel/radiusd.h>
31
32 #include        <grp.h>
33 #include        <pwd.h>
34 #include        <sys/stat.h>
35
36 #include "config.h"
37
38 #ifdef HAVE_SHADOW_H
39 #  include      <shadow.h>
40 #endif
41
42 #ifdef OSFC2
43 #  include      <sys/security.h>
44 #  include      <prot.h>
45 #endif
46
47 #ifdef OSFSIA
48 #  include      <sia.h>
49 #  include      <siad.h>
50 #endif
51
52 #include        <freeradius-devel/modules.h>
53 #include        <freeradius-devel/sysutmp.h>
54
55 static char trans[64] =
56    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
57 #define ENC(c) trans[c]
58
59 struct unix_instance {
60         const char *radwtmp;
61 };
62
63 static const CONF_PARSER module_config[] = {
64         { "radwtmp",  PW_TYPE_STRING_PTR,
65           offsetof(struct unix_instance,radwtmp), NULL,   "NULL" },
66
67         { NULL, -1, 0, NULL, NULL }             /* end the list */
68 };
69
70
71 /*
72  *      The Group = handler.
73  */
74 static int groupcmp(void *instance, UNUSED REQUEST *req, VALUE_PAIR *request,
75                     VALUE_PAIR *check, VALUE_PAIR *check_pairs,
76                     VALUE_PAIR **reply_pairs)
77 {
78         struct passwd   *pwd;
79         struct group    *grp;
80         char            **member;
81         char            *username;
82         int             retval;
83         VALUE_PAIR      *vp;
84
85         instance = instance;
86         check_pairs = check_pairs;
87         reply_pairs = reply_pairs;
88
89         /*
90          *      No user name, doesn't compare.
91          */
92         vp = pairfind(request, PW_STRIPPED_USER_NAME, 0);
93         if (!vp) {
94                 vp = pairfind(request, PW_USER_NAME, 0);
95                 if (!vp) {
96                         return -1;
97                 }
98         }
99         username = (char *)vp->vp_strvalue;
100
101         pwd = getpwnam(username);
102         if (pwd == NULL)
103                 return -1;
104
105         grp = getgrnam((char *)check->vp_strvalue);
106         if (grp == NULL)
107                 return -1;
108
109         retval = (pwd->pw_gid == grp->gr_gid) ? 0 : -1;
110         if (retval < 0) {
111                 for (member = grp->gr_mem; *member && retval; member++) {
112                         if (strcmp(*member, pwd->pw_name) == 0)
113                                 retval = 0;
114                 }
115         }
116         return retval;
117 }
118
119
120 /*
121  *      Detach.
122  */
123 static int unix_detach(void *instance)
124 {
125 #define inst ((struct unix_instance *)instance)
126
127         paircompare_unregister(PW_GROUP, groupcmp);
128 #ifdef PW_GROUP_NAME
129         paircompare_unregister(PW_GROUP_NAME, groupcmp);
130 #endif
131 #undef inst
132         free(instance);
133         return 0;
134 }
135
136 /*
137  *      Read the config
138  */
139 static int unix_instantiate(CONF_SECTION *conf, void **instance)
140 {
141         struct unix_instance *inst;
142
143         /*
144          *      Allocate room for the instance.
145          */
146         inst = *instance = rad_malloc(sizeof(*inst));
147         if (!inst) {
148                 return -1;
149         }
150         memset(inst, 0, sizeof(*inst));
151
152         /*
153          *      Parse the configuration, failing if we can't do so.
154          */
155         if (cf_section_parse(conf, inst, module_config) < 0) {
156                 unix_detach(inst);
157                 return -1;
158         }
159
160         /* FIXME - delay these until a group file has been read so we know
161          * groupcmp can actually do something */
162         paircompare_register(PW_GROUP, PW_USER_NAME, groupcmp, NULL);
163 #ifdef PW_GROUP_NAME /* compat */
164         paircompare_register(PW_GROUP_NAME, PW_USER_NAME, groupcmp, NULL);
165 #endif
166
167 #undef inst
168
169         return 0;
170 }
171
172
173 /*
174  *      Pull the users password from where-ever, and add it to
175  *      the given vp list.
176  */
177 static int unix_getpw(UNUSED void *instance, REQUEST *request,
178                       VALUE_PAIR **vp_list)
179 {
180         const char      *name;
181         const char      *encrypted_pass;
182 #ifdef HAVE_GETSPNAM
183         struct spwd     *spwd = NULL;
184 #endif
185 #ifdef OSFC2
186         struct pr_passwd *pr_pw;
187 #else
188         struct passwd   *pwd;
189 #endif
190 #ifdef HAVE_GETUSERSHELL
191         char            *shell;
192 #endif
193         VALUE_PAIR      *vp;
194
195         /*
196          *      We can only authenticate user requests which HAVE
197          *      a User-Name attribute.
198          */
199         if (!request->username) {
200                 return RLM_MODULE_NOOP;
201         }
202
203         name = (char *)request->username->vp_strvalue;
204         encrypted_pass = NULL;
205
206 #ifdef OSFC2
207         if ((pr_pw = getprpwnam(name)) == NULL)
208                 return RLM_MODULE_NOTFOUND;
209         encrypted_pass = pr_pw->ufld.fd_encrypt;
210
211         /*
212          *      Check if account is locked.
213          */
214         if (pr_pw->uflg.fg_lock!=1) {
215                 radlog(L_AUTH, "rlm_unix: [%s]: account locked", name);
216                 return RLM_MODULE_USERLOCK;
217         }
218 #else /* OSFC2 */
219         if ((pwd = getpwnam(name)) == NULL) {
220                 return RLM_MODULE_NOTFOUND;
221         }
222         encrypted_pass = pwd->pw_passwd;
223 #endif /* OSFC2 */
224
225 #ifdef HAVE_GETSPNAM
226         /*
227          *      See if there is a shadow password.
228          *
229          *      Only query the _system_ shadow file if the encrypted
230          *      password from the passwd file is < 10 characters (i.e.
231          *      a valid password would never crypt() to it).  This will
232          *      prevents users from using NULL password fields as things
233          *      stand right now.
234          */
235         if ((encrypted_pass == NULL) || (strlen(encrypted_pass) < 10)) {
236                 if ((spwd = getspnam(name)) == NULL) {
237                         return RLM_MODULE_NOTFOUND;
238                 }
239                 encrypted_pass = spwd->sp_pwdp;
240         }
241 #endif  /* HAVE_GETSPNAM */
242
243 /*
244  *      These require 'pwd != NULL', which isn't true on OSFC2
245  */
246 #ifndef OSFC2
247 #ifdef DENY_SHELL
248         /*
249          *      Users with a particular shell are denied access
250          */
251         if (strcmp(pwd->pw_shell, DENY_SHELL) == 0) {
252                 radlog_request(L_AUTH, 0, request,
253                                "rlm_unix: [%s]: invalid shell", name);
254                 return RLM_MODULE_REJECT;
255         }
256 #endif
257
258 #ifdef HAVE_GETUSERSHELL
259         /*
260          *      Check /etc/shells for a valid shell. If that file
261          *      contains /RADIUSD/ANY/SHELL then any shell will do.
262          */
263         while ((shell = getusershell()) != NULL) {
264                 if (strcmp(shell, pwd->pw_shell) == 0 ||
265                     strcmp(shell, "/RADIUSD/ANY/SHELL") == 0) {
266                         break;
267                 }
268         }
269         endusershell();
270         if (shell == NULL) {
271                 radlog_request(L_AUTH, 0, request, "[%s]: invalid shell [%s]",
272                        name, pwd->pw_shell);
273                 return RLM_MODULE_REJECT;
274         }
275 #endif
276 #endif /* OSFC2 */
277
278 #if defined(HAVE_GETSPNAM) && !defined(M_UNIX)
279         /*
280          *      Check if password has expired.
281          */
282         if (spwd && spwd->sp_expire > 0 &&
283             (request->timestamp / 86400) > spwd->sp_expire) {
284                 radlog_request(L_AUTH, 0, request, "[%s]: password has expired", name);
285                 return RLM_MODULE_REJECT;
286         }
287 #endif
288
289 #if defined(__FreeBSD__) || defined(bsdi) || defined(_PWF_EXPIRE)
290         /*
291          *      Check if password has expired.
292          */
293         if ((pwd->pw_expire > 0) &&
294             (request->timestamp > pwd->pw_expire)) {
295                 radlog_request(L_AUTH, 0, request, "[%s]: password has expired", name);
296                 return RLM_MODULE_REJECT;
297         }
298 #endif
299
300         /*
301          *      We might have a passwordless account.
302          *
303          *      FIXME: Maybe add Auth-Type := Accept?
304          */
305         if (encrypted_pass[0] == 0)
306                 return RLM_MODULE_NOOP;
307
308         vp = pairmake("Crypt-Password", encrypted_pass, T_OP_SET);
309         if (!vp) return RLM_MODULE_FAIL;
310
311         pairmove(vp_list, &vp);
312         pairfree(&vp);          /* might not be NULL; */
313
314         return RLM_MODULE_UPDATED;
315 }
316
317
318 /*
319  *      Pull the users password from where-ever, and add it to
320  *      the given vp list.
321  */
322 static int unix_authorize(void *instance, REQUEST *request)
323 {
324         return unix_getpw(instance, request, &request->config_items);
325 }
326
327 /*
328  *      Pull the users password from where-ever, and add it to
329  *      the given vp list.
330  */
331 static int unix_authenticate(void *instance, REQUEST *request)
332 {
333 #ifdef OSFSIA
334         char            *info[2];
335         char            *progname = "radius";
336         SIAENTITY       *ent = NULL;
337
338         info[0] = progname;
339         info[1] = NULL;
340         if (sia_ses_init (&ent, 1, info, NULL, name, NULL, 0, NULL) !=
341             SIASUCCESS)
342                 return RLM_MODULE_NOTFOUND;
343         if ((ret = sia_ses_authent (NULL, passwd, ent)) != SIASUCCESS) {
344                 if (ret & SIASTOP)
345                         sia_ses_release (&ent);
346                 return RLM_MODULE_NOTFOUND;
347         }
348         if (sia_ses_estab (NULL, ent) != SIASUCCESS) {
349                 sia_ses_release (&ent);
350                 return RLM_MODULE_NOTFOUND;
351         }
352 #else  /* OSFSIA */
353         int rcode;
354         VALUE_PAIR *vp = NULL;
355
356         if (!request->password ||
357             (request->password->attribute != PW_USER_PASSWORD)) {
358                 radlog_request(L_AUTH, 0, request, "Attribute \"User-Password\" is required for authentication.");
359                 return RLM_MODULE_INVALID;
360         }
361
362         rcode = unix_getpw(instance, request, &vp);
363         if (rcode != RLM_MODULE_UPDATED) return rcode;
364
365         /*
366          *      0 means "ok"
367          */
368         if (fr_crypt_check((char *) request->password->vp_strvalue,
369                              (char *) vp->vp_strvalue) != 0) {
370                 radlog_request(L_AUTH, 0, request, "invalid password \"%s\"",
371                                request->username->vp_strvalue);
372                 return RLM_MODULE_REJECT;
373         }
374 #endif /* OSFFIA */
375
376         return RLM_MODULE_OK;
377 }
378
379
380 /*
381  *      UUencode 4 bits base64. We use this to turn a 4 byte field
382  *      (an IP address) into 6 bytes of ASCII. This is used for the
383  *      wtmp file if we didn't find a short name in the naslist file.
384  */
385 static char *uue(void *in)
386 {
387         int i;
388         static unsigned char res[7];
389         unsigned char *data = (unsigned char *)in;
390
391         res[0] = ENC( data[0] >> 2 );
392         res[1] = ENC( ((data[0] << 4) & 060) + ((data[1] >> 4) & 017) );
393         res[2] = ENC( ((data[1] << 2) & 074) + ((data[2] >> 6) & 03) );
394         res[3] = ENC( data[2] & 077 );
395
396         res[4] = ENC( data[3] >> 2 );
397         res[5] = ENC( (data[3] << 4) & 060 );
398         res[6] = 0;
399
400         for(i = 0; i < 6; i++) {
401                 if (res[i] == ' ') res[i] = '`';
402                 if (res[i] < 32 || res[i] > 127)
403                         printf("uue: protocol error ?!\n");
404         }
405         return (char *)res;
406 }
407
408
409 /*
410  *      Unix accounting - write a wtmp file.
411  */
412 static int unix_accounting(void *instance, REQUEST *request)
413 {
414         VALUE_PAIR      *vp;
415         FILE            *fp;
416         struct utmp     ut;
417         time_t          t;
418         char            buf[64];
419         const char      *s;
420         int             delay = 0;
421         int             status = -1;
422         int             nas_address = 0;
423         int             framed_address = 0;
424 #ifdef USER_PROCESS
425         int             protocol = -1;
426 #endif
427         int             nas_port = 0;
428         int             port_seen = 0;
429         struct unix_instance *inst = (struct unix_instance *) instance;
430
431         /*
432          *      No radwtmp.  Don't do anything.
433          */
434         if (!inst->radwtmp) {
435                 RDEBUG2("No radwtmp file configured.  Ignoring accounting request.");
436                 return RLM_MODULE_NOOP;
437         }
438
439         if (request->packet->src_ipaddr.af != AF_INET) {
440                 RDEBUG2("IPv6 is not supported!");
441                 return RLM_MODULE_NOOP;
442         }
443
444         /*
445          *      Which type is this.
446          */
447         if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE, 0))==NULL) {
448                 radlog(L_ERR, "rlm_unix: no Accounting-Status-Type attribute in request.");
449                 return RLM_MODULE_NOOP;
450         }
451         status = vp->vp_integer;
452
453         /*
454          *      FIXME: handle PW_STATUS_ALIVE like 1.5.4.3 did.
455          */
456         if (status != PW_STATUS_START &&
457             status != PW_STATUS_STOP)
458                 return RLM_MODULE_NOOP;
459
460         /*
461          *      We're only interested in accounting messages
462          *      with a username in it.
463          */
464         if (pairfind(request->packet->vps, PW_USER_NAME, 0) == NULL)
465                 return RLM_MODULE_NOOP;
466
467         t = request->timestamp;
468         memset(&ut, 0, sizeof(ut));
469
470         /*
471          *      First, find the interesting attributes.
472          */
473         for (vp = request->packet->vps; vp; vp = vp->next) {
474                 switch (vp->attribute) {
475                         case PW_USER_NAME:
476                                 if (vp->length >= sizeof(ut.ut_name)) {
477                                         memcpy(ut.ut_name, (char *)vp->vp_strvalue, sizeof(ut.ut_name));
478                                 } else {
479                                         strlcpy(ut.ut_name, (char *)vp->vp_strvalue, sizeof(ut.ut_name));
480                                 }
481                                 break;
482                         case PW_LOGIN_IP_HOST:
483                         case PW_FRAMED_IP_ADDRESS:
484                                 framed_address = vp->vp_ipaddr;
485                                 break;
486 #ifdef USER_PROCESS
487                         case PW_FRAMED_PROTOCOL:
488                                 protocol = vp->vp_integer;
489                                 break;
490 #endif
491                         case PW_NAS_IP_ADDRESS:
492                                 nas_address = vp->vp_ipaddr;
493                                 break;
494                         case PW_NAS_PORT:
495                                 nas_port = vp->vp_integer;
496                                 port_seen = 1;
497                                 break;
498                         case PW_ACCT_DELAY_TIME:
499                                 delay = vp->vp_ipaddr;
500                                 break;
501                 }
502         }
503
504         /*
505          *      We don't store !root sessions, or sessions
506          *      where we didn't see a NAS-Port attribute.
507          */
508         if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
509                 return RLM_MODULE_NOOP;
510
511         /*
512          *      If we didn't find out the NAS address, use the
513          *      originator's IP address.
514          */
515         if (nas_address == 0) {
516                 nas_address = request->packet->src_ipaddr.ipaddr.ip4addr.s_addr;
517         }
518         s = request->client->shortname;
519         if (!s || s[0] == 0) s = uue(&(nas_address));
520
521 #ifdef __linux__
522         /*
523          *      Linux has a field for the client address.
524          */
525         ut.ut_addr = framed_address;
526 #endif
527         /*
528          *      We use the tty field to store the terminal servers' port
529          *      and address so that the tty field is unique.
530          */
531         snprintf(buf, sizeof(buf), "%03d:%s", nas_port, s);
532         strlcpy(ut.ut_line, buf, sizeof(ut.ut_line));
533
534         /*
535          *      We store the dynamic IP address in the hostname field.
536          */
537 #ifdef UT_HOSTSIZE
538         if (framed_address) {
539                 ip_ntoa(buf, framed_address);
540                 strlcpy(ut.ut_host, buf, sizeof(ut.ut_host));
541         }
542 #endif
543 #ifdef HAVE_UTMPX_H
544         ut.ut_xtime = t- delay;
545 #else
546         ut.ut_time = t - delay;
547 #endif
548 #ifdef USER_PROCESS
549         /*
550          *      And we can use the ID field to store
551          *      the protocol.
552          */
553         if (protocol == PW_PPP)
554                 strcpy(ut.ut_id, "P");
555         else if (protocol == PW_SLIP)
556                 strcpy(ut.ut_id, "S");
557         else
558                 strcpy(ut.ut_id, "T");
559         ut.ut_type = status == PW_STATUS_STOP ? DEAD_PROCESS : USER_PROCESS;
560 #endif
561         if (status == PW_STATUS_STOP)
562                 ut.ut_name[0] = 0;
563
564         /*
565          *      Write a RADIUS wtmp log file.
566          *
567          *      Try to open the file if we can't, we don't write the
568          *      wtmp file. If we can try to write. If we fail,
569          *      return RLM_MODULE_FAIL ..
570          */
571         if ((fp = fopen(inst->radwtmp, "a")) != NULL) {
572                 if ((fwrite(&ut, sizeof(ut), 1, fp)) != 1) {
573                         fclose(fp);
574                         return RLM_MODULE_FAIL;
575                 }
576                 fclose(fp);
577         } else
578                 return RLM_MODULE_FAIL;
579
580         return RLM_MODULE_OK;
581 }
582
583 /* globally exported name */
584 module_t rlm_unix = {
585         RLM_MODULE_INIT,
586         "System",
587         RLM_TYPE_THREAD_UNSAFE | RLM_TYPE_CHECK_CONFIG_SAFE,
588         unix_instantiate,               /* instantiation */
589         unix_detach,                    /* detach */
590         {
591                 unix_authenticate,    /* authentication */
592                 unix_authorize,       /* authorization */
593                 NULL,                 /* preaccounting */
594                 unix_accounting,      /* accounting */
595                 NULL,                  /* checksimul */
596                 NULL,                   /* pre-proxy */
597                 NULL,                   /* post-proxy */
598                 NULL                    /* post-auth */
599         },
600 };