File: pdm.c

package info (click to toggle)
pdm 0.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 48 kB
  • ctags: 7
  • sloc: ansic: 96; makefile: 52
file content (131 lines) | stat: -rw-r--r-- 3,492 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Copyright (C) 2001 Philip Blundell <philb@gnu.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#include <pwd.h>
#include <sys/types.h>
#include <shadow.h>
#include <crypt.h>
#include <unistd.h>
#include <stdlib.h>

#include <gtk/gtk.h>

static const char *current_username;
static GtkWidget *label_result;

static void
set_username(GtkWidget *widget,
	     gpointer data)
{
  current_username = (const char *)data;
}

static void
slurp_passwd(GtkWidget *menu)
{
  struct passwd *pw;
  while (pw = getpwent(), pw != NULL)
    {
      const char *name = g_strdup(pw->pw_name);
      GtkWidget *item = gtk_menu_item_new_with_label(name);
      gtk_signal_connect(GTK_OBJECT(item), "activate", GTK_SIGNAL_FUNC(set_username), (gpointer)name);
      gtk_menu_append(GTK_MENU(menu), item);
      if (current_username == NULL)
	current_username = name;
    }
}

static void
enter_callback(GtkWidget *widget,
	       GtkWidget *entry)
{
  gchar *pwstr;
  struct passwd *pwe;
  struct spwd *spe;
  char *p;

  if (current_username == NULL)
    return;

  pwstr = gtk_entry_get_text(GTK_ENTRY(entry));
  gtk_entry_set_text(GTK_ENTRY(entry), "");

  pwe = getpwnam(current_username);
  if (pwe == NULL)
    goto login_incorrect;

  spe = getspnam(current_username);
  if (spe)
    pwe->pw_passwd = spe->sp_pwdp;

  p = crypt(pwstr, pwe->pw_passwd);
  if (strcmp(p, pwe->pw_passwd))
    goto login_incorrect;

  setuid(pwe->pw_uid);
  setgid(pwe->pw_gid);
  setenv("HOME", pwe->pw_dir, 1);
  chdir(pwe->pw_dir);

  execl("/etc/X11/Xsession", "/etc/X11/Xsession", NULL);

  return;

 login_incorrect:
  gtk_label_set_text(GTK_LABEL(label_result), "Login incorrect");
}

int
main(int argc, char *argv[])
{
  GtkWidget *window, *label_login, *label_password;
  GtkWidget *option, *entry, *menu;
  GtkWidget *table;

  gtk_init(&argc, &argv);
  
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  label_login = gtk_label_new("login:");
  label_password = gtk_label_new("password:");
  label_result = gtk_label_new("");
  gtk_misc_set_alignment(GTK_MISC(label_login), 1.00f, 0.5f);
  gtk_misc_set_alignment(GTK_MISC(label_password), 1.00f, 0.5f);
  
  entry = gtk_entry_new();
  gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
  gtk_entry_set_max_length(GTK_ENTRY(entry), 16);

  option = gtk_option_menu_new();
  menu = gtk_menu_new();
  slurp_passwd(menu);
  gtk_option_menu_set_menu(GTK_OPTION_MENU(option), menu);

  gtk_signal_connect(GTK_OBJECT(entry), "activate",
		     GTK_SIGNAL_FUNC(enter_callback), entry);

  table = gtk_table_new(2, 3, FALSE);
  gtk_widget_set_usize(option, -1, 32);
  gtk_widget_set_usize(label_result, -1, 32);
  gtk_table_attach(GTK_TABLE(table), label_login, 0, 1, 0, 1, 0, 0, 5, 2);
  gtk_table_attach(GTK_TABLE(table), label_password, 0, 1, 1, 2, 0, 0, 5, 2);
  gtk_table_attach(GTK_TABLE(table), option, 1, 2, 0, 1, GTK_EXPAND | GTK_FILL, 0, 5, 2);
  gtk_table_attach(GTK_TABLE(table), entry, 1, 2, 1, 2, GTK_EXPAND | GTK_FILL, 0, 5, 2);
  gtk_table_attach(GTK_TABLE(table), label_result, 1, 2, 2, 3, GTK_EXPAND | GTK_FILL, 0, 5, 2);

  gtk_container_add(GTK_CONTAINER(window), table);

  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_widget_show_all(window);
  gtk_widget_grab_focus(entry);

  gtk_main();

  return 0;
}