Translation update done using Pootle.
[gammu.git] / smsd / uid.c
blob5df649f48e02c3ab8a993cbe9937325c48319f0c
1 #include <sys/types.h>
2 #include <unistd.h>
3 #include <pwd.h>
4 #include <grp.h>
5 #include <stdlib.h>
6 #include <string.h>
8 #include "uid.h"
10 /**
11 * Sets UID.
13 gboolean set_uid_gid(SMSD_Parameters *params)
15 /* gid is set allways (either through fill_uid or by user */
16 if (setgid(params->gid) != 0)
17 return FALSE;
19 /* Only set group */
20 if (params->uid == -1)
21 return TRUE;
23 if (initgroups(params->user, params->gid) != 0)
24 return FALSE;
26 if (setuid(params->uid) != 0)
27 return FALSE;
29 if (seteuid(params->uid) != 0)
30 return FALSE;
32 return TRUE;
35 /**
36 * Sets GID.
38 gboolean set_gid(int gid)
40 return setgid(gid) == 0;
43 /**
44 * Gets UID from string.
46 gboolean fill_uid(SMSD_Parameters *params, const char *name)
48 struct passwd *pwd;
49 char *endptr;
50 long uid;
52 pwd = getpwnam(name);
54 if (pwd == NULL) {
55 /* Try to handle it as a number */
56 uid = strtol(name, &endptr, 10);
57 if (*endptr == 0 && uid > 0) {
58 pwd = getpwuid(uid);
62 if (pwd != NULL) {
63 params->uid = pwd->pw_uid;
64 params->user = strdup(pwd->pw_name);
65 if (params->gid == -1) {
66 params->gid = pwd->pw_gid;
68 return TRUE;
71 return FALSE;
74 /**
75 * Gets GID from string.
77 gboolean fill_gid(SMSD_Parameters *params, const char *name)
79 struct group *grp;
80 char *endptr;
81 long gid;
83 grp = getgrnam(name);
85 /* Try to handle it as a number */
86 if (grp == NULL) {
87 gid = strtol(name, &endptr, 10);
88 if (*endptr == 0 && gid > 0) {
89 grp = getgrgid(gid);
93 if (grp != NULL) {
94 params->gid = grp->gr_gid;
95 params->group = strdup(grp->gr_name);
96 return TRUE;
99 return FALSE;