pam_access.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. /*
  2. * pam_access module
  3. *
  4. * Written by Alexei Nogin <alexei@nogin.dnttm.ru> 1997/06/15
  5. * (I took login_access from logdaemon-5.6 and converted it to PAM
  6. * using parts of pam_time code.)
  7. *
  8. ************************************************************************
  9. * Copyright message from logdaemon-5.6 (original file name DISCLAIMER)
  10. ************************************************************************
  11. * Copyright 1995 by Wietse Venema. All rights reserved. Individual files
  12. * may be covered by other copyrights (as noted in the file itself.)
  13. *
  14. * This material was originally written and compiled by Wietse Venema at
  15. * Eindhoven University of Technology, The Netherlands, in 1990, 1991,
  16. * 1992, 1993, 1994 and 1995.
  17. *
  18. * Redistribution and use in source and binary forms are permitted
  19. * provided that this entire copyright notice is duplicated in all such
  20. * copies.
  21. *
  22. * This software is provided "as is" and without any expressed or implied
  23. * warranties, including, without limitation, the implied warranties of
  24. * merchantability and fitness for any particular purpose.
  25. *************************************************************************
  26. */
  27. #include "config.h"
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <unistd.h>
  31. #include <stdarg.h>
  32. #include <syslog.h>
  33. #include <string.h>
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include <pwd.h>
  37. #include <grp.h>
  38. #include <errno.h>
  39. #include <ctype.h>
  40. #include <sys/utsname.h>
  41. #include <arpa/inet.h>
  42. #include <netdb.h>
  43. #include <sys/socket.h>
  44. #include <glob.h>
  45. #ifdef HAVE_LIBAUDIT
  46. #include <libaudit.h>
  47. #endif
  48. #include <security/_pam_macros.h>
  49. #include <security/pam_modules.h>
  50. #include <security/pam_modutil.h>
  51. #include <security/pam_ext.h>
  52. #include "pam_cc_compat.h"
  53. #include "pam_inline.h"
  54. /* login_access.c from logdaemon-5.6 with several changes by A.Nogin: */
  55. /*
  56. * This module implements a simple but effective form of login access
  57. * control based on login names and on host (or domain) names, internet
  58. * addresses (or network numbers), or on terminal line names in case of
  59. * non-networked logins. Diagnostics are reported through syslog(3).
  60. *
  61. * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  62. */
  63. #if !defined(MAXHOSTNAMELEN) || (MAXHOSTNAMELEN < 64)
  64. #undef MAXHOSTNAMELEN
  65. #define MAXHOSTNAMELEN 256
  66. #endif
  67. /* Delimiters for fields and for lists of users, ttys or hosts. */
  68. #define ALL 2
  69. #define YES 1
  70. #define NO 0
  71. #define NOMATCH -1
  72. /*
  73. * A structure to bundle up all login-related information to keep the
  74. * functional interfaces as generic as possible.
  75. */
  76. struct login_info {
  77. const struct passwd *user;
  78. const char *from;
  79. const char *config_file;
  80. const char *hostname;
  81. int debug; /* Print debugging messages. */
  82. int only_new_group_syntax; /* Only allow group entries of the form "(xyz)" */
  83. int noaudit; /* Do not audit denials */
  84. const char *fs; /* field separator */
  85. const char *sep; /* list-element separator */
  86. int from_remote_host; /* If PAM_RHOST was used for from */
  87. struct addrinfo *res; /* Cached DNS resolution of from */
  88. int gai_rv; /* Cached retval of getaddrinfo */
  89. };
  90. /* Parse module config arguments */
  91. static int
  92. parse_args(pam_handle_t *pamh, struct login_info *loginfo,
  93. int argc, const char **argv)
  94. {
  95. int i;
  96. loginfo->noaudit = NO;
  97. loginfo->debug = NO;
  98. loginfo->only_new_group_syntax = NO;
  99. loginfo->fs = ":";
  100. loginfo->sep = ", \t";
  101. for (i=0; i<argc; ++i) {
  102. const char *str;
  103. if ((str = pam_str_skip_prefix(argv[i], "fieldsep=")) != NULL) {
  104. /* the admin wants to override the default field separators */
  105. loginfo->fs = str;
  106. } else if ((str = pam_str_skip_prefix(argv[i], "listsep=")) != NULL) {
  107. /* the admin wants to override the default list separators */
  108. loginfo->sep = str;
  109. } else if ((str = pam_str_skip_prefix(argv[i], "accessfile=")) != NULL) {
  110. FILE *fp = fopen(str, "r");
  111. if (fp) {
  112. loginfo->config_file = str;
  113. fclose(fp);
  114. } else {
  115. pam_syslog(pamh, LOG_ERR,
  116. "failed to open accessfile=[%s]: %m", str);
  117. return 0;
  118. }
  119. } else if (strcmp (argv[i], "debug") == 0) {
  120. loginfo->debug = YES;
  121. } else if (strcmp (argv[i], "nodefgroup") == 0) {
  122. loginfo->only_new_group_syntax = YES;
  123. } else if (strcmp (argv[i], "noaudit") == 0) {
  124. loginfo->noaudit = YES;
  125. } else {
  126. pam_syslog(pamh, LOG_ERR, "unrecognized option [%s]", argv[i]);
  127. }
  128. }
  129. return 1; /* OK */
  130. }
  131. /* --- static functions for checking whether the user should be let in --- */
  132. typedef int match_func (pam_handle_t *, char *, struct login_info *);
  133. static int list_match (pam_handle_t *, char *, char *, struct login_info *,
  134. match_func *);
  135. static int user_match (pam_handle_t *, char *, struct login_info *);
  136. static int group_match (pam_handle_t *, const char *, const char *, int);
  137. static int from_match (pam_handle_t *, char *, struct login_info *);
  138. static int remote_match (pam_handle_t *, char *, struct login_info *);
  139. static int string_match (pam_handle_t *, const char *, const char *, int);
  140. static int network_netmask_match (pam_handle_t *, const char *, const char *, struct login_info *);
  141. /* isipaddr - find out if string provided is an IP address or not */
  142. static int
  143. isipaddr (const char *string, int *addr_type,
  144. struct sockaddr_storage *addr)
  145. {
  146. struct sockaddr_storage local_addr;
  147. int is_ip;
  148. /* We use struct sockaddr_storage addr because
  149. * struct in_addr/in6_addr is an integral part
  150. * of struct sockaddr and we doesn't want to
  151. * use its value.
  152. */
  153. if (addr == NULL)
  154. addr = &local_addr;
  155. memset(addr, 0, sizeof(struct sockaddr_storage));
  156. /* first ipv4 */
  157. if (inet_pton(AF_INET, string, addr) > 0)
  158. {
  159. if (addr_type != NULL)
  160. *addr_type = AF_INET;
  161. is_ip = YES;
  162. }
  163. else if (inet_pton(AF_INET6, string, addr) > 0)
  164. { /* then ipv6 */
  165. if (addr_type != NULL) {
  166. *addr_type = AF_INET6;
  167. }
  168. is_ip = YES;
  169. }
  170. else
  171. is_ip = NO;
  172. return is_ip;
  173. }
  174. /* are_addresses_equal - translate IP address strings to real IP
  175. * addresses and compare them to find out if they are equal.
  176. * If netmask was provided it will be used to focus comparison to
  177. * relevant bits.
  178. */
  179. static int
  180. are_addresses_equal (const char *ipaddr0, const char *ipaddr1,
  181. const char *netmask)
  182. {
  183. struct sockaddr_storage addr0;
  184. struct sockaddr_storage addr1;
  185. int addr_type0 = 0;
  186. int addr_type1 = 0;
  187. if (isipaddr (ipaddr0, &addr_type0, &addr0) == NO)
  188. return NO;
  189. if (isipaddr (ipaddr1, &addr_type1, &addr1) == NO)
  190. return NO;
  191. if (addr_type0 != addr_type1)
  192. /* different address types */
  193. return NO;
  194. if (netmask != NULL) {
  195. /* Got a netmask, so normalize addresses? */
  196. struct sockaddr_storage nmask;
  197. unsigned char *byte_a, *byte_nm;
  198. memset(&nmask, 0, sizeof(struct sockaddr_storage));
  199. if (inet_pton(addr_type0, netmask, (void *)&nmask) > 0) {
  200. unsigned int i;
  201. byte_a = (unsigned char *)(&addr0);
  202. byte_nm = (unsigned char *)(&nmask);
  203. for (i=0; i<sizeof(struct sockaddr_storage); i++) {
  204. byte_a[i] = byte_a[i] & byte_nm[i];
  205. }
  206. byte_a = (unsigned char *)(&addr1);
  207. byte_nm = (unsigned char *)(&nmask);
  208. for (i=0; i<sizeof(struct sockaddr_storage); i++) {
  209. byte_a[i] = byte_a[i] & byte_nm[i];
  210. }
  211. }
  212. }
  213. /* Are the two addresses equal? */
  214. if (memcmp((void *)&addr0, (void *)&addr1,
  215. sizeof(struct sockaddr_storage)) == 0) {
  216. return(YES);
  217. }
  218. return(NO);
  219. }
  220. static char *
  221. number_to_netmask (long netmask, int addr_type,
  222. char *ipaddr_buf, size_t ipaddr_buf_len)
  223. {
  224. /* We use struct sockaddr_storage addr because
  225. * struct in_addr/in6_addr is an integral part
  226. * of struct sockaddr and we doesn't want to
  227. * use its value.
  228. */
  229. struct sockaddr_storage nmask;
  230. unsigned char *byte_nm;
  231. const char *ipaddr_dst = NULL;
  232. int i, ip_bytes;
  233. if (netmask == 0) {
  234. /* mask 0 is the same like no mask */
  235. return(NULL);
  236. }
  237. memset(&nmask, 0, sizeof(struct sockaddr_storage));
  238. if (addr_type == AF_INET6) {
  239. /* ipv6 address mask */
  240. ip_bytes = 16;
  241. } else {
  242. /* default might be an ipv4 address mask */
  243. addr_type = AF_INET;
  244. ip_bytes = 4;
  245. }
  246. byte_nm = (unsigned char *)(&nmask);
  247. /* translate number to mask */
  248. for (i=0; i<ip_bytes; i++) {
  249. if (netmask >= 8) {
  250. byte_nm[i] = 0xff;
  251. netmask -= 8;
  252. } else
  253. if (netmask > 0) {
  254. byte_nm[i] = 0xff << (8 - netmask);
  255. break;
  256. } else
  257. if (netmask <= 0) {
  258. break;
  259. }
  260. }
  261. /* now generate netmask address string */
  262. ipaddr_dst = inet_ntop(addr_type, &nmask, ipaddr_buf, ipaddr_buf_len);
  263. if (ipaddr_dst == ipaddr_buf) {
  264. return (ipaddr_buf);
  265. }
  266. return (NULL);
  267. }
  268. /* login_access - match username/group and host/tty with access control file */
  269. static int
  270. login_access (pam_handle_t *pamh, struct login_info *item)
  271. {
  272. FILE *fp;
  273. char line[BUFSIZ];
  274. char *perm; /* becomes permission field */
  275. char *users; /* becomes list of login names */
  276. char *froms; /* becomes list of terminals or hosts */
  277. int match = NO;
  278. #ifdef HAVE_LIBAUDIT
  279. int nonall_match = NO;
  280. #endif
  281. int end;
  282. int lineno = 0; /* for diagnostics */
  283. char *sptr;
  284. if (item->debug)
  285. pam_syslog (pamh, LOG_DEBUG,
  286. "login_access: user=%s, from=%s, file=%s",
  287. item->user->pw_name,
  288. item->from, item->config_file);
  289. /*
  290. * Process the table one line at a time and stop at the first match.
  291. * Blank lines and lines that begin with a '#' character are ignored.
  292. * Non-comment lines are broken at the ':' character. All fields are
  293. * mandatory. The first field should be a "+" or "-" character. A
  294. * non-existing table means no access control.
  295. */
  296. if ((fp = fopen(item->config_file, "r"))!=NULL) {
  297. while (!match && fgets(line, sizeof(line), fp)) {
  298. lineno++;
  299. if (line[end = strlen(line) - 1] != '\n') {
  300. pam_syslog(pamh, LOG_ERR,
  301. "%s: line %d: missing newline or line too long",
  302. item->config_file, lineno);
  303. continue;
  304. }
  305. if (line[0] == '#')
  306. continue; /* comment line */
  307. while (end > 0 && isspace(line[end - 1]))
  308. end--;
  309. line[end] = 0; /* strip trailing whitespace */
  310. if (line[0] == 0) /* skip blank lines */
  311. continue;
  312. /* Allow field separator in last field of froms */
  313. if (!(perm = strtok_r(line, item->fs, &sptr))
  314. || !(users = strtok_r(NULL, item->fs, &sptr))
  315. || !(froms = strtok_r(NULL, "\n", &sptr))) {
  316. pam_syslog(pamh, LOG_ERR, "%s: line %d: bad field count",
  317. item->config_file, lineno);
  318. continue;
  319. }
  320. if (perm[0] != '+' && perm[0] != '-') {
  321. pam_syslog(pamh, LOG_ERR, "%s: line %d: bad first field",
  322. item->config_file, lineno);
  323. continue;
  324. }
  325. if (item->debug)
  326. pam_syslog (pamh, LOG_DEBUG,
  327. "line %d: %s : %s : %s", lineno, perm, users, froms);
  328. match = list_match(pamh, users, NULL, item, user_match);
  329. if (item->debug)
  330. pam_syslog (pamh, LOG_DEBUG, "user_match=%d, \"%s\"",
  331. match, item->user->pw_name);
  332. if (match) {
  333. match = list_match(pamh, froms, NULL, item, from_match);
  334. #ifdef HAVE_LIBAUDIT
  335. if (!match && perm[0] == '+') {
  336. nonall_match = YES;
  337. }
  338. #endif
  339. if (item->debug)
  340. pam_syslog (pamh, LOG_DEBUG,
  341. "from_match=%d, \"%s\"", match, item->from);
  342. }
  343. }
  344. (void) fclose(fp);
  345. } else if (errno == ENOENT) {
  346. /* This is no error. */
  347. pam_syslog(pamh, LOG_WARNING, "warning: cannot open %s: %m",
  348. item->config_file);
  349. } else {
  350. pam_syslog(pamh, LOG_ERR, "cannot open %s: %m", item->config_file);
  351. return NO;
  352. }
  353. #ifdef HAVE_LIBAUDIT
  354. if (!item->noaudit && (match == YES || (match == ALL &&
  355. nonall_match == YES)) && line[0] == '-') {
  356. pam_modutil_audit_write(pamh, AUDIT_ANOM_LOGIN_LOCATION,
  357. "pam_access", 0);
  358. }
  359. #endif
  360. if (match == NO)
  361. return NOMATCH;
  362. if (line[0] == '+')
  363. return YES;
  364. return NO;
  365. }
  366. /* list_match - match an item against a list of tokens with exceptions */
  367. static int
  368. list_match(pam_handle_t *pamh, char *list, char *sptr,
  369. struct login_info *item, match_func *match_fn)
  370. {
  371. char *tok;
  372. int match = NO;
  373. if (item->debug && list != NULL)
  374. pam_syslog (pamh, LOG_DEBUG,
  375. "list_match: list=%s, item=%s", list, item->user->pw_name);
  376. /*
  377. * Process tokens one at a time. We have exhausted all possible matches
  378. * when we reach an "EXCEPT" token or the end of the list. If we do find
  379. * a match, look for an "EXCEPT" list and recurse to determine whether
  380. * the match is affected by any exceptions.
  381. */
  382. for (tok = strtok_r(list, item->sep, &sptr); tok != 0;
  383. tok = strtok_r(NULL, item->sep, &sptr)) {
  384. if (strcasecmp(tok, "EXCEPT") == 0) /* EXCEPT: give up */
  385. break;
  386. if ((match = (*match_fn) (pamh, tok, item))) /* YES */
  387. break;
  388. }
  389. /* Process exceptions to matches. */
  390. if (match != NO) {
  391. while ((tok = strtok_r(NULL, item->sep, &sptr)) && strcasecmp(tok, "EXCEPT"))
  392. /* VOID */ ;
  393. if (tok == 0)
  394. return match;
  395. if (list_match(pamh, NULL, sptr, item, match_fn) == NO)
  396. return YES; /* drop special meaning of ALL */
  397. }
  398. return (NO);
  399. }
  400. /* netgroup_match - match group against machine or user */
  401. static int
  402. netgroup_match (pam_handle_t *pamh, const char *netgroup,
  403. const char *machine, const char *user, int debug)
  404. {
  405. int retval;
  406. char *mydomain = NULL;
  407. #ifdef HAVE_GETDOMAINNAME
  408. char domainname_res[256];
  409. if (getdomainname (domainname_res, sizeof (domainname_res)) == 0)
  410. {
  411. if (domainname_res[0] != '\0' && strcmp (domainname_res, "(none)") != 0)
  412. {
  413. mydomain = domainname_res;
  414. }
  415. }
  416. #endif
  417. #ifdef HAVE_INNETGR
  418. retval = innetgr (netgroup, machine, user, mydomain);
  419. #else
  420. retval = 0;
  421. pam_syslog (pamh, LOG_ERR, "pam_access does not have netgroup support");
  422. #endif
  423. if (debug == YES)
  424. pam_syslog (pamh, LOG_DEBUG,
  425. "netgroup_match: %d (netgroup=%s, machine=%s, user=%s, domain=%s)",
  426. retval, netgroup ? netgroup : "NULL",
  427. machine ? machine : "NULL",
  428. user ? user : "NULL", mydomain ? mydomain : "NULL");
  429. return retval;
  430. }
  431. /* user_match - match a username against one token */
  432. static int
  433. user_match (pam_handle_t *pamh, char *tok, struct login_info *item)
  434. {
  435. char *string = item->user->pw_name;
  436. struct login_info fake_item;
  437. char *at;
  438. int rv;
  439. if (item->debug)
  440. pam_syslog (pamh, LOG_DEBUG,
  441. "user_match: tok=%s, item=%s", tok, string);
  442. /*
  443. * If a token has the magic value "ALL" the match always succeeds.
  444. * Otherwise, return YES if the token fully matches the username, if the
  445. * token is a group that contains the username, or if the token is the
  446. * name of the user's primary group.
  447. */
  448. /* Try to split on a pattern (@*[^@]+)(@+.*) */
  449. for (at = tok; *at == '@'; ++at);
  450. if (tok[0] == '(' && tok[strlen(tok) - 1] == ')') {
  451. return (group_match (pamh, tok, string, item->debug));
  452. } else if ((at = strchr(at, '@')) != NULL) {
  453. /* split user@host pattern */
  454. if (item->hostname == NULL)
  455. return NO;
  456. memcpy (&fake_item, item, sizeof(fake_item));
  457. fake_item.from = item->hostname;
  458. fake_item.gai_rv = 0;
  459. fake_item.res = NULL;
  460. fake_item.from_remote_host = 1; /* hostname should be resolvable */
  461. *at = 0;
  462. if (!user_match (pamh, tok, item))
  463. return NO;
  464. rv = from_match (pamh, at + 1, &fake_item);
  465. if (fake_item.gai_rv == 0 && fake_item.res)
  466. freeaddrinfo(fake_item.res);
  467. return rv;
  468. } else if (tok[0] == '@') { /* netgroup */
  469. const char *hostname = NULL;
  470. if (tok[1] == '@') { /* add hostname to netgroup match */
  471. if (item->hostname == NULL)
  472. return NO;
  473. ++tok;
  474. hostname = item->hostname;
  475. }
  476. return (netgroup_match (pamh, tok + 1, hostname, string, item->debug));
  477. } else if ((rv=string_match (pamh, tok, string, item->debug)) != NO) /* ALL or exact match */
  478. return rv;
  479. else if (item->only_new_group_syntax == NO &&
  480. pam_modutil_user_in_group_nam_nam (pamh,
  481. item->user->pw_name, tok))
  482. /* try group membership */
  483. return YES;
  484. return NO;
  485. }
  486. /* group_match - match a username against token named group */
  487. static int
  488. group_match (pam_handle_t *pamh, const char *tok, const char* usr,
  489. int debug)
  490. {
  491. char grptok[BUFSIZ];
  492. if (debug)
  493. pam_syslog (pamh, LOG_DEBUG,
  494. "group_match: grp=%s, user=%s", tok, usr);
  495. if (strlen(tok) < 3)
  496. return NO;
  497. /* token is received under the format '(...)' */
  498. memset(grptok, 0, BUFSIZ);
  499. strncpy(grptok, tok + 1, strlen(tok) - 2);
  500. if (pam_modutil_user_in_group_nam_nam(pamh, usr, grptok))
  501. return YES;
  502. return NO;
  503. }
  504. /* from_match - match a host or tty against a list of tokens */
  505. static int
  506. from_match (pam_handle_t *pamh, char *tok, struct login_info *item)
  507. {
  508. const char *string = item->from;
  509. int rv;
  510. if (item->debug)
  511. pam_syslog (pamh, LOG_DEBUG,
  512. "from_match: tok=%s, item=%s", tok, string);
  513. /*
  514. * If a token has the magic value "ALL" the match always succeeds. Return
  515. * YES if the token fully matches the string. If the token is a domain
  516. * name, return YES if it matches the last fields of the string. If the
  517. * token has the magic value "LOCAL", return YES if the from field was
  518. * not taken by PAM_RHOST. If the token is a network number, return YES
  519. * if it matches the head of the string.
  520. */
  521. if (string == NULL) {
  522. return NO;
  523. } else if (tok[0] == '@') { /* netgroup */
  524. return (netgroup_match (pamh, tok + 1, string, (char *) 0, item->debug));
  525. } else if ((rv = string_match(pamh, tok, string, item->debug)) != NO) {
  526. /* ALL or exact match */
  527. return rv;
  528. } else if (strcasecmp(tok, "LOCAL") == 0) {
  529. /* LOCAL matches only local accesses */
  530. if (!item->from_remote_host)
  531. return YES;
  532. return NO;
  533. } else if (item->from_remote_host) {
  534. return remote_match(pamh, tok, item);
  535. }
  536. return NO;
  537. }
  538. static int
  539. remote_match (pam_handle_t *pamh, char *tok, struct login_info *item)
  540. {
  541. const char *string = item->from;
  542. size_t tok_len = strlen(tok);
  543. size_t str_len;
  544. if (tok[0] == '.') { /* domain: match last fields */
  545. if ((str_len = strlen(string)) > tok_len
  546. && strcasecmp(tok, string + str_len - tok_len) == 0)
  547. return YES;
  548. } else if (tok[tok_len - 1] == '.') {
  549. struct addrinfo hint;
  550. memset (&hint, '\0', sizeof (hint));
  551. hint.ai_flags = AI_CANONNAME;
  552. hint.ai_family = AF_INET;
  553. if (item->gai_rv != 0)
  554. return NO;
  555. else if (!item->res &&
  556. (item->gai_rv = getaddrinfo (string, NULL, &hint, &item->res)) != 0)
  557. return NO;
  558. else
  559. {
  560. struct addrinfo *runp = item->res;
  561. while (runp != NULL)
  562. {
  563. char buf[INET_ADDRSTRLEN+2];
  564. if (runp->ai_family == AF_INET)
  565. {
  566. DIAG_PUSH_IGNORE_CAST_ALIGN;
  567. inet_ntop (runp->ai_family,
  568. &((struct sockaddr_in *) runp->ai_addr)->sin_addr,
  569. buf, sizeof (buf));
  570. DIAG_POP_IGNORE_CAST_ALIGN;
  571. strcat (buf, ".");
  572. if (strncmp(tok, buf, tok_len) == 0)
  573. {
  574. return YES;
  575. }
  576. }
  577. runp = runp->ai_next;
  578. }
  579. }
  580. return NO;
  581. }
  582. /* Assume network/netmask with an IP of a host. */
  583. return network_netmask_match(pamh, tok, string, item);
  584. }
  585. /* string_match - match a string against one token */
  586. static int
  587. string_match (pam_handle_t *pamh, const char *tok, const char *string,
  588. int debug)
  589. {
  590. if (debug)
  591. pam_syslog (pamh, LOG_DEBUG,
  592. "string_match: tok=%s, item=%s", tok, string);
  593. /*
  594. * If the token has the magic value "ALL" the match always succeeds.
  595. * Otherwise, return YES if the token fully matches the string.
  596. * "NONE" token matches NULL string.
  597. */
  598. if (strcasecmp(tok, "ALL") == 0) { /* all: always matches */
  599. return (ALL);
  600. } else if (string != NULL) {
  601. if (strcasecmp(tok, string) == 0) { /* try exact match */
  602. return (YES);
  603. }
  604. } else if (strcasecmp(tok, "NONE") == 0) {
  605. return (YES);
  606. }
  607. return (NO);
  608. }
  609. /* network_netmask_match - match a string against one token
  610. * where string is a hostname or ip (v4,v6) address and tok
  611. * represents either a single ip (v4,v6) address or a network/netmask
  612. */
  613. static int
  614. network_netmask_match (pam_handle_t *pamh,
  615. const char *tok, const char *string, struct login_info *item)
  616. {
  617. char *netmask_ptr;
  618. char netmask_string[MAXHOSTNAMELEN + 1];
  619. int addr_type;
  620. if (item->debug)
  621. pam_syslog (pamh, LOG_DEBUG,
  622. "network_netmask_match: tok=%s, item=%s", tok, string);
  623. /* OK, check if tok is of type addr/mask */
  624. if ((netmask_ptr = strchr(tok, '/')) != NULL)
  625. {
  626. long netmask = 0;
  627. /* YES */
  628. *netmask_ptr = 0;
  629. netmask_ptr++;
  630. if (isipaddr(tok, &addr_type, NULL) == NO)
  631. { /* no netaddr */
  632. return NO;
  633. }
  634. /* check netmask */
  635. if (isipaddr(netmask_ptr, NULL, NULL) == NO)
  636. { /* netmask as integre value */
  637. char *endptr = NULL;
  638. netmask = strtol(netmask_ptr, &endptr, 0);
  639. if ((endptr == netmask_ptr) || (*endptr != '\0'))
  640. { /* invalid netmask value */
  641. return NO;
  642. }
  643. if ((netmask < 0)
  644. || (addr_type == AF_INET && netmask > 32)
  645. || (addr_type == AF_INET6 && netmask > 128))
  646. { /* netmask value out of range */
  647. return NO;
  648. }
  649. netmask_ptr = number_to_netmask(netmask, addr_type,
  650. netmask_string, MAXHOSTNAMELEN);
  651. }
  652. }
  653. else
  654. /* NO, then check if it is only an addr */
  655. if (isipaddr(tok, NULL, NULL) != YES)
  656. {
  657. return NO;
  658. }
  659. if (isipaddr(string, NULL, NULL) != YES)
  660. {
  661. /* Assume network/netmask with a name of a host. */
  662. struct addrinfo hint;
  663. memset (&hint, '\0', sizeof (hint));
  664. hint.ai_flags = AI_CANONNAME;
  665. hint.ai_family = AF_UNSPEC;
  666. if (item->gai_rv != 0)
  667. return NO;
  668. else if (!item->res &&
  669. (item->gai_rv = getaddrinfo (string, NULL, &hint, &item->res)) != 0)
  670. return NO;
  671. else
  672. {
  673. struct addrinfo *runp = item->res;
  674. while (runp != NULL)
  675. {
  676. char buf[INET6_ADDRSTRLEN];
  677. DIAG_PUSH_IGNORE_CAST_ALIGN;
  678. inet_ntop (runp->ai_family,
  679. runp->ai_family == AF_INET
  680. ? (void *) &((struct sockaddr_in *) runp->ai_addr)->sin_addr
  681. : (void *) &((struct sockaddr_in6 *) runp->ai_addr)->sin6_addr,
  682. buf, sizeof (buf));
  683. DIAG_POP_IGNORE_CAST_ALIGN;
  684. if (are_addresses_equal(buf, tok, netmask_ptr))
  685. {
  686. return YES;
  687. }
  688. runp = runp->ai_next;
  689. }
  690. }
  691. }
  692. else
  693. return (are_addresses_equal(string, tok, netmask_ptr));
  694. return NO;
  695. }
  696. /* --- public PAM management functions --- */
  697. int
  698. pam_sm_authenticate (pam_handle_t *pamh, int flags UNUSED,
  699. int argc, const char **argv)
  700. {
  701. struct login_info loginfo;
  702. const char *user=NULL;
  703. const void *void_from=NULL;
  704. const char *from;
  705. const char *default_config = PAM_ACCESS_CONFIG;
  706. struct passwd *user_pw;
  707. char hostname[MAXHOSTNAMELEN + 1];
  708. int rv;
  709. /* set username */
  710. if (pam_get_user(pamh, &user, NULL) != PAM_SUCCESS) {
  711. pam_syslog(pamh, LOG_NOTICE, "cannot determine user name");
  712. return PAM_USER_UNKNOWN;
  713. }
  714. if ((user_pw=pam_modutil_getpwnam(pamh, user))==NULL)
  715. return (PAM_USER_UNKNOWN);
  716. /*
  717. * Bundle up the arguments to avoid unnecessary clumsiness later on.
  718. */
  719. memset(&loginfo, '\0', sizeof(loginfo));
  720. loginfo.user = user_pw;
  721. loginfo.config_file = default_config;
  722. /* parse the argument list */
  723. if (!parse_args(pamh, &loginfo, argc, argv)) {
  724. pam_syslog(pamh, LOG_ERR, "failed to parse the module arguments");
  725. return PAM_ABORT;
  726. }
  727. /* remote host name */
  728. if (pam_get_item(pamh, PAM_RHOST, &void_from)
  729. != PAM_SUCCESS) {
  730. pam_syslog(pamh, LOG_ERR, "cannot find the remote host name");
  731. return PAM_ABORT;
  732. }
  733. from = void_from;
  734. if ((from==NULL) || (*from=='\0')) {
  735. /* local login, set tty name */
  736. loginfo.from_remote_host = 0;
  737. if (pam_get_item(pamh, PAM_TTY, &void_from) != PAM_SUCCESS
  738. || void_from == NULL) {
  739. D(("PAM_TTY not set, probing stdin"));
  740. from = ttyname(STDIN_FILENO);
  741. if (from != NULL) {
  742. if (pam_set_item(pamh, PAM_TTY, from) != PAM_SUCCESS)
  743. pam_syslog(pamh, LOG_WARNING, "couldn't set tty name");
  744. } else {
  745. if (pam_get_item(pamh, PAM_SERVICE, &void_from) != PAM_SUCCESS
  746. || void_from == NULL) {
  747. pam_syslog (pamh, LOG_ERR,
  748. "cannot determine remote host, tty or service name");
  749. return PAM_ABORT;
  750. }
  751. from = void_from;
  752. if (loginfo.debug)
  753. pam_syslog (pamh, LOG_DEBUG,
  754. "cannot determine tty or remote hostname, using service %s",
  755. from);
  756. }
  757. }
  758. else
  759. from = void_from;
  760. if (from[0] == '/') { /* full path, remove device path. */
  761. const char *f;
  762. from++;
  763. if ((f = strchr(from, '/')) != NULL) {
  764. from = f + 1;
  765. }
  766. }
  767. }
  768. else
  769. loginfo.from_remote_host = 1;
  770. loginfo.from = from;
  771. hostname[sizeof(hostname)-1] = '\0';
  772. if (gethostname(hostname, sizeof(hostname)-1) == 0)
  773. loginfo.hostname = hostname;
  774. else {
  775. pam_syslog (pamh, LOG_ERR, "gethostname failed: %m");
  776. loginfo.hostname = NULL;
  777. }
  778. rv = login_access(pamh, &loginfo);
  779. if (rv == NOMATCH && loginfo.config_file == default_config) {
  780. glob_t globbuf;
  781. int i, glob_rv;
  782. /* We do not manipulate locale as setlocale() is not
  783. * thread safe. We could use uselocale() in future.
  784. */
  785. glob_rv = glob(ACCESS_CONF_GLOB, GLOB_ERR, NULL, &globbuf);
  786. if (!glob_rv) {
  787. /* Parse the *.conf files. */
  788. for (i = 0; globbuf.gl_pathv[i] != NULL; i++) {
  789. loginfo.config_file = globbuf.gl_pathv[i];
  790. rv = login_access(pamh, &loginfo);
  791. if (rv != NOMATCH)
  792. break;
  793. }
  794. globfree(&globbuf);
  795. }
  796. }
  797. if (loginfo.gai_rv == 0 && loginfo.res)
  798. freeaddrinfo(loginfo.res);
  799. if (rv) {
  800. return (PAM_SUCCESS);
  801. } else {
  802. pam_syslog(pamh, LOG_ERR,
  803. "access denied for user `%s' from `%s'",user,from);
  804. return (PAM_PERM_DENIED);
  805. }
  806. }
  807. int
  808. pam_sm_setcred (pam_handle_t *pamh UNUSED, int flags UNUSED,
  809. int argc UNUSED, const char **argv UNUSED)
  810. {
  811. return PAM_IGNORE;
  812. }
  813. int
  814. pam_sm_acct_mgmt (pam_handle_t *pamh, int flags,
  815. int argc, const char **argv)
  816. {
  817. return pam_sm_authenticate (pamh, flags, argc, argv);
  818. }
  819. int
  820. pam_sm_open_session(pam_handle_t *pamh, int flags,
  821. int argc, const char **argv)
  822. {
  823. return pam_sm_authenticate(pamh, flags, argc, argv);
  824. }
  825. int
  826. pam_sm_close_session(pam_handle_t *pamh, int flags,
  827. int argc, const char **argv)
  828. {
  829. return pam_sm_authenticate(pamh, flags, argc, argv);
  830. }
  831. int
  832. pam_sm_chauthtok(pam_handle_t *pamh, int flags,
  833. int argc, const char **argv)
  834. {
  835. return pam_sm_authenticate(pamh, flags, argc, argv);
  836. }
  837. /* end of module definition */