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