int main(int argc, char **argv) { struct aXInfo xinfo; struct aOpts opts; #if HAVE_XF86MISC int xf86misc_major = -1; int xf86misc_minor = -1; #endif int arg = 0; const char* cursor_args = NULL; const char* background_args = NULL; opts.auth = NULL; opts.cursor = alock_cursors[0]; opts.background = alock_backgrounds[0]; /* parse options */ if (argc != 1) { for(arg = 1; arg <= argc; arg++) { if (!strcmp(argv[arg - 1], "-bg")) { if (arg < argc) { char* char_tmp; struct aBackground* bg_tmp = NULL; struct aBackground** i; if (strcmp(argv[arg], "list") == 0) { for(i = alock_backgrounds; *i; ++i) { printf("%s\n", (*i)->name); } exit(EXIT_SUCCESS); } for(i = alock_backgrounds; *i; ++i) { char_tmp = strstr(argv[arg], (*i)->name); if(char_tmp && char_tmp == argv[arg]) { background_args = char_tmp; bg_tmp = *i; opts.background = bg_tmp; ++arg; break; } } if (bg_tmp == NULL) { printf("%s", "alock: error, couldnt find the bg-module you specified.\n"); exit(EXIT_FAILURE); } } else { printf("%s", "alock, error, missing argument\n"); displayUsage(); exit(EXIT_FAILURE); } } else if (!strcmp(argv[arg - 1], "-auth")) { if (arg < argc) { char* char_tmp; struct aAuth* auth_tmp = NULL; struct aAuth** i; if (!strcmp(argv[arg], "list")) { for(i = alock_authmodules; *i; ++i) { printf("%s\n", (*i)->name); } exit(EXIT_SUCCESS); } for(i = alock_authmodules; *i; ++i) { char_tmp = strstr(argv[arg], (*i)->name); if(char_tmp && char_tmp == argv[arg]) { auth_tmp = (*i); if (auth_tmp->init(argv[arg]) == 0) { printf("alock: error, failed init of [%s].\n", auth_tmp->name); exit(EXIT_FAILURE); } opts.auth = auth_tmp; ++arg; break; } } if (auth_tmp == NULL) { printf("%s", "alock: error, couldnt find the auth-module you specified.\n"); exit(EXIT_FAILURE); } } else { printf("%s", "alock, error, missing argument\n"); displayUsage(); exit(EXIT_FAILURE); } } else if (strcmp(argv[arg - 1], "-cursor") == 0) { if (arg < argc) { char* char_tmp; struct aCursor* cursor_tmp = NULL; struct aCursor** i; if (strcmp(argv[arg], "list") == 0) { for(i = alock_cursors; *i; ++i) { printf("%s\n", (*i)->name); } exit(EXIT_SUCCESS); } for(i = alock_cursors; *i; ++i) { char_tmp = strstr(argv[arg], (*i)->name); if(char_tmp && char_tmp == argv[arg]) { cursor_args = char_tmp; cursor_tmp = *i; opts.cursor= cursor_tmp; ++arg; break; } } if (!cursor_tmp) { printf("%s", "alock: error, couldnt find the cursor-module you specified.\n"); exit(EXIT_FAILURE); } } else { printf("%s", "alock, error, missing argument\n"); displayUsage(); exit(EXIT_FAILURE); } } else if (strcmp(argv[arg - 1], "-h") == 0) { displayUsage(); exit(EXIT_SUCCESS); } else if (strcmp(argv[arg - 1], "-v") == 0) { printf("alock-%s by m.gumz 2005 - 2006\n", VERSION); exit(EXIT_SUCCESS); } } } initStartTime(); initXInfo(&xinfo); if (detectOtherInstance(&xinfo)) { printf("%s", "alock: error, another instance seems to be running\n"); exit(EXIT_FAILURE); } if (!opts.auth) { printf("%s", "alock: error, no auth-method specified.\n"); displayUsage(); exit(EXIT_FAILURE); } if (opts.background->init(background_args, &xinfo) == 0) { printf("alock: error, couldnt init [%s] with [%s].\n", opts.background->name, background_args); exit(EXIT_FAILURE); } if (opts.cursor->init(cursor_args, &xinfo) == 0) { printf("alock: error, couldnt init [%s] with [%s].\n", opts.cursor->name, cursor_args); exit(EXIT_FAILURE); } { int scr; for (scr = 0; scr < xinfo.nr_screens; scr++) { XSelectInput(xinfo.display, xinfo.window[scr], KeyPressMask|KeyReleaseMask); XMapWindow(xinfo.display, xinfo.window[scr]); XRaiseWindow(xinfo.display, xinfo.window[scr]); } } /* try to grab 2 times, another process (windowmanager) may have grabbed * the keyboard already */ if ((XGrabKeyboard(xinfo.display, xinfo.window[0], True, GrabModeAsync, GrabModeAsync, CurrentTime)) != GrabSuccess) { sleep(1); if ((XGrabKeyboard(xinfo.display, xinfo.window[0], True, GrabModeAsync, GrabModeAsync, CurrentTime)) != GrabSuccess) { printf("%s", "alock: couldnt grab the keyboard.\n"); exit(EXIT_FAILURE); } } #if HAVE_XF86MISC { if (XF86MiscQueryVersion(xinfo.display, &xf86misc_major, &xf86misc_minor) == True) { if (xf86misc_major >= 0 && xf86misc_minor >= 5 && XF86MiscSetGrabKeysState(xinfo.display, False) == MiscExtGrabStateLocked) { printf("%s", "alock: cant disable xserver hotkeys to remove grabs.\n"); exit(EXIT_FAILURE); } printf("%s", "disabled AllowDeactivateGrabs and AllowClosedownGrabs\n."); } } #endif /* TODO: think about it: do we really need NR_SCREEN cursors ? we grab the * pointer on :*.0 anyway ... */ if (XGrabPointer(xinfo.display, xinfo.window[0], False, None, GrabModeAsync, GrabModeAsync, None, xinfo.cursor[0], CurrentTime) != GrabSuccess) { XUngrabKeyboard(xinfo.display, CurrentTime); printf("%s", "alock: couldnt grab the pointer.\n"); exit(EXIT_FAILURE); } registerInstance(&xinfo); eventLoop(&opts, &xinfo); unregisterInstance(&xinfo); opts.auth->deinit(); opts.cursor->deinit(&xinfo); opts.background->deinit(&xinfo); #if HAVE_XF86MISC if (xf86misc_major >= 0 && xf86misc_minor >= 5) { XF86MiscSetGrabKeysState(xinfo.display, True); XFlush(xinfo.display); } #endif XCloseDisplay(xinfo.display); return EXIT_SUCCESS; }
int main(int argc, char **argv) { struct aXInfo xinfo; struct aOpts opts = { alock_authmodules[0], alock_inputs[0], alock_cursors[0], alock_backgrounds[0], }; #if HAVE_X11_EXTENSIONS_XF86MISC_H int xf86misc_major = -1; int xf86misc_minor = -1; #endif int arg; const char *optarg; const char *auth_args = NULL; const char *input_args = NULL; const char *cursor_args = NULL; const char *background_args = NULL; /* parse options */ if (argc > 1) { for (arg = 1; arg < argc; arg++) { if (!strcmp(argv[arg], "-bg")) { optarg = argv[++arg]; if (optarg != NULL) { struct aBackground **i; if (strcmp(optarg, "list") == 0) { printf("list of available background modules:\n"); for (i = alock_backgrounds; *i; ++i) { printf("%s\n", (*i)->name); } exit(EXIT_SUCCESS); } for (i = alock_backgrounds; *i; ++i) { if (strstr(optarg, (*i)->name) == optarg) { background_args = optarg; opts.background = *i; break; } } if (*i == NULL) { fprintf(stderr, "alock: background module not found\n"); exit(EXIT_FAILURE); } } else { fprintf(stderr, "alock: option requires an argument -- '%s'\n", "bg"); exit(EXIT_FAILURE); } } else if (!strcmp(argv[arg], "-auth")) { optarg = argv[++arg]; if (optarg != NULL) { struct aAuth **i; if (strcmp(optarg, "list") == 0) { printf("list of available authentication modules:\n"); for (i = alock_authmodules; *i; ++i) { printf("%s\n", (*i)->name); } exit(EXIT_SUCCESS); } for (i = alock_authmodules; *i; ++i) { if (strstr(optarg, (*i)->name) == optarg) { auth_args = optarg; opts.auth = *i; break; } } if (*i == NULL) { fprintf(stderr, "alock: authentication module not found\n"); exit(EXIT_FAILURE); } } else { fprintf(stderr, "alock: option requires an argument -- '%s'\n", "auth"); exit(EXIT_FAILURE); } } else if (!strcmp(argv[arg], "-cursor")) { optarg = argv[++arg]; if (optarg != NULL) { struct aCursor **i; if (strcmp(argv[arg], "list") == 0) { printf("list of available cursor modules:\n"); for (i = alock_cursors; *i; ++i) { printf("%s\n", (*i)->name); } exit(EXIT_SUCCESS); } for (i = alock_cursors; *i; ++i) { if (strstr(optarg, (*i)->name) == optarg) { cursor_args = optarg; opts.cursor = *i; break; } } if (*i == NULL) { fprintf(stderr, "alock: cursor module not found\n"); exit(EXIT_FAILURE); } } else { fprintf(stderr, "alock: option requires an argument -- '%s'\n", "cursor"); exit(EXIT_FAILURE); } } else if (!strcmp(argv[arg], "-input")) { optarg = argv[++arg]; if (optarg != NULL) { struct aInput **i; if (strcmp(argv[arg], "list") == 0) { printf("list of available input modules:\n"); for (i = alock_inputs; *i; ++i) { printf("%s\n", (*i)->name); } exit(EXIT_SUCCESS); } for (i = alock_inputs; *i; ++i) { if (strstr(optarg, (*i)->name) == optarg) { input_args = optarg; opts.input = *i; break; } } if (*i == NULL) { fprintf(stderr, "alock: input module not found\n"); exit(EXIT_FAILURE); } } else { fprintf(stderr, "alock: option requires an argument -- '%s'\n", "input"); exit(EXIT_FAILURE); } } else if (strcmp(argv[arg], "-h") == 0) { printf("alock [-h] [-auth type:options] [-bg type:options]" " [-cursor type:options] [-input type:options]\n"); exit(EXIT_SUCCESS); } else { fprintf(stderr, "alock: invalid option '%s'\n", argv[arg]); exit(EXIT_FAILURE); } } } /* required for correct input handling */ setlocale(LC_ALL, ""); initXInfo(&xinfo); if (detectOtherInstance(&xinfo)) { fprintf(stderr, "alock: another instance seems to be running\n"); exit(EXIT_FAILURE); } if (opts.auth->init(auth_args) == 0) { fprintf(stderr, "alock: failed init of [%s] with [%s]\n", opts.auth->name, auth_args); exit(EXIT_FAILURE); } /* We can be installed setuid root to support shadow passwords, and we don't need root privileges any longer. --marekm */ setuid(getuid()); if (opts.input->init(input_args, &xinfo) == 0) { fprintf(stderr, "alock: failed init of [%s] with [%s]\n", opts.input->name, input_args); exit(EXIT_FAILURE); } if (opts.background->init(background_args, &xinfo) == 0) { fprintf(stderr, "alock: failed init of [%s] with [%s]\n", opts.background->name, background_args); exit(EXIT_FAILURE); } if (opts.cursor->init(cursor_args, &xinfo) == 0) { fprintf(stderr, "alock: failed init of [%s] with [%s]\n", opts.cursor->name, cursor_args); exit(EXIT_FAILURE); } { int scr; for (scr = 0; scr < xinfo.screens; scr++) { XSelectInput(xinfo.display, xinfo.window[scr], KeyPressMask|KeyReleaseMask); XMapWindow(xinfo.display, xinfo.window[scr]); XRaiseWindow(xinfo.display, xinfo.window[scr]); } } /* try to grab 2 times, another process (windowmanager) may have grabbed * the keyboard already */ if ((XGrabKeyboard(xinfo.display, xinfo.window[0], True, GrabModeAsync, GrabModeAsync, CurrentTime)) != GrabSuccess) { sleep(1); if ((XGrabKeyboard(xinfo.display, xinfo.window[0], True, GrabModeAsync, GrabModeAsync, CurrentTime)) != GrabSuccess) { printf("alock: couldnt grab the keyboard\n"); exit(EXIT_FAILURE); } } #if HAVE_X11_EXTENSIONS_XF86MISC_H { if (XF86MiscQueryVersion(xinfo.display, &xf86misc_major, &xf86misc_minor) == True) { if (xf86misc_major >= 0 && xf86misc_minor >= 5 && XF86MiscSetGrabKeysState(xinfo.display, False) == MiscExtGrabStateLocked) { fprintf(stderr, "alock: cant disable xserver hotkeys to remove grabs\n"); exit(EXIT_FAILURE); } printf("disabled AllowDeactivateGrabs and AllowClosedownGrabs\n"); } } #endif /* TODO: think about it: do we really need NR_SCREEN cursors ? we grab the * pointer on :*.0 anyway ... */ if (XGrabPointer(xinfo.display, xinfo.window[0], False, None, GrabModeAsync, GrabModeAsync, None, xinfo.cursor[0], CurrentTime) != GrabSuccess) { XUngrabKeyboard(xinfo.display, CurrentTime); fprintf(stderr, "alock: couldnt grab the pointer\n"); exit(EXIT_FAILURE); } registerInstance(&xinfo); eventLoop(&opts, &xinfo); unregisterInstance(&xinfo); opts.auth->deinit(); opts.input->deinit(&xinfo); opts.cursor->deinit(&xinfo); opts.background->deinit(&xinfo); #if HAVE_X11_EXTENSIONS_XF86MISC_H if (xf86misc_major >= 0 && xf86misc_minor >= 5) { XF86MiscSetGrabKeysState(xinfo.display, True); XFlush(xinfo.display); } #endif XCloseDisplay(xinfo.display); return EXIT_SUCCESS; }
int main(int argc, char **argv) { struct aXInfo xinfo; struct aOpts opts; int arg = 0; const char* cursor_args = NULL; const char* background_args = NULL; int precheck = 0; opts.auth = NULL; opts.cursor = tlock_cursors[0]; opts.background = tlock_backgrounds[0]; opts.flash = 0; opts.gids = 0; /* parse options */ if (argc != 1) { for (arg = 1; arg <= argc; arg++) { /** * Background options. */ if (!strcmp(argv[arg - 1], "-bg")) { if (arg < argc) { char* char_tmp; struct aBackground* bg_tmp = NULL; struct aBackground** i; /* * List all the available backgrounds. */ if (strcmp(argv[arg], "list") == 0) { for (i = tlock_backgrounds; *i; ++i) { printf("%s\n", (*i)->name); } exit(EXIT_SUCCESS); } /* * Assigning the backgrounds to the option array. */ for (i = tlock_backgrounds; *i; ++i) { char_tmp = strstr(argv[arg], (*i)->name); if (char_tmp && char_tmp == argv[arg]) { background_args = char_tmp; bg_tmp = *i; opts.background = bg_tmp; ++arg; break; } } /** * Abort when background is not found. */ if (bg_tmp == NULL) { printf("%s", "tlock: error, couldn't find the bg-module you specified.\n"); exit(EXIT_FAILURE); } } else { printf("%s", "tlock, error, missing argument\n"); displayUsage(); exit(EXIT_FAILURE); } } else if (!strcmp(argv[arg - 1], "-auth")) { if (arg < argc) { char* char_tmp; struct aAuth* auth_tmp = NULL; struct aAuth** i; if (!strcmp(argv[arg], "list")) { for (i = tlock_authmodules; *i; ++i) { printf("%s\n", (*i)->name); } exit(EXIT_SUCCESS); } for (i = tlock_authmodules; *i; ++i) { char_tmp = strstr(argv[arg], (*i)->name); if (char_tmp && char_tmp == argv[arg]) { auth_tmp = (*i); if (auth_tmp->init(argv[arg]) == 0) { printf("%s.%s: error, failed init of [%s] with arguments [%s].\n", __FILE__, __FUNCTION__, auth_tmp->name, argv[arg]); exit(EXIT_FAILURE); } else { printf("%s.%s: initialized [%s] with arguments [%s].\n", __FILE__, __FUNCTION__, auth_tmp->name, argv[arg]); } opts.auth = auth_tmp; ++arg; break; } } if (auth_tmp == NULL) { printf("%s", "tlock: error, couldnt find the auth-module you specified.\n"); exit(EXIT_FAILURE); } } else { printf("%s", "tlock, error, missing argument\n"); displayUsage(); exit(EXIT_FAILURE); } } else if (strcmp(argv[arg - 1], "-cursor") == 0) { if (arg < argc) { char* char_tmp; struct aCursor* cursor_tmp = NULL; struct aCursor** i; if (strcmp(argv[arg], "list") == 0) { for (i = tlock_cursors; *i; ++i) { printf("%s\n", (*i)->name); } exit(EXIT_SUCCESS); } for (i = tlock_cursors; *i; ++i) { char_tmp = strstr(argv[arg], (*i)->name); if (char_tmp && char_tmp == argv[arg]) { cursor_args = char_tmp; cursor_tmp = *i; opts.cursor = cursor_tmp; ++arg; break; } } if (!cursor_tmp) { printf("%s", "tlock: error, couldn't find the cursor-module you specified.\n"); exit(EXIT_FAILURE); } } else { printf("%s", "tlock, error, missing argument\n"); displayUsage(); exit(EXIT_FAILURE); } } else { if (strcmp(argv[arg - 1], "-h") == 0) { displayUsage(); exit(EXIT_SUCCESS); } else if (strcmp(argv[arg - 1], "-v") == 0) { printf("tlock-%s by André 'Bananaman' Kaan\n", VERSION); exit(EXIT_SUCCESS); } else if (strcmp(argv[arg - 1], "-pre") == 0) { precheck = 1; } else if (strcmp(argv[arg - 1], "-flash") == 0) { opts.flash = 1; } else if (strcmp(argv[arg - 1], "-gids") == 0) { opts.gids = 1; } } } } /** * */ initStartTime(); /* * */ initXInfo(&xinfo); if (detectOtherInstance(&xinfo)) { printf("%s", "tlock: error, another instance seems to be running\n"); exit(EXIT_FAILURE); } /* * List the usage of the authentication. */ if (!opts.auth) { printf("%s", "tlock: error, no auth-method specified.\n"); displayUsage(); exit(EXIT_FAILURE); } /* * */ if (opts.background->init(background_args, &xinfo) == 0) { printf("tlock: error, couldn't init [%s] with [%s].\n", opts.background->name, background_args); exit(EXIT_FAILURE); } if (opts.cursor->init(cursor_args, &xinfo) == 0) { printf("tlock: error, couldn't init [%s] with [%s].\n", opts.cursor->name, cursor_args); exit(EXIT_FAILURE); } LOG("initializing screens"); { int scr; for (scr = 0; scr < xinfo.nr_screens; scr++) { XSelectInput(xinfo.display, xinfo.window[scr], ButtonReleaseMask | ButtonPressMask); XMapWindow(xinfo.display, xinfo.window[scr]); XRaiseWindow(xinfo.display, xinfo.window[scr]); } } LOG("initializing keyboard control"); /* try to grab 2 times, another process (windowmanager) may have grabbed * the keyboard already */ if ((XGrabKeyboard(xinfo.display, xinfo.window[0], True, GrabModeAsync, GrabModeAsync, CurrentTime)) != GrabSuccess) { sleep(1); if ((XGrabKeyboard(xinfo.display, xinfo.window[0], True, GrabModeAsync, GrabModeAsync, CurrentTime)) != GrabSuccess) { printf("%s", "tlock: couldn't grab the keyboard.\n"); exit(EXIT_FAILURE); } } /* TODO: think about it: do we really need NR_SCREEN cursors ? we grab the * pointer on :*.0 anyway ... */ LOG("initializing mouse control"); if (XGrabPointer(xinfo.display, xinfo.window[0], False, ButtonPressMask | ButtonReleaseMask, // needed for the propagation of the pointer events on window[0] GrabModeAsync, GrabModeAsync, None, xinfo.cursor[0], CurrentTime) != GrabSuccess) { XUngrabKeyboard(xinfo.display, CurrentTime); printf("%s", "tlock: couldn't grab the pointer.\n"); exit(EXIT_FAILURE); } openlog("tlock", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1); syslog(LOG_NOTICE, "program started by user %d(%s), accepting groups as '%s' only.", getuid(), getenv("USER"), opts.gids ? "id" : "names" ); // pre authorization check if (precheck == 1) { if (opts.auth != NULL) { int ret = opts.auth->auth(NULL, NULL, 0); syslog( LOG_NOTICE, "%s: %s[exit=%d]\n", __FILE__, __FUNCTION__, ret); if (ret != 1) { registerInstance(&xinfo); eventLoop(&opts, &xinfo); LOG("exiting eventloop, and unregister"); unregisterInstance(&xinfo); } } } else { registerInstance(&xinfo); eventLoop(&opts, &xinfo); LOG("exiting eventloop, and unregister"); unregisterInstance(&xinfo); } closelog(); opts.auth->deinit(); opts.cursor->deinit(&xinfo); opts.background->deinit(&xinfo); XCloseDisplay(xinfo.display); return EXIT_SUCCESS; }