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