pam_misc.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* pam_misc.c -- This is random stuff
  2. *
  3. * Copyright (c) Andrew G. Morgan <morgan@kernel.org> 2000-2003
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, and the entire permission notice in its entirety,
  11. * including the disclaimer of warranties.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote
  16. * products derived from this software without specific prior
  17. * written permission.
  18. *
  19. * ALTERNATIVELY, this product may be distributed under the terms of
  20. * the GNU Public License, in which case the provisions of the GPL are
  21. * required INSTEAD OF the above restrictions. (This clause is
  22. * necessary due to a potential bad interaction between the GPL and
  23. * the restrictions contained in a BSD-style copyright.)
  24. *
  25. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  26. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  27. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  29. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  30. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  33. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  35. * OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include "pam_private.h"
  38. #include <stdarg.h>
  39. #include <stdlib.h>
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <syslog.h>
  43. #include <ctype.h>
  44. char *_pam_StrTok(char *from, const char *format, char **next)
  45. /*
  46. * this function is a variant of the standard strtok, it differs in that
  47. * it takes an additional argument and doesn't nul terminate tokens until
  48. * they are actually reached.
  49. */
  50. {
  51. char table[256], *end;
  52. int i;
  53. if (from == NULL && (from = *next) == NULL)
  54. return from;
  55. /* initialize table */
  56. for (i=1; i<256; table[i++] = '\0');
  57. for (i=0; format[i] ;
  58. table[(unsigned char)format[i++]] = 'y');
  59. /* look for first non-format char */
  60. while (*from && table[(unsigned char)*from]) {
  61. ++from;
  62. }
  63. if (*from == '[') {
  64. /*
  65. * special case, "[...]" is considered to be a single
  66. * object. Note, however, if one of the format[] chars is
  67. * '[' this single string will not be read correctly.
  68. * Note, any '[' inside the outer "[...]" pair will survive.
  69. * Note, the first ']' will terminate this string, but
  70. * that "\]" will get compressed into "]". That is:
  71. *
  72. * "[..[..\]..]..." --> "..[..].."
  73. */
  74. char *to;
  75. for (to=end=++from; *end && *end != ']'; ++to, ++end) {
  76. if (*end == '\\' && end[1] == ']')
  77. ++end;
  78. if (to != end) {
  79. *to = *end;
  80. }
  81. }
  82. if (to != end) {
  83. *to = '\0';
  84. }
  85. /* note, this string is stripped of its edges: "..." is what
  86. remains */
  87. } else if (*from) {
  88. /* simply look for next blank char */
  89. for (end=from; *end && !table[(unsigned char)*end]; ++end);
  90. } else {
  91. return (*next = NULL); /* no tokens left */
  92. }
  93. /* now terminate what we have */
  94. if (*end)
  95. *end++ = '\0';
  96. /* indicate what it left */
  97. if (*end) {
  98. *next = end;
  99. } else {
  100. *next = NULL; /* have found last token */
  101. }
  102. /* return what we have */
  103. return from;
  104. }
  105. /*
  106. * Safe duplication of character strings. "Paranoid"; don't leave
  107. * evidence of old token around for later stack analysis.
  108. */
  109. char *_pam_strdup(const char *x)
  110. {
  111. register char *new=NULL;
  112. if (x != NULL) {
  113. register int len;
  114. len = strlen (x) + 1; /* length of string including NUL */
  115. if ((new = malloc(len)) == NULL) {
  116. len = 0;
  117. pam_syslog(NULL, LOG_CRIT, "_pam_strdup: failed to get memory");
  118. } else {
  119. strcpy (new, x);
  120. }
  121. x = NULL;
  122. }
  123. return new; /* return the duplicate or NULL on error */
  124. }
  125. /*
  126. * Safe duplication of memory buffers. "Paranoid"; don't leave
  127. * evidence of old token around for later stack analysis.
  128. */
  129. char *_pam_memdup(const char *x, int len)
  130. {
  131. register char *new=NULL;
  132. if (x != NULL) {
  133. if ((new = malloc(len)) == NULL) {
  134. len = 0;
  135. pam_syslog(NULL, LOG_CRIT, "_pam_memdup: failed to get memory");
  136. } else {
  137. memcpy (new, x, len);
  138. }
  139. x = NULL;
  140. }
  141. return new; /* return the duplicate or NULL on error */
  142. }
  143. /* Generate argv, argc from s */
  144. /* caller must free(argv) */
  145. int _pam_mkargv(const char *s, char ***argv, int *argc)
  146. {
  147. int l;
  148. int argvlen = 0;
  149. char *sbuf, *sbuf_start;
  150. char **our_argv = NULL;
  151. char **argvbuf;
  152. char *argvbufp;
  153. #ifdef PAM_DEBUG
  154. int count=0;
  155. #endif
  156. D(("_pam_mkargv called: %s",s));
  157. *argc = 0;
  158. l = strlen(s);
  159. if (l) {
  160. if ((sbuf = sbuf_start = _pam_strdup(s)) == NULL) {
  161. pam_syslog(NULL, LOG_CRIT,
  162. "pam_mkargv: null returned by _pam_strdup");
  163. D(("arg NULL"));
  164. } else {
  165. /* Overkill on the malloc, but not large */
  166. argvlen = (l + 1) * ((sizeof(char)) + sizeof(char *));
  167. if ((our_argv = argvbuf = malloc(argvlen)) == NULL) {
  168. pam_syslog(NULL, LOG_CRIT,
  169. "pam_mkargv: null returned by malloc");
  170. } else {
  171. char *tmp=NULL;
  172. argvbufp = (char *) argvbuf + (l * sizeof(char *));
  173. D(("[%s]",sbuf));
  174. while ((sbuf = _pam_StrTok(sbuf, " \n\t", &tmp))) {
  175. D(("arg #%d",++count));
  176. D(("->[%s]",sbuf));
  177. strcpy(argvbufp, sbuf);
  178. D(("copied token"));
  179. *argvbuf = argvbufp;
  180. argvbufp += strlen(argvbufp) + 1;
  181. D(("stepped in argvbufp"));
  182. (*argc)++;
  183. argvbuf++;
  184. sbuf = NULL;
  185. D(("loop again?"));
  186. }
  187. }
  188. _pam_drop(sbuf_start);
  189. }
  190. }
  191. *argv = our_argv;
  192. D(("_pam_mkargv returned"));
  193. return(argvlen);
  194. }
  195. /*
  196. * this function is used to protect the modules from accidental or
  197. * semi-mallicious harm that an application may do to confuse the API.
  198. */
  199. void _pam_sanitize(pam_handle_t *pamh)
  200. {
  201. int old_caller_is = pamh->caller_is;
  202. /*
  203. * this is for security. We reset the auth-tokens here.
  204. */
  205. __PAM_TO_MODULE(pamh);
  206. pam_set_item(pamh, PAM_AUTHTOK, NULL);
  207. pam_set_item(pamh, PAM_OLDAUTHTOK, NULL);
  208. pamh->caller_is = old_caller_is;
  209. }
  210. /*
  211. * This function scans the array and replaces the _PAM_ACTION_UNDEF
  212. * entries with the default action.
  213. */
  214. void _pam_set_default_control(int *control_array, int default_action)
  215. {
  216. int i;
  217. for (i=0; i<_PAM_RETURN_VALUES; ++i) {
  218. if (control_array[i] == _PAM_ACTION_UNDEF) {
  219. control_array[i] = default_action;
  220. }
  221. }
  222. }
  223. /*
  224. * This function is used to parse a control string. This string is a
  225. * series of tokens of the following form:
  226. *
  227. * "[ ]*return_code[ ]*=[ ]*action/[ ]".
  228. */
  229. #include "pam_tokens.h"
  230. void _pam_parse_control(int *control_array, char *tok)
  231. {
  232. const char *error;
  233. int ret;
  234. while (*tok) {
  235. int act, len;
  236. /* skip leading space */
  237. while (isspace((int)*tok) && *++tok);
  238. if (!*tok)
  239. break;
  240. /* identify return code */
  241. for (ret=0; ret<=_PAM_RETURN_VALUES; ++ret) {
  242. len = strlen(_pam_token_returns[ret]);
  243. if (!strncmp(_pam_token_returns[ret], tok, len)) {
  244. break;
  245. }
  246. }
  247. if (ret > _PAM_RETURN_VALUES || !*(tok += len)) {
  248. error = "expecting return value";
  249. goto parse_error;
  250. }
  251. /* observe '=' */
  252. while (isspace((int)*tok) && *++tok);
  253. if (!*tok || *tok++ != '=') {
  254. error = "expecting '='";
  255. goto parse_error;
  256. }
  257. /* skip leading space */
  258. while (isspace((int)*tok) && *++tok);
  259. if (!*tok) {
  260. error = "expecting action";
  261. goto parse_error;
  262. }
  263. /* observe action type */
  264. for (act=0; act < (-(_PAM_ACTION_UNDEF)); ++act) {
  265. len = strlen(_pam_token_actions[act]);
  266. if (!strncmp(_pam_token_actions[act], tok, len)) {
  267. act *= -1;
  268. tok += len;
  269. break;
  270. }
  271. }
  272. if (act > 0) {
  273. /*
  274. * Either we have a number or we have hit an error. In
  275. * principle, there is nothing to stop us accepting
  276. * negative offsets. (Although we would have to think of
  277. * another way of encoding the tokens.) However, I really
  278. * think this would be both hard to administer and easily
  279. * cause looping problems. So, for now, we will just
  280. * allow forward jumps. (AGM 1998/1/7)
  281. */
  282. if (!isdigit((int)*tok)) {
  283. error = "expecting jump number";
  284. goto parse_error;
  285. }
  286. /* parse a number */
  287. act = 0;
  288. do {
  289. act *= 10;
  290. act += *tok - '0'; /* XXX - this assumes ascii behavior */
  291. } while (*++tok && isdigit((int)*tok));
  292. if (! act) {
  293. /* we do not allow 0 jumps. There is a token ('ignore')
  294. for that */
  295. error = "expecting non-zero";
  296. goto parse_error;
  297. }
  298. }
  299. /* set control_array element */
  300. if (ret != _PAM_RETURN_VALUES) {
  301. control_array[ret] = act;
  302. } else {
  303. /* set the default to 'act' */
  304. _pam_set_default_control(control_array, act);
  305. }
  306. }
  307. /* that was a success */
  308. return;
  309. parse_error:
  310. /* treat everything as bad */
  311. pam_syslog(NULL, LOG_ERR, "pam_parse: %s; [...%s]", error, tok);
  312. for (ret=0; ret<_PAM_RETURN_VALUES; control_array[ret++]=_PAM_ACTION_BAD);
  313. }