~ubuntu-branches/ubuntu/precise/network-manager/precise

« back to all changes in this revision

Viewing changes to src/nm-session-utils.c

  • Committer: Package Import Robot
  • Author(s): Mathieu Trudel-Lapierre
  • Date: 2012-02-09 16:45:41 UTC
  • mfrom: (1.1.53)
  • Revision ID: package-import@ubuntu.com-20120209164541-4h90zknlsfdb7x35
Tags: 0.9.2.0+git201202091925.c721477-0ubuntu1
* upstream snapshot 2012-02-09 19:25:59 (GMT)
  + c721477d11d4fe144111d6d2eec8f93f2e9186c9
* debian/patches/avoid-periodic-disk-wakeups.patch: refreshed.
* debian/patches/nl3-default-ip6-route.patch: refreshed.
* debian/libnm-glib4.symbols: add symbols:
  + nm_active_connection_get_master@Base
  + nm_client_new_async@Base
  + nm_client_new_finish@Base
  + nm_remote_settings_new_async@Base
  + nm_remote_settings_new_finish@Base
  + nm_device_get_state_reason@Base
* debian/libnm-util2.symbols: add symbols:
  + nm_setting_802_1x_get_pac_file@Base
  + nm_setting_infiniband_get_transport_mode@Base

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
 
2
/* This program is free software; you can redistribute it and/or modify
 
3
 * it under the terms of the GNU General Public License as published by
 
4
 * the Free Software Foundation; either version 2 of the License, or
 
5
 * (at your option) any later version.
 
6
 *
 
7
 * This program is distributed in the hope that it will be useful,
 
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
 * GNU General Public License for more details.
 
11
 *
 
12
 * You should have received a copy of the GNU General Public License along
 
13
 * with this program; if not, write to the Free Software Foundation, Inc.,
 
14
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
15
 *
 
16
 * Copyright (C) 2012 Red Hat, Inc.
 
17
 * Author: Dan Williams <dcbw@redhat.com>
 
18
 */
 
19
 
 
20
#include <pwd.h>
 
21
#include <sys/types.h>
 
22
 
 
23
#include "nm-session-utils.h"
 
24
 
 
25
/********************************************************************/
 
26
 
 
27
GQuark
 
28
nm_session_monitor_error_quark (void)
 
29
{
 
30
        static GQuark ret = 0;
 
31
 
 
32
        if (G_UNLIKELY (ret == 0))
 
33
                ret = g_quark_from_static_string ("nm-session-monitor-error");
 
34
        return ret;
 
35
}
 
36
 
 
37
#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
 
38
 
 
39
GType
 
40
nm_session_monitor_error_get_type (void)
 
41
{
 
42
        static GType etype = 0;
 
43
 
 
44
        if (etype == 0) {
 
45
                static const GEnumValue values[] = {
 
46
                        /* Some I/O operation on the CK database failed */
 
47
                        ENUM_ENTRY (NM_SESSION_MONITOR_ERROR_IO_ERROR, "IOError"),
 
48
                        /* Error parsing the CK database */
 
49
                        ENUM_ENTRY (NM_SESSION_MONITOR_ERROR_MALFORMED_DATABASE, "MalformedDatabase"),
 
50
                        /* Username or UID could could not be found */
 
51
                        ENUM_ENTRY (NM_SESSION_MONITOR_ERROR_UNKNOWN_USER, "UnknownUser"),
 
52
                        /* No ConsoleKit database */
 
53
                        ENUM_ENTRY (NM_SESSION_MONITOR_ERROR_NO_DATABASE, "NoDatabase"),
 
54
                        { 0, 0, 0 }
 
55
                };
 
56
 
 
57
                etype = g_enum_register_static ("NMSessionMonitorError", values);
 
58
        }
 
59
        return etype;
 
60
}
 
61
 
 
62
/********************************************************************/
 
63
 
 
64
gboolean
 
65
nm_session_uid_to_user (uid_t uid, const char **out_user, GError **error)
 
66
{
 
67
        struct passwd *pw;
 
68
 
 
69
        pw = getpwuid (uid);
 
70
        if (!pw) {
 
71
                g_set_error (error,
 
72
                                 NM_SESSION_MONITOR_ERROR,
 
73
                                 NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
 
74
                                 "Could not get username for UID %d",
 
75
                                 uid);
 
76
                return FALSE;
 
77
        }
 
78
 
 
79
        if (out_user)
 
80
                *out_user = pw->pw_name;
 
81
        return TRUE;
 
82
}
 
83
 
 
84
gboolean
 
85
nm_session_user_to_uid (const char *user, uid_t *out_uid, GError **error)
 
86
{
 
87
        struct passwd *pw;
 
88
 
 
89
        pw = getpwnam (user);
 
90
        if (!pw) {
 
91
                g_set_error (error,
 
92
                                 NM_SESSION_MONITOR_ERROR,
 
93
                                 NM_SESSION_MONITOR_ERROR_UNKNOWN_USER,
 
94
                                 "Could not get UID for username '%s'",
 
95
                                 user);
 
96
                return FALSE;
 
97
        }
 
98
 
 
99
        /* Ugly, but hey, use ConsoleKit */
 
100
        if (out_uid)
 
101
                *out_uid = pw->pw_uid;
 
102
        return TRUE;
 
103
}
 
104