~ubuntu-branches/ubuntu/wily/libuser/wily

« back to all changes in this revision

Viewing changes to apps/lchsh.c

  • Committer: Bazaar Package Importer
  • Author(s): Pierre Habouzit
  • Date: 2006-09-03 21:58:15 UTC
  • mto: (2.1.1 edgy) (1.1.5 upstream) (3.1.1 lenny)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20060903215815-rtvvfzhaer8ymyp4
Tags: upstream-0.54.6-2.1.dfsg.1
ImportĀ upstreamĀ versionĀ 0.54.6-2.1.dfsg.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2001,2002, 2004 Red Hat, Inc.
 
2
 * Copyright (C) 2001,2002, 2004, 2006 Red Hat, Inc.
3
3
 *
4
4
 * This is free software; you can redistribute it and/or modify it under
5
5
 * the terms of the GNU Library General Public License as published by
16
16
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
17
 */
18
18
 
19
 
#ident "$Id: lchsh.c,v 1.18 2005/09/12 18:21:34 mitr Exp $"
 
19
#ident "$Id: lchsh.c,v 1.21 2006/05/01 05:10:43 mitr Exp $"
20
20
 
21
21
#ifdef HAVE_CONFIG_H
22
22
#include "config.h"
35
35
int
36
36
main(int argc, const char **argv)
37
37
{
38
 
        const char *user = NULL;
39
 
        struct lu_context *ctx = NULL;
 
38
        const char *user;
 
39
        struct lu_context *ctx;
40
40
        struct lu_error *error = NULL;
41
 
        struct lu_ent *ent = NULL;
42
 
        GValueArray *values = NULL;
43
 
        GValue *value, val;
 
41
        struct lu_ent *ent;
 
42
        GValueArray *values;
44
43
        int interactive = FALSE;
45
44
        int c;
46
 
        struct lu_prompt prompts[1];
47
45
        poptContext popt;
48
46
        struct poptOption options[] = {
49
47
                {"interactive", 'i', POPT_ARG_NONE, &interactive, 0,
71
69
        /* If no user was specified, or we're setuid, force the user name to
72
70
         * be that of the current user. */
73
71
        if ((user == NULL) || (geteuid() != getuid())) {
74
 
                struct passwd *pwd = NULL;
 
72
                struct passwd *pwd;
 
73
 
75
74
                pwd = getpwuid(getuid());
76
75
                if (pwd != NULL) {
77
76
                        user = g_strdup(pwd->pw_name);
109
108
        /* Read the user's shell. */
110
109
        values = lu_ent_get(ent, LU_LOGINSHELL);
111
110
        if (values != NULL) {
 
111
                struct lu_prompt prompts[1];
 
112
                GValue *value, val;
 
113
 
112
114
                value = g_value_array_get_nth(values, 0);
113
115
                /* Fill in the prompt structure using the user's shell. */
114
116
                memset(prompts, 0, sizeof(prompts));
119
121
                prompts[0].default_value = lu_value_strdup(value);
120
122
                /* Prompt for a new shell. */
121
123
                if (lu_prompt_console(prompts, G_N_ELEMENTS(prompts),
122
 
                                      NULL, &error)) {
123
 
                        /* Modify the in-memory structure's shell attribute. */
124
 
                        memset(&val, 0, sizeof(val));
125
 
                        g_value_init(&val, G_TYPE_STRING);
126
 
                        g_value_set_string(&val, prompts[0].value);
127
 
                        if (prompts[0].free_value != NULL) {
128
 
                                prompts[0].free_value(prompts[0].value);
129
 
                                prompts[0].value = NULL;
130
 
                        }
131
 
                        lu_ent_clear(ent, LU_LOGINSHELL);
132
 
                        lu_ent_add(ent, LU_LOGINSHELL, &val);
133
 
                        /* Modify the user's record in the information store. */
134
 
                        if (lu_user_modify(ctx, ent, &error)) {
135
 
                                g_print(_("Shell changed.\n"));
136
 
                                lu_hup_nscd();
137
 
                        } else
138
 
                                fprintf(stderr, _("Shell not changed: %s\n"),
139
 
                                        lu_strerror(error));
140
 
                        g_value_unset(&val);
141
 
                }
 
124
                                      NULL, &error) == FALSE) {
 
125
                        fprintf(stderr, _("Shell not changed: %s\n"),
 
126
                                lu_strerror(error));
 
127
                        return 1;
 
128
                }
 
129
                /* Modify the in-memory structure's shell attribute. */
 
130
                memset(&val, 0, sizeof(val));
 
131
                g_value_init(&val, G_TYPE_STRING);
 
132
                g_value_set_string(&val, prompts[0].value);
 
133
                if (prompts[0].free_value != NULL) {
 
134
                        prompts[0].free_value(prompts[0].value);
 
135
                        prompts[0].value = NULL;
 
136
                }
 
137
                lu_ent_clear(ent, LU_LOGINSHELL);
 
138
                lu_ent_add(ent, LU_LOGINSHELL, &val);
 
139
                /* Modify the user's record in the information store. */
 
140
                if (lu_user_modify(ctx, ent, &error)) {
 
141
                        g_print(_("Shell changed.\n"));
 
142
                        lu_hup_nscd();
 
143
                } else {
 
144
                        fprintf(stderr, _("Shell not changed: %s\n"),
 
145
                                lu_strerror(error));
 
146
                        return 1;
 
147
                }
 
148
                g_value_unset(&val);
142
149
        }
143
150
 
144
151
        lu_ent_free(ent);