Re-arrange entries in the module_t structure, and do some
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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        "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        "radiusd.h"
54 #include        "modules.h"
55 #include        "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, 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->strvalue;
102
103         pwd = getpwnam(username);
104         if (pwd == NULL)
105                 return -1;
106
107         grp = getgrnam((char *)check->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(void *instance, REQUEST *request, VALUE_PAIR **vp_list)
182 {
183         const char      *name;
184         const char      *encrypted_pass;
185 #ifdef HAVE_GETSPNAM
186         struct spwd     *spwd = NULL;
187 #endif
188 #ifdef OSFC2
189         struct pr_passwd *pr_pw;
190 #else
191         struct passwd   *pwd;
192 #endif
193 #ifdef HAVE_GETUSERSHELL
194         char            *shell;
195 #endif
196         VALUE_PAIR      *vp;
197
198         /*
199          *      We can only authenticate user requests which HAVE
200          *      a User-Name attribute.
201          */
202         if (!request->username) {
203                 return RLM_MODULE_NOOP;
204         }
205
206         name = (char *)request->username->strvalue;
207         encrypted_pass = NULL;
208
209 #ifdef OSFC2
210         if ((pr_pw = getprpwnam(name)) == NULL)
211                 return RLM_MODULE_NOTFOUND;
212         encrypted_pass = pr_pw->ufld.fd_encrypt;
213
214         /*
215          *      Check if account is locked.
216          */
217         if (pr_pw->uflg.fg_lock!=1) {
218                 radlog(L_AUTH, "rlm_unix: [%s]: account locked", name);
219                 return RLM_MODULE_USERLOCK;
220         }
221 #else /* OSFC2 */
222         if ((pwd = getpwnam(name)) == NULL) {
223                 return RLM_MODULE_NOTFOUND;
224         }
225         encrypted_pass = pwd->pw_passwd;
226 #endif /* OSFC2 */
227
228 #ifdef HAVE_GETSPNAM
229         /*
230          *      See if there is a shadow password.
231          *
232          *      Only query the _system_ shadow file if the encrypted
233          *      password from the passwd file is < 10 characters (i.e.
234          *      a valid password would never crypt() to it).  This will
235          *      prevents users from using NULL password fields as things
236          *      stand right now.
237          */
238         if ((encrypted_pass == NULL) || (strlen(encrypted_pass) < 10)) {
239                 if ((spwd = getspnam(name)) == NULL) {
240                         return RLM_MODULE_NOTFOUND;
241                 }
242                 encrypted_pass = spwd->sp_pwdp;
243         }
244 #endif  /* HAVE_GETSPNAM */
245
246 /*
247  *      These require 'pwd != NULL', which isn't true on OSFC2
248  */
249 #ifndef OSFC2
250 #ifdef DENY_SHELL
251         /*
252          *      Users with a particular shell are denied access
253          */
254         if (strcmp(pwd->pw_shell, DENY_SHELL) == 0) {
255                 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell", name);
256                 return RLM_MODULE_REJECT;
257         }
258 #endif
259
260 #ifdef HAVE_GETUSERSHELL
261         /*
262          *      Check /etc/shells for a valid shell. If that file
263          *      contains /RADIUSD/ANY/SHELL then any shell will do.
264          */
265         while ((shell = getusershell()) != NULL) {
266                 if (strcmp(shell, pwd->pw_shell) == 0 ||
267                     strcmp(shell, "/RADIUSD/ANY/SHELL") == 0) {
268                         break;
269                 }
270         }
271         endusershell();
272         if (shell == NULL) {
273                 radlog(L_AUTH, "rlm_unix: [%s]: invalid shell [%s]",
274                        name, pwd->pw_shell);
275                 return RLM_MODULE_REJECT;
276         }
277 #endif
278 #endif /* OSFC2 */
279
280 #if defined(HAVE_GETSPNAM) && !defined(M_UNIX)
281         /*
282          *      Check if password has expired.
283          */
284         if (spwd && spwd->sp_expire > 0 &&
285             (request->timestamp / 86400) > spwd->sp_expire) {
286                 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
287                 return RLM_MODULE_REJECT;
288         }
289 #endif
290
291 #if defined(__FreeBSD__) || defined(bsdi) || defined(_PWF_EXPIRE)
292         /*
293          *      Check if password has expired.
294          */
295         if ((pwd->pw_expire > 0) &&
296             (request->timestamp > pwd->pw_expire)) {
297                 radlog(L_AUTH, "rlm_unix: [%s]: password has expired", name);
298                 return RLM_MODULE_REJECT;
299         }
300 #endif
301
302         /*
303          *      We might have a passwordless account.
304          *
305          *      FIXME: Maybe add Auth-Type := Accept?
306          */
307         if (encrypted_pass[0] == 0)
308                 return RLM_MODULE_NOOP;
309
310         vp = pairmake("Crypt-Password", encrypted_pass, T_OP_SET);
311         if (!vp) return RLM_MODULE_FAIL;
312
313         pairmove(vp_list, &vp);
314         pairfree(&vp);          /* might not be NULL; */
315
316         return RLM_MODULE_UPDATED;
317 }
318
319
320 /*
321  *      Pull the users password from where-ever, and add it to
322  *      the given vp list.
323  */
324 static int unix_authorize(void *instance, REQUEST *request)
325 {
326         return unix_getpw(instance, request, &request->config_items);
327 }
328
329 /*
330  *      Pull the users password from where-ever, and add it to
331  *      the given vp list.
332  */
333 static int unix_authenticate(void *instance, REQUEST *request)
334 {
335 #ifdef OSFSIA
336         char            *info[2];
337         char            *progname = "radius";
338         SIAENTITY       *ent = NULL;
339
340         info[0] = progname;
341         info[1] = NULL;
342         if (sia_ses_init (&ent, 1, info, NULL, name, NULL, 0, NULL) !=
343             SIASUCCESS)
344                 return RLM_MODULE_NOTFOUND;
345         if ((ret = sia_ses_authent (NULL, passwd, ent)) != SIASUCCESS) {
346                 if (ret & SIASTOP)
347                         sia_ses_release (&ent);
348                 return RLM_MODULE_NOTFOUND;
349         }
350         if (sia_ses_estab (NULL, ent) != SIASUCCESS) {
351                 sia_ses_release (&ent);
352                 return RLM_MODULE_NOTFOUND;
353         }
354 #else  /* OSFSIA */
355         int rcode;
356         VALUE_PAIR *vp = NULL;
357
358         if (!request->password ||
359             (request->password->attribute != PW_USER_PASSWORD)) {
360                 radlog(L_AUTH, "rlm_unix: Attribute \"User-Password\" is required for authentication.");
361                 return RLM_MODULE_INVALID;
362         }
363
364         rcode = unix_getpw(instance, request, &vp);
365         if (rcode != RLM_MODULE_UPDATED) return rcode;
366
367         /*
368          *      0 means "ok"
369          */
370         if (lrad_crypt_check((char *) request->password->strvalue,
371                              (char *) vp->strvalue) != 0) {
372                 radlog(L_AUTH, "rlm_unix: [%s]: invalid password",
373                        request->username->strvalue);
374                 return RLM_MODULE_REJECT;
375         }
376 #endif /* OSFFIA */
377
378         return RLM_MODULE_OK;
379 }
380
381
382 /*
383  *      UUencode 4 bits base64. We use this to turn a 4 byte field
384  *      (an IP address) into 6 bytes of ASCII. This is used for the
385  *      wtmp file if we didn't find a short name in the naslist file.
386  */
387 static char *uue(void *in)
388 {
389         int i;
390         static unsigned char res[7];
391         unsigned char *data = (unsigned char *)in;
392
393         res[0] = ENC( data[0] >> 2 );
394         res[1] = ENC( ((data[0] << 4) & 060) + ((data[1] >> 4) & 017) );
395         res[2] = ENC( ((data[1] << 2) & 074) + ((data[2] >> 6) & 03) );
396         res[3] = ENC( data[2] & 077 );
397
398         res[4] = ENC( data[3] >> 2 );
399         res[5] = ENC( (data[3] << 4) & 060 );
400         res[6] = 0;
401
402         for(i = 0; i < 6; i++) {
403                 if (res[i] == ' ') res[i] = '`';
404                 if (res[i] < 32 || res[i] > 127)
405                         printf("uue: protocol error ?!\n");
406         }
407         return (char *)res;
408 }
409
410
411 /*
412  *      Unix accounting - write a wtmp file.
413  */
414 static int unix_accounting(void *instance, REQUEST *request)
415 {
416         VALUE_PAIR      *vp;
417         FILE            *fp;
418         struct utmp     ut;
419         time_t          t;
420         char            buf[64];
421         const char      *s;
422         int             delay = 0;
423         int             status = -1;
424         int             nas_address = 0;
425         int             framed_address = 0;
426         int             protocol = -1;
427         int             nas_port = 0;
428         int             port_seen = 0;
429         int             nas_port_type = 0;
430         struct unix_instance *inst = (struct unix_instance *) instance;
431
432         /*
433          *      No radwtmp.  Don't do anything.
434          */
435         if (!inst->radwtmp) {
436                 DEBUG2("rlm_unix: No radwtmp file configured.  Ignoring accounting request.");
437                 return RLM_MODULE_NOOP;
438         }
439
440         if (request->packet->src_ipaddr.af != AF_INET) {
441                 DEBUG2("rlm_unix: IPv6 is not supported!");
442                 return RLM_MODULE_NOOP;
443         }
444
445         /*
446          *      Which type is this.
447          */
448         if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE))==NULL) {
449                 radlog(L_ERR, "rlm_unix: no Accounting-Status-Type attribute in request.");
450                 return RLM_MODULE_NOOP;
451         }
452         status = vp->lvalue;
453
454         /*
455          *      FIXME: handle PW_STATUS_ALIVE like 1.5.4.3 did.
456          */
457         if (status != PW_STATUS_START &&
458             status != PW_STATUS_STOP)
459                 return RLM_MODULE_NOOP;
460
461         /*
462          *      We're only interested in accounting messages
463          *      with a username in it.
464          */
465         if ((vp = pairfind(request->packet->vps, PW_USER_NAME)) == NULL)
466                 return RLM_MODULE_NOOP;
467
468         t = request->timestamp;
469         memset(&ut, 0, sizeof(ut));
470
471         /*
472          *      First, find the interesting attributes.
473          */
474         for (vp = request->packet->vps; vp; vp = vp->next) {
475                 switch (vp->attribute) {
476                         case PW_USER_NAME:
477                                 if (vp->length >= sizeof(ut.ut_name)) {
478                                         memcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
479                                 } else {
480                                         strNcpy(ut.ut_name, (char *)vp->strvalue, sizeof(ut.ut_name));
481                                 }
482                                 break;
483                         case PW_LOGIN_IP_HOST:
484                         case PW_FRAMED_IP_ADDRESS:
485                                 framed_address = vp->lvalue;
486                                 break;
487                         case PW_FRAMED_PROTOCOL:
488                                 protocol = vp->lvalue;
489                                 break;
490                         case PW_NAS_IP_ADDRESS:
491                                 nas_address = vp->lvalue;
492                                 break;
493                         case PW_NAS_PORT:
494                                 nas_port = vp->lvalue;
495                                 port_seen = 1;
496                                 break;
497                         case PW_ACCT_DELAY_TIME:
498                                 delay = vp->lvalue;
499                                 break;
500                         case PW_NAS_PORT_TYPE:
501                                 nas_port_type = vp->lvalue;
502                                 break;
503                 }
504         }
505
506         /*
507          *      We don't store !root sessions, or sessions
508          *      where we didn't see a NAS-Port attribute.
509          */
510         if (strncmp(ut.ut_name, "!root", sizeof(ut.ut_name)) == 0 || !port_seen)
511                 return RLM_MODULE_NOOP;
512
513         s = "";
514
515         /*
516          *      If we didn't find out the NAS address, use the
517          *      originator's IP address.
518          */
519         if (nas_address == 0) {
520                 RADCLIENT *cl;
521                 nas_address = request->packet->src_ipaddr.ipaddr.ip4addr.s_addr;
522                 
523                 if ((cl = client_find_old(&request->packet->src_ipaddr)) != NULL)
524                         s = cl->shortname;
525         }
526         if (!s || s[0] == 0) s = uue(&(nas_address));
527         
528 #ifdef __linux__
529         /*
530          *      Linux has a field for the client address.
531          */
532         ut.ut_addr = framed_address;
533 #endif
534         /*
535          *      We use the tty field to store the terminal servers' port
536          *      and address so that the tty field is unique.
537          */
538         sprintf(buf, "%03d:%s", nas_port, s);
539         strNcpy(ut.ut_line, buf, sizeof(ut.ut_line));
540
541         /*
542          *      We store the dynamic IP address in the hostname field.
543          */
544 #ifdef UT_HOSTSIZE
545         if (framed_address) {
546                 ip_ntoa(buf, framed_address);
547                 strncpy(ut.ut_host, buf, sizeof(ut.ut_host));
548         }
549 #endif
550 #ifdef HAVE_UTMPX_H
551         ut.ut_xtime = t- delay;
552 #else
553         ut.ut_time = t - delay;
554 #endif
555 #ifdef USER_PROCESS
556         /*
557          *      And we can use the ID field to store
558          *      the protocol.
559          */
560         if (protocol == PW_PPP)
561                 strcpy(ut.ut_id, "P");
562         else if (protocol == PW_SLIP)
563                 strcpy(ut.ut_id, "S");
564         else
565                 strcpy(ut.ut_id, "T");
566         ut.ut_type = status == PW_STATUS_STOP ? DEAD_PROCESS : USER_PROCESS;
567 #endif
568         if (status == PW_STATUS_STOP)
569                 ut.ut_name[0] = 0;
570
571         /*
572          *      Write a RADIUS wtmp log file.
573          *
574          *      Try to open the file if we can't, we don't write the
575          *      wtmp file. If we can try to write. If we fail,
576          *      return RLM_MODULE_FAIL ..
577          */
578         if ((fp = fopen(inst->radwtmp, "a")) != NULL) {
579                 if ((fwrite(&ut, sizeof(ut), 1, fp)) != 1) {
580                         fclose(fp);
581                         return RLM_MODULE_FAIL;
582                 }
583                 fclose(fp);
584         } else
585                 return RLM_MODULE_FAIL;
586
587         return RLM_MODULE_OK;
588 }
589
590 /* globally exported name */
591 module_t rlm_unix = {
592         RLM_MODULE_INIT,
593         "System",
594         RLM_TYPE_THREAD_UNSAFE,        /* type */
595         unix_instantiate,               /* instantiation */
596         unix_detach,                    /* detach */
597         {
598                 unix_authenticate,    /* authentication */
599                 unix_authorize,       /* authorization */
600                 NULL,                 /* preaccounting */
601                 unix_accounting,      /* accounting */
602                 NULL,                  /* checksimul */
603                 NULL,                   /* pre-proxy */
604                 NULL,                   /* post-proxy */
605                 NULL                    /* post-auth */
606         },
607 };