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