Browse code

- user2uid and group2gid made available to modules

Jan Janak authored on 13/12/2005 01:05:04
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,84 @@
0
+/*
1
+ *$Id$
2
+ *
3
+ * various general purpose functions
4
+ *
5
+ * Copyright (C) 2001-2003 FhG Fokus
6
+ *
7
+ * This file is part of ser, a free SIP server.
8
+ *
9
+ * ser is free software; you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation; either version 2 of the License, or
12
+ * (at your option) any later version
13
+ *
14
+ * For a license to use the ser software under conditions
15
+ * other than those described here, or to purchase support for this
16
+ * software, please contact iptel.org by e-mail at the following addresses:
17
+ *    info@iptel.org
18
+ *
19
+ * ser is distributed in the hope that it will be useful,
20
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
+ * GNU General Public License for more details.
23
+ *
24
+ * You should have received a copy of the GNU General Public License 
25
+ * along with this program; if not, write to the Free Software 
26
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
+ *
28
+ */
29
+
30
+#include <sys/types.h>
31
+#include <pwd.h>
32
+#include <grp.h>
33
+#include <stdlib.h>
34
+#include "ut.h"
35
+
36
+/* converts a username into uid:gid,
37
+ * returns -1 on error & 0 on success */
38
+int user2uid(int* uid, int* gid, char* user)
39
+{
40
+	char* tmp;
41
+	struct passwd *pw_entry;
42
+	
43
+	if (user){
44
+		*uid=strtol(user, &tmp, 10);
45
+		if ((tmp==0) ||(*tmp)){
46
+			/* maybe it's a string */
47
+			pw_entry=getpwnam(user);
48
+			if (pw_entry==0){
49
+				goto error;
50
+			}
51
+			*uid=pw_entry->pw_uid;
52
+			if (gid) *gid=pw_entry->pw_gid;
53
+		}
54
+		return 0;
55
+	}
56
+error:
57
+	return -1;
58
+}
59
+
60
+
61
+
62
+/* converts a group name into a gid
63
+ * returns -1 on error, 0 on success */
64
+int group2gid(int* gid, char* group)
65
+{
66
+	char* tmp;
67
+	struct group  *gr_entry;
68
+	
69
+	if (group){
70
+		*gid=strtol(group, &tmp, 10);
71
+		if ((tmp==0) ||(*tmp)){
72
+			/* maybe it's a string */
73
+			gr_entry=getgrnam(group);
74
+			if (gr_entry==0){
75
+				goto error;
76
+			}
77
+			*gid=gr_entry->gr_gid;
78
+		}
79
+		return 0;
80
+	}
81
+error:
82
+	return -1;
83
+}
... ...
@@ -419,4 +419,12 @@ static inline int str2int(str* _s, unsigned int* _r)
419 419
 	return 0;
420 420
 }
421 421
 
422
+/* converts a username into uid:gid,
423
+ * returns -1 on error & 0 on success */
424
+int user2uid(int* uid, int* gid, char* user);
425
+
426
+/* converts a group name into a gid
427
+ * returns -1 on error, 0 on success */
428
+int group2gid(int* gid, char* group);
429
+
422 430
 #endif