auth.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. /*
  2. auth.c -- Authorization Management
  3. This modules supports a user/role/ability based authorization scheme.
  4. In this scheme Users have passwords and can have multiple roles. A role is associated with the ability to do
  5. things like "admin" or "user" or "support". A role may have abilities (which are typically verbs) like "add",
  6. "shutdown".
  7. When the web server starts up, it loads a route and authentication configuration file that specifies the Users,
  8. Roles and Routes. Routes specify the required abilities to access URLs by specifying the URL prefix. Once logged
  9. in, the user's abilities are tested against the route abilities. When the web server receivess a request, the set of
  10. Routes is consulted to select the best route. If the routes requires abilities, the user must be logged in and
  11. authenticated.
  12. Three authentication backend protocols are supported:
  13. HTTP basic authentication which uses browser dialogs and clear text passwords (insecure unless over TLS)
  14. HTTP digest authentication which uses browser dialogs
  15. Web form authentication which uses a web page form to login (insecure unless over TLS)
  16. Copyright (c) All Rights Reserved. See details at the end of the file.
  17. */
  18. /********************************* Includes ***********************************/
  19. #include "goahead.h"
  20. #include "user.h"
  21. #if ME_GOAHEAD_AUTH
  22. #if ME_COMPILER_HAS_PAM
  23. #include <security/pam_appl.h>
  24. #endif
  25. /*********************************** Locals ***********************************/
  26. static WebsHash users = -1;
  27. static WebsHash roles = -1;
  28. static char *masterSecret;
  29. static int autoLogin = ME_GOAHEAD_AUTO_LOGIN;
  30. //static WebsVerify verifyPassword = websVerifyPasswordFromFile;
  31. static WebsVerify verifyPassword = websVerifyPasswordFromBMC;
  32. #if ME_COMPILER_HAS_PAM
  33. typedef struct {
  34. char *name;
  35. char *password;
  36. } UserInfo;
  37. #if MACOSX
  38. typedef int Gid;
  39. #else
  40. typedef gid_t Gid;
  41. #endif
  42. #endif
  43. /********************************** Forwards **********************************/
  44. static void computeAbilities(WebsHash abilities, char *role, int depth);
  45. static void computeUserAbilities(WebsUser *user);
  46. static WebsUser *createUser(char *username, char *password, char *roles);
  47. static void freeRole(WebsRole *rp);
  48. static void freeUser(WebsUser *up);
  49. static void logoutServiceProc(Webs *wp);
  50. static void loginServiceProc(Webs *wp);
  51. #if ME_GOAHEAD_JAVASCRIPT && FUTURE
  52. static int jsCan(int jsid, Webs *wp, int argc, char **argv);
  53. #endif
  54. #if ME_GOAHEAD_DIGEST
  55. static char *calcDigest(Webs *wp, char *username, char *password);
  56. static char *createDigestNonce(Webs *wp);
  57. static char *parseDigestNonce(char *nonce, char **secret, char **realm, WebsTime *when);
  58. #endif
  59. #if ME_COMPILER_HAS_PAM
  60. static int pamChat(int msgCount, const struct pam_message **msg, struct pam_response **resp, void *data);
  61. #endif
  62. /************************************ Code ************************************/
  63. PUBLIC bool websAuthenticate(Webs *wp)
  64. {
  65. WebsRoute *route;
  66. char *username;
  67. int cached;
  68. assert(wp);
  69. assert(wp->route);
  70. route = wp->route;
  71. if (!route || !route->authType || autoLogin) {
  72. /* Authentication not required */
  73. return 1;
  74. }
  75. cached = 0;
  76. if (wp->cookie && websGetSession(wp, 0) != 0) {
  77. /*
  78. Retrieve authentication state from the session storage. Faster than re-authenticating.
  79. */
  80. if ((username = (char*) websGetSessionVar(wp, WEBS_SESSION_USERNAME, 0)) != 0) {
  81. cached = 1;
  82. wfree(wp->username);
  83. wp->username = sclone(username);
  84. }
  85. }
  86. if (!cached) {
  87. if (wp->authType && !smatch(wp->authType, route->authType)) {
  88. websError(wp, HTTP_CODE_UNAUTHORIZED, "Access denied. Wrong authentication protocol type.");
  89. return 0;
  90. }
  91. if (wp->authDetails && route->parseAuth) {
  92. if (!(route->parseAuth)(wp)) {
  93. wp->username = 0;
  94. }
  95. }
  96. if (!wp->username || !*wp->username) {
  97. if (route->askLogin) {
  98. (route->askLogin)(wp);
  99. }
  100. websRedirectByStatus(wp, HTTP_CODE_UNAUTHORIZED);
  101. return 0;
  102. }
  103. if (!(route->verify)(wp)) {
  104. if (route->askLogin) {
  105. (route->askLogin)(wp);
  106. }
  107. websRedirectByStatus(wp, HTTP_CODE_UNAUTHORIZED);
  108. return 0;
  109. }
  110. /*
  111. Store authentication state and user in session storage
  112. */
  113. if (websGetSession(wp, 1) != 0) {
  114. websSetSessionVar(wp, WEBS_SESSION_USERNAME, wp->username);
  115. }
  116. }
  117. return 1;
  118. }
  119. PUBLIC int websOpenAuth(int minimal)
  120. {
  121. char sbuf[64];
  122. assert(minimal == 0 || minimal == 1);
  123. if ((users = hashCreate(-1)) < 0) {
  124. return -1;
  125. }
  126. if ((roles = hashCreate(-1)) < 0) {
  127. return -1;
  128. }
  129. if (!minimal) {
  130. fmt(sbuf, sizeof(sbuf), "%x:%x", rand(), time(0));
  131. masterSecret = websMD5(sbuf);
  132. #if ME_GOAHEAD_JAVASCRIPT && FUTURE
  133. websJsDefine("can", jsCan);
  134. #endif
  135. websDefineAction("login", loginServiceProc);
  136. websDefineAction("logout", logoutServiceProc);
  137. }
  138. if (smatch(ME_GOAHEAD_AUTH_STORE, "file")) {
  139. verifyPassword = websVerifyPasswordFromFile;
  140. #if ME_COMPILER_HAS_PAM
  141. } else if (smatch(ME_GOAHEAD_AUTH_STORE, "pam")) {
  142. verifyPassword = websVerifyPasswordFromPam;
  143. #endif
  144. } else if (smatch(ME_GOAHEAD_AUTH_STORE, "BMC")) {
  145. verifyPassword = websVerifyPasswordFromBMC;
  146. }
  147. return 0;
  148. }
  149. PUBLIC void websCloseAuth()
  150. {
  151. WebsKey *key, *next;
  152. wfree(masterSecret);
  153. if (users >= 0) {
  154. for (key = hashFirst(users); key; key = next) {
  155. next = hashNext(users, key);
  156. freeUser(key->content.value.symbol);
  157. }
  158. hashFree(users);
  159. users = -1;
  160. }
  161. if (roles >= 0) {
  162. for (key = hashFirst(roles); key; key = next) {
  163. next = hashNext(roles, key);
  164. freeRole(key->content.value.symbol);
  165. }
  166. hashFree(roles);
  167. roles = -1;
  168. }
  169. }
  170. #if KEEP
  171. PUBLIC int websWriteAuthFile(char *path)
  172. {
  173. FILE *fp;
  174. WebsKey *kp, *ap;
  175. WebsRole *role;
  176. WebsUser *user;
  177. char *tempFile;
  178. assert(path && *path);
  179. tempFile = websTempFile(NULL, NULL);
  180. if ((fp = fopen(tempFile, "w" FILE_TEXT)) == 0) {
  181. error("Cannot open %s", tempFile);
  182. wfree(tempFile);
  183. return -1;
  184. }
  185. fprintf(fp, "#\n# %s - Authorization data\n#\n\n", basename(path));
  186. if (roles >= 0) {
  187. for (kp = hashFirst(roles); kp; kp = hashNext(roles, kp)) {
  188. role = kp->content.value.symbol;
  189. fprintf(fp, "role name=%s abilities=", kp->name.value.string);
  190. for (ap = hashFirst(role->abilities); ap; ap = hashNext(role->abilities, ap)) {
  191. fprintf(fp, "%s,", ap->name.value.string);
  192. }
  193. fputc('\n', fp);
  194. }
  195. fputc('\n', fp);
  196. }
  197. if (users >= 0) {
  198. for (kp = hashFirst(users); kp; kp = hashNext(users, kp)) {
  199. user = kp->content.value.symbol;
  200. fprintf(fp, "user name=%s password=%s roles=%s", user->name, user->password, user->roles);
  201. fputc('\n', fp);
  202. }
  203. }
  204. fclose(fp);
  205. unlink(path);
  206. if (rename(tempFile, path) < 0) {
  207. error("Cannot create new %s", path);
  208. wfree(tempFile);
  209. return -1;
  210. }
  211. wfree(tempFile);
  212. return 0;
  213. }
  214. #endif
  215. static WebsUser *createUser(char *username, char *password, char *roles) //jimbo todo
  216. {
  217. WebsUser *user;
  218. assert(username);
  219. if ((user = walloc(sizeof(WebsUser))) == 0) {
  220. return 0;
  221. }
  222. user->name = sclone(username);
  223. user->roles = sclone(roles);
  224. user->password = sclone(password);
  225. user->abilities = -1;
  226. return user;
  227. }
  228. WebsUser *websAddUser(char *username, char *password, char *roles) //jimbo todo
  229. {
  230. WebsUser *user;
  231. if (!username) {
  232. error("User is missing name");
  233. return 0;
  234. }
  235. if (websLookupUser(username)) {
  236. error("User %s already exists", username);
  237. /* Already exists */
  238. return 0;
  239. }
  240. if ((user = createUser(username, password, roles)) == 0) {
  241. return 0;
  242. }
  243. if (hashEnter(users, username, valueSymbol(user), 0) == 0) {
  244. return 0;
  245. }
  246. return user;
  247. }
  248. PUBLIC int websRemoveUser(char *username) //jimbo todo
  249. {
  250. WebsKey *key;
  251. assert(username);
  252. if ((key = hashLookup(users, username)) != 0) {
  253. freeUser(key->content.value.symbol);
  254. }
  255. return hashDelete(users, username);
  256. }
  257. static void freeUser(WebsUser *up) //jimbo delete
  258. {
  259. assert(up);
  260. hashFree(up->abilities);
  261. wfree(up->name);
  262. wfree(up->password);
  263. wfree(up->roles);
  264. wfree(up);
  265. }
  266. PUBLIC int websSetUserPassword(char *username, char *password) //jimbo todo
  267. {
  268. WebsUser *user;
  269. assert(username);
  270. if ((user = websLookupUser(username)) == 0) {
  271. return -1;
  272. }
  273. wfree(user->password);
  274. user->password = sclone(password);
  275. return 0;
  276. }
  277. PUBLIC int websSetUserRoles(char *username, char *roles) //jimbo todo
  278. {
  279. WebsUser *user;
  280. assert(username);
  281. if ((user = websLookupUser(username)) == 0) {
  282. return -1;
  283. }
  284. wfree(user->roles);
  285. user->roles = sclone(roles);
  286. computeUserAbilities(user);
  287. return 0;
  288. }
  289. WebsUser *websLookupUser(char *username)
  290. {
  291. WebsKey *key;
  292. assert(username);
  293. if ((key = hashLookup(users, username)) == 0) {
  294. return 0;
  295. }
  296. return (WebsUser*) key->content.value.symbol;
  297. }
  298. static void computeAbilities(WebsHash abilities, char *role, int depth)
  299. {
  300. WebsRole *rp;
  301. WebsKey *key;
  302. assert(abilities >= 0);
  303. assert(role && *role);
  304. assert(depth >= 0);
  305. if (depth > 20) {
  306. error("Recursive ability definition for %s", role);
  307. return;
  308. }
  309. if (roles >= 0) {
  310. if ((key = hashLookup(roles, role)) != 0) {
  311. rp = (WebsRole*) key->content.value.symbol;
  312. for (key = hashFirst(rp->abilities); key; key = hashNext(rp->abilities, key)) {
  313. computeAbilities(abilities, key->name.value.string, ++depth);
  314. }
  315. } else {
  316. hashEnter(abilities, role, valueInteger(0), 0);
  317. }
  318. }
  319. }
  320. static void computeUserAbilities(WebsUser *user)
  321. {
  322. char *ability, *roles, *tok;
  323. assert(user);
  324. if ((user->abilities = hashCreate(-1)) == 0) {
  325. return;
  326. }
  327. roles = sclone(user->roles);
  328. for (ability = stok(roles, " \t,", &tok); ability; ability = stok(NULL, " \t,", &tok)) {
  329. computeAbilities(user->abilities, ability, 0);
  330. }
  331. #if ME_DEBUG
  332. {
  333. WebsKey *key;
  334. trace(5 | WEBS_RAW_MSG, "User \"%s\" has abilities: ", user->name);
  335. for (key = hashFirst(user->abilities); key; key = hashNext(user->abilities, key)) {
  336. trace(5 | WEBS_RAW_MSG, "%s ", key->name.value.string);
  337. ability = key->name.value.string;
  338. }
  339. trace(5, "");
  340. }
  341. #endif
  342. wfree(roles);
  343. }
  344. PUBLIC void websComputeAllUserAbilities()
  345. {
  346. WebsUser *user;
  347. WebsKey *sym;
  348. if (users) {
  349. for (sym = hashFirst(users); sym; sym = hashNext(users, sym)) {
  350. user = (WebsUser*) sym->content.value.symbol;
  351. computeUserAbilities(user);
  352. }
  353. }
  354. }
  355. WebsRole *websAddRole(char *name, WebsHash abilities)
  356. {
  357. WebsRole *rp;
  358. if (!name) {
  359. error("Role is missing name");
  360. return 0;
  361. }
  362. if (hashLookup(roles, name)) {
  363. error("Role %s already exists", name);
  364. /* Already exists */
  365. return 0;
  366. }
  367. if ((rp = walloc(sizeof(WebsRole))) == 0) {
  368. return 0;
  369. }
  370. rp->abilities = abilities;
  371. if (hashEnter(roles, name, valueSymbol(rp), 0) == 0) {
  372. return 0;
  373. }
  374. return rp;
  375. }
  376. static void freeRole(WebsRole *rp)
  377. {
  378. assert(rp);
  379. hashFree(rp->abilities);
  380. wfree(rp);
  381. }
  382. /*
  383. Does not recompute abilities for users that use this role
  384. */
  385. PUBLIC int websRemoveRole(char *name)
  386. {
  387. WebsRole *rp;
  388. WebsKey *sym;
  389. assert(name && *name);
  390. if (roles) {
  391. if ((sym = hashLookup(roles, name)) == 0) {
  392. return -1;
  393. }
  394. rp = sym->content.value.symbol;
  395. hashFree(rp->abilities);
  396. wfree(rp);
  397. return hashDelete(roles, name);
  398. }
  399. return -1;
  400. }
  401. PUBLIC WebsHash websGetUsers()
  402. {
  403. return users;
  404. }
  405. PUBLIC WebsHash websGetRoles()
  406. {
  407. return roles;
  408. }
  409. PUBLIC bool websLoginUser(Webs *wp, char *username, char *password)
  410. {
  411. assert(wp);
  412. assert(wp->route);
  413. assert(username);
  414. assert(password);
  415. printf("websLoginUser, username: %s, password: %s\n", username, password);
  416. if (!wp->route || !wp->route->verify) {
  417. return 0;
  418. }
  419. wfree(wp->username);
  420. wp->username = sclone(username);
  421. wfree(wp->password);
  422. wp->password = sclone(password);
  423. if (!(wp->route->verify)(wp)) {
  424. trace(2, "Password does not match");
  425. return 0;
  426. }
  427. trace(2, "Login successful for %s", username);
  428. websCreateSession(wp);
  429. websSetSessionVar(wp, WEBS_SESSION_USERNAME, wp->username);
  430. return 1;
  431. }
  432. PUBLIC bool websLogoutUser(Webs *wp)
  433. {
  434. assert(wp);
  435. websRemoveSessionVar(wp, WEBS_SESSION_USERNAME);
  436. websDestroySession(wp);
  437. if (smatch(wp->authType, "basic") || smatch(wp->authType, "digest")) {
  438. websError(wp, HTTP_CODE_UNAUTHORIZED, "Logged out.");
  439. return 0;
  440. }
  441. websRedirectByStatus(wp, HTTP_CODE_OK);
  442. return 1;
  443. }
  444. /*
  445. Internal login service routine for Form-based auth
  446. */
  447. static void loginServiceProc(Webs *wp)
  448. {
  449. WebsRoute *route;
  450. assert(wp);
  451. route = wp->route;
  452. assert(route);
  453. if (websLoginUser(wp, websGetVar(wp, "username", ""), websGetVar(wp, "password", ""))) {
  454. /* If the application defines a referrer session var, redirect to that */
  455. char *referrer;
  456. if ((referrer = websGetSessionVar(wp, "referrer", 0)) != 0) {
  457. websRedirect(wp, referrer);
  458. } else {
  459. websRedirectByStatus(wp, HTTP_CODE_OK);
  460. }
  461. websSetSessionVar(wp, "loginStatus", "ok");
  462. } else {
  463. if (route->askLogin) {
  464. (route->askLogin)(wp);
  465. }
  466. websSetSessionVar(wp, "loginStatus", "failed");
  467. websRedirectByStatus(wp, HTTP_CODE_UNAUTHORIZED);
  468. }
  469. }
  470. static void logoutServiceProc(Webs *wp)
  471. {
  472. websLogoutUser(wp);
  473. }
  474. static void basicLogin(Webs *wp)
  475. {
  476. assert(wp);
  477. assert(wp->route);
  478. wfree(wp->authResponse);
  479. wp->authResponse = sfmt("Basic realm=\"%s\"", ME_GOAHEAD_REALM);
  480. }
  481. void websSetPasswordStoreVerify(WebsVerify verify)
  482. {
  483. verifyPassword = verify;
  484. }
  485. WebsVerify websGetPasswordStoreVerify()
  486. {
  487. return verifyPassword;
  488. }
  489. PUBLIC bool websVerifyPasswordFromFile(Webs *wp)
  490. {
  491. char passbuf[ME_GOAHEAD_LIMIT_PASSWORD * 3 + 3];
  492. bool success;
  493. assert(wp);
  494. if (!wp->user && (wp->user = websLookupUser(wp->username)) == 0) {
  495. trace(5, "verifyUser: Unknown user \"%s\"", wp->username);
  496. return 0;
  497. }
  498. /*
  499. Verify the password. If using Digest auth, we compare the digest of the password.
  500. Otherwise we encode the plain-text password and compare that
  501. */
  502. if (!wp->encoded) {
  503. fmt(passbuf, sizeof(passbuf), "%s:%s:%s", wp->username, ME_GOAHEAD_REALM, wp->password);
  504. wfree(wp->password);
  505. wp->password = websMD5(passbuf);
  506. printf("---> encoded password: %s\n", wp->password);
  507. wp->encoded = 1;
  508. }
  509. if (wp->digest) {
  510. success = smatch(wp->password, wp->digest);
  511. printf("---> password: %s, digest: %s\n", wp->password, wp->digest);
  512. } else {
  513. success = smatch(wp->password, wp->user->password);
  514. printf("---> password: %s, user->password: %s\n", wp->password, wp->user->password);
  515. }
  516. if (success) {
  517. trace(5, "User \"%s\" authenticated", wp->username);
  518. } else {
  519. trace(5, "Password for user \"%s\" failed to authenticate", wp->username);
  520. }
  521. return success;
  522. }
  523. #if ME_COMPILER_HAS_PAM
  524. /*
  525. Copy this routine if creating your own custom password store back end
  526. */
  527. PUBLIC bool websVerifyPasswordFromPam(Webs *wp)
  528. {
  529. WebsBuf abilities;
  530. pam_handle_t *pamh;
  531. UserInfo info;
  532. struct pam_conv conv = { pamChat, &info };
  533. struct group *gp;
  534. int res, i;
  535. assert(wp);
  536. assert(wp->username && wp->username);
  537. assert(wp->password);
  538. assert(!wp->encoded);
  539. info.name = (char*) wp->username;
  540. info.password = (char*) wp->password;
  541. pamh = NULL;
  542. if ((res = pam_start("login", info.name, &conv, &pamh)) != PAM_SUCCESS) {
  543. return 0;
  544. }
  545. if ((res = pam_authenticate(pamh, PAM_DISALLOW_NULL_AUTHTOK)) != PAM_SUCCESS) {
  546. pam_end(pamh, PAM_SUCCESS);
  547. trace(5, "httpPamVerifyUser failed to verify %s", wp->username);
  548. return 0;
  549. }
  550. pam_end(pamh, PAM_SUCCESS);
  551. trace(5, "httpPamVerifyUser verified %s", wp->username);
  552. if (!wp->user) {
  553. wp->user = websLookupUser(wp->username);
  554. }
  555. if (!wp->user) {
  556. Gid groups[32];
  557. int ngroups;
  558. /*
  559. Create a temporary user with a abilities set to the groups
  560. */
  561. ngroups = sizeof(groups) / sizeof(Gid);
  562. if ((i = getgrouplist(wp->username, 99999, groups, &ngroups)) >= 0) {
  563. bufCreate(&abilities, 128, -1);
  564. for (i = 0; i < ngroups; i++) {
  565. if ((gp = getgrgid(groups[i])) != 0) {
  566. bufPutStr(&abilities, gp->gr_name);
  567. bufPutc(&abilities, ' ');
  568. }
  569. }
  570. bufAddNull(&abilities);
  571. trace(5, "Create temp user \"%s\" with abilities: %s", wp->username, abilities.servp);
  572. if ((wp->user = websAddUser(wp->username, 0, abilities.servp)) == 0) {
  573. return 0;
  574. }
  575. computeUserAbilities(wp->user);
  576. }
  577. }
  578. return 1;
  579. }
  580. /*
  581. Callback invoked by the pam_authenticate function
  582. */
  583. static int pamChat(int msgCount, const struct pam_message **msg, struct pam_response **resp, void *data)
  584. {
  585. UserInfo *info;
  586. struct pam_response *reply;
  587. int i;
  588. i = 0;
  589. reply = 0;
  590. info = (UserInfo*) data;
  591. if (resp == 0 || msg == 0 || info == 0) {
  592. return PAM_CONV_ERR;
  593. }
  594. if ((reply = calloc(msgCount, sizeof(struct pam_response))) == 0) {
  595. return PAM_CONV_ERR;
  596. }
  597. for (i = 0; i < msgCount; i++) {
  598. reply[i].resp_retcode = 0;
  599. reply[i].resp = 0;
  600. switch (msg[i]->msg_style) {
  601. case PAM_PROMPT_ECHO_ON:
  602. reply[i].resp = sclone(info->name);
  603. break;
  604. case PAM_PROMPT_ECHO_OFF:
  605. /* Retrieve the user password and pass onto pam */
  606. reply[i].resp = sclone(info->password);
  607. break;
  608. default:
  609. free(reply);
  610. return PAM_CONV_ERR;
  611. }
  612. }
  613. *resp = reply;
  614. return PAM_SUCCESS;
  615. }
  616. #endif /* ME_COMPILER_HAS_PAM */
  617. #if ME_GOAHEAD_JAVASCRIPT && FUTURE
  618. static int jsCan(int jsid, Webs *wp, int argc, char **argv)
  619. {
  620. assert(jsid >= 0);
  621. assert(wp);
  622. if (websCanString(wp, argv[0])) {
  623. // FUTURE
  624. return 0;
  625. }
  626. return 1;
  627. }
  628. #endif
  629. static bool parseBasicDetails(Webs *wp)
  630. {
  631. char *cp, *userAuth;
  632. assert(wp);
  633. /*
  634. Split userAuth into userid and password
  635. */
  636. cp = 0;
  637. if ((userAuth = websDecode64(wp->authDetails)) != 0) {
  638. if ((cp = strchr(userAuth, ':')) != NULL) {
  639. *cp++ = '\0';
  640. }
  641. }
  642. wfree(wp->username);
  643. wfree(wp->password);
  644. if (cp) {
  645. wp->username = sclone(userAuth);
  646. wp->password = sclone(cp);
  647. wp->encoded = 0;
  648. } else {
  649. wp->username = sclone("");
  650. wp->password = sclone("");
  651. }
  652. wfree(userAuth);
  653. return 1;
  654. }
  655. #if ME_GOAHEAD_DIGEST
  656. static void digestLogin(Webs *wp)
  657. {
  658. char *nonce, *opaque;
  659. assert(wp);
  660. assert(wp->route);
  661. nonce = createDigestNonce(wp);
  662. /* Opaque is unused. Set to anything */
  663. opaque = "5ccc069c403ebaf9f0171e9517f40e41";
  664. wfree(wp->authResponse);
  665. wp->authResponse = sfmt(
  666. "Digest realm=\"%s\", domain=\"%s\", qop=\"%s\", nonce=\"%s\", opaque=\"%s\", algorithm=\"%s\", stale=\"%s\"",
  667. ME_GOAHEAD_REALM, websGetServerUrl(), "auth", nonce, opaque, "MD5", "FALSE");
  668. wfree(nonce);
  669. }
  670. static bool parseDigestDetails(Webs *wp)
  671. {
  672. WebsTime when;
  673. char *decoded, *value, *tok, *key, *keyBuf, *dp, *sp, *secret, *realm;
  674. int seenComma;
  675. assert(wp);
  676. key = keyBuf = sclone(wp->authDetails);
  677. while (*key) {
  678. while (*key && isspace((uchar) *key)) {
  679. key++;
  680. }
  681. tok = key;
  682. while (*tok && !isspace((uchar) *tok) && *tok != ',' && *tok != '=') {
  683. tok++;
  684. }
  685. if (*tok) {
  686. *tok++ = '\0';
  687. }
  688. while (isspace((uchar) *tok)) {
  689. tok++;
  690. }
  691. seenComma = 0;
  692. if (*tok == '\"') {
  693. value = ++tok;
  694. while (*tok != '\"' && *tok != '\0') {
  695. tok++;
  696. }
  697. } else {
  698. value = tok;
  699. while (*tok != ',' && *tok != '\0') {
  700. tok++;
  701. }
  702. seenComma++;
  703. }
  704. if (*tok) {
  705. *tok++ = '\0';
  706. }
  707. /*
  708. Handle back-quoting
  709. */
  710. if (strchr(value, '\\')) {
  711. for (dp = sp = value; *sp; sp++) {
  712. if (*sp == '\\') {
  713. sp++;
  714. }
  715. *dp++ = *sp++;
  716. }
  717. *dp = '\0';
  718. }
  719. /*
  720. user, response, oqaque, uri, realm, nonce, nc, cnonce, qop
  721. */
  722. switch (tolower((uchar) *key)) {
  723. case 'a':
  724. if (scaselesscmp(key, "algorithm") == 0) {
  725. break;
  726. } else if (scaselesscmp(key, "auth-param") == 0) {
  727. break;
  728. }
  729. break;
  730. case 'c':
  731. if (scaselesscmp(key, "cnonce") == 0) {
  732. wfree(wp->cnonce);
  733. wp->cnonce = sclone(value);
  734. }
  735. break;
  736. case 'd':
  737. if (scaselesscmp(key, "domain") == 0) {
  738. break;
  739. }
  740. break;
  741. case 'n':
  742. if (scaselesscmp(key, "nc") == 0) {
  743. wfree(wp->nc);
  744. wp->nc = sclone(value);
  745. } else if (scaselesscmp(key, "nonce") == 0) {
  746. wfree(wp->nonce);
  747. wp->nonce = sclone(value);
  748. }
  749. break;
  750. case 'o':
  751. if (scaselesscmp(key, "opaque") == 0) {
  752. wfree(wp->opaque);
  753. wp->opaque = sclone(value);
  754. }
  755. break;
  756. case 'q':
  757. if (scaselesscmp(key, "qop") == 0) {
  758. wfree(wp->qop);
  759. wp->qop = sclone(value);
  760. }
  761. break;
  762. case 'r':
  763. if (scaselesscmp(key, "realm") == 0) {
  764. wfree(wp->realm);
  765. wp->realm = sclone(value);
  766. } else if (scaselesscmp(key, "response") == 0) {
  767. /* Store the response digest in the password field. This is MD5(user:realm:password) */
  768. wfree(wp->password);
  769. wp->password = sclone(value);
  770. wp->encoded = 1;
  771. }
  772. break;
  773. case 's':
  774. if (scaselesscmp(key, "stale") == 0) {
  775. break;
  776. }
  777. case 'u':
  778. if (scaselesscmp(key, "uri") == 0) {
  779. wfree(wp->digestUri);
  780. wp->digestUri = sclone(value);
  781. } else if (scaselesscmp(key, "username") == 0 || scaselesscmp(key, "user") == 0) {
  782. wfree(wp->username);
  783. wp->username = sclone(value);
  784. }
  785. break;
  786. default:
  787. /* Just ignore keywords we don't understand */
  788. ;
  789. }
  790. key = tok;
  791. if (!seenComma) {
  792. while (*key && *key != ',') {
  793. key++;
  794. }
  795. if (*key) {
  796. key++;
  797. }
  798. }
  799. }
  800. wfree(keyBuf);
  801. if (wp->username == 0 || wp->realm == 0 || wp->nonce == 0 || wp->route == 0 || wp->password == 0) {
  802. return 0;
  803. }
  804. if (wp->qop && (wp->cnonce == 0 || wp->nc == 0)) {
  805. return 0;
  806. }
  807. if (wp->qop == 0) {
  808. wp->qop = sclone("");
  809. }
  810. /*
  811. Validate the nonce value - prevents replay attacks
  812. */
  813. when = 0; secret = 0; realm = 0;
  814. decoded = parseDigestNonce(wp->nonce, &secret, &realm, &when);
  815. if (!smatch(masterSecret, secret)) {
  816. trace(2, "Access denied: Nonce mismatch");
  817. wfree(decoded);
  818. return 0;
  819. } else if (!smatch(realm, ME_GOAHEAD_REALM)) {
  820. trace(2, "Access denied: Realm mismatch");
  821. wfree(decoded);
  822. return 0;
  823. } else if (!smatch(wp->qop, "auth")) {
  824. trace(2, "Access denied: Bad qop");
  825. wfree(decoded);
  826. return 0;
  827. } else if ((when + (5 * 60)) < time(0)) {
  828. trace(2, "Access denied: Nonce is stale");
  829. wfree(decoded);
  830. return 0;
  831. }
  832. if (!wp->user) {
  833. if ((wp->user = websLookupUser(wp->username)) == 0) {
  834. trace(2, "Access denied: user is unknown");
  835. wfree(decoded);
  836. return 0;
  837. }
  838. }
  839. wfree(decoded);
  840. wfree(wp->digest);
  841. wp->digest = calcDigest(wp, 0, wp->user->password);
  842. return 1;
  843. }
  844. /*
  845. Create a nonce value for digest authentication (RFC 2617)
  846. */
  847. static char *createDigestNonce(Webs *wp)
  848. {
  849. static int64 next = 0;
  850. char nonce[256];
  851. assert(wp);
  852. assert(wp->route);
  853. fmt(nonce, sizeof(nonce), "%s:%s:%x:%x", masterSecret, ME_GOAHEAD_REALM, time(0), next++);
  854. return websEncode64(nonce);
  855. }
  856. static char *parseDigestNonce(char *nonce, char **secret, char **realm, WebsTime *when)
  857. {
  858. char *tok, *decoded, *whenStr;
  859. assert(nonce && *nonce);
  860. assert(secret);
  861. assert(realm);
  862. assert(when);
  863. if ((decoded = websDecode64(nonce)) == 0) {
  864. return 0;
  865. }
  866. *secret = stok(decoded, ":", &tok);
  867. *realm = stok(NULL, ":", &tok);
  868. whenStr = stok(NULL, ":", &tok);
  869. *when = hextoi(whenStr);
  870. return decoded;
  871. }
  872. /*
  873. Get a Digest value using the MD5 algorithm -- See RFC 2617 to understand this code.
  874. */
  875. static char *calcDigest(Webs *wp, char *username, char *password)
  876. {
  877. char a1Buf[256], a2Buf[256], digestBuf[256];
  878. char *ha1, *ha2, *method, *result;
  879. assert(wp);
  880. assert(password);
  881. /*
  882. Compute HA1. If username == 0, then the password is already expected to be in the HA1 format
  883. (MD5(username:realm:password).
  884. */
  885. if (username == 0) {
  886. ha1 = sclone(password);
  887. } else {
  888. fmt(a1Buf, sizeof(a1Buf), "%s:%s:%s", username, wp->realm, password);
  889. ha1 = websMD5(a1Buf);
  890. }
  891. /*
  892. HA2
  893. */
  894. method = wp->method;
  895. fmt(a2Buf, sizeof(a2Buf), "%s:%s", method, wp->digestUri);
  896. ha2 = websMD5(a2Buf);
  897. /*
  898. H(HA1:nonce:HA2)
  899. */
  900. if (scmp(wp->qop, "auth") == 0) {
  901. fmt(digestBuf, sizeof(digestBuf), "%s:%s:%s:%s:%s:%s", ha1, wp->nonce, wp->nc, wp->cnonce, wp->qop, ha2);
  902. } else if (scmp(wp->qop, "auth-int") == 0) {
  903. fmt(digestBuf, sizeof(digestBuf), "%s:%s:%s:%s:%s:%s", ha1, wp->nonce, wp->nc, wp->cnonce, wp->qop, ha2);
  904. } else {
  905. fmt(digestBuf, sizeof(digestBuf), "%s:%s:%s", ha1, wp->nonce, ha2);
  906. }
  907. result = websMD5(digestBuf);
  908. wfree(ha1);
  909. wfree(ha2);
  910. return result;
  911. }
  912. #endif /* ME_GOAHEAD_DIGEST */
  913. PUBLIC int websSetRouteAuth(WebsRoute *route, char *auth)
  914. {
  915. WebsParseAuth parseAuth;
  916. WebsAskLogin askLogin;
  917. assert(route);
  918. assert(auth && *auth);
  919. askLogin = 0;
  920. parseAuth = 0;
  921. if (smatch(auth, "basic")) {
  922. askLogin = basicLogin;
  923. parseAuth = parseBasicDetails;
  924. #if ME_GOAHEAD_DIGEST
  925. } else if (smatch(auth, "digest")) {
  926. askLogin = digestLogin;
  927. parseAuth = parseDigestDetails;
  928. #endif
  929. } else {
  930. auth = 0;
  931. }
  932. route->authType = sclone(auth);
  933. route->askLogin = askLogin;
  934. route->parseAuth = parseAuth;
  935. return 0;
  936. }
  937. #endif /* ME_GOAHEAD_AUTH */
  938. /*
  939. Copyright (c) Embedthis Software. All Rights Reserved.
  940. This software is distributed under commercial and open source licenses.
  941. You may use the Embedthis GoAhead open source license or you may acquire
  942. a commercial license from Embedthis Software. You agree to be fully bound
  943. by the terms of either license. Consult the LICENSE.md distributed with
  944. this software for full details and other copyrights.
  945. */