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