]> git.sur5r.net Git - ngadmin/blob - lib/src/misc.c
Split lib code into several files
[ngadmin] / lib / src / misc.c
1
2 #include <ngadmin.h>
3
4 #include <attr.h>
5 #include <protocol.h>
6
7 #include "lib.h"
8 #include "network.h"
9
10
11 int ngadmin_setName (struct ngadmin *nga, const char *name)
12 {
13         List *attr;
14         struct attr *at;
15         struct swi_attr *sa;
16         int ret = ERR_OK;
17         
18         
19         if (nga == NULL)
20                 return ERR_INVARG;
21         
22         sa = nga->current;
23         if (sa == NULL)
24                 return ERR_NOTLOG;
25         
26         
27         attr = createEmptyList();
28         if (name == NULL)
29                 at = newEmptyAttr(ATTR_NAME);
30         else
31                 at = newAttr(ATTR_NAME, strlen(name), strdup(name));
32         pushBackList(attr, at);
33         ret = writeRequest(nga, attr);
34         if (ret != ERR_OK)
35                 goto end;
36          
37         /* successful, also update local name */
38         if (name == NULL)
39                 memset(sa->name, '\0', NAME_SIZE);
40         else
41                 strncpy(sa->name, name, NAME_SIZE);
42         
43 end:
44         return ret;
45 }
46
47
48 int ngadmin_changePassword (struct ngadmin *nga, const char* pass)
49 {
50         List *attr;
51         struct attr *at;
52         int ret = ERR_OK;
53         
54         
55         if (nga == NULL || pass == NULL)
56                 return ERR_INVARG;
57         else if (nga->current == NULL)
58                 return ERR_NOTLOG;
59         
60         
61         attr = createEmptyList();
62         at = newAttr(ATTR_NEW_PASSWORD, strlen(pass), strdup(pass));
63         if (nga->encrypt_pass)
64                 passwordEndecode(at->data, at->size);
65         pushBackList(attr, at);
66         ret = writeRequest(nga, attr);
67         if (ret != ERR_OK)
68                 goto end;
69         
70         /* successful, also update local password */
71         strncpy(nga->password, pass, PASSWORD_MAX);
72         
73 end:
74         
75         return ret;
76 }
77
78
79 int ngadmin_restart (struct ngadmin *nga)
80 {
81         List *attr;
82         
83         
84         attr = createEmptyList();
85         pushBackList(attr, newByteAttr(ATTR_RESTART, 1));
86         
87         
88         return writeRequest(nga, attr);
89 }
90
91
92 int ngadmin_defaults (struct ngadmin *nga)
93 {
94         List *attr;
95         int ret = ERR_OK;
96         
97         
98         attr = createEmptyList();
99         pushBackList(attr, newByteAttr(ATTR_DEFAULTS, 1));
100         ret = writeRequest(nga, attr);
101         if (ret != ERR_OK)
102                 goto end;
103         
104         
105         /* successful: delog and clean list */
106         free(nga->swi_tab);
107         nga->swi_tab = NULL;
108         nga->swi_count = 0;
109         nga->current = NULL;
110         
111 end:
112         return ret;
113 }
114
115