/xdm-1.1.11/greeter/greet.c

# · C · 832 lines · 641 code · 90 blank · 101 comment · 102 complexity · 83609a800d10a0596848e66585d87189 MD5 · raw file

  1. /*
  2. Copyright 1988, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included
  9. in all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  11. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  13. IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
  14. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  15. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  16. OTHER DEALINGS IN THE SOFTWARE.
  17. Except as contained in this notice, the name of The Open Group shall
  18. not be used in advertising or otherwise to promote the sale, use or
  19. other dealings in this Software without prior written authorization
  20. from The Open Group.
  21. */
  22. /*
  23. * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
  24. *
  25. * Permission is hereby granted, free of charge, to any person obtaining a
  26. * copy of this software and associated documentation files (the "Software"),
  27. * to deal in the Software without restriction, including without limitation
  28. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  29. * and/or sell copies of the Software, and to permit persons to whom the
  30. * Software is furnished to do so, subject to the following conditions:
  31. *
  32. * The above copyright notice and this permission notice (including the next
  33. * paragraph) shall be included in all copies or substantial portions of the
  34. * Software.
  35. *
  36. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  37. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  38. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  39. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  40. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  41. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  42. * DEALINGS IN THE SOFTWARE.
  43. */
  44. /*
  45. * xdm - display manager daemon
  46. * Author: Keith Packard, X Consortium
  47. *
  48. * widget to get username/password
  49. *
  50. */
  51. #ifdef HAVE_CONFIG_H
  52. # include "config.h"
  53. #endif
  54. #include <X11/Intrinsic.h>
  55. #include <X11/StringDefs.h>
  56. #include <X11/Shell.h>
  57. #include <X11/XKBlib.h>
  58. #ifdef USE_XINERAMA
  59. # include <X11/extensions/Xinerama.h>
  60. #endif
  61. #include "dm.h"
  62. #include "dm_error.h"
  63. #include "greet.h"
  64. #include "LoginP.h"
  65. #if defined(HAVE_OPENLOG) && defined(HAVE_SYSLOG_H)
  66. # define USE_SYSLOG
  67. # include <syslog.h>
  68. # ifndef LOG_AUTHPRIV
  69. # define LOG_AUTHPRIV LOG_AUTH
  70. # endif
  71. # ifndef LOG_PID
  72. # define LOG_PID 0
  73. # endif
  74. #endif
  75. #ifdef HAVE_LIBAUDIT
  76. #include <libaudit.h>
  77. #include <pwd.h>
  78. #else
  79. #define log_to_audit_system(l,h,s) do { ; } while (0)
  80. #endif
  81. #include <string.h>
  82. #if defined(SECURE_RPC) && defined(sun)
  83. /* Go figure, there's no getdomainname() prototype available */
  84. extern int getdomainname(char *name, size_t len);
  85. #endif
  86. /*
  87. * Function pointers filled in by the initial call ito the library
  88. */
  89. int (*__xdm_PingServer)(struct display *d, Display *alternateDpy) = NULL;
  90. void (*__xdm_SessionPingFailed)(struct display *d) = NULL;
  91. void (*__xdm_Debug)(const char * fmt, ...) = NULL;
  92. void (*__xdm_RegisterCloseOnFork)(int fd) = NULL;
  93. void (*__xdm_SecureDisplay)(struct display *d, Display *dpy) = NULL;
  94. void (*__xdm_UnsecureDisplay)(struct display *d, Display *dpy) = NULL;
  95. void (*__xdm_ClearCloseOnFork)(int fd) = NULL;
  96. void (*__xdm_SetupDisplay)(struct display *d) = NULL;
  97. void (*__xdm_LogError)(const char * fmt, ...) = NULL;
  98. void (*__xdm_SessionExit)(struct display *d, int status, int removeAuth) = NULL;
  99. void (*__xdm_DeleteXloginResources)(struct display *d, Display *dpy) = NULL;
  100. int (*__xdm_source)(char **environ, char *file) = NULL;
  101. char **(*__xdm_defaultEnv)(void) = NULL;
  102. char **(*__xdm_setEnv)(char **e, char *name, char *value) = NULL;
  103. char **(*__xdm_putEnv)(const char *string, char **env) = NULL;
  104. char **(*__xdm_parseArgs)(char **argv, char *string) = NULL;
  105. void (*__xdm_printEnv)(char **e) = NULL;
  106. char **(*__xdm_systemEnv)(struct display *d, char *user, char *home) = NULL;
  107. void (*__xdm_LogOutOfMem)(const char * fmt, ...) = NULL;
  108. void (*__xdm_setgrent)(void) = NULL;
  109. struct group *(*__xdm_getgrent)(void) = NULL;
  110. void (*__xdm_endgrent)(void) = NULL;
  111. # ifdef HAVE_GETSPNAM
  112. struct spwd *(*__xdm_getspnam)(GETSPNAM_ARGS) = NULL;
  113. # ifndef QNX4
  114. void (*__xdm_endspent)(void) = NULL;
  115. # endif /* QNX4 doesn't use endspent */
  116. # endif
  117. struct passwd *(*__xdm_getpwnam)(GETPWNAM_ARGS) = NULL;
  118. # if defined(linux) || defined(__GLIBC__)
  119. void (*__xdm_endpwent)(void) = NULL;
  120. # endif
  121. char *(*__xdm_crypt)(CRYPT_ARGS) = NULL;
  122. # ifdef USE_PAM
  123. pam_handle_t **(*__xdm_thepamhp)(void) = NULL;
  124. # endif
  125. #ifdef SECURE_RPC
  126. # include <rpc/rpc.h>
  127. # include <rpc/key_prot.h>
  128. #endif
  129. #ifdef K5AUTH
  130. # include <krb5/krb5.h>
  131. #endif
  132. static int done, code;
  133. #ifndef USE_PAM
  134. static char name[NAME_LEN], password[PASSWORD_LEN];
  135. #endif
  136. static Widget toplevel;
  137. static Widget login;
  138. static XtAppContext context;
  139. static XtIntervalId pingTimeout;
  140. #ifdef USE_PAM
  141. static int pamconv(int num_msg,
  142. # ifndef sun
  143. const
  144. # endif
  145. struct pam_message **msg,
  146. struct pam_response **response, void *appdata_ptr);
  147. # define PAM_ERROR_PRINT(pamfunc, pamh) \
  148. LogError("%s failure: %s\n", pamfunc, pam_strerror(pamh, pam_error))
  149. struct myconv_data {
  150. struct display *d;
  151. struct greet_info *greet;
  152. char *username_display;
  153. };
  154. #endif
  155. /*ARGSUSED*/
  156. static void
  157. GreetPingServer (
  158. XtPointer closure,
  159. XtIntervalId *intervalId)
  160. {
  161. struct display *d;
  162. d = (struct display *) closure;
  163. if (!PingServer (d, XtDisplay (toplevel)))
  164. SessionPingFailed (d);
  165. pingTimeout = XtAppAddTimeOut (context, d->pingInterval * 60 * 1000,
  166. GreetPingServer, (closure));
  167. }
  168. /*ARGSUSED*/
  169. static void
  170. GreetDone (
  171. Widget w,
  172. LoginData *data,
  173. int status)
  174. {
  175. Debug ("GreetDone: %s, (password is %d long)\n",
  176. data->name, strlen (data->passwd));
  177. switch (status) {
  178. case NOTIFY_OK:
  179. #ifndef USE_PAM
  180. strncpy (name, data->name, sizeof(name));
  181. name[sizeof(name)-1] = '\0';
  182. strncpy (password, data->passwd, sizeof(password));
  183. password[sizeof(password)-1] = '\0';
  184. #endif
  185. code = 0;
  186. done = 1;
  187. break;
  188. case NOTIFY_ABORT:
  189. Debug ("RESERVER_DISPLAY\n");
  190. code = RESERVER_DISPLAY;
  191. done = 1;
  192. break;
  193. case NOTIFY_RESTART:
  194. Debug ("REMANAGE_DISPLAY\n");
  195. code = REMANAGE_DISPLAY;
  196. done = 1;
  197. break;
  198. case NOTIFY_ABORT_DISPLAY:
  199. Debug ("UNMANAGE_DISPLAY\n");
  200. code = UNMANAGE_DISPLAY;
  201. done = 1;
  202. break;
  203. }
  204. #ifndef USE_PAM
  205. if (done) {
  206. bzero (data->name, NAME_LEN);
  207. bzero (data->passwd, PASSWORD_LEN);
  208. }
  209. #endif
  210. }
  211. static Display *
  212. InitGreet (struct display *d)
  213. {
  214. Arg arglist[10];
  215. int i;
  216. static int argc;
  217. Screen *scrn;
  218. static char *argv[] = { "xlogin", NULL };
  219. Display *dpy;
  220. #ifdef USE_XINERAMA
  221. XineramaScreenInfo *screens;
  222. int s_num;
  223. #endif
  224. Debug ("greet %s\n", d->name);
  225. argc = 1;
  226. XtToolkitInitialize ();
  227. context = XtCreateApplicationContext();
  228. dpy = XtOpenDisplay (context, d->name, "xlogin", "Xlogin", NULL, 0,
  229. &argc, argv);
  230. if (!dpy)
  231. return NULL;
  232. #ifdef XKB
  233. {
  234. int opcode, evbase, errbase, majret, minret;
  235. unsigned int value = XkbPCF_GrabsUseXKBStateMask;
  236. if (XkbQueryExtension (dpy, &opcode, &evbase, &errbase, &majret, &minret)) {
  237. if (!XkbSetPerClientControls (dpy, XkbPCF_GrabsUseXKBStateMask, &value))
  238. LogError ("%s\n", "SetPerClientControls failed");
  239. }
  240. }
  241. #endif
  242. RegisterCloseOnFork (ConnectionNumber (dpy));
  243. SecureDisplay (d, dpy);
  244. i = 0;
  245. scrn = XDefaultScreenOfDisplay(dpy);
  246. XtSetArg(arglist[i], XtNscreen, scrn); i++;
  247. XtSetArg(arglist[i], XtNargc, argc); i++;
  248. XtSetArg(arglist[i], XtNargv, argv); i++;
  249. toplevel = XtAppCreateShell ((String) NULL, "Xlogin",
  250. applicationShellWidgetClass, dpy, arglist, i);
  251. i = 0;
  252. XtSetArg (arglist[i], XtNnotifyDone, (XtPointer)GreetDone); i++;
  253. if (!d->authorize || d->authorizations || !d->authComplain)
  254. {
  255. XtSetArg (arglist[i], XtNsecureSession, True); i++;
  256. }
  257. login = XtCreateManagedWidget ("login", loginWidgetClass, toplevel,
  258. arglist, i);
  259. XtRealizeWidget (toplevel);
  260. #ifdef USE_XINERAMA
  261. if (
  262. XineramaIsActive(dpy) &&
  263. (screens = XineramaQueryScreens(dpy, &s_num)) != NULL
  264. )
  265. {
  266. XWarpPointer(dpy, None, XRootWindowOfScreen (scrn),
  267. 0, 0, 0, 0,
  268. screens[0].x_org + screens[0].width / 2,
  269. screens[0].y_org + screens[0].height / 2);
  270. XFree(screens);
  271. }
  272. else
  273. #endif
  274. XWarpPointer(dpy, None, XRootWindowOfScreen (scrn),
  275. 0, 0, 0, 0,
  276. XWidthOfScreen(scrn) / 2,
  277. XHeightOfScreen(scrn) / 2);
  278. if (d->pingInterval)
  279. {
  280. pingTimeout = XtAppAddTimeOut (context, d->pingInterval * 60 * 1000,
  281. GreetPingServer, (XtPointer) d);
  282. }
  283. return dpy;
  284. }
  285. static void
  286. CloseGreet (struct display *d)
  287. {
  288. Boolean allow;
  289. Arg arglist[1];
  290. Display *dpy = XtDisplay(toplevel);
  291. if (pingTimeout)
  292. {
  293. XtRemoveTimeOut (pingTimeout);
  294. pingTimeout = 0;
  295. }
  296. UnsecureDisplay (d, dpy);
  297. XtSetArg (arglist[0], XtNallowAccess, (char *) &allow);
  298. XtGetValues (login, arglist, 1);
  299. if (allow)
  300. {
  301. Debug ("Disabling access control\n");
  302. XSetAccessControl (dpy, DisableAccess);
  303. }
  304. XtDestroyWidget (toplevel);
  305. toplevel = NULL;
  306. login = NULL; /* child of toplevel, which we just destroyed */
  307. ClearCloseOnFork (XConnectionNumber (dpy));
  308. XCloseDisplay (dpy);
  309. Debug ("Greet connection closed\n");
  310. }
  311. #define WHITESPACE 0
  312. #define ARGUMENT 1
  313. static int
  314. Greet (struct display *d, struct greet_info *greet)
  315. {
  316. XEvent event;
  317. Arg arglist[1];
  318. XtSetArg (arglist[0], XtNallowAccess, False);
  319. XtSetValues (login, arglist, 1);
  320. Debug ("dispatching %s\n", d->name);
  321. done = 0;
  322. while (!done) {
  323. XtAppNextEvent (context, &event);
  324. switch (event.type) {
  325. case MappingNotify:
  326. XRefreshKeyboardMapping(&event.xmapping);
  327. break;
  328. default:
  329. XtDispatchEvent (&event);
  330. break;
  331. }
  332. }
  333. XFlush (XtDisplay (toplevel));
  334. Debug ("Done dispatch %s\n", d->name);
  335. if (code == 0)
  336. {
  337. #ifndef USE_PAM
  338. char *ptr;
  339. unsigned int c,state = WHITESPACE;
  340. /*
  341. * Process the name string to get rid of white spaces.
  342. */
  343. for (ptr = name; state == WHITESPACE; ptr++)
  344. {
  345. c = (unsigned int)(*ptr);
  346. if (c == ' ')
  347. continue;
  348. state = ARGUMENT;
  349. break;
  350. }
  351. greet->name = ptr;
  352. greet->password = password;
  353. #endif /* USE_PAM */
  354. XtSetArg (arglist[0], XtNsessionArgument, (char *) &(greet->string));
  355. XtGetValues (login, arglist, 1);
  356. Debug ("sessionArgument: %s\n", greet->string ? greet->string : "<NULL>");
  357. }
  358. return code;
  359. }
  360. static void
  361. FailedLogin (struct display *d, const char *username)
  362. {
  363. #ifdef USE_SYSLOG
  364. if (username == NULL)
  365. username = "username unavailable";
  366. syslog(LOG_AUTHPRIV|LOG_NOTICE,
  367. "LOGIN FAILURE ON %s, %s",
  368. d->name, username);
  369. #endif
  370. DrawFail (login);
  371. }
  372. #ifdef USE_PAM
  373. #ifdef HAVE_LIBAUDIT
  374. static void
  375. log_to_audit_system(const pam_handle_t *pamhp, int success)
  376. {
  377. struct passwd *pw = NULL;
  378. char *hostname = NULL, *tty = NULL, *login=NULL;
  379. int audit_fd;
  380. audit_fd = audit_open();
  381. pam_get_item(pamhp, PAM_RHOST, &hostname);
  382. pam_get_item(pamhp, PAM_TTY, &tty);
  383. pam_get_item(pamhp, PAM_USER, &login);
  384. if (login)
  385. pw = getpwnam(login);
  386. audit_log_acct_message(audit_fd, AUDIT_USER_LOGIN,
  387. NULL, "login", login ? login : "(unknown)",
  388. pw ? pw->pw_uid : -1, hostname, NULL, tty, success);
  389. close(audit_fd);
  390. }
  391. #endif
  392. #endif
  393. _X_EXPORT
  394. greet_user_rtn GreetUser(
  395. struct display *d,
  396. Display ** dpy,
  397. struct verify_info *verify,
  398. struct greet_info *greet,
  399. struct dlfuncs *dlfuncs)
  400. {
  401. int i;
  402. Arg arglist[2];
  403. /*
  404. * These must be set before they are used.
  405. */
  406. __xdm_PingServer = dlfuncs->_PingServer;
  407. __xdm_SessionPingFailed = dlfuncs->_SessionPingFailed;
  408. __xdm_Debug = dlfuncs->_Debug;
  409. __xdm_RegisterCloseOnFork = dlfuncs->_RegisterCloseOnFork;
  410. __xdm_SecureDisplay = dlfuncs->_SecureDisplay;
  411. __xdm_UnsecureDisplay = dlfuncs->_UnsecureDisplay;
  412. __xdm_ClearCloseOnFork = dlfuncs->_ClearCloseOnFork;
  413. __xdm_SetupDisplay = dlfuncs->_SetupDisplay;
  414. __xdm_LogError = dlfuncs->_LogError;
  415. __xdm_SessionExit = dlfuncs->_SessionExit;
  416. __xdm_DeleteXloginResources = dlfuncs->_DeleteXloginResources;
  417. __xdm_source = dlfuncs->_source;
  418. __xdm_defaultEnv = dlfuncs->_defaultEnv;
  419. __xdm_setEnv = dlfuncs->_setEnv;
  420. __xdm_putEnv = dlfuncs->_putEnv;
  421. __xdm_parseArgs = dlfuncs->_parseArgs;
  422. __xdm_printEnv = dlfuncs->_printEnv;
  423. __xdm_systemEnv = dlfuncs->_systemEnv;
  424. __xdm_LogOutOfMem = dlfuncs->_LogOutOfMem;
  425. __xdm_setgrent = dlfuncs->_setgrent;
  426. __xdm_getgrent = dlfuncs->_getgrent;
  427. __xdm_endgrent = dlfuncs->_endgrent;
  428. # ifdef HAVE_GETSPNAM
  429. __xdm_getspnam = dlfuncs->_getspnam;
  430. # ifndef QNX4
  431. __xdm_endspent = dlfuncs->_endspent;
  432. # endif /* QNX4 doesn't use endspent */
  433. # endif
  434. __xdm_getpwnam = dlfuncs->_getpwnam;
  435. # if defined(linux) || defined(__GLIBC__)
  436. __xdm_endpwent = dlfuncs->_endpwent;
  437. # endif
  438. __xdm_crypt = dlfuncs->_crypt;
  439. # ifdef USE_PAM
  440. __xdm_thepamhp = dlfuncs->_thepamhp;
  441. # endif
  442. *dpy = InitGreet (d);
  443. /*
  444. * Run the setup script - note this usually will not work when
  445. * the server is grabbed, so we don't even bother trying.
  446. */
  447. if (!d->grabServer)
  448. SetupDisplay (d);
  449. if (!*dpy) {
  450. LogError ("Cannot reopen display %s for greet window\n", d->name);
  451. exit (RESERVER_DISPLAY);
  452. }
  453. XtSetArg (arglist[0], XtNallowNullPasswd,
  454. (char *) &(greet->allow_null_passwd));
  455. XtSetArg (arglist[1], XtNallowRootLogin,
  456. (char *) &(greet->allow_root_login));
  457. XtGetValues (login, arglist, 2);
  458. for (;;) {
  459. #ifdef USE_PAM
  460. /* Run PAM conversation */
  461. pam_handle_t **pamhp = thepamhp();
  462. int pam_error;
  463. unsigned int pam_flags = 0;
  464. struct myconv_data pcd = { d, greet, NULL };
  465. struct pam_conv pc = { pamconv, &pcd };
  466. const char * pam_fname;
  467. const char * login_prompt;
  468. SetPrompt(login, LOGIN_PROMPT_USERNAME, NULL, LOGIN_PROMPT_NOT_SHOWN, False);
  469. login_prompt = GetPrompt(login, LOGIN_PROMPT_USERNAME);
  470. SetPrompt(login, LOGIN_PROMPT_PASSWORD, NULL, LOGIN_PROMPT_NOT_SHOWN, False);
  471. # define RUN_AND_CHECK_PAM_ERROR(function, args) \
  472. do { \
  473. pam_error = function args; \
  474. if (pam_error != PAM_SUCCESS) { \
  475. PAM_ERROR_PRINT(#function, *pamhp); \
  476. goto pam_done; \
  477. } \
  478. } while (0)
  479. RUN_AND_CHECK_PAM_ERROR(pam_start,
  480. ("xdm", NULL, &pc, pamhp));
  481. /* Set default login prompt to xdm's default from Xresources */
  482. if (login_prompt != NULL) {
  483. RUN_AND_CHECK_PAM_ERROR(pam_set_item,
  484. (*pamhp, PAM_USER_PROMPT, login_prompt));
  485. }
  486. if (d->name[0] != ':') { /* Displaying to remote host */
  487. char *hostname = strdup(d->name);
  488. if (hostname == NULL) {
  489. LogOutOfMem("GreetUser");
  490. } else {
  491. char *colon = strrchr(hostname, ':');
  492. if (colon != NULL)
  493. *colon = '\0';
  494. RUN_AND_CHECK_PAM_ERROR(pam_set_item,
  495. (*pamhp, PAM_RHOST, hostname));
  496. free(hostname);
  497. }
  498. } else { /* Displaying on local host */
  499. const char *ttyname = NULL;
  500. #ifdef __sun
  501. /* Solaris PAM & auditing insist this is a device file that can
  502. be found under /dev, so we can't use the display name */
  503. char vtpath[16];
  504. if ((d->windowPath) && !(strchr(d->windowPath, ':'))) {
  505. /* if path is simply a VT, with no intermediaries, use it */
  506. snprintf(vtpath, sizeof(vtpath), "/dev/vt/%s", d->windowPath);
  507. ttyname = vtpath;
  508. }
  509. #else
  510. /* On all other OS'es we just pass the display name for PAM_TTY */
  511. ttyname = d->name;
  512. #endif
  513. RUN_AND_CHECK_PAM_ERROR(pam_set_item, (*pamhp, PAM_TTY, ttyname));
  514. }
  515. if (!greet->allow_null_passwd) {
  516. pam_flags |= PAM_DISALLOW_NULL_AUTHTOK;
  517. }
  518. RUN_AND_CHECK_PAM_ERROR(pam_authenticate,
  519. (*pamhp, pam_flags));
  520. /* handle expired passwords */
  521. pam_error = pam_acct_mgmt(*pamhp, pam_flags);
  522. pam_fname = "pam_acct_mgmt";
  523. if (pam_error == PAM_NEW_AUTHTOK_REQD) {
  524. ShowChangePasswdMessage(login);
  525. do {
  526. pam_error = pam_chauthtok(*pamhp, PAM_CHANGE_EXPIRED_AUTHTOK);
  527. } while ((pam_error == PAM_AUTHTOK_ERR) ||
  528. (pam_error == PAM_TRY_AGAIN));
  529. pam_fname = "pam_chauthtok";
  530. }
  531. if (pam_error != PAM_SUCCESS) {
  532. PAM_ERROR_PRINT(pam_fname, *pamhp);
  533. goto pam_done;
  534. }
  535. RUN_AND_CHECK_PAM_ERROR(pam_setcred,
  536. (*pamhp, 0));
  537. {
  538. char *username = NULL;
  539. RUN_AND_CHECK_PAM_ERROR(pam_get_item,
  540. (*pamhp, PAM_USER, (void *) &username));
  541. if (username != NULL) {
  542. Debug("PAM_USER: %s\n", username);
  543. greet->name = username;
  544. greet->password = NULL;
  545. }
  546. }
  547. pam_done:
  548. if (code != 0)
  549. {
  550. CloseGreet (d);
  551. SessionExit (d, code, FALSE);
  552. }
  553. if ((pam_error == PAM_SUCCESS) && (Verify (d, greet, verify))) {
  554. SetPrompt (login, 1, "Login Successful", LOGIN_TEXT_INFO, False);
  555. SetValue (login, 1, NULL);
  556. log_to_audit_system(*pamhp, 1);
  557. break;
  558. } else {
  559. /* Try to fill in username for failed login error log */
  560. char *username = greet->name;
  561. if (username == NULL) {
  562. RUN_AND_CHECK_PAM_ERROR(pam_get_item,
  563. (*pamhp, PAM_USER,
  564. (void *) &username));
  565. }
  566. FailedLogin (d, username);
  567. log_to_audit_system(*pamhp, 0);
  568. RUN_AND_CHECK_PAM_ERROR(pam_end,
  569. (*pamhp, pam_error));
  570. }
  571. #else /* not PAM */
  572. /*
  573. * Greet user, requesting name/password
  574. */
  575. code = Greet (d, greet);
  576. if (code != 0)
  577. {
  578. CloseGreet (d);
  579. SessionExit (d, code, FALSE);
  580. }
  581. /*
  582. * Verify user
  583. */
  584. if (Verify (d, greet, verify))
  585. break;
  586. else
  587. {
  588. FailedLogin (d, greet->name);
  589. bzero (greet->name, strlen(greet->name));
  590. bzero (greet->password, strlen(greet->password));
  591. }
  592. #endif
  593. }
  594. DeleteXloginResources (d, *dpy);
  595. CloseGreet (d);
  596. Debug ("Greet loop finished\n");
  597. /*
  598. * Run system-wide initialization file
  599. */
  600. if (source (verify->systemEnviron, d->startup) != 0)
  601. {
  602. Debug ("Startup program %s exited with non-zero status\n",
  603. d->startup);
  604. SessionExit (d, OBEYSESS_DISPLAY, FALSE);
  605. }
  606. /*
  607. * for user-based authorization schemes,
  608. * add the user to the server's allowed "hosts" list.
  609. */
  610. for (i = 0; i < d->authNum; i++)
  611. {
  612. #ifdef SECURE_RPC
  613. if (d->authorizations[i]->name_length == 9 &&
  614. memcmp(d->authorizations[i]->name, "SUN-DES-1", 9) == 0)
  615. {
  616. XHostAddress addr;
  617. char netname[MAXNETNAMELEN+1];
  618. char domainname[MAXNETNAMELEN+1];
  619. getdomainname(domainname, sizeof domainname);
  620. user2netname (netname, verify->uid, domainname);
  621. addr.family = FamilyNetname;
  622. addr.length = strlen (netname);
  623. addr.address = netname;
  624. XAddHost (*dpy, &addr);
  625. }
  626. #endif
  627. #ifdef K5AUTH
  628. if (d->authorizations[i]->name_length == 14 &&
  629. memcmp(d->authorizations[i]->name, "MIT-KERBEROS-5", 14) == 0)
  630. {
  631. /* Update server's auth file with user-specific info.
  632. * Don't need to AddHost because X server will do that
  633. * automatically when it reads the cache we are about
  634. * to point it at.
  635. */
  636. extern Xauth *Krb5GetAuthFor();
  637. XauDisposeAuth (d->authorizations[i]);
  638. d->authorizations[i] =
  639. Krb5GetAuthFor(14, "MIT-KERBEROS-5", d->name);
  640. SaveServerAuthorizations (d, d->authorizations, d->authNum);
  641. }
  642. #endif
  643. }
  644. return Greet_Success;
  645. }
  646. #ifdef USE_PAM
  647. static int pamconv(int num_msg,
  648. # ifndef sun
  649. const
  650. # endif
  651. struct pam_message **msg,
  652. struct pam_response **response, void *appdata_ptr)
  653. {
  654. int i;
  655. int greetCode;
  656. int status = PAM_SUCCESS;
  657. const char *pam_msg_styles[5]
  658. = { "<invalid pam msg style>",
  659. "PAM_PROMPT_ECHO_OFF", "PAM_PROMPT_ECHO_ON",
  660. "PAM_ERROR_MSG", "PAM_TEXT_INFO" } ;
  661. struct pam_message *m;
  662. struct pam_response *r;
  663. struct myconv_data *d = (struct myconv_data *) appdata_ptr;
  664. pam_handle_t **pamhp = thepamhp();
  665. *response = calloc(num_msg, sizeof (struct pam_response));
  666. if (*response == NULL)
  667. return (PAM_BUF_ERR);
  668. m = (struct pam_message *)*msg;
  669. r = *response;
  670. if (login == NULL) {
  671. status = PAM_CONV_ERR;
  672. goto pam_error;
  673. }
  674. for (i = 0; i < num_msg; i++ , m++ , r++) {
  675. char *username;
  676. int promptId = 0;
  677. loginPromptState pStyle = LOGIN_PROMPT_ECHO_OFF;
  678. if ((pam_get_item(*pamhp, PAM_USER, (void *) &username) == PAM_SUCCESS)
  679. && (username != NULL) && (*username != '\0')) {
  680. SetPrompt(login, LOGIN_PROMPT_USERNAME,
  681. NULL, LOGIN_TEXT_INFO, False);
  682. SetValue(login, LOGIN_PROMPT_USERNAME, username);
  683. promptId = 1;
  684. }
  685. Debug("pam_msg: %s (%d): '%s'\n",
  686. ((m->msg_style > 0) && (m->msg_style <= 4)) ?
  687. pam_msg_styles[m->msg_style] : pam_msg_styles[0],
  688. m->msg_style, m->msg);
  689. switch (m->msg_style) {
  690. case PAM_ERROR_MSG:
  691. ErrorMessage(login, m->msg, True);
  692. break;
  693. case PAM_TEXT_INFO:
  694. SetPrompt (login, promptId, m->msg, LOGIN_TEXT_INFO, True);
  695. SetValue (login, promptId, NULL);
  696. break;
  697. case PAM_PROMPT_ECHO_ON:
  698. pStyle = LOGIN_PROMPT_ECHO_ON;
  699. /* FALLTHROUGH */
  700. case PAM_PROMPT_ECHO_OFF:
  701. SetPrompt (login, promptId, m->msg, pStyle, False);
  702. SetValue (login, promptId, NULL);
  703. greetCode = Greet (d->d, d->greet);
  704. if (greetCode != 0) {
  705. status = PAM_CONV_ERR;
  706. goto pam_error;
  707. } else {
  708. r->resp = strdup(GetValue(login, promptId));
  709. SetValue(login, promptId, NULL);
  710. if (r->resp == NULL) {
  711. status = PAM_BUF_ERR;
  712. goto pam_error;
  713. }
  714. /* Debug("pam_resp: '%s'\n", r->resp); */
  715. }
  716. break;
  717. default:
  718. LogError("Unknown PAM msg_style: %d\n", m->msg_style);
  719. }
  720. }
  721. pam_error:
  722. if (status != PAM_SUCCESS) {
  723. /* free responses */
  724. r = *response;
  725. for (i = 0; i < num_msg; i++, r++) {
  726. if (r->resp) {
  727. bzero(r->resp, strlen(r->resp));
  728. free(r->resp);
  729. }
  730. }
  731. free(*response);
  732. *response = NULL;
  733. }
  734. return status;
  735. }
  736. #endif